extract class name from assign statement if anonymous class is being assigned

This commit is contained in:
leaf corcoran 2012-11-01 09:19:04 -07:00
parent 97394fbbc9
commit f98f822891
3 changed files with 34 additions and 29 deletions

View File

@ -463,12 +463,8 @@ Statement = Transformer({
t = ntype(value) t = ntype(value)
end end
if types.cascading[t] then if types.cascading[t] then
transformed = build.group({ local ret
{ ret = function(stm)
"declare",
names
},
self.transform.statement(value, function(stm)
if types.is_value(stm) then if types.is_value(stm) then
return { return {
"assign", "assign",
@ -480,7 +476,13 @@ Statement = Transformer({
else else
return stm return stm
end end
end) end
transformed = build.group({
{
"declare",
names
},
self.transform.statement(value, ret, node)
}) })
end end
end end
@ -897,7 +899,7 @@ Statement = Transformer({
if_stm if_stm
}) })
end, end,
class = function(self, node, ret) class = function(self, node, ret, parent_assign)
local _, name, parent_val, body = unpack(node) local _, name, parent_val, body = unpack(node)
local statements = { } local statements = { }
local properties = { } local properties = { }
@ -974,10 +976,10 @@ Statement = Transformer({
smart_node(constructor) smart_node(constructor)
constructor.arrow = "fat" constructor.arrow = "fat"
end end
local real_name local real_name = name or parent_assign and parent_assign[2][1]
local _exp_0 = ntype(name) local _exp_0 = ntype(real_name)
if "chain" == _exp_0 then if "chain" == _exp_0 then
local last = name[#name] local last = real_name[#real_name]
local _exp_1 = ntype(last) local _exp_1 = ntype(last)
if "dot" == _exp_1 then if "dot" == _exp_1 then
real_name = { real_name = {
@ -996,7 +998,7 @@ Statement = Transformer({
real_name = { real_name = {
"string", "string",
'"', '"',
name real_name
} }
end end
local cls = build.table({ local cls = build.table({

View File

@ -209,13 +209,15 @@ Statement = Transformer {
t = ntype value t = ntype value
if types.cascading[t] if types.cascading[t]
build.group { ret = (stm) ->
{"declare", names}
@transform.statement value, (stm) ->
if types.is_value stm if types.is_value stm
{"assign", names, {stm}} {"assign", names, {stm}}
else else
stm stm
build.group {
{"declare", names}
@transform.statement value, ret, node
} }
transformed or node transformed or node
@ -456,7 +458,7 @@ Statement = Transformer {
if_stm if_stm
} }
class: (node, ret) => class: (node, ret, parent_assign) =>
_, name, parent_val, body = unpack node _, name, parent_val, body = unpack node
-- split apart properties and statements -- split apart properties and statements
@ -505,9 +507,10 @@ Statement = Transformer {
smart_node constructor smart_node constructor
constructor.arrow = "fat" constructor.arrow = "fat"
real_name = switch ntype(name) real_name = name or parent_assign and parent_assign[2][1]
real_name = switch ntype real_name
when "chain" when "chain"
last = name[#name] last = real_name[#real_name]
switch ntype last switch ntype last
when "dot" when "dot"
{"string", '"', last[2]} {"string", '"', last[2]}
@ -518,7 +521,7 @@ Statement = Transformer {
when "nil" when "nil"
"nil" "nil"
else else
{"string", '"', name} {"string", '"', real_name}
cls = build.table { cls = build.table {
{"__init", constructor} {"__init", constructor}

View File

@ -768,7 +768,7 @@ do
end end
end, end,
__base = _base_0, __base = _base_0,
__name = nil, __name = "a",
__parent = _parent_0 __parent = _parent_0
}, { }, {
__index = function(cls, name) __index = function(cls, name)
@ -885,7 +885,7 @@ do
end end
end, end,
__base = _base_0, __base = _base_0,
__name = nil, __name = "d",
__parent = _parent_0 __parent = _parent_0
}, { }, {
__index = function(cls, name) __index = function(cls, name)