Skip to content

Commit 4cf6652

Browse files
committed
allow leading logical inside if condition
1 parent 36dd387 commit 4cf6652

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

lib/coffeescript/rewriter.js

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

src/rewriter.coffee

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,8 @@ exports.Rewriter = class Rewriter
645645
condition = (token, i) ->
646646
[tag] = token
647647
[prevTag] = @tokens[i - 1]
648-
tag is 'TERMINATOR' or (tag is 'INDENT' and prevTag not in SINGLE_LINERS)
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)
649650

650651
action = (token, i) ->
651652
if token[0] isnt 'INDENT' or (token.generated and not token.fromThen)
@@ -674,8 +675,8 @@ exports.Rewriter = class Rewriter
674675
# LEADING_OR token to disambiguate grammar.
675676
tagLeadingLogical: ->
676677
@scanTokens (token, i, tokens) ->
677-
return 1 unless token[0] is 'TERMINATOR' and tokens.length >= i + 2 and (operatorToken = tokens[i + 1])[0] in ['&&', '||']
678-
token[0] = "LEADING_#{if operatorToken[0] is '&&' then 'AND' else 'OR'}"
678+
return 1 unless token[0] is 'TERMINATOR' and tokens.length >= i + 2 and (operatorToken = tokens[i + 1])[0] in LEADING_LOGICAL
679+
token[0] = "LEADING_#{LEADING_LOGICAL_NAMES[operatorToken[0]]}"
679680
token[1] = operatorToken[1]
680681
token[2].last_line = operatorToken[2].last_line
681682
token[2].last_column = operatorToken[2].last_column
@@ -775,3 +776,8 @@ DISCARDED = ['(', ')', '[', ']', '{', '}', '.', '..', '...', ',', '=', '++', '--
775776
'INTERPOLATION_START', 'INTERPOLATION_END', 'LEADING_WHEN', 'OUTDENT', 'PARAM_END',
776777
'REGEX_START', 'REGEX_END', 'RETURN', 'STRING_END', 'THROW', 'UNARY', 'YIELD'
777778
].concat IMPLICIT_UNSPACED_CALL.concat IMPLICIT_END.concat CALL_CLOSERS.concat CONTROL_IN_IMPLICIT
779+
780+
LEADING_LOGICAL_NAMES =
781+
'&&': 'AND'
782+
'||': 'OR'
783+
LEADING_LOGICAL = Object.keys LEADING_LOGICAL_NAMES

test/formatting.coffee

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,23 @@ test "logical and/or should continue lines", ->
638638
c:
639639
1
640640
or 3
641+
642+
eq 3,
643+
if yes
644+
and yes
645+
3
646+
647+
eq 3,
648+
if yes
649+
and yes
650+
3
651+
652+
eq 3,
653+
if yes
654+
and yes
655+
3
656+
657+
eq 3,
658+
if yes
659+
and yes
660+
3

0 commit comments

Comments
 (0)