Skip to content

Commit abc2599

Browse files
committed
fix export ^ with class declarations inside of do
1 parent 67584ec commit abc2599

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

moonscript/transform.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local data = require("moonscript.data")
55
local reversed = util.reversed
66
local ntype, build, smart_node, is_slice, value_is_singular = types.ntype, types.build, types.smart_node, types.is_slice, types.value_is_singular
77
local insert = table.insert
8+
local mtype = util.moon.type
89
local implicitly_return
910
do
1011
local _parent_0 = nil
@@ -233,7 +234,7 @@ find_assigns = function(body, out)
233234
return out
234235
end
235236
local hoist_declarations
236-
hoist_declarations = function(body, rules)
237+
hoist_declarations = function(body)
237238
local assigns = { }
238239
local _list_0 = find_assigns(body)
239240
for _index_0 = 1, #_list_0 do
@@ -246,7 +247,11 @@ hoist_declarations = function(body, rules)
246247
end
247248
end
248249
end
249-
return table.insert(body, 1, {
250+
local idx = 1
251+
while mtype(body[idx]) == Run do
252+
idx = idx + 1
253+
end
254+
return table.insert(body, idx, {
250255
"declare",
251256
assigns
252257
})
@@ -1092,6 +1097,7 @@ Statement = Transformer({
10921097
local _with_0 = build
10931098
local out_body = {
10941099
Run(function(self)
1100+
self:put_name(name)
10951101
return self:set("super", function(block, chain)
10961102
if chain then
10971103
local slice = (function()

moonscript/transform.moon

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import reversed from util
99
import ntype, build, smart_node, is_slice, value_is_singular from types
1010
import insert from table
1111

12+
mtype = util.moon.type
13+
1214
export Statement, Value, NameProxy, LocalName, Run
1315

1416
local implicitly_return
@@ -92,15 +94,19 @@ find_assigns = (body, out={}) ->
9294
table.insert out, thing[2] -- extract names
9395
out
9496

95-
hoist_declarations = (body, rules) ->
97+
hoist_declarations = (body) ->
9698
assigns = {}
9799

98100
-- hoist the plain old assigns
99101
for names in *find_assigns body
100102
for name in *names
101103
table.insert assigns, name if type(name) == "string"
102104

103-
table.insert body, 1, {"declare", assigns}
105+
-- insert after runs
106+
idx = 1
107+
while mtype(body[idx]) == Run do idx += 1
108+
109+
table.insert body, idx, {"declare", assigns}
104110

105111
expand_elseif_assign = (ifstm) ->
106112
for i = 4, #ifstm
@@ -551,6 +557,9 @@ Statement = Transformer {
551557
with build
552558
out_body = {
553559
Run =>
560+
-- make sure we don't assign the class to a local inside the do
561+
@put_name name
562+
554563
@set "super", (block, chain) ->
555564
if chain
556565
slice = [item for item in *chain[3,]]

tests/inputs/class.moon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,11 @@ class (-> require "moon")!.Something extends Hello.World
147147
nil
148148

149149

150+
151+
--
152+
153+
export ^
154+
class Something
155+
nil
156+
157+

tests/outputs/class.lua

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,5 +752,44 @@ do
752752
(function()
753753
return require("moon")
754754
end)().Something = _class_0
755+
end
756+
do
757+
local _parent_0 = nil
758+
local _base_0 = { }
759+
_base_0.__index = _base_0
760+
if _parent_0 then
761+
setmetatable(_base_0, _parent_0.__base)
762+
end
763+
local _class_0 = setmetatable({
764+
__init = function(self, ...)
765+
if _parent_0 then
766+
return _parent_0.__init(self, ...)
767+
end
768+
end,
769+
__base = _base_0,
770+
__name = "Something",
771+
__parent = _parent_0
772+
}, {
773+
__index = function(cls, name)
774+
local val = rawget(_base_0, name)
775+
if val == nil and _parent_0 then
776+
return _parent_0[name]
777+
else
778+
return val
779+
end
780+
end,
781+
__call = function(cls, ...)
782+
local _self_0 = setmetatable({}, _base_0)
783+
cls.__init(_self_0, ...)
784+
return _self_0
785+
end
786+
})
787+
_base_0.__class = _class_0
788+
local self = _class_0
789+
_ = nil
790+
if _parent_0 and _parent_0.__inherited then
791+
_parent_0.__inherited(_parent_0, _class_0)
792+
end
793+
Something = _class_0
755794
return _class_0
756795
end

0 commit comments

Comments
 (0)