Skip to content

Commit

Permalink
Allow linebreak/indent in destructured for variable (#5286)
Browse files Browse the repository at this point in the history
* allow linebreak/indent in for variable pattern

* tests

* <

* condition
  • Loading branch information
helixbass authored and GeoffreyBooth committed Jan 1, 2020
1 parent 33bbef9 commit ba41b44
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lib/coffeescript/lexer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ exports.Lexer = class Lexer
if tag is 'WHEN' and @tag() in LINE_BREAK
tag = 'LEADING_WHEN'
else if tag is 'FOR'
@seenFor = yes
@seenFor = {endsLength: @ends.length}
else if tag is 'UNLESS'
tag = 'IF'
else if tag is 'IMPORT'
Expand Down Expand Up @@ -514,7 +514,7 @@ exports.Lexer = class Lexer

prev = @prev()
backslash = prev?[0] is '\\'
@seenFor = no unless backslash and @seenFor
@seenFor = no unless (backslash or @seenFor?.endsLength < @ends.length) and @seenFor
@seenImport = no unless (backslash and @seenImport) or @importSpecifierList
@seenExport = no unless (backslash and @seenExport) or @exportSpecifierList

Expand Down
37 changes: 37 additions & 0 deletions test/comprehensions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -576,3 +576,40 @@ test "#3778: Consistently always cache for loop range boundaries and steps, even
a = 3; arrayEq [1, 2, 3], (for n in [1..+a] then a = 4; n)
a = 1; arrayEq [1, 2, 3], (for n in [1..3] by a then a = 4; n)
a = 1; arrayEq [1, 2, 3], (for n in [1..3] by +a then a = 4; n)

test "for pattern variables can linebreak/indent", ->
listOfObjects = [a: 1]
sum = 0
for {
a
somethingElse
anotherProperty
} in listOfObjects
sum += a
eq a, 1

sum = 0
sum += a for {
a,
somethingElse,
anotherProperty,
} in listOfObjects
eq a, 1

listOfArrays = [[2]]
sum = 0
for [
a
nonexistentElement
anotherNonexistentElement
] in listOfArrays
sum += a
eq a, 2

sum = 0
sum += a for [
a,
nonexistentElement,
anotherNonexistentElement
] in listOfArrays
eq a, 2

0 comments on commit ba41b44

Please sign in to comment.