Skip to content

Commit e00fa5d

Browse files
xixixaoGeoffreyBooth
authored andcommitted
Fix #4533: chained calls incorrectly wrapping enclosing implicit objects (#4534)
1 parent 51c0657 commit e00fa5d

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

lib/coffee-script/rewriter.js

+21-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rewriter.coffee

+10-4
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,12 @@ exports.Rewriter = class Rewriter
149149
forward = (n) -> i - startIdx + n
150150

151151
# Helper functions
152-
inImplicit = -> stackTop()?[2]?.ours
153-
inImplicitCall = -> inImplicit() and stackTop()?[0] is '('
154-
inImplicitObject = -> inImplicit() and stackTop()?[0] is '{'
152+
isImplicit = (stackItem) -> stackItem?[2]?.ours
153+
isImplicitObject = (stackItem) -> isImplicit(stackItem) and stackItem?[0] is '{'
154+
isImplicitCall = (stackItem) -> isImplicit(stackItem) and stackItem?[0] is '('
155+
inImplicit = -> isImplicit stackTop()
156+
inImplicitCall = -> isImplicitCall stackTop()
157+
inImplicitObject = -> isImplicitObject stackTop()
155158
# Unclosed control statement inside implicit parens (like
156159
# class declaration or if-conditionals)
157160
inImplicitControl = -> inImplicit and stackTop()?[0] is 'CONTROL'
@@ -298,7 +301,10 @@ exports.Rewriter = class Rewriter
298301
# .g b
299302
# .h a
300303

301-
stackTop()[2].sameLine = no if inImplicitObject() and tag in LINEBREAKS
304+
# Mark all enclosing objects as not sameLine
305+
if tag in LINEBREAKS
306+
for stackItem in stack by -1 when isImplicitObject stackItem
307+
stackItem[2].sameLine = no
302308

303309
newLine = prevTag is 'OUTDENT' or prevToken.newLine
304310
if tag in IMPLICIT_END or tag in CALL_CLOSERS and newLine

test/formatting.coffee

+11
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ test "indented heredoc", ->
128128
# * single line arguments
129129
# * inline function literal
130130
# * inline object literal
131+
#
132+
# * chaining inside
133+
# * implicit object literal
131134

132135
test "chaining after outdent", ->
133136
id = (x) -> x
@@ -221,6 +224,14 @@ test "chaining should work within spilling ternary", ->
221224
.a
222225
eq 3, result.h
223226

227+
test "method call chaining inside objects", ->
228+
f = (x) -> c: 42
229+
result =
230+
a: f 1
231+
b: f a: 1
232+
.c
233+
eq 42, result.b
234+
224235
# Nested blocks caused by paren unwrapping
225236
test "#1492: Nested blocks don't cause double semicolons", ->
226237
js = CoffeeScript.compile '(0;0)'

0 commit comments

Comments
 (0)