Skip to content

Commit 74bf845

Browse files
committed
Fix #4533: chained calls incorrectly wrapping enclosing implicit objects
1 parent ac1b2b5 commit 74bf845

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
@@ -151,9 +151,12 @@ class exports.Rewriter
151151
forward = (n) -> i - startIdx + n
152152

153153
# Helper functions
154-
inImplicit = -> stackTop()?[2]?.ours
155-
inImplicitCall = -> inImplicit() and stackTop()?[0] is '('
156-
inImplicitObject = -> inImplicit() and stackTop()?[0] is '{'
154+
isImplicit = (stackItem) -> stackItem?[2]?.ours
155+
isImplicitObject = (stackItem) -> isImplicit(stackItem) and stackItem?[0] is '{'
156+
isImplicitCall = (stackItem) -> isImplicit(stackItem) and stackItem?[0] is '('
157+
inImplicit = -> isImplicit stackTop()
158+
inImplicitCall = -> isImplicitCall stackTop()
159+
inImplicitObject = -> isImplicitObject stackTop()
157160
# Unclosed control statement inside implicit parens (like
158161
# class declaration or if-conditionals)
159162
inImplicitControl = -> inImplicit and stackTop()?[0] is 'CONTROL'
@@ -300,7 +303,10 @@ class exports.Rewriter
300303
# .g b
301304
# .h a
302305

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

305311
newLine = prevTag is 'OUTDENT' or prevToken.newLine
306312
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
@@ -198,6 +201,14 @@ test "#1495, method call chaining", ->
198201
).join ', '
199202
eq 'a, b, c', result
200203

204+
test "method call chaining inside objects", ->
205+
id = (x) -> x
206+
result =
207+
a: id 1
208+
b: id {a: 42}
209+
.a
210+
eq 42, result.b
211+
201212
# Nested blocks caused by paren unwrapping
202213
test "#1492: Nested blocks don't cause double semicolons", ->
203214
js = CoffeeScript.compile '(0;0)'

0 commit comments

Comments
 (0)