Skip to content

Fix #3441: parentheses wrapping expression throw invalid error #4849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions lib/coffeescript/grammar.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 36 additions & 3 deletions lib/coffeescript/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions src/grammar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,11 @@ grammar =
Value: [
o 'Assignable'
o 'Literal', -> new Value $1
o 'Parenthetical', -> new Value $1
o 'Parenthetical', ->
if $1.constructor.name is 'Parens'
new Value $1
else
$1
o 'Range', -> new Value $1
o 'Invocation', -> new Value $1
o 'This'
Expand Down Expand Up @@ -597,8 +601,22 @@ grammar =
# where only values are accepted, wrapping it in parentheses will always do
# the trick.
Parenthetical: [
o '( Body )', -> new Parens $2
o '( INDENT Body OUTDENT )', -> new Parens $3
o '( Body )', ->
# When `Parens` block includes a `StatementLiteral` (e.g. `(b; break) for a in arr`),
# it won't compile since `Parens` (`(b; break)`) is compiled as `Value` and
# pure statement (`break`) can't be used in an expression.
# For this reasons, we return `Block`, which is passed further to `Value` and `Expression`.
stm = $2.contains (n) -> n.constructor.name is 'StatementLiteral'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather we don't add such code to the grammar. We can have a node function for that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we try not to use constructor.name for obfuscation reasons IIRC.

if stm
LOC(2)(Block.wrap [$2])
else
new Parens $2
o '( INDENT Body OUTDENT )', ->
stm = $3.contains (n) -> n.constructor.name is 'StatementLiteral'
if stm
LOC(3)(Block.wrap [$3])
else
new Parens $3
]

# The condition portion of a while loop.
Expand Down
31 changes: 31 additions & 0 deletions test/control_flow.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,34 @@ test "#4267: lots of for-loops in the same scope", ->
true
"""
ok CoffeeScript.eval(code)

# Issue #3441: Parentheses wrapping expression throw invalid error in `then` clause
test "#3441: `StatementLiteral` in parentheses", ->
i = 0
r1 = ((i++; break) while i < 10)
arrayEq r1, []

i = 0
r2 = ((i++; continue) while i < 10)
arrayEq r2, []

i = 0
r4 = while i < 10 then (i++; break)
arrayEq r4, []

i = 0
r5 = while i < 10 then (i++; continue)
arrayEq r5, []

arr = [0..9]
r6 = ((a; break) for a in arr)
arrayEq r6, []

r7 = ((a; continue) for a in arr)
arrayEq r7, []

r8 = for a in arr then (a; break)
arrayEq r8, []

r9 = for a in arr then (a; continue)
arrayEq r9, []