Skip to content

Commit af2c443

Browse files
committed
don't alter class constructor at all
1 parent a44c5ac commit af2c443

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

moonscript/transform.lua

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ local Statement = Transformer({
882882
end
883883
end
884884
end
885-
local constructor = nil
885+
local constructor
886886
properties = (function()
887887
local _accum_0 = { }
888888
local _len_0 = 1
@@ -914,7 +914,7 @@ local Statement = Transformer({
914914
local base_name = NameProxy("base")
915915
local self_name = NameProxy("self")
916916
local cls_name = NameProxy("class")
917-
if not constructor then
917+
if not (constructor) then
918918
constructor = build.fndef({
919919
args = {
920920
{
@@ -939,9 +939,6 @@ local Statement = Transformer({
939939
})
940940
}
941941
})
942-
else
943-
smart_node(constructor)
944-
constructor.arrow = "fat"
945942
end
946943
local real_name = name or parent_assign and parent_assign[2][1]
947944
local _exp_0 = ntype(real_name)

moonscript/transform.moon

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ Statement = Transformer {
482482
insert properties, tuple
483483

484484
-- find constructor
485-
constructor = nil
485+
local constructor
486486
properties = for tuple in *properties
487487
key = tuple[1]
488488
if key[1] == "key_literal" and key[2] == constructor_name
@@ -496,7 +496,7 @@ Statement = Transformer {
496496
self_name = NameProxy "self"
497497
cls_name = NameProxy "class"
498498

499-
if not constructor
499+
unless constructor
500500
constructor = build.fndef {
501501
args: {{"..."}}
502502
arrow: "fat"
@@ -509,9 +509,6 @@ Statement = Transformer {
509509
}
510510
}
511511
}
512-
else
513-
smart_node constructor
514-
constructor.arrow = "fat"
515512

516513
real_name = name or parent_assign and parent_assign[2][1]
517514
real_name = switch ntype real_name

tests/inputs/class.moon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,7 @@ class Something
172172

173173
--
174174

175+
class X
176+
new: hi
177+
175178
nil

tests/outputs/class.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,4 +1030,37 @@ do
10301030
end
10311031
Something = _class_0
10321032
end
1033+
do
1034+
local _parent_0 = nil
1035+
local _base_0 = { }
1036+
_base_0.__index = _base_0
1037+
if _parent_0 then
1038+
setmetatable(_base_0, _parent_0.__base)
1039+
end
1040+
local _class_0 = setmetatable({
1041+
__init = hi,
1042+
__base = _base_0,
1043+
__name = "X",
1044+
__parent = _parent_0
1045+
}, {
1046+
__index = function(cls, name)
1047+
local val = rawget(_base_0, name)
1048+
if val == nil and _parent_0 then
1049+
return _parent_0[name]
1050+
else
1051+
return val
1052+
end
1053+
end,
1054+
__call = function(cls, ...)
1055+
local _self_0 = setmetatable({}, _base_0)
1056+
cls.__init(_self_0, ...)
1057+
return _self_0
1058+
end
1059+
})
1060+
_base_0.__class = _class_0
1061+
if _parent_0 and _parent_0.__inherited then
1062+
_parent_0.__inherited(_parent_0, _class_0)
1063+
end
1064+
X = _class_0
1065+
end
10331066
return nil

0 commit comments

Comments
 (0)