Skip to content

Commit 50674cb

Browse files
Julian RosseGeoffreyBooth
authored andcommitted
[CS2] Fix #3199: throw multiline implicit object (#4599)
* throw multiline implicit object [Fixes #3199] * restrict to Object * test error on non-object * test error on call indented non-object
1 parent 3be9038 commit 50674cb

File tree

7 files changed

+255
-163
lines changed

7 files changed

+255
-163
lines changed

lib/coffeescript/grammar.js

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

lib/coffeescript/lexer.js

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

lib/coffeescript/parser.js

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

src/grammar.coffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ grammar =
241241
# A return statement from a function body.
242242
Return: [
243243
o 'RETURN Expression', -> new Return $2
244+
o 'RETURN INDENT Object OUTDENT', -> new Return new Value $3
244245
o 'RETURN', -> new Return
245246
]
246247

@@ -557,6 +558,7 @@ grammar =
557558
# Throw an exception object.
558559
Throw: [
559560
o 'THROW Expression', -> new Throw $2
561+
o 'THROW INDENT Object OUTDENT', -> new Throw new Value $3
560562
]
561563

562564
# Parenthetical expressions. Note that the **Parenthetical** is a **Value**,

src/lexer.coffee

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ exports.Lexer = class Lexer
413413
return indent.length
414414

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

895893
formatString: (str, options) ->
896894
@replaceUnicodeCodePointEscapes str.replace(STRING_OMIT, '$1'), options
@@ -1250,3 +1248,8 @@ LINE_BREAK = ['INDENT', 'OUTDENT', 'TERMINATOR']
12501248
12511249
# Additional indent in front of these is ignored.
12521250
INDENTABLE_CLOSERS = [')', '}', ']']
1251+
1252+
# Tokens that, when appearing at the end of a line, suppress a following TERMINATOR/INDENT token
1253+
UNFINISHED = ['\\', '.', '?.', '?::', 'UNARY', 'MATH', 'UNARY_MATH', '+', '-',
1254+
'**', 'SHIFT', 'RELATION', 'COMPARE', '&', '^', '|', '&&', '||',
1255+
'BIN?', 'EXTENDS', 'DEFAULT']

test/error_messages.coffee

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,3 +1636,65 @@ test "#4283: error message for implicit call", ->
16361636
(a, b c) ->
16371637
^
16381638
'''
1639+
1640+
test "#3199: error message for call indented non-object", ->
1641+
assertErrorFormat '''
1642+
fn = ->
1643+
fn
1644+
1
1645+
''', '''
1646+
[stdin]:3:1: error: unexpected indentation
1647+
1
1648+
^^
1649+
'''
1650+
1651+
test "#3199: error message for call indented comprehension", ->
1652+
assertErrorFormat '''
1653+
fn = ->
1654+
fn
1655+
x for x in [1, 2, 3]
1656+
''', '''
1657+
[stdin]:3:1: error: unexpected indentation
1658+
x for x in [1, 2, 3]
1659+
^^
1660+
'''
1661+
1662+
test "#3199: error message for return indented non-object", ->
1663+
assertErrorFormat '''
1664+
return
1665+
1
1666+
''', '''
1667+
[stdin]:2:3: error: unexpected number
1668+
1
1669+
^
1670+
'''
1671+
1672+
test "#3199: error message for return indented comprehension", ->
1673+
assertErrorFormat '''
1674+
return
1675+
x for x in [1, 2, 3]
1676+
''', '''
1677+
[stdin]:2:3: error: unexpected identifier
1678+
x for x in [1, 2, 3]
1679+
^
1680+
'''
1681+
1682+
test "#3199: error message for throw indented non-object", ->
1683+
assertErrorFormat '''
1684+
throw
1685+
1
1686+
''', '''
1687+
[stdin]:2:3: error: unexpected number
1688+
1
1689+
^
1690+
'''
1691+
1692+
test "#3199: error message for throw indented comprehension", ->
1693+
assertErrorFormat '''
1694+
throw
1695+
x for x in [1, 2, 3]
1696+
''', '''
1697+
[stdin]:2:3: error: unexpected identifier
1698+
x for x in [1, 2, 3]
1699+
^
1700+
'''

test/formatting.coffee

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,16 @@ test "#3906: handle further indentation inside indented chain", ->
384384
)
385385
1
386386
'''
387+
388+
test "#3199: throw multiline implicit object", ->
389+
x = do ->
390+
if no then throw
391+
type: 'a'
392+
msg: 'b'
393+
eq undefined, x
394+
395+
y = do ->
396+
if no then return
397+
type: 'a'
398+
msg: 'b'
399+
eq undefined, y

0 commit comments

Comments
 (0)