Skip to content

Commit

Permalink
Fix jashkenas#2998: Object literal with inconsistent indentation shou…
Browse files Browse the repository at this point in the history
…ld throw an error
  • Loading branch information
GeoffreyBooth committed Aug 4, 2017
1 parent ae7f97b commit 97a7b2f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
7 changes: 5 additions & 2 deletions lib/coffeescript/rewriter.js

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

6 changes: 5 additions & 1 deletion src/rewriter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,12 @@ exports.Rewriter = class Rewriter
if (stackTag is '{' or stackTag is 'INDENT' and @tag(stackIdx - 1) is '{') and
(startsLine or @tag(s - 1) is ',' or @tag(s - 1) is '{')
return forward(1)
# Is this an indentation error within what was intended to be one object?
else if s > 3 and @tag(s - 1) in LINEBREAKS and @tag(s - 2) in LINEBREAKS and
@tag(s - 3) is '}' and @tokens[s - 3].generated
throwSyntaxError 'unexpected indentation', @tokens[s - 1][2]

startImplicitObject(s, !!startsLine)
startImplicitObject s, startsLine?
return forward(2)

# End implicit calls when chaining method calls
Expand Down
25 changes: 25 additions & 0 deletions test/error_messages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1742,3 +1742,28 @@ test "#3199: error message for throw indented comprehension", ->
x for x in [1, 2, 3]
^
'''

test "#2998: Indentation mismatch in object shouldn’t produce two objects", ->
assertErrorFormat '''
colors =
45: 'cyan'
52: 'sepia'
100: 'olive'
124: 'red'
''', '''
[stdin]:4:3: error: unexpected indentation
100: 'olive'
^
'''

# Former object test that is now invalid because it looks too much like a mistake.
# It used to test that string-keyed objects shouldn’t suppress newlines.
assertErrorFormat '''
one =
'>!': 3
six: -> 10
''', '''
[stdin]:3:1: error: unexpected indentation
six: -> 10
^
'''
7 changes: 0 additions & 7 deletions test/objects.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,6 @@ ok obj.misdent.toString() is ',,,'
#542: Objects leading expression statement should be parenthesized.
{f: -> ok yes }.f() + 1

# String-keyed objects shouldn't suppress newlines.
one =
'>!': 3
six: -> 10
ok not one.six

# Shorthand objects with property references.
obj =
### comment one ###
Expand Down Expand Up @@ -696,4 +690,3 @@ test "#4579: Postfix for/while/until in first line of implicit object literals",
baz: 1337
arrayEq [4, 3, 2, 1, 0], six.foo.bar
eq 1337, six.foo.baz

0 comments on commit 97a7b2f

Please sign in to comment.