Skip to content
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

[CS2] Fix #3199: throw multiline implicit object #4599

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions lib/coffeescript/grammar.js

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

8 changes: 5 additions & 3 deletions lib/coffeescript/lexer.js

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

318 changes: 162 additions & 156 deletions lib/coffeescript/parser.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/grammar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ grammar =
# A return statement from a function body.
Return: [
o 'RETURN Expression', -> new Return $2
o 'RETURN INDENT Object OUTDENT', -> new Return new Value $3
o 'RETURN', -> new Return
]

Expand Down Expand Up @@ -557,6 +558,7 @@ grammar =
# Throw an exception object.
Throw: [
o 'THROW Expression', -> new Throw $2
o 'THROW INDENT Object OUTDENT', -> new Throw new Value $3
]

# Parenthetical expressions. Note that the **Parenthetical** is a **Value**,
Expand Down
11 changes: 7 additions & 4 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ exports.Lexer = class Lexer
return indent.length

if size > @indent
if noNewlines or @tag() is 'RETURN'
if noNewlines
@indebt = size - @indent
@suppressNewlines()
return indent.length
Expand Down Expand Up @@ -888,9 +888,7 @@ exports.Lexer = class Lexer
# Are we in the midst of an unfinished expression?
unfinished: ->
LINE_CONTINUER.test(@chunk) or
@tag() in ['\\', '.', '?.', '?::', 'UNARY', 'MATH', 'UNARY_MATH', '+', '-',
'**', 'SHIFT', 'RELATION', 'COMPARE', '&', '^', '|', '&&', '||',
'BIN?', 'THROW', 'EXTENDS', 'DEFAULT']
@tag() in UNFINISHED

formatString: (str, options) ->
@replaceUnicodeCodePointEscapes str.replace(STRING_OMIT, '$1'), options
Expand Down Expand Up @@ -1250,3 +1248,8 @@ LINE_BREAK = ['INDENT', 'OUTDENT', 'TERMINATOR']

# Additional indent in front of these is ignored.
INDENTABLE_CLOSERS = [')', '}', ']']

# Tokens that, when appearing at the end of a line, suppress a following TERMINATOR/INDENT token
UNFINISHED = ['\\', '.', '?.', '?::', 'UNARY', 'MATH', 'UNARY_MATH', '+', '-',
'**', 'SHIFT', 'RELATION', 'COMPARE', '&', '^', '|', '&&', '||',
'BIN?', 'EXTENDS', 'DEFAULT']
62 changes: 62 additions & 0 deletions test/error_messages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1636,3 +1636,65 @@ test "#4283: error message for implicit call", ->
(a, b c) ->
^
'''

test "#3199: error message for call indented non-object", ->
assertErrorFormat '''
fn = ->
fn
1
''', '''
[stdin]:3:1: error: unexpected indentation
1
^^
'''

test "#3199: error message for call indented comprehension", ->
assertErrorFormat '''
fn = ->
fn
x for x in [1, 2, 3]
''', '''
[stdin]:3:1: error: unexpected indentation
x for x in [1, 2, 3]
^^
'''

test "#3199: error message for return indented non-object", ->
assertErrorFormat '''
return
1
''', '''
[stdin]:2:3: error: unexpected number
1
^
'''

test "#3199: error message for return indented comprehension", ->
assertErrorFormat '''
return
x for x in [1, 2, 3]
''', '''
[stdin]:2:3: error: unexpected identifier
x for x in [1, 2, 3]
^
'''

test "#3199: error message for throw indented non-object", ->
assertErrorFormat '''
throw
1
''', '''
[stdin]:2:3: error: unexpected number
1
^
'''

test "#3199: error message for throw indented comprehension", ->
assertErrorFormat '''
throw
x for x in [1, 2, 3]
''', '''
[stdin]:2:3: error: unexpected identifier
x for x in [1, 2, 3]
^
'''
13 changes: 13 additions & 0 deletions test/formatting.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,16 @@ test "#3906: handle further indentation inside indented chain", ->
)
1
'''

test "#3199: throw multiline implicit object", ->
x = do ->
if no then throw
type: 'a'
msg: 'b'
eq undefined, x

y = do ->
if no then return
type: 'a'
msg: 'b'
eq undefined, y