Skip to content

Commit

Permalink
Fix continuing first line of explicit [/{ literal (#5397)
Browse files Browse the repository at this point in the history
  • Loading branch information
edemaine authored Jan 26, 2022
1 parent 6fd58ef commit 3d39d20
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/v2/browser-compiler-legacy/coffeescript.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/v2/browser-compiler-modern/coffeescript.js

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions docs/v2/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -30853,6 +30853,42 @@ <h2>Another heading</h2>
eq 2, a.b.d
eq 3, a.e

test "#5368: continuing object and array literals", ->
{ a
b: { c }
} = {a: 1, b: {c: 2}}
eq a, 1
eq c, 2

[d
e: f
] = [3, {e: 4}]
eq d, 3
eq f, 4
A =
[d
e: f
]
eq A[0], 3
eq A[1].e, 4

for obj in [
{a: a
c: c
}
{a: a
c
}
{a
c: c
}
{a
c
}
]
eq obj.a, 1
eq obj.c, 2

</script>
<script type="text/x-coffeescript" class="test" id="operators">
# Operators
Expand Down
2 changes: 1 addition & 1 deletion lib/coffeescript-browser-compiler-legacy/coffeescript.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/coffeescript-browser-compiler-modern/coffeescript.js

Large diffs are not rendered by default.

20 changes: 11 additions & 9 deletions lib/coffeescript/rewriter.js

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

16 changes: 12 additions & 4 deletions src/rewriter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,15 @@ exports.Rewriter = class Rewriter
# if f(a: 1)
#
# which is probably always unintended.
# Furthermore don’t allow this in literal arrays, as
# that creates grammatical ambiguities.
# Furthermore don’t allow this in the first line of a literal array
# or explicit object, as that creates grammatical ambiguities (#5368).
if tag in IMPLICIT_FUNC and
@indexOfTag(i + 1, 'INDENT') > -1 and @looksObjectish(i + 2) and
not @findTagsBackwards(i, ['CLASS', 'EXTENDS', 'IF', 'CATCH',
'SWITCH', 'LEADING_WHEN', 'FOR', 'WHILE', 'UNTIL'])
'SWITCH', 'LEADING_WHEN', 'FOR', 'WHILE', 'UNTIL']) and
not ((s = stackTop()?[0]) in ['{', '['] and
not isImplicit(stackTop()) and
@findTagsBackwards(i, s))
startImplicitCall i + 1
stack.push ['INDENT', i + 2]
return forward(3)
Expand All @@ -339,9 +342,14 @@ exports.Rewriter = class Rewriter

startsLine = s <= 0 or @tag(s - 1) in LINEBREAKS or tokens[s - 1].newLine
# Are we just continuing an already declared object?
# Including the case where we indent on the line after an explicit '{'.
if stackTop()
[stackTag, stackIdx] = stackTop()
if (stackTag is '{' or stackTag is 'INDENT' and @tag(stackIdx - 1) is '{') and
stackNext = stack[stack.length - 2]
if (stackTag is '{' or
stackTag is 'INDENT' and stackNext?[0] is '{' and
not isImplicit(stackNext) and
@findTagsBackwards(stackIdx-1, ['{'])) and
(startsLine or @tag(s - 1) is ',' or @tag(s - 1) is '{') and
@tag(s - 1) not in UNFINISHED
return forward(1)
Expand Down
36 changes: 36 additions & 0 deletions test/objects.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -930,3 +930,39 @@ test "#5292: implicit object after line continuer in implicit object property va
eq 1, a.b.c
eq 2, a.b.d
eq 3, a.e

test "#5368: continuing object and array literals", ->
{ a
b: { c }
} = {a: 1, b: {c: 2}}
eq a, 1
eq c, 2

[d
e: f
] = [3, {e: 4}]
eq d, 3
eq f, 4
A =
[d
e: f
]
eq A[0], 3
eq A[1].e, 4

for obj in [
{a: a
c: c
}
{a: a
c
}
{a
c: c
}
{a
c
}
]
eq obj.a, 1
eq obj.c, 2

0 comments on commit 3d39d20

Please sign in to comment.