From f229f791a9909d0f2d46254b8e1c758f2b5aeade Mon Sep 17 00:00:00 2001 From: Timothy Jones Date: Thu, 21 Oct 2010 14:37:58 +1300 Subject: [PATCH] Proper testing, this time. --- lib/lexer.js | 17 +++++++---------- lib/rewriter.js | 2 +- src/lexer.coffee | 10 ++++------ src/rewriter.coffee | 2 +- test/test_operations.coffee | 6 +++++- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/lexer.js b/lib/lexer.js index 29f3449a..274548e2 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -302,23 +302,20 @@ return true; }; Lexer.prototype.whitespaceToken = function() { - var match, prev; - if (!(match = WHITESPACE.exec(this.chunk))) { + var match, nline, prev; + if (!((match = WHITESPACE.exec(this.chunk)) || (nline = this.chunk.substring(0, 1) === '\n'))) { return false; } prev = last(this.tokens); if (prev) { - prev.spaced = true; + prev[match ? 'spaced' : 'newLine'] = true; } - this.i += match[0].length; - return true; + if (match) { + this.i += match[0].length; + } + return !!match; }; Lexer.prototype.newlineToken = function(newlines) { - var prev; - prev = last(this.tokens); - if (prev) { - prev.newLine = true; - } if (this.tag() !== 'TERMINATOR') { this.token('TERMINATOR', '\n'); } diff --git a/lib/rewriter.js b/lib/rewriter.js index c75af4d4..512f2e45 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -201,7 +201,7 @@ if (prev && !prev.spaced && tag === '?') { token.call = true; } - if (!(callObject || ((prev != null) ? prev.spaced : undefined) && (prev.call || (_ref2 = prev[0], __indexOf.call(IMPLICIT_FUNC, _ref2) >= 0)) && (__indexOf.call(IMPLICIT_CALL, tag) >= 0 || !token.spaced && __indexOf.call(IMPLICIT_UNSPACED_CALL, tag) >= 0))) { + if (!(callObject || ((prev != null) ? prev.spaced : undefined) && (prev.call || (_ref2 = prev[0], __indexOf.call(IMPLICIT_FUNC, _ref2) >= 0)) && (__indexOf.call(IMPLICIT_CALL, tag) >= 0 || !(token.spaced || token.newLine) && __indexOf.call(IMPLICIT_UNSPACED_CALL, tag) >= 0))) { return 1; } tokens.splice(i, 0, ['CALL_START', '(', token[2]]); diff --git a/src/lexer.coffee b/src/lexer.coffee index d9c5d562..ad7f0f9e 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -281,16 +281,14 @@ exports.Lexer = class Lexer # Matches and consumes non-meaningful whitespace. Tag the previous token # as being "spaced", because there are some cases where it makes a difference. whitespaceToken: -> - return false unless match = WHITESPACE.exec @chunk + return false unless (match = WHITESPACE.exec @chunk) or nline = @chunk.substring(0, 1) is '\n' prev = last @tokens - prev.spaced = true if prev - @i += match[0].length - true + prev[if match then 'spaced' else 'newLine'] = true if prev + @i += match[0].length if match + !!match # Generate a newline token. Consecutive newlines get merged together. newlineToken: (newlines) -> - prev = last @tokens - prev.newLine = true if prev @token 'TERMINATOR', '\n' unless @tag() is 'TERMINATOR' true diff --git a/src/rewriter.coffee b/src/rewriter.coffee index 8135f217..501653cf 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -165,7 +165,7 @@ class exports.Rewriter token.call = yes if prev and not prev.spaced and tag is '?' return 1 unless callObject or prev?.spaced and (prev.call or prev[0] in IMPLICIT_FUNC) and - (tag in IMPLICIT_CALL or not token.spaced and tag in IMPLICIT_UNSPACED_CALL) + (tag in IMPLICIT_CALL or not (token.spaced or token.newLine) and tag in IMPLICIT_UNSPACED_CALL) tokens.splice i, 0, ['CALL_START', '(', token[2]] @detectEnd i + (if callObject then 2 else 1), (token, i) -> return yes if not seenSingle and token.fromThen diff --git a/test/test_operations.coffee b/test/test_operations.coffee index 07beb3e9..ce8fa0b3 100644 --- a/test/test_operations.coffee +++ b/test/test_operations.coffee @@ -155,6 +155,10 @@ ok a() not in [b(),c()] and share is 3 # Operators should respect new lines as spaced. -a = 123 + +a = (123) + 456 ok a is 579 + +a = "1#{2}3" + +"456" +ok a is '123456'