fixing parens-around-implicit-function-with-multiline-chained-chaser bug

This commit is contained in:
Jeremy Ashkenas
2010-04-01 23:38:20 -04:00
parent 3605168e85
commit 1c628e7883
3 changed files with 16 additions and 3 deletions

View File

@@ -139,10 +139,11 @@
// Insert the implicit parentheses here, so that the parser doesn't have to
// deal with them.
Rewriter.prototype.add_implicit_parentheses = function add_implicit_parentheses() {
var calls, parens, stack;
var calls, parens, stack, start_parens;
stack = [0];
calls = 0;
parens = 0;
start_parens = 0;
return this.scan_tokens(__bind(function(prev, token, post, i) {
var _a, _b, _c, idx, last, open, size, stack_pointer, tag, tmp;
tag = token[0];
@@ -161,7 +162,7 @@
stack[stack.length - 1] += last;
}
open = stack[stack.length - 1] > 0;
if (!(typeof post !== "undefined" && post !== null) || (parens === 0 && include(IMPLICIT_END, tag))) {
if (!(typeof post !== "undefined" && post !== null) || (start_parens > parens) || (parens === 0 && include(IMPLICIT_END, tag))) {
if (tag === 'INDENT' && prev && include(IMPLICIT_BLOCK, prev[0])) {
return 1;
}
@@ -184,6 +185,7 @@
return 1;
}
calls = 0;
start_parens = tag === '(' ? parens - 1 : parens;
this.tokens.splice(i, 0, ['CALL_START', '(', token[2]]);
stack[stack.length - 1] += 1;
return 2;

View File

@@ -111,6 +111,7 @@ exports.Rewriter: class Rewriter
stack: [0]
calls: 0
parens: 0
start_parens: 0
@scan_tokens (prev, token, post, i) =>
tag: token[0]
switch tag
@@ -123,7 +124,7 @@ exports.Rewriter: class Rewriter
last: stack.pop()
stack[stack.length - 1]: + last
open: stack[stack.length - 1] > 0
if !post? or (parens is 0 and include IMPLICIT_END, tag)
if !post? or (start_parens > parens) or (parens is 0 and include IMPLICIT_END, tag)
return 1 if tag is 'INDENT' and prev and include IMPLICIT_BLOCK, prev[0]
return 1 if tag is 'OUTDENT' and token.generated
if open or tag is 'INDENT'
@@ -136,6 +137,7 @@ exports.Rewriter: class Rewriter
return size
return 1 unless prev and include(IMPLICIT_FUNC, prev[0]) and include(IMPLICIT_CALL, tag)
calls: 0
start_parens: if tag is '(' then parens - 1 else parens
@tokens.splice i, 0, ['CALL_START', '(', token[2]]
stack[stack.length - 1]: + 1
return 2

View File

@@ -128,3 +128,12 @@ combine: (func, num) -> func() * num
result: combine (-> 1 + 2), 3
ok result is 9
# Test for calls/parens/multiline-chains.
f: (x) -> x
result: (f 1).toString()
.length
ok result is 1