Skip to content

Commit f757614

Browse files
committed
Place ending heregex tokens one index earlier
This is an upstream port of decaffeinate#17 The lexer generates fake tokens for interpolated heregexes, and the ending tokens were being placed where the start (inclusive) and end (inclusive) index were one past the end of the heregex. This meant that in a case like `[a ///#{b}///]`, the end tokens of the heregex and also the implicit function call end were all being placed at the `]`, so the AST location data would say that the function call ends at the end of the `]`. To fix, I can just subtract 1 from the position of those ending heregex tokens so that their end lines up with the end of the heregex itself. This is similar to previous fixes that changed `OUTDENT` and `CALL_END` tokens so that the end of the token lines up with the end of the AST node.
1 parent 0d13231 commit f757614

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

lib/coffee-script/lexer.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lexer.coffee

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,10 @@ exports.Lexer = class Lexer
332332
@token 'CALL_START', '(', 0, 0
333333
@mergeInterpolationTokens tokens, {delimiter: '"', double: yes}, @formatHeregex
334334
if flags
335-
@token ',', ',', index, 0
336-
@token 'STRING', '"' + flags + '"', index, flags.length
337-
@token ')', ')', end, 0
338-
@token 'REGEX_END', ')', end, 0
335+
@token ',', ',', index - 1, 0
336+
@token 'STRING', '"' + flags + '"', index - 1, flags.length
337+
@token ')', ')', end - 1, 0
338+
@token 'REGEX_END', ')', end - 1, 0
339339

340340
end
341341

test/location.coffee

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,29 @@ test "Verify indented heredocs have the right position", ->
564564
eq stringToken[2].last_line, 3
565565
eq stringToken[2].last_column, 4
566566

567+
test "Verify heregexes with interpolations have the right ending position", ->
568+
source = '''
569+
[a ///#{b}///g]
570+
'''
571+
[..., stringEnd, comma, flagsString, regexCallEnd, regexEnd, fnCallEnd,
572+
arrayEnd, terminator] = CoffeeScript.tokens source
573+
574+
eq comma[0], ','
575+
eq arrayEnd[0], ']'
576+
577+
assertColumn = (token, column) ->
578+
eq token[2].first_line, 0
579+
eq token[2].first_column, column
580+
eq token[2].last_line, 0
581+
eq token[2].last_column, column
582+
583+
arrayEndColumn = arrayEnd[2].first_column
584+
for token in [comma, flagsString]
585+
assertColumn token, arrayEndColumn - 2
586+
for token in [regexCallEnd, regexEnd, fnCallEnd]
587+
assertColumn token, arrayEndColumn - 1
588+
assertColumn arrayEnd, arrayEndColumn
589+
567590
test "Verify all tokens get a location", ->
568591
doesNotThrow ->
569592
tokens = CoffeeScript.tokens testScript

0 commit comments

Comments
 (0)