Skip to content

Commit c9b121c

Browse files
committed
extract declarations doesn't puke on nil statement in body
1 parent af2c443 commit c9b121c

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

moonscript/transform.lua

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,31 @@ extract_declarations = function(self, body, start, out)
9999
out = { }
100100
end
101101
for i = start, #body do
102-
local stm = self.transform.statement(body[i])
103-
body[i] = stm
104-
local _exp_0 = stm[1]
105-
if "assign" == _exp_0 or "declare" == _exp_0 then
106-
local _list_0 = stm[2]
107-
for _index_0 = 1, #_list_0 do
108-
local name = _list_0[_index_0]
109-
if type(name) == "string" then
110-
insert(out, name)
102+
local _continue_0 = false
103+
repeat
104+
local stm = body[i]
105+
if stm == nil then
106+
_continue_0 = true
107+
break
108+
end
109+
stm = self.transform.statement(stm)
110+
body[i] = stm
111+
local _exp_0 = stm[1]
112+
if "assign" == _exp_0 or "declare" == _exp_0 then
113+
local _list_0 = stm[2]
114+
for _index_0 = 1, #_list_0 do
115+
local name = _list_0[_index_0]
116+
if type(name) == "string" then
117+
insert(out, name)
118+
end
111119
end
120+
elseif "group" == _exp_0 then
121+
extract_declarations(self, stm[2], 1, out)
112122
end
113-
elseif "group" == _exp_0 then
114-
extract_declarations(self, stm[2], 1, out)
123+
_continue_0 = true
124+
until true
125+
if not _continue_0 then
126+
break
115127
end
116128
end
117129
return out

moonscript/transform.moon

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ is_singular = (body) ->
4747
-- this mutates body searching for assigns
4848
extract_declarations = (body=@current_stms, start=@current_stm_i + 1, out={}) =>
4949
for i=start,#body
50-
stm = @transform.statement body[i]
50+
stm = body[i]
51+
continue if stm == nil
52+
stm = @transform.statement stm
5153
body[i] = stm
5254
switch stm[1]
5355
when "assign", "declare"

tests/inputs/local.moon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,9 @@ do
7575
d = 2323
7676

7777

78+
do
79+
local *
80+
-- this generates a nil value in the body
81+
for a in *{} do a
82+
7883
g = 2323 -- test if anything leaked

tests/outputs/local.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,11 @@ do
7070
d = 200
7171
d = 2323
7272
end
73+
do
74+
local _list_0 = { }
75+
for _index_0 = 1, #_list_0 do
76+
local a = _list_0[_index_0]
77+
local _ = a
78+
end
79+
end
7380
local g = 2323

0 commit comments

Comments
 (0)