Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15a70f8

Browse files
committed
Implemented method call chaining
1 parent 873ed07 commit 15a70f8

3 files changed

Lines changed: 49 additions & 10 deletions

File tree

lib/coffee-script/rewriter.js

Lines changed: 3 additions & 3 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class exports.Rewriter
131131

132132
@scanTokens (token, i, tokens) ->
133133
[tag] = token
134-
[prevTag] = if i > 0 then tokens[i - 1] else []
134+
[prevTag] = prevToken = if i > 0 then tokens[i - 1] else []
135135
[nextTag] = if i < tokens.length - 1 then tokens[i + 1] else []
136136
stackTop = -> stack[stack.length - 1]
137137
startIdx = i
@@ -275,7 +275,14 @@ class exports.Rewriter
275275
# c
276276
# .h a
277277
#
278-
if prevTag is 'OUTDENT' and inImplicitCall() and tag in ['.', '?.', '::', '?::']
278+
# and also
279+
#
280+
# f a
281+
# .g b
282+
# .h a
283+
#
284+
if (prevTag is 'OUTDENT' or prevToken.newLine) and inImplicitCall() and
285+
tag in ['.', '?.', '::', '?::']
279286
endImplicitCall()
280287
return forward(1)
281288

test/formatting.coffee

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,47 @@ test "chained accesses split on period/newline, backwards and forwards", ->
4444
.reverse()
4545
.reverse()
4646
arrayEq ['c','b','a'], result
47-
arrayEq ['c','b','a'], str
47+
arrayEq ['c','b','a'],
48+
str
4849
.split('')
4950
.reverse()
5051
.reverse()
5152
.reverse()
52-
arrayEq ['c','b','a'], str.
53+
arrayEq ['c','b','a'],
54+
str.
5355
split('')
5456
.reverse().
5557
reverse()
5658
.reverse()
5759

60+
test "#1495, method call chaining", ->
61+
str = 'abc'
62+
63+
result = str.split ''
64+
.join ', '
65+
eq 'a, b, c', result
66+
67+
result = str
68+
.split ''
69+
.join ', '
70+
eq 'a, b, c', result
71+
72+
eq 'a, b, c', (str
73+
.split ''
74+
.join ', '
75+
)
76+
77+
eq 'abc',
78+
'aaabbbccc'.replace /(\w)\1\1/g, '$1$1'
79+
.replace /([abc])\1/g, '$1'
80+
81+
# Unreadable code, not a real-life use case
82+
result = str.split ''.
83+
split ''
84+
.join('')
85+
.join ', '
86+
eq 'a, b, c', result
87+
5888
# Operators
5989

6090
test "newline suppression for operators", ->
@@ -65,9 +95,11 @@ test "newline suppression for operators", ->
6595
eq 6, six
6696

6797
test "`?.` and `::` should continue lines", ->
68-
ok not Date
69-
::
70-
?.foo
98+
ok not (
99+
Date
100+
::
101+
?.foo
102+
)
71103
#eq Object::toString, Date?.
72104
#prototype
73105
#::

0 commit comments

Comments
 (0)