Skip to content

Commit

Permalink
WIP: Extend continuation to implicit objects following comma
Browse files Browse the repository at this point in the history
  • Loading branch information
edemaine committed Jan 25, 2022
1 parent 5fb5ef7 commit c6acecb
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 17 deletions.
13 changes: 13 additions & 0 deletions docs/v2/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -30889,6 +30889,19 @@ <h2>Another heading</h2>
eq obj.a, 1
eq obj.c, 2

test '#5330: continuing implicit starting after comma', ->
a = [
b1: 1
b2: 2
, c1: 1
c2: 2
]
eq a.length, 2
eq a[0].b1, 1
eq a[0].b2, 2
eq a[1].c1, 1
eq a[1].c2, 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.

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

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

13 changes: 9 additions & 4 deletions src/rewriter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,10 @@ exports.Rewriter = class Rewriter
#
# 1. We have seen a `CONTROL` argument on the line.
# 2. The last token before the indent is part of the list below.
if prevTag not in ['=>', '->', '[', '(', ',', '{', 'ELSE', '=']
# 3. Continuing an implicit object starting after an array's comma.
if prevTag not in ['=>', '->', '[', '(', ',', '{', 'ELSE', '='] and
not (inImplicitObject() and stackTop2()?[0] == '[' and
tokens[stackTop()[1]-1]?[0] is ',')
while inImplicitCall() or inImplicitObject() and prevTag isnt ':'
if inImplicitCall()
endImplicitCall()
Expand Down Expand Up @@ -347,9 +350,11 @@ exports.Rewriter = class Rewriter
if stackTop()
[stackTag, stackIdx] = stackTop()
if (stackTag is '{' or
stackTag is 'INDENT' and stackTop2()?[0] is '{' and
not isImplicit(stackTop2()) and
@findTagsBackwards(stackIdx-1, ['{'])) and
stackTag is 'INDENT' and (top2 = stackTop2())?[0] is '{' and
(tokens[top2[1]][2] ? tokens[top2[1]].origin[2]).last_line ==
tokens[stackIdx-1]?[2].first_line and
(not isImplicit(top2) or startsLine and
tokens[top2[1]-1]?[0] == ',')) and
(startsLine or @tag(s - 1) is ',' or @tag(s - 1) is '{') and
@tag(s - 1) not in UNFINISHED
return forward(1)
Expand Down
17 changes: 17 additions & 0 deletions test/objects.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -966,3 +966,20 @@ test "#5368: continuing object and array literals", ->
]
eq obj.a, 1
eq obj.c, 2

test '#5330: continuing implicit starting after comma', ->
a = [
b1: 1
b2: 2
, c1: 1
c2: 2
, d1: 1
d2: 2
]
eq a.length, 3
eq a[0].b1, 1
eq a[0].b2, 2
eq a[1].c1, 1
eq a[1].c2, 2
eq a[2].d1, 1
eq a[2].d2, 2

0 comments on commit c6acecb

Please sign in to comment.