Skip to content

Commit 99a5b81

Browse files
committed
continue object property values
1 parent 0f3f5ac commit 99a5b81

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

lib/coffeescript/rewriter.js

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

src/rewriter.coffee

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ exports.Rewriter = class Rewriter
378378
# and the implicit object didn't start the line or the next line doesn’t look like
379379
# the continuation of an object.
380380
else if inImplicitObject() and tag is 'TERMINATOR' and prevTag isnt ',' and
381-
not (startsLine and @looksObjectish(i + 1))
381+
not (startsLine and (@looksObjectish(i + 1) or @isLeadingLogical(i)))
382382
endImplicitObject()
383383
else if inImplicitControl() and tokens[stackTop()[1]][0] is 'CLASS' and tag is 'TERMINATOR'
384384
stack.pop()
@@ -645,8 +645,7 @@ exports.Rewriter = class Rewriter
645645
condition = (token, i) ->
646646
[tag] = token
647647
[prevTag] = @tokens[i - 1]
648-
[nextTag] = @tokens[i + 1] unless i is @tokens.length - 1
649-
tag is 'TERMINATOR' and nextTag not in LEADING_LOGICAL or (tag is 'INDENT' and prevTag not in SINGLE_LINERS)
648+
tag is 'TERMINATOR' and not @isLeadingLogical(i) or (tag is 'INDENT' and prevTag not in SINGLE_LINERS)
650649

651650
action = (token, i) ->
652651
if token[0] isnt 'INDENT' or (token.generated and not token.fromThen)
@@ -671,6 +670,11 @@ exports.Rewriter = class Rewriter
671670
token[1].generated = yes if token.generated
672671
1
673672

673+
# Returns `yes` if standing in front of what will become a LEADING_AND or
674+
# LEADING_OR, ie TERMINATOR followed by && or ||
675+
isLeadingLogical: (i) ->
676+
@tag(i) is 'TERMINATOR' and @tag(i + 1) in LEADING_LOGICAL
677+
674678
# Convert TERMINATOR followed by && or || into a single LEADING_AND or
675679
# LEADING_OR token to disambiguate grammar.
676680
tagLeadingLogical: ->

test/formatting.coffee

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,3 +658,27 @@ test "logical and/or should continue lines", ->
658658
if yes
659659
and yes
660660
3
661+
662+
test "leading and/or should continue object property", ->
663+
obj =
664+
a: yes
665+
and yes
666+
eq yes, obj.a
667+
668+
obj =
669+
b: 1
670+
a: yes
671+
and yes
672+
eq yes, obj.a
673+
674+
obj =
675+
a: yes
676+
and yes
677+
b: 1
678+
eq yes, obj.a
679+
680+
# when object doesn't start line, don't treat as continuation of object property
681+
f = -> no
682+
eq yes,
683+
f a: 1
684+
or yes

0 commit comments

Comments
 (0)