From deead4bfad61796f2143d0d1ffe0ae949937219c Mon Sep 17 00:00:00 2001 From: minodisk Date: Sun, 13 Jul 2014 14:03:32 +0900 Subject: [PATCH] Fix wrong location issue in heregex interpolation --- lib/coffee-script/lexer.js | 3 +- src/lexer.coffee | 2 +- test/location.coffee | 172 ++++++++++++++++++++++++++++++++++++- 3 files changed, 171 insertions(+), 6 deletions(-) diff --git a/lib/coffee-script/lexer.js b/lib/coffee-script/lexer.js index 9e22da79..43658f54 100644 --- a/lib/coffee-script/lexer.js +++ b/lib/coffee-script/lexer.js @@ -290,7 +290,8 @@ this.token('CALL_START', '(', 0, 0); tokens = []; _ref2 = this.interpolateString(body, { - regex: true + regex: true, + strOffset: 3 }); for (_i = 0, _len = _ref2.length; _i < _len; _i++) { token = _ref2[_i]; diff --git a/src/lexer.coffee b/src/lexer.coffee index c6f9a647..dbf4a49f 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -263,7 +263,7 @@ exports.Lexer = class Lexer @token 'IDENTIFIER', 'RegExp', 0, 0 @token 'CALL_START', '(', 0, 0 tokens = [] - for token in @interpolateString(body, regex: yes) + for token in @interpolateString(body, regex: yes, strOffset: 3) [tag, value] = token if tag is 'TOKENS' tokens.push value... diff --git a/test/location.coffee b/test/location.coffee index ca90a5d8..84b5d991 100644 --- a/test/location.coffee +++ b/test/location.coffee @@ -128,10 +128,6 @@ test 'Verify locations in string interpolation (in "string", multiple interpolat eq tokens.length, 10 [{}, {}, {}, a, {}, b, {}, c] = tokens - console.log a - console.log b - console.log c - eq a[2].first_line, 1 eq a[2].first_column, 2 eq a[2].last_line, 1 @@ -315,6 +311,174 @@ test 'Verify locations in string interpolation (in """string""", multiple interp eq c[2].last_line, 9 eq c[2].last_column, 2 +test 'Verify locations in heregex interpolation (in ///regex///, multiple interpolation)', -> + tokens = CoffeeScript.tokens '///#{a}b#{c}///' + + eq tokens.length, 11 + [{}, {}, {}, {}, a, {}, b, {}, c] = tokens + + eq a[2].first_line, 0 + eq a[2].first_column, 5 + eq a[2].last_line, 0 + eq a[2].last_column, 5 + + eq b[2].first_line, 0 + eq b[2].first_column, 7 + eq b[2].last_line, 0 + eq b[2].last_column, 7 + + eq c[2].first_line, 0 + eq c[2].first_column, 10 + eq c[2].last_line, 0 + eq c[2].last_column, 10 + +test 'Verify locations in heregex interpolation (in ///regex///, multiple interpolation)', -> + tokens = CoffeeScript.tokens '///a#{b}c///' + + eq tokens.length, 9 + [{}, {}, a, {}, b, {}, c] = tokens + + eq a[2].first_line, 0 + eq a[2].first_column, 3 + eq a[2].last_line, 0 + eq a[2].last_column, 3 + + eq b[2].first_line, 0 + eq b[2].first_column, 6 + eq b[2].last_line, 0 + eq b[2].last_column, 6 + + eq c[2].first_line, 0 + eq c[2].first_column, 8 + eq c[2].last_line, 0 + eq c[2].last_column, 8 + +test 'Verify locations in heregex interpolation (in ///regex///, multiple interpolation and line breaks)', -> + tokens = CoffeeScript.tokens '///#{a}\nb\n#{c}///' + + eq tokens.length, 11 + [{}, {}, {}, {}, a, {}, b, {}, c] = tokens + + eq a[2].first_line, 0 + eq a[2].first_column, 5 + eq a[2].last_line, 0 + eq a[2].last_column, 5 + + eq b[2].first_line, 0 + eq b[2].first_column, 7 + eq b[2].last_line, 1 + eq b[2].last_column, 1 + + eq c[2].first_line, 2 + eq c[2].first_column, 2 + eq c[2].last_line, 2 + eq c[2].last_column, 2 + +test 'Verify locations in heregex interpolation (in ///regex///, multiple interpolation and line breaks)', -> + tokens = CoffeeScript.tokens '///#{a}\n\n\nb\n\n\n#{c}///' + + eq tokens.length, 11 + [{}, {}, {}, {}, a, {}, b, {}, c] = tokens + + eq a[2].first_line, 0 + eq a[2].first_column, 5 + eq a[2].last_line, 0 + eq a[2].last_column, 5 + + eq b[2].first_line, 0 + eq b[2].first_column, 7 + eq b[2].last_line, 5 + eq b[2].last_column, 0 + + eq c[2].first_line, 6 + eq c[2].first_column, 2 + eq c[2].last_line, 6 + eq c[2].last_column, 2 + +test 'Verify locations in heregex interpolation (in ///regex///, multiple interpolation and line breaks)', -> + tokens = CoffeeScript.tokens '///a\n\n\n#{b}\n\n\nc///' + + eq tokens.length, 9 + [{}, {}, a, {}, b, {}, c] = tokens + + eq a[2].first_line, 0 + eq a[2].first_column, 3 + eq a[2].last_line, 2 + eq a[2].last_column, 0 + + eq b[2].first_line, 3 + eq b[2].first_column, 2 + eq b[2].last_line, 3 + eq b[2].last_column, 2 + + eq c[2].first_line, 3 + eq c[2].first_column, 4 + eq c[2].last_line, 6 + eq c[2].last_column, 0 + +test 'Verify locations in heregex interpolation (in ///regex///, multiple interpolation and line breaks and stating with linebreak)', -> + tokens = CoffeeScript.tokens '///\n#{a}\nb\n#{c}///' + + eq tokens.length, 11 + [{}, {}, {}, {}, a, {}, b, {}, c] = tokens + + eq a[2].first_line, 1 + eq a[2].first_column, 2 + eq a[2].last_line, 1 + eq a[2].last_column, 2 + + eq b[2].first_line, 1 + eq b[2].first_column, 4 + eq b[2].last_line, 2 + eq b[2].last_column, 1 + + eq c[2].first_line, 3 + eq c[2].first_column, 2 + eq c[2].last_line, 3 + eq c[2].last_column, 2 + +test 'Verify locations in heregex interpolation (in ///regex///, multiple interpolation and line breaks and stating with linebreak)', -> + tokens = CoffeeScript.tokens '///\n\n\n#{a}\n\n\nb\n\n\n#{c}///' + + eq tokens.length, 11 + [{}, {}, {}, {}, a, {}, b, {}, c] = tokens + + eq a[2].first_line, 3 + eq a[2].first_column, 2 + eq a[2].last_line, 3 + eq a[2].last_column, 2 + + eq b[2].first_line, 3 + eq b[2].first_column, 4 + eq b[2].last_line, 8 + eq b[2].last_column, 0 + + eq c[2].first_line, 9 + eq c[2].first_column, 2 + eq c[2].last_line, 9 + eq c[2].last_column, 2 + +test 'Verify locations in heregex interpolation (in ///regex///, multiple interpolation and line breaks and stating with linebreak)', -> + tokens = CoffeeScript.tokens '///\n\n\na\n\n\n#{b}\n\n\nc///' + + eq tokens.length, 9 + [{}, {}, a, {}, b, {}, c] = tokens + + eq a[2].first_line, 0 + eq a[2].first_column, 3 + eq a[2].last_line, 5 + eq a[2].last_column, 0 + + eq b[2].first_line, 6 + eq b[2].first_column, 2 + eq b[2].last_line, 6 + eq b[2].last_column, 2 + + eq c[2].first_line, 6 + eq c[2].first_column, 4 + eq c[2].last_line, 9 + eq c[2].last_column, 0 + test "Verify all tokens get a location", -> doesNotThrow -> tokens = CoffeeScript.tokens testScript