Skip to content

Commit cad4aea

Browse files
committed
put exported declared names so they can be assigned in deeper scopes
1 parent d772547 commit cad4aea

File tree

5 files changed

+80
-21
lines changed

5 files changed

+80
-21
lines changed

moonscript/compile.lua

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,12 @@ do
370370
elseif "string" == _exp_0 then
371371
real_name = name
372372
end
373-
if not (is_local or real_name and not self:has_name(real_name)) then
373+
if not (is_local or real_name and not self:has_name(real_name, true)) then
374+
_continue_0 = true
375+
break
376+
end
377+
self:put_name(real_name)
378+
if self:name_exported(real_name) then
374379
_continue_0 = true
375380
break
376381
end
@@ -385,16 +390,19 @@ do
385390
end
386391
return _accum_0
387392
end)()
388-
local _list_0 = undeclared
389-
for _index_0 = 1, #_list_0 do
390-
local name = _list_0[_index_0]
391-
self:put_name(name)
392-
end
393393
return undeclared
394394
end,
395395
whitelist_names = function(self, names)
396396
self._name_whitelist = Set(names)
397397
end,
398+
name_exported = function(self, name)
399+
if self.export_all then
400+
return true
401+
end
402+
if self.export_proper and name:match("^%u") then
403+
return true
404+
end
405+
end,
398406
put_name = function(self, name, ...)
399407
local value = ...
400408
if select("#", ...) == 0 then
@@ -406,13 +414,8 @@ do
406414
self._names[name] = value
407415
end,
408416
has_name = function(self, name, skip_exports)
409-
if not skip_exports then
410-
if self.export_all then
411-
return true
412-
end
413-
if self.export_proper and name:match("^%u") then
414-
return true
415-
end
417+
if not skip_exports and self:name_exported(name) then
418+
return true
416419
end
417420
local yes = self._names[name]
418421
if yes == nil and self.parent then

moonscript/compile.moon

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,21 @@ class Block
216216
when NameProxy then name\get_name self
217217
when "string" then name
218218

219-
continue unless is_local or real_name and not @has_name real_name
219+
continue unless is_local or real_name and not @has_name real_name, true
220+
-- put exported names so they can be assigned to in deeper scope
221+
@put_name real_name
222+
continue if @name_exported real_name
220223
real_name
221224

222-
@put_name name for name in *undeclared
223225
undeclared
224226

225227
whitelist_names: (names) =>
226228
@_name_whitelist = Set names
227229

230+
name_exported: (name) =>
231+
return true if @export_all
232+
return true if @export_proper and name\match"^%u"
233+
228234
put_name: (name, ...) =>
229235
value = ...
230236
value = true if select("#", ...) == 0
@@ -233,9 +239,7 @@ class Block
233239
@_names[name] = value
234240

235241
has_name: (name, skip_exports) =>
236-
if not skip_exports
237-
return true if @export_all
238-
return true if @export_proper and name\match"^%u"
242+
return true if not skip_exports and @name_exported name
239243

240244
yes = @_names[name]
241245
if yes == nil and @parent

moonscript/transform.moon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ Statement = Transformer {
195195
if destructure.has_destructure names
196196
return destructure.split_assign node
197197

198+
-- print util.dump node
198199
node
199200

200201
continue: (node) =>

tests/inputs/export.moon

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,30 @@ do
4848
j = 2000
4949

5050

51+
do
52+
export *
53+
x = 3434
54+
if y then
55+
x = 10
56+
57+
do
58+
export *
59+
if y then
60+
x = 10
61+
x = 3434
62+
63+
do
64+
do
65+
export *
66+
67+
k = 1212
68+
69+
do
70+
h = 100
71+
72+
y = ->
73+
h = 100
74+
k = 100
75+
76+
h = 100
77+

tests/outputs/export.lua

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ do
6767
end
6868
do
6969
if this then
70-
local What = 232
70+
What = 232
7171
else
72-
local What = 4343
72+
What = 4343
7373
end
7474
x, y, z = 1, 2, 3
7575
y = function()
@@ -78,6 +78,30 @@ do
7878
do
7979
local _with_0 = tmp
8080
local j = 2000
81-
return _with_0
8281
end
82+
end
83+
do
84+
x = 3434
85+
if y then
86+
x = 10
87+
end
88+
end
89+
do
90+
if y then
91+
local x = 10
92+
end
93+
x = 3434
94+
end
95+
do
96+
do
97+
k = 1212
98+
do
99+
local h = 100
100+
end
101+
y = function()
102+
local h = 100
103+
k = 100
104+
end
105+
end
106+
local h = 100
83107
end

0 commit comments

Comments
 (0)