Skip to content

Commit c82a0c6

Browse files
committed
allow leading logical inside if condition
1 parent 1d671d2 commit c82a0c6

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

655656
action = (token, i) ->
656657
if token[0] isnt 'INDENT' or (token.generated and not token.fromThen)
@@ -666,8 +667,8 @@ exports.Rewriter = class Rewriter
666667
# LEADING_OR token to disambiguate grammar.
667668
tagLeadingLogical: ->
668669
@scanTokens (token, i, tokens) ->
669-
return 1 unless token[0] is 'TERMINATOR' and tokens.length >= i + 2 and (operatorToken = tokens[i + 1])[0] in ['&&', '||']
670-
token[0] = "LEADING_#{if operatorToken[0] is '&&' then 'AND' else 'OR'}"
670+
return 1 unless token[0] is 'TERMINATOR' and tokens.length >= i + 2 and (operatorToken = tokens[i + 1])[0] in LEADING_LOGICAL
671+
token[0] = "LEADING_#{LEADING_LOGICAL_NAMES[operatorToken[0]]}"
671672
token[1] = operatorToken[1]
672673
token[2].last_line = operatorToken[2].last_line
673674
token[2].last_column = operatorToken[2].last_column
@@ -766,3 +767,8 @@ DISCARDED = ['(', ')', '[', ']', '{', '}', '.', '..', '...', ',', '=', '++', '--
766767
'OUTDENT', 'PARAM_END', 'REGEX_START', 'REGEX_END', 'RETURN', 'STRING_END', 'THROW',
767768
'UNARY', 'YIELD'
768769
].concat IMPLICIT_UNSPACED_CALL.concat IMPLICIT_END.concat CALL_CLOSERS.concat CONTROL_IN_IMPLICIT
770+
771+
LEADING_LOGICAL_NAMES =
772+
'&&': 'AND'
773+
'||': 'OR'
774+
LEADING_LOGICAL = Object.keys LEADING_LOGICAL_NAMES

test/formatting.coffee

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,23 @@ test "logical and/or should continue lines", ->
622622
c:
623623
1
624624
or 3
625+
626+
eq 3,
627+
if yes
628+
and yes
629+
3
630+
631+
eq 3,
632+
if yes
633+
and yes
634+
3
635+
636+
eq 3,
637+
if yes
638+
and yes
639+
3
640+
641+
eq 3,
642+
if yes
643+
and yes
644+
3

0 commit comments

Comments
 (0)