1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Implemented method call chaining

This commit is contained in:
xixixao 2013-11-27 03:41:52 +00:00
parent 873ed071d4
commit 15a70f863c
3 changed files with 49 additions and 10 deletions

View file

@ -149,9 +149,9 @@
var stack;
stack = [];
return this.scanTokens(function(token, i, tokens) {
var endImplicitCall, endImplicitObject, forward, inImplicit, inImplicitCall, inImplicitControl, inImplicitObject, nextTag, offset, prevTag, s, sameLine, stackIdx, stackTag, stackTop, startIdx, startImplicitCall, startImplicitObject, startsLine, tag, _ref, _ref1, _ref2, _ref3, _ref4, _ref5;
var endImplicitCall, endImplicitObject, forward, inImplicit, inImplicitCall, inImplicitControl, inImplicitObject, nextTag, offset, prevTag, prevToken, s, sameLine, stackIdx, stackTag, stackTop, startIdx, startImplicitCall, startImplicitObject, startsLine, tag, _ref, _ref1, _ref2, _ref3, _ref4, _ref5;
tag = token[0];
prevTag = (i > 0 ? tokens[i - 1] : [])[0];
prevTag = (prevToken = i > 0 ? tokens[i - 1] : [])[0];
nextTag = (i < tokens.length - 1 ? tokens[i + 1] : [])[0];
stackTop = function() {
return stack[stack.length - 1];
@ -285,7 +285,7 @@
startImplicitObject(s, !!startsLine);
return forward(2);
}
if (prevTag === 'OUTDENT' && inImplicitCall() && (tag === '.' || tag === '?.' || tag === '::' || tag === '?::')) {
if ((prevTag === 'OUTDENT' || prevToken.newLine) && inImplicitCall() && (tag === '.' || tag === '?.' || tag === '::' || tag === '?::')) {
endImplicitCall();
return forward(1);
}

View file

@ -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
@ -275,7 +275,14 @@ class exports.Rewriter
# c
# .h a
#
if prevTag is 'OUTDENT' and inImplicitCall() and tag in ['.', '?.', '::', '?::']
# and also
#
# f a
# .g b
# .h a
#
if (prevTag is 'OUTDENT' or prevToken.newLine) and inImplicitCall() and
tag in ['.', '?.', '::', '?::']
endImplicitCall()
return forward(1)

View file

@ -44,17 +44,47 @@ 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'
# Unreadable code, not a real-life use case
result = str.split ''.
split ''
.join('')
.join ', '
eq 'a, b, c', result
# Operators
test "newline suppression for operators", ->
@ -65,9 +95,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
#::