Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement #1495, Method call chaining #3263

Merged
merged 2 commits into from
Nov 28, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions lib/coffee-script/rewriter.js

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

28 changes: 23 additions & 5 deletions src/rewriter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class exports.Rewriter

@scanTokens (token, i, tokens) ->
[tag] = token
[prevTag] = if i > 0 then tokens[i - 1] else []
[prevTag] = prevToken = if i > 0 then tokens[i - 1] else []
[nextTag] = if i < tokens.length - 1 then tokens[i + 1] else []
stackTop = -> stack[stack.length - 1]
startIdx = i
Expand Down Expand Up @@ -159,6 +159,10 @@ class exports.Rewriter
tokens.splice i, 0, generate 'CALL_END', ')'
i += 1

endAllImplicitCalls = ->
while inImplicitCall()
endImplicitCall()

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should probably be a return here, to avoid storing/returning the value of the comprehension.

startImplicitObject = (j, startsLine = yes) ->
idx = j ? i
stack.push ['{', idx, sameLine: yes, startsLine: startsLine, ours: yes]
Expand Down Expand Up @@ -275,9 +279,19 @@ class exports.Rewriter
# c
# .h a
#
if prevTag is 'OUTDENT' and inImplicitCall() and tag in ['.', '?.', '::', '?::']
endImplicitCall()
return forward(1)
# and also
#
# f a
# .g b
# .h a
#
if inImplicitCall() and tag in CALL_CLOSERS
if prevTag is 'OUTDENT'
endImplicitCall()
return forward(1)
if prevToken.newLine
endAllImplicitCalls()
return forward(1)

stackTop()[2].sameLine = no if inImplicitObject() and tag in LINEBREAKS

Expand Down Expand Up @@ -356,7 +370,8 @@ class exports.Rewriter
token[1] isnt ';' and token[0] in SINGLE_CLOSERS and
not (token[0] is 'TERMINATOR' and @tag(i + 1) in EXPRESSION_CLOSE) and
not (token[0] is 'ELSE' and starter isnt 'THEN') and
not (token[0] in ['CATCH', 'FINALLY'] and starter in ['->', '=>'])
not (token[0] in ['CATCH', 'FINALLY'] and starter in ['->', '=>']) or
token[0] in CALL_CLOSERS and @tokens[i - 1].newLine

action = (token, i) ->
@tokens.splice (if @tag(i - 1) is ',' then i - 1 else i), 0, outdent
Expand Down Expand Up @@ -471,3 +486,6 @@ SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADIN

# Tokens that end a line.
LINEBREAKS = ['TERMINATOR', 'INDENT', 'OUTDENT']

# Tokens that close open calls when they follow a newline.
CALL_CLOSERS = ['.', '?.', '::', '?::']
55 changes: 50 additions & 5 deletions test/formatting.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,60 @@ test "chained accesses split on period/newline, backwards and forwards", ->
.reverse()
.reverse()
arrayEq ['c','b','a'], result
arrayEq ['c','b','a'], str
arrayEq ['c','b','a'],
str
.split('')
.reverse()
.reverse()
.reverse()
arrayEq ['c','b','a'], str.
arrayEq ['c','b','a'],
str.
split('')
.reverse().
reverse()
.reverse()

test "#1495, method call chaining", ->
str = 'abc'

result = str.split ''
.join ', '
eq 'a, b, c', result

result = str
.split ''
.join ', '
eq 'a, b, c', result

eq 'a, b, c', (str
.split ''
.join ', '
)

eq 'abc',
'aaabbbccc'.replace /(\w)\1\1/g, '$1$1'
.replace /([abc])\1/g, '$1'

# Nested calls
result = [1..3]
.slice Math.max 0, 1
.concat [3]
arrayEq result, [2, 3, 3]

# Single line function arguments.
result = [1..6]
.map (x) -> x * x
.filter (x) -> x % 2 is 0
.reverse()
arrayEq result, [36, 16, 4]

# The parens are forced
result = str.split(''.
split ''
.join ''
).join ', '
eq 'a, b, c', result

# Operators

test "newline suppression for operators", ->
Expand All @@ -65,9 +108,11 @@ test "newline suppression for operators", ->
eq 6, six

test "`?.` and `::` should continue lines", ->
ok not Date
::
?.foo
ok not (
Date
::
?.foo
)
#eq Object::toString, Date?.
#prototype
#::
Expand Down