Skip to content

Commit b8a8667

Browse files
committed
fix gkz#1098
Most of the time, the precedence difference between `&&` and `||` doesn't need to be attended, because it's the same in LiveScript and JavaScript. But when synthetic `&&` nodes are created, it's important to ensure that the children of the node either must have had higher precedence when parsed, are also synthetic and represent expressions with higher precedence, or are wrapped in `Parens`. The reported bug, and a similar issue with the binary `?` operator, are the consequences if not.
1 parent d36873e commit b8a8667

4 files changed

Lines changed: 16 additions & 4 deletions

File tree

lib/ast.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ast.ls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ class exports.Binary extends Node
14561456

14571457
compileExistence: (o) ->
14581458
if @void or not o.level
1459-
x = Binary \&& Existence(@first, true), @second
1459+
x = Binary \&& Existence(@first, true), Parens @second.unwrap!
14601460
return (x <<< {+void})compile-node o
14611461
x = @first.cache o, true
14621462
sn(this, If(Existence x.0; x.1)add-else(@second)compile-expression o)
@@ -2799,7 +2799,7 @@ class exports.If extends Node
27992799
{then: thn, else: els or Literal \void} = this
28002800
@void and thn.void = els.void = true
28012801
if not @else and (@cond or @void)
2802-
return Parens Binary \&& @if, thn .compile o
2802+
return Parens Binary \&& @if, (Parens thn.unwrap!) .compile o
28032803
code = [sn(this, @if.compile o, LEVEL_COND)]
28042804
pad = if els.is-complex! then \\n + o.indent += TAB else ' '
28052805
code.push "#pad", sn(thn, "? "), (thn.compile o, LEVEL_LIST), "#pad", sn(els, ": "), (els.compile o, LEVEL_LIST)

test/existence.ls

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,11 @@ eq val.join(' '), '5 4 3 2 1'
269269

270270
# Ensure `var that` is declared even if the tested variable exists
271271
eq 'var a, that, b;\na = 0;\nif ((that = a) != null) {\n b = that;\n}', LiveScript.compile 'a = 0; b = that if a?' {+bare,-header}
272+
273+
# `?` has lower precedence than `or`
274+
f = -> ok false
275+
a = 0 ? f! or f!
276+
eq 0 a
277+
278+
# ... even when result is not used
279+
0 ? f! or f!

test/if.ls

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,7 @@ else if true =>
168168
ok 0
169169
else
170170
ok 0
171+
172+
# https://github.com/gkz/LiveScript/issues/1098
173+
f = -> ok false
174+
while (if false then f! or f!) then

0 commit comments

Comments
 (0)