Skip to content

Commit ba41b44

Browse files
helixbassGeoffreyBooth
authored andcommitted
Allow linebreak/indent in destructured for variable (#5286)
* allow linebreak/indent in for variable pattern * tests * < * condition
1 parent 33bbef9 commit ba41b44

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

lib/coffeescript/lexer.js

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

src/lexer.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ exports.Lexer = class Lexer
182182
if tag is 'WHEN' and @tag() in LINE_BREAK
183183
tag = 'LEADING_WHEN'
184184
else if tag is 'FOR'
185-
@seenFor = yes
185+
@seenFor = {endsLength: @ends.length}
186186
else if tag is 'UNLESS'
187187
tag = 'IF'
188188
else if tag is 'IMPORT'
@@ -514,7 +514,7 @@ exports.Lexer = class Lexer
514514

515515
prev = @prev()
516516
backslash = prev?[0] is '\\'
517-
@seenFor = no unless backslash and @seenFor
517+
@seenFor = no unless (backslash or @seenFor?.endsLength < @ends.length) and @seenFor
518518
@seenImport = no unless (backslash and @seenImport) or @importSpecifierList
519519
@seenExport = no unless (backslash and @seenExport) or @exportSpecifierList
520520

test/comprehensions.coffee

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,3 +576,40 @@ test "#3778: Consistently always cache for loop range boundaries and steps, even
576576
a = 3; arrayEq [1, 2, 3], (for n in [1..+a] then a = 4; n)
577577
a = 1; arrayEq [1, 2, 3], (for n in [1..3] by a then a = 4; n)
578578
a = 1; arrayEq [1, 2, 3], (for n in [1..3] by +a then a = 4; n)
579+
580+
test "for pattern variables can linebreak/indent", ->
581+
listOfObjects = [a: 1]
582+
sum = 0
583+
for {
584+
a
585+
somethingElse
586+
anotherProperty
587+
} in listOfObjects
588+
sum += a
589+
eq a, 1
590+
591+
sum = 0
592+
sum += a for {
593+
a,
594+
somethingElse,
595+
anotherProperty,
596+
} in listOfObjects
597+
eq a, 1
598+
599+
listOfArrays = [[2]]
600+
sum = 0
601+
for [
602+
a
603+
nonexistentElement
604+
anotherNonexistentElement
605+
] in listOfArrays
606+
sum += a
607+
eq a, 2
608+
609+
sum = 0
610+
sum += a for [
611+
a,
612+
nonexistentElement,
613+
anotherNonexistentElement
614+
] in listOfArrays
615+
eq a, 2

0 commit comments

Comments
 (0)