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 2 commits
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
22 changes: 19 additions & 3 deletions lib/coffeescript/grammar.js

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

10 changes: 10 additions & 0 deletions lib/coffeescript/nodes.js

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

31 changes: 28 additions & 3 deletions lib/coffeescript/parser.js

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

22 changes: 19 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.hasParentheses()
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can use unwrap

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nice.

new Value $1
else
$1
o 'Range', -> new Value $1
o 'Invocation', -> new Value $1
o 'This'
Expand Down Expand Up @@ -597,8 +601,20 @@ 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`.
if $2.hasStatementLiteral()
LOC(2)(Block.wrap [$2])
else
new Parens $2
o '( INDENT Body OUTDENT )', ->
if $3.hasStatementLiteral()
LOC(3)(Block.wrap [$3])
else
new Parens $3
]

# The condition portion of a while loop.
Expand Down
6 changes: 6 additions & 0 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,12 @@ exports.Base = class Base
answer = answer.concat fragments
answer

hasStatementLiteral: ->
@contains (n) -> n instanceof StatementLiteral

hasParentheses: ->
@ instanceof Parens

#### HoistTarget

# A **HoistTargetNode** represents the output location in the node tree for a hoisted node.
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, []