Skip to content

Commit e55fa25

Browse files
committed
class inner expressions run in class scope
1 parent 255a92f commit e55fa25

File tree

4 files changed

+67
-13
lines changed

4 files changed

+67
-13
lines changed

moonscript/compile.lua

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require("moonscript.compile.format")
55
require("moonscript.compile.statement")
66
require("moonscript.compile.value")
77
local transform = require("moonscript.transform")
8-
local NameProxy = transform.NameProxy
8+
local NameProxy, LocalName = transform.NameProxy, transform.LocalName
99
local Set
1010
do
1111
local _table_0 = require("moonscript.data")
@@ -122,15 +122,19 @@ Block = (function()
122122
local _list_0 = names
123123
for _index_0 = 1, #_list_0 do
124124
local name = _list_0[_index_0]
125-
local t = util.moon.type(name)
125+
local is_local = false
126126
local real_name
127-
if t == NameProxy then
127+
local _exp_0 = util.moon.type(name)
128+
if LocalName == _exp_0 then
129+
is_local = true
128130
real_name = name:get_name(self)
129-
elseif t == "string" then
131+
elseif NameProxy == _exp_0 then
132+
real_name = name:get_name(self)
133+
elseif "string" == _exp_0 then
130134
real_name = name
131135
end
132136
local _value_0
133-
if real_name and not self:has_name(real_name) then
137+
if is_local or real_name and not self:has_name(real_name) then
134138
_value_0 = real_name
135139
end
136140
if _value_0 ~= nil then

moonscript/compile.moon

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require "moonscript.compile.value"
99

1010
transform = require "moonscript.transform"
1111

12-
import NameProxy from transform
12+
import NameProxy, LocalName from transform
1313
import Set from require "moonscript.data"
1414
import ntype from require "moonscript.types"
1515

@@ -88,12 +88,15 @@ class Block
8888

8989
declare: (names) =>
9090
undeclared = for name in *names
91-
t = util.moon.type(name)
92-
real_name = if t == NameProxy
93-
name\get_name self
94-
elseif t == "string"
95-
name
96-
real_name if real_name and not @has_name real_name
91+
is_local = false
92+
real_name = switch util.moon.type name
93+
when LocalName
94+
is_local = true
95+
name\get_name self
96+
when NameProxy then name\get_name self
97+
when "string" then name
98+
99+
real_name if is_local or real_name and not @has_name real_name
97100

98101
@put_name name for name in *undeclared
99102
undeclared

moonscript/transform.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,33 @@ local data = require("moonscript.data")
55
local reversed = util.reversed
66
local ntype, build, smart_node, is_slice = types.ntype, types.build, types.smart_node, types.is_slice
77
local insert = table.insert
8+
LocalName = (function()
9+
local _parent_0 = nil
10+
local _base_0 = {
11+
get_name = function(self)
12+
return self.name
13+
end
14+
}
15+
_base_0.__index = _base_0
16+
if _parent_0 then
17+
setmetatable(_base_0, getmetatable(_parent_0).__index)
18+
end
19+
local _class_0 = setmetatable({
20+
__init = function(self, name)
21+
self.name = name
22+
self[1] = "temp_name"
23+
end
24+
}, {
25+
__index = _base_0,
26+
__call = function(cls, ...)
27+
local _self_0 = setmetatable({}, _base_0)
28+
cls.__init(_self_0, ...)
29+
return _self_0
30+
end
31+
})
32+
_base_0.__class = _class_0
33+
return _class_0
34+
end)()
835
NameProxy = (function()
936
local _parent_0 = nil
1037
local _base_0 = {
@@ -771,6 +798,16 @@ Statement = Transformer({
771798
}),
772799
_with_0.assign_one(cls_name, cls),
773800
_with_0.assign_one(base_name:chain("__class"), cls_name),
801+
_with_0.group((function()
802+
if #statements > 0 then
803+
return {
804+
_with_0.assign_one(LocalName("self"), cls_name),
805+
_with_0.group(statements)
806+
}
807+
else
808+
return { }
809+
end
810+
end)()),
774811
cls_name
775812
})
776813
value = _with_0.group({

moonscript/transform.moon

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import reversed from util
99
import ntype, build, smart_node, is_slice from types
1010
import insert from table
1111

12-
export Statement, Value, NameProxy, Run
12+
export Statement, Value, NameProxy, LocalName, Run
13+
14+
-- always declares as local
15+
class LocalName
16+
new: (@name) => self[1] = "temp_name"
17+
get_name: => @name
1318

1419
class NameProxy
1520
new: (@prefix) =>
@@ -428,6 +433,11 @@ Statement = Transformer {
428433
.assign_one cls_name, cls
429434
.assign_one base_name\chain"__class", cls_name
430435

436+
.group if #statements > 0 {
437+
.assign_one LocalName"self", cls_name
438+
.group statements
439+
} else {}
440+
431441
cls_name
432442
}
433443

0 commit comments

Comments
 (0)