mirror of
https://github.com/leafo/moonscript.git
synced 2024-11-22 02:44:23 +00:00
classes can implicitly return #46
This commit is contained in:
parent
1be72f57b7
commit
f8925a46a6
@ -877,7 +877,7 @@ Statement = Transformer({
|
||||
if_stm
|
||||
})
|
||||
end,
|
||||
class = function(self, node)
|
||||
class = function(self, node, ret)
|
||||
local _, name, parent_val, body = unpack(node)
|
||||
local statements = { }
|
||||
local properties = { }
|
||||
@ -1213,7 +1213,12 @@ Statement = Transformer({
|
||||
values = {
|
||||
_with_0.block_exp(out_body)
|
||||
}
|
||||
})
|
||||
}),
|
||||
(function()
|
||||
if ret then
|
||||
return ret(name)
|
||||
end
|
||||
end)()
|
||||
})
|
||||
end
|
||||
return value
|
||||
@ -1342,15 +1347,15 @@ implicitly_return = function(scope)
|
||||
stm = scope.transform.statement(stm)
|
||||
t = ntype(stm)
|
||||
end
|
||||
if types.manual_return[t] or not types.is_value(stm) then
|
||||
if types.cascading[t] then
|
||||
is_top = false
|
||||
return scope.transform.statement(stm, fn)
|
||||
elseif types.manual_return[t] or not types.is_value(stm) then
|
||||
if is_top and t == "return" and stm[2] == "" then
|
||||
return nil
|
||||
else
|
||||
return stm
|
||||
end
|
||||
elseif types.cascading[t] then
|
||||
is_top = false
|
||||
return scope.transform.statement(stm, fn)
|
||||
else
|
||||
if t == "comprehension" and not types.comprehension_has_value(stm) then
|
||||
return stm
|
||||
|
@ -439,7 +439,7 @@ Statement = Transformer {
|
||||
if_stm
|
||||
}
|
||||
|
||||
class: (node) =>
|
||||
class: (node, ret) =>
|
||||
_, name, parent_val, body = unpack node
|
||||
|
||||
-- split apart properties and statements
|
||||
@ -620,6 +620,8 @@ Statement = Transformer {
|
||||
names: {name}
|
||||
values: {.block_exp out_body}
|
||||
}
|
||||
if ret
|
||||
ret name
|
||||
}
|
||||
|
||||
value
|
||||
@ -694,15 +696,15 @@ implicitly_return = (scope) ->
|
||||
stm = scope.transform.statement stm
|
||||
t = ntype stm
|
||||
|
||||
if types.manual_return[t] or not types.is_value stm
|
||||
if types.cascading[t]
|
||||
is_top = false
|
||||
scope.transform.statement stm, fn
|
||||
elseif types.manual_return[t] or not types.is_value stm
|
||||
-- remove blank return statement
|
||||
if is_top and t == "return" and stm[2] == ""
|
||||
nil
|
||||
else
|
||||
stm
|
||||
elseif types.cascading[t]
|
||||
is_top = false
|
||||
scope.transform.statement stm, fn
|
||||
else
|
||||
if t == "comprehension" and not types.comprehension_has_value stm
|
||||
stm
|
||||
|
@ -12,7 +12,8 @@ cascading = data.Set({
|
||||
"if",
|
||||
"unless",
|
||||
"with",
|
||||
"switch"
|
||||
"switch",
|
||||
"class"
|
||||
})
|
||||
is_value = function(stm)
|
||||
local compile, transform = moonscript.compile, moonscript.transform
|
||||
|
@ -14,7 +14,7 @@ manual_return = data.Set{"foreach", "for", "while", "return"}
|
||||
-- Assigns and returns are bubbled into their bodies.
|
||||
-- All cascading statement transform functions accept a second arugment that
|
||||
-- is the transformation to apply to the last statement in their body
|
||||
cascading = data.Set{ "if", "unless", "with", "switch" }
|
||||
cascading = data.Set{ "if", "unless", "with", "switch", "class" }
|
||||
|
||||
is_value = (stm) ->
|
||||
import compile, transform from moonscript
|
||||
|
@ -129,4 +129,7 @@ class Whacko
|
||||
|
||||
print "hello"
|
||||
|
||||
yyy = ->
|
||||
class Cool
|
||||
nil
|
||||
|
||||
|
@ -589,4 +589,48 @@ Whacko = (function()
|
||||
end
|
||||
return _class_0
|
||||
end)()
|
||||
return print("hello")
|
||||
print("hello")
|
||||
local yyy
|
||||
yyy = function()
|
||||
local Cool
|
||||
Cool = (function()
|
||||
local _parent_0 = nil
|
||||
local _base_0 = { }
|
||||
_base_0.__index = _base_0
|
||||
if _parent_0 then
|
||||
setmetatable(_base_0, _parent_0.__base)
|
||||
end
|
||||
local _class_0 = setmetatable({
|
||||
__init = function(self, ...)
|
||||
if _parent_0 then
|
||||
return _parent_0.__init(self, ...)
|
||||
end
|
||||
end,
|
||||
__base = _base_0,
|
||||
__name = "Cool",
|
||||
__parent = _parent_0
|
||||
}, {
|
||||
__index = function(cls, name)
|
||||
local val = rawget(_base_0, name)
|
||||
if val == nil and _parent_0 then
|
||||
return _parent_0[name]
|
||||
else
|
||||
return val
|
||||
end
|
||||
end,
|
||||
__call = function(cls, ...)
|
||||
local _self_0 = setmetatable({}, _base_0)
|
||||
cls.__init(_self_0, ...)
|
||||
return _self_0
|
||||
end
|
||||
})
|
||||
_base_0.__class = _class_0
|
||||
local self = _class_0
|
||||
_ = nil
|
||||
if _parent_0 and _parent_0.__inherited then
|
||||
_parent_0.__inherited(_parent_0, _class_0)
|
||||
end
|
||||
return _class_0
|
||||
end)()
|
||||
return Cool
|
||||
end
|
Loading…
Reference in New Issue
Block a user