Skip to content

Commit 9d2665f

Browse files
committed
let colon stubs in assignments get simplified
1 parent a926e96 commit 9d2665f

4 files changed

Lines changed: 30 additions & 16 deletions

File tree

moonscript/transform.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,11 @@ Statement = Transformer({
378378
if num_names == 1 and num_values == 1 then
379379
local first_value = values[1]
380380
local first_name = names[1]
381+
local first_type = ntype(first_value)
382+
if first_type == "chain" then
383+
first_value = Value:transform_once(self, first_value)
384+
first_type = ntype(first_value)
385+
end
381386
local _exp_0 = ntype(first_value)
382387
if "block_exp" == _exp_0 then
383388
local block_body = first_value[2]
@@ -397,6 +402,8 @@ Statement = Transformer({
397402
})
398403
elseif "comprehension" == _exp_0 or "tblcomprehension" == _exp_0 or "foreach" == _exp_0 or "for" == _exp_0 or "while" == _exp_0 then
399404
return build.assign_one(first_name, Value:transform_once(self, first_value))
405+
else
406+
values[1] = first_value
400407
end
401408
end
402409
local transformed
@@ -1538,7 +1545,7 @@ Value = Transformer({
15381545
local base_name = NameProxy("base")
15391546
local fn_name = NameProxy("fn")
15401547
local is_super = ntype(node[2]) == "ref" and node[2][2] == "super"
1541-
return self.transform.value(build.block_exp({
1548+
return build.block_exp({
15421549
build.assign({
15431550
names = {
15441551
base_name
@@ -1580,7 +1587,7 @@ Value = Transformer({
15801587
})
15811588
}
15821589
})
1583-
}))
1590+
})
15841591
end
15851592
end,
15861593
block_exp = function(self, node)

moonscript/transform.moon

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ Statement = Transformer {
206206
if num_names == 1 and num_values == 1
207207
first_value = values[1]
208208
first_name = names[1]
209+
first_type = ntype first_value
210+
211+
-- reduce colon stub chain to block exp
212+
if first_type == "chain"
213+
first_value = Value\transform_once @, first_value
214+
first_type = ntype first_value
209215

210216
switch ntype first_value
211217
when "block_exp"
@@ -220,6 +226,8 @@ Statement = Transformer {
220226

221227
when "comprehension", "tblcomprehension", "foreach", "for", "while"
222228
return build.assign_one first_name, Value\transform_once @, first_value
229+
else
230+
values[1] = first_value
223231

224232
-- bubble cascading assigns
225233
transformed = if num_values == 1
@@ -922,7 +930,7 @@ Value = Transformer {
922930
fn_name = NameProxy "fn"
923931

924932
is_super = ntype(node[2]) == "ref" and node[2][2] == "super"
925-
@transform.value build.block_exp {
933+
build.block_exp {
926934
build.assign {
927935
names: {base_name}
928936
values: {node}

spec/outputs/class.lua

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,11 @@ do
266266
local _base_0 = {
267267
val = 2323,
268268
something = function(self)
269-
return (function()
270-
local _base_1 = _parent_0
271-
local _fn_0 = _base_1.something
272-
return function(...)
273-
return _fn_0(self, ...)
274-
end
275-
end)()
269+
local _base_1 = _parent_0
270+
local _fn_0 = _base_1.something
271+
return function(...)
272+
return _fn_0(self, ...)
273+
end
276274
end
277275
}
278276
_base_0.__index = _base_0

spec/outputs/stub.lua

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ local x = {
44
return print(self.val)
55
end
66
}
7-
local fn = (function()
7+
local fn
8+
do
89
local _base_0 = x
910
local _fn_0 = _base_0.val
10-
return function(...)
11+
fn = function(...)
1112
return _fn_0(_base_0, ...)
1213
end
13-
end)()
14+
end
1415
print(fn())
1516
print(x:val())
16-
x = (function(...)
17+
do
1718
local _base_0 = hello(...)
1819
local _fn_0 = _base_0.world
19-
return function(...)
20+
x = function(...)
2021
return _fn_0(_base_0, ...)
2122
end
22-
end)(...)
23+
end

0 commit comments

Comments
 (0)