Skip to content

Commit c19a7fd

Browse files
committed
allow assignment in unless block, fixes #251
1 parent dfc4f7b commit c19a7fd

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

moonscript/transform/statement.lua

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -484,17 +484,35 @@ return Transformer({
484484
return wrapped
485485
end,
486486
unless = function(self, node)
487-
return {
488-
"if",
489-
{
490-
"not",
487+
local clause = node[2]
488+
if ntype(clause) == "assign" then
489+
if destructure.has_destructure(clause[2]) then
490+
error("destructure not allowed in unless assignment")
491+
end
492+
return build["do"]({
493+
clause,
491494
{
492-
"parens",
493-
node[2]
495+
"if",
496+
{
497+
"not",
498+
clause[2][1]
499+
},
500+
unpack(node, 3)
494501
}
495-
},
496-
unpack(node, 3)
497-
}
502+
})
503+
else
504+
return {
505+
"if",
506+
{
507+
"not",
508+
{
509+
"parens",
510+
clause
511+
}
512+
},
513+
unpack(node, 3)
514+
}
515+
end
498516
end,
499517
["if"] = function(self, node, ret)
500518
if ntype(node[2]) == "assign" then

moonscript/transform/statement.moon

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,19 @@ Transformer {
280280
wrapped
281281

282282
unless: (node) =>
283-
{ "if", {"not", {"parens", node[2]}}, unpack node, 3 }
283+
clause = node[2]
284+
285+
if ntype(clause) == "assign"
286+
if destructure.has_destructure clause[2]
287+
error "destructure not allowed in unless assignment"
288+
289+
build.do {
290+
clause
291+
{ "if", {"not", clause[2][1]}, unpack node, 3 }
292+
}
293+
294+
else
295+
{ "if", {"not", {"parens", clause}}, unpack node, 3 }
284296

285297
if: (node, ret) =>
286298
-- expand assign in cond
@@ -300,7 +312,7 @@ Transformer {
300312
}
301313
else
302314
name = assign[2][1]
303-
return build["do"] {
315+
return build.do {
304316
assign
305317
{"if", name, unpack node, 3}
306318
}

spec/inputs/cond.moon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ print "hello" unless value
147147

148148
dddd = {1,2,3} unless value
149149

150+
151+
--
152+
153+
do
154+
j = 100
155+
unless j = hi!
156+
error "not j!"
157+
150158
----------------
151159

152160
a = 12

spec/outputs/cond.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ if not (value) then
246246
3
247247
}
248248
end
249+
do
250+
local j = 100
251+
do
252+
j = hi()
253+
if not j then
254+
error("not j!")
255+
end
256+
end
257+
end
249258
local a = 12
250259
local c, b
251260
if something then

0 commit comments

Comments
 (0)