mirror of
https://github.com/leafo/moonscript.git
synced 2024-11-22 02:44:23 +00:00
destructure from value directly if local
This commit is contained in:
parent
71dad43c03
commit
9ac382fc49
@ -429,6 +429,16 @@ do
|
||||
return yes
|
||||
end
|
||||
end,
|
||||
is_local = function(self, node)
|
||||
local t = mtype(node)
|
||||
if t == "string" then
|
||||
return self:has_name(node, false)
|
||||
end
|
||||
if t == NameProxy or t == LocalName then
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end,
|
||||
free_name = function(self, prefix, dont_put)
|
||||
prefix = prefix or "moon"
|
||||
local searching = true
|
||||
|
@ -241,6 +241,8 @@ class Block
|
||||
name = name\get_name self if NameProxy == mtype name
|
||||
@_names[name] = value
|
||||
|
||||
-- Check if a name is defined in the current or any enclosing scope
|
||||
-- skip_exports: ignore names that have been exported using `export`
|
||||
has_name: (name, skip_exports) =>
|
||||
return true if not skip_exports and @name_exported name
|
||||
|
||||
@ -251,6 +253,12 @@ class Block
|
||||
else
|
||||
yes
|
||||
|
||||
is_local: (node) =>
|
||||
t = mtype node
|
||||
return @has_name(node, false) if t == "string"
|
||||
return true if t == NameProxy or t == LocalName
|
||||
false
|
||||
|
||||
free_name: (prefix, dont_put) =>
|
||||
prefix = prefix or "moon"
|
||||
searching = true
|
||||
|
@ -411,7 +411,7 @@ local Statement = Transformer({
|
||||
end
|
||||
node = transformed or node
|
||||
if destructure.has_destructure(names) then
|
||||
return destructure.split_assign(node)
|
||||
return destructure.split_assign(self, node)
|
||||
end
|
||||
return node
|
||||
end,
|
||||
@ -646,7 +646,7 @@ local Statement = Transformer({
|
||||
if destructure.has_destructure(assign[2]) then
|
||||
local name = NameProxy("des")
|
||||
body = {
|
||||
destructure.build_assign(assign[2][1], name),
|
||||
destructure.build_assign(self, assign[2][1], name),
|
||||
build.group(node[3])
|
||||
}
|
||||
return build["do"]({
|
||||
@ -725,7 +725,7 @@ local Statement = Transformer({
|
||||
do
|
||||
local _with_0 = NameProxy("des")
|
||||
local proxy = _with_0
|
||||
insert(destructures, destructure.build_assign(name, proxy))
|
||||
insert(destructures, destructure.build_assign(self, name, proxy))
|
||||
_accum_0[_len_0] = _with_0
|
||||
end
|
||||
else
|
||||
|
@ -189,8 +189,9 @@ Statement = Transformer {
|
||||
|
||||
node = transformed or node
|
||||
|
||||
|
||||
if destructure.has_destructure names
|
||||
return destructure.split_assign node
|
||||
return destructure.split_assign @, node
|
||||
|
||||
-- print util.dump node
|
||||
node
|
||||
@ -304,7 +305,7 @@ Statement = Transformer {
|
||||
name = NameProxy "des"
|
||||
|
||||
body = {
|
||||
destructure.build_assign assign[2][1], name
|
||||
destructure.build_assign @, assign[2][1], name
|
||||
build.group node[3]
|
||||
}
|
||||
|
||||
@ -363,7 +364,7 @@ Statement = Transformer {
|
||||
node.names = for i, name in ipairs node.names
|
||||
if ntype(name) == "table"
|
||||
with proxy = NameProxy "des"
|
||||
insert destructures, destructure.build_assign name, proxy
|
||||
insert destructures, destructure.build_assign @, name, proxy
|
||||
else
|
||||
name
|
||||
|
||||
|
@ -108,7 +108,7 @@ extract_assign_names = function(name, accum, prefix)
|
||||
return accum
|
||||
end
|
||||
local build_assign
|
||||
build_assign = function(destruct_literal, receiver)
|
||||
build_assign = function(scope, destruct_literal, receiver)
|
||||
local extracted_names = extract_assign_names(destruct_literal)
|
||||
local names = { }
|
||||
local values = { }
|
||||
@ -118,7 +118,7 @@ build_assign = function(destruct_literal, receiver)
|
||||
values
|
||||
}
|
||||
local obj
|
||||
if mtype(receiver) == NameProxy then
|
||||
if scope:is_local(receiver) then
|
||||
obj = receiver
|
||||
else
|
||||
do
|
||||
@ -139,7 +139,7 @@ build_assign = function(destruct_literal, receiver)
|
||||
for _index_0 = 1, #_list_0 do
|
||||
local tuple = _list_0[_index_0]
|
||||
insert(names, tuple[1])
|
||||
insert(values, obj:chain(unpack(tuple[2])))
|
||||
insert(values, NameProxy.chain(obj, unpack(tuple[2])))
|
||||
end
|
||||
return build.group({
|
||||
{
|
||||
@ -150,7 +150,7 @@ build_assign = function(destruct_literal, receiver)
|
||||
})
|
||||
end
|
||||
local split_assign
|
||||
split_assign = function(assign)
|
||||
split_assign = function(scope, assign)
|
||||
local names, values = unpack(assign, 2)
|
||||
local g = { }
|
||||
local total_names = #names
|
||||
@ -182,7 +182,7 @@ split_assign = function(assign)
|
||||
end)()
|
||||
})
|
||||
end
|
||||
insert(g, build_assign(n, values[i]))
|
||||
insert(g, build_assign(scope, n, values[i]))
|
||||
start = i + 1
|
||||
end
|
||||
end
|
||||
|
@ -47,7 +47,7 @@ extract_assign_names = (name, accum={}, prefix={}) ->
|
||||
|
||||
accum
|
||||
|
||||
build_assign = (destruct_literal, receiver) ->
|
||||
build_assign = (scope, destruct_literal, receiver) ->
|
||||
extracted_names = extract_assign_names destruct_literal
|
||||
|
||||
names = {}
|
||||
@ -55,7 +55,7 @@ build_assign = (destruct_literal, receiver) ->
|
||||
|
||||
inner = {"assign", names, values}
|
||||
|
||||
obj = if mtype(receiver) == NameProxy
|
||||
obj = if scope\is_local receiver
|
||||
receiver
|
||||
else
|
||||
with obj = NameProxy "obj"
|
||||
@ -66,7 +66,8 @@ build_assign = (destruct_literal, receiver) ->
|
||||
|
||||
for tuple in *extracted_names
|
||||
insert names, tuple[1]
|
||||
insert values, obj\chain unpack tuple[2]
|
||||
insert values, NameProxy.chain obj, unpack tuple[2]
|
||||
-- insert values, obj\chain unpack tuple[2]
|
||||
|
||||
build.group {
|
||||
{"declare", names}
|
||||
@ -74,7 +75,7 @@ build_assign = (destruct_literal, receiver) ->
|
||||
}
|
||||
|
||||
-- applies to destructuring to a assign node
|
||||
split_assign = (assign) ->
|
||||
split_assign = (scope, assign) ->
|
||||
names, values = unpack assign, 2
|
||||
|
||||
g = {}
|
||||
@ -96,7 +97,7 @@ split_assign = (assign) ->
|
||||
values[i]
|
||||
}
|
||||
|
||||
insert g, build_assign n, values[i]
|
||||
insert g, build_assign scope, n, values[i]
|
||||
|
||||
start = i + 1
|
||||
|
||||
|
@ -76,3 +76,21 @@ elseif {c,d} = thang
|
||||
else
|
||||
print "NO"
|
||||
|
||||
|
||||
--
|
||||
|
||||
do
|
||||
z = "yeah"
|
||||
{a,b,c} = z
|
||||
|
||||
do
|
||||
{a,b,c} = z
|
||||
|
||||
(z) ->
|
||||
{a,b,c} = z
|
||||
|
||||
do
|
||||
z = "oo"
|
||||
(k) ->
|
||||
{a,b,c} = z
|
||||
|
||||
|
@ -14,10 +14,7 @@ do
|
||||
hello, world = _obj_0.hello, _obj_0.world
|
||||
end
|
||||
local no, thing
|
||||
do
|
||||
local _obj_0 = world
|
||||
no, thing = _obj_0.yes, _obj_0[1]
|
||||
end
|
||||
no, thing = world.yes, world[1]
|
||||
local d
|
||||
do
|
||||
local _obj_0 = yeah
|
||||
@ -69,23 +66,11 @@ local futurists = {
|
||||
}
|
||||
}
|
||||
local name, street, city
|
||||
do
|
||||
local _obj_0 = futurists
|
||||
name, street, city = _obj_0.poet.name, _obj_0.poet.address[1], _obj_0.poet.address[2]
|
||||
end
|
||||
name, street, city = futurists.poet.name, futurists.poet.address[1], futurists.poet.address[2]
|
||||
print(name, street, city)
|
||||
do
|
||||
local _obj_0 = x
|
||||
self.world = _obj_0[1]
|
||||
end
|
||||
do
|
||||
local _obj_0 = x
|
||||
a.b, c.y, func().z = _obj_0[1], _obj_0[2], _obj_0[3]
|
||||
end
|
||||
do
|
||||
local _obj_0 = x
|
||||
self.world = _obj_0.world
|
||||
end
|
||||
self.world = x[1]
|
||||
a.b, c.y, func().z = x[1], x[2], x[3]
|
||||
self.world = x.world
|
||||
thing = {
|
||||
{
|
||||
1,
|
||||
@ -140,8 +125,27 @@ do
|
||||
c, d = _des_1[1], _des_1[2]
|
||||
print(c, d)
|
||||
else
|
||||
return print("NO")
|
||||
print("NO")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
do
|
||||
local z = "yeah"
|
||||
a, b, c = z[1], z[2], z[3]
|
||||
end
|
||||
do
|
||||
do
|
||||
local _obj_0 = z
|
||||
a, b, c = _obj_0[1], _obj_0[2], _obj_0[3]
|
||||
end
|
||||
end
|
||||
_ = function(z)
|
||||
a, b, c = z[1], z[2], z[3]
|
||||
end
|
||||
do
|
||||
local z = "oo"
|
||||
return function(k)
|
||||
a, b, c = z[1], z[2], z[3]
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user