diff --git a/moonscript/transform/statement.lua b/moonscript/transform/statement.lua index 47dd0de..7303666 100644 --- a/moonscript/transform/statement.lua +++ b/moonscript/transform/statement.lua @@ -484,17 +484,35 @@ return Transformer({ return wrapped end, unless = function(self, node) - return { - "if", - { - "not", + 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, { - "parens", - node[2] + "if", + { + "not", + clause[2][1] + }, + unpack(node, 3) } - }, - unpack(node, 3) - } + }) + else + return { + "if", + { + "not", + { + "parens", + clause + } + }, + unpack(node, 3) + } + end end, ["if"] = function(self, node, ret) if ntype(node[2]) == "assign" then diff --git a/moonscript/transform/statement.moon b/moonscript/transform/statement.moon index 42c3ec7..f5cf3a5 100644 --- a/moonscript/transform/statement.moon +++ b/moonscript/transform/statement.moon @@ -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} } diff --git a/spec/inputs/cond.moon b/spec/inputs/cond.moon index 3bf966f..18e42b9 100644 --- a/spec/inputs/cond.moon +++ b/spec/inputs/cond.moon @@ -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 diff --git a/spec/outputs/cond.lua b/spec/outputs/cond.lua index 30e8762..35e7ac5 100644 --- a/spec/outputs/cond.lua +++ b/spec/outputs/cond.lua @@ -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