Skip to content

Commit 5d9851b

Browse files
committed
export keyword works before class declaration, fixed export test
1 parent d50f336 commit 5d9851b

File tree

8 files changed

+88
-18
lines changed

8 files changed

+88
-18
lines changed

docs/reference.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,9 @@ variables directly:
586586

587587
export some_number, message_str = 100, "hello world"
588588

589+
Additionally, a class declaration can be prefixed with the export keyword in
590+
order to export it.
591+
589592
### Export All & Export Proper
590593

591594
The `export` statement can also take special symbols `*` and `^`.

moonscript/parse.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,10 @@ local build_grammar = wrap(function()
410410
TableBlock = SpaceBreak^1 * Advance * TableBlockInner * PopIndent / mark"table",
411411

412412
ClassDecl = key"class" * Name * (key"extends" * Exp + C"")^-1 * TableBlock / mark"class",
413-
Export = key"export" * (Ct(NameList) * (sym"=" * Ct(ExpListLow))^-1 + op"*" + op"^") / mark"export",
413+
Export = key"export" * (
414+
Cc"class" * ClassDecl +
415+
op"*" + op"^" +
416+
Ct(NameList) * (sym"=" * Ct(ExpListLow))^-1) / mark"export",
414417

415418
KeyValue = (sym":" * Name) / self_assign + Ct((SimpleName + sym"[" * Exp * sym"]") * symx":" * (Exp + TableBlock)),
416419
KeyValueList = KeyValue * (sym"," * KeyValue)^0,

moonscript/transform.lua

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,26 @@ Statement = Transformer({
217217
end,
218218
export = function(node)
219219
if #node > 2 then
220-
return build.group({
221-
node,
222-
build.assign({
223-
names = node[2],
224-
values = node[3]
220+
if node[2] == "class" then
221+
local cls = smart_node(node[3])
222+
return build.group({
223+
{
224+
"export",
225+
{
226+
cls.name
227+
}
228+
},
229+
cls
225230
})
226-
})
231+
else
232+
return build.group({
233+
node,
234+
build.assign({
235+
names = node[2],
236+
values = node[3]
237+
})
238+
})
239+
end
227240
else
228241
return nil
229242
end

moonscript/transform.moon

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,20 @@ Statement = Transformer {
127127
export: (node) ->
128128
-- assign values if they are included
129129
if #node > 2
130-
build.group {
131-
node
132-
build.assign {
133-
names: node[2]
134-
values: node[3]
130+
if node[2] == "class"
131+
cls = smart_node node[3]
132+
build.group {
133+
{"export", {cls.name}}
134+
cls
135+
}
136+
else
137+
build.group {
138+
node
139+
build.assign {
140+
names: node[2]
141+
values: node[3]
142+
}
135143
}
136-
}
137144
else
138145
nil
139146

moonscript/types.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ is_slice = function(node)
2424
end
2525
local t = { }
2626
local node_types = {
27+
class = {
28+
{
29+
"name",
30+
"Tmp"
31+
},
32+
{
33+
"body",
34+
t
35+
}
36+
},
2737
fndef = {
2838
{
2939
"args",

moonscript/types.moon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ is_slice = (node) ->
2525

2626
t = {}
2727
node_types = {
28+
class: {
29+
{"name", "Tmp"}
30+
{"body", t}
31+
}
2832
fndef: {
2933
{"args", t}
3034
{"whitelist", t}

tests/inputs/export.moon

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11

2+
export a,b,c = 223, 343
3+
export cool = "dad"
4+
5+
export class Something
6+
umm: "cool"
7+
28
What = if this
39
232
410
else
@@ -36,5 +42,3 @@ with tmp
3642
j = 2000
3743

3844

39-
export a,b,c = 223, 343
40-
export cool = "dad"

tests/outputs/export.lua

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
a, b, c = 223, 343
2+
cool = "dad"
3+
Something = (function()
4+
local _parent_0 = nil
5+
local _base_0 = {
6+
umm = "cool"
7+
}
8+
_base_0.__index = _base_0
9+
if _parent_0 then
10+
setmetatable(_base_0, getmetatable(_parent_0).__index)
11+
end
12+
local _class_0 = setmetatable({
13+
__init = function(self, ...)
14+
if _parent_0 then
15+
return _parent_0.__init(self, ...)
16+
end
17+
end
18+
}, {
19+
__index = _base_0,
20+
__call = function(cls, ...)
21+
local _self_0 = setmetatable({}, _base_0)
22+
cls.__init(_self_0, ...)
23+
return _self_0
24+
end
25+
})
26+
_base_0.__class = _class_0
27+
return _class_0
28+
end)()
129
local What
230
if this then
331
What = 232
@@ -28,6 +56,4 @@ end
2856
do
2957
local _with_0 = tmp
3058
local j = 2000
31-
end
32-
a, b, c = 223, 343
33-
cool = "dad"
59+
end

0 commit comments

Comments
 (0)