From aac96792820a3212f0c592b71bce50e7ad3002b1 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Mon, 19 Apr 2010 23:18:39 -0400 Subject: [PATCH] merging in gfodor's excellent Rewriter patch. --- lib/rewriter.js | 16 ++++++++++++---- src/rewriter.coffee | 19 ++++++++----------- test/test_chaining.coffee | 5 +++-- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/lib/rewriter.js b/lib/rewriter.js index 5b278246..1a5c5bb7 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -285,6 +285,8 @@ // it with the inverse of what we've just popped. // 3. Keep track of "debt" for tokens that we manufacture, to make sure we end // up balanced in the end. + // 4. Be careful not to alter array or parentheses delimiters with overzealous + // rewriting. Rewriter.prototype.rewrite_closing_parens = function rewrite_closing_parens() { var _a, debt, key, stack, val; stack = []; @@ -295,7 +297,7 @@ (debt[key] = 0); }} return this.scan_tokens(__bind(function(prev, token, post, i) { - var inv, match, mtag, tag; + var inv, match, mtag, oppos, tag; tag = token[0]; inv = INVERSES[token[0]]; if (include(EXPRESSION_START, tag)) { @@ -309,12 +311,18 @@ } else { match = stack.pop(); mtag = match[0]; - if (tag === INVERSES[mtag]) { + oppos = INVERSES[mtag]; + if (tag === oppos) { return 1; } debt[mtag] += 1; - val = mtag === 'INDENT' ? match[1] : INVERSES[mtag]; - this.tokens.splice(i, 0, [INVERSES[mtag], val]); + val = [oppos, mtag === 'INDENT' ? match[1] : oppos]; + if ((this.tokens[i + 2] == undefined ? undefined : this.tokens[i + 2][0]) === mtag) { + this.tokens.splice(i + 3, 0, val); + stack.push(match); + } else { + this.tokens.splice(i, 0, val); + } return 1; } } else { diff --git a/src/rewriter.coffee b/src/rewriter.coffee index 8ebe38ff..670b0c62 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -209,7 +209,8 @@ exports.Rewriter: class Rewriter # it with the inverse of what we've just popped. # 3. Keep track of "debt" for tokens that we manufacture, to make sure we end # up balanced in the end. - # + # 4. Be careful not to alter array or parentheses delimiters with overzealous + # rewriting. rewrite_closing_parens: -> stack: [] debt: {} @@ -228,19 +229,15 @@ exports.Rewriter: class Rewriter else match: stack.pop() mtag: match[0] - return 1 if tag is INVERSES[mtag] + oppos: INVERSES[mtag] + return 1 if tag is oppos debt[mtag]: + 1 - val: if mtag is 'INDENT' then match[1] else INVERSES[mtag] - - # Edge case, lookahead and if we are inserting in front of a - # subsequent opening token, insert ahead of it and re-queue onto - # stack. - if @tokens[i + 2]?[0] == mtag - @tokens.splice i + 3, 0, [INVERSES[mtag], val] + val: [oppos, if mtag is 'INDENT' then match[1] else oppos] + if @tokens[i + 2]?[0] is mtag + @tokens.splice i + 3, 0, val stack.push(match) else - @tokens.splice i, 0, [INVERSES[mtag], val] - + @tokens.splice i, 0, val return 1 else return 1 diff --git a/test/test_chaining.coffee b/test/test_chaining.coffee index 30a618c3..040418d7 100644 --- a/test/test_chaining.coffee +++ b/test/test_chaining.coffee @@ -35,12 +35,13 @@ six: ok six is 6 -# Bug due to rewriting issue with indented array literals + +# Ensure that indented array literals don't trigger whitespace rewriting. func: () -> ok arguments.length is 1 func( - [[[[[], + [[[[[], []], [[]]]], []])