Skip to content

Commit 66492a3

Browse files
committed
continue object property values
1 parent fc0e82e commit 66492a3

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()
@@ -650,8 +650,7 @@ exports.Rewriter = class Rewriter
650650
condition = (token, i) ->
651651
[tag] = token
652652
[prevTag] = @tokens[i - 1]
653-
[nextTag] = @tokens[i + 1] unless i is @tokens.length - 1
654-
tag is 'TERMINATOR' and nextTag not in LEADING_LOGICAL or (tag is 'INDENT' and prevTag not in SINGLE_LINERS)
653+
tag is 'TERMINATOR' and not @isLeadingLogical(i) or (tag is 'INDENT' and prevTag not in SINGLE_LINERS)
655654

656655
action = (token, i) ->
657656
if token[0] isnt 'INDENT' or (token.generated and not token.fromThen)
@@ -663,6 +662,11 @@ exports.Rewriter = class Rewriter
663662
@detectEnd i + 1, condition, action
664663
return 1
665664

665+
# Returns `yes` if standing in front of what will become a LEADING_AND or
666+
# LEADING_OR, ie TERMINATOR followed by && or ||
667+
isLeadingLogical: (i) ->
668+
@tag(i) is 'TERMINATOR' and @tag(i + 1) in LEADING_LOGICAL
669+
666670
# Convert TERMINATOR followed by && or || into a single LEADING_AND or
667671
# LEADING_OR token to disambiguate grammar.
668672
tagLeadingLogical: ->

test/formatting.coffee

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,3 +642,27 @@ test "logical and/or should continue lines", ->
642642
if yes
643643
and yes
644644
3
645+
646+
test "leading and/or should continue object property", ->
647+
obj =
648+
a: yes
649+
and yes
650+
eq yes, obj.a
651+
652+
obj =
653+
b: 1
654+
a: yes
655+
and yes
656+
eq yes, obj.a
657+
658+
obj =
659+
a: yes
660+
and yes
661+
b: 1
662+
eq yes, obj.a
663+
664+
# when object doesn't start line, don't treat as continuation of object property
665+
f = -> no
666+
eq yes,
667+
f a: 1
668+
or yes

0 commit comments

Comments
 (0)