allow assignment in unless block, fixes #251

This commit is contained in:
leaf corcoran 2016-09-25 14:07:35 -07:00
parent dfc4f7b016
commit c19a7fd2c3
4 changed files with 58 additions and 11 deletions

View File

@ -484,17 +484,35 @@ return Transformer({
return wrapped
end,
unless = function(self, node)
local clause = node[2]
if ntype(clause) == "assign" then
if destructure.has_destructure(clause[2]) then
error("destructure not allowed in unless assignment")
end
return build["do"]({
clause,
{
"if",
{
"not",
clause[2][1]
},
unpack(node, 3)
}
})
else
return {
"if",
{
"not",
{
"parens",
node[2]
clause
}
},
unpack(node, 3)
}
end
end,
["if"] = function(self, node, ret)
if ntype(node[2]) == "assign" then

View File

@ -280,7 +280,19 @@ Transformer {
wrapped
unless: (node) =>
{ "if", {"not", {"parens", node[2]}}, unpack node, 3 }
clause = node[2]
if ntype(clause) == "assign"
if destructure.has_destructure clause[2]
error "destructure not allowed in unless assignment"
build.do {
clause
{ "if", {"not", clause[2][1]}, unpack node, 3 }
}
else
{ "if", {"not", {"parens", clause}}, unpack node, 3 }
if: (node, ret) =>
-- expand assign in cond
@ -300,7 +312,7 @@ Transformer {
}
else
name = assign[2][1]
return build["do"] {
return build.do {
assign
{"if", name, unpack node, 3}
}

View File

@ -147,6 +147,14 @@ print "hello" unless value
dddd = {1,2,3} unless value
--
do
j = 100
unless j = hi!
error "not j!"
----------------
a = 12

View File

@ -246,6 +246,15 @@ if not (value) then
3
}
end
do
local j = 100
do
j = hi()
if not j then
error("not j!")
end
end
end
local a = 12
local c, b
if something then