Skip to content

Commit 88693e4

Browse files
committed
Fix location data for implicit CALL_END tokens
Fixes decaffeinate/decaffeinate#446 In addition to OUTDENT tokens, CALL_END tokens can also be virtual tokens without a real location, and sometimes they end up with a location that's incorrect.
1 parent ce971b7 commit 88693e4

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

lib/coffee-script/rewriter.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rewriter.coffee

+2-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ class exports.Rewriter
374374
# location corresponding to the last "real" token under the node.
375375
fixOutdentLocationData: ->
376376
@scanTokens (token, i, tokens) ->
377-
return 1 unless token[0] is 'OUTDENT'
377+
return 1 unless token[0] is 'OUTDENT' or
378+
(token.generated and token[0] is 'CALL_END')
378379
prevLocationData = tokens[i - 1][2]
379380
token[2] =
380381
first_line: prevLocationData.last_line

test/location.coffee

+41
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,47 @@ test "Verify OUTDENT tokens are located at the end of the previous token", ->
487487
eq outdent[2].last_line, number[2].last_line
488488
eq outdent[2].last_column, number[2].last_column
489489

490+
test "Verify OUTDENT and CALL_END tokens are located at the end of the previous token", ->
491+
source = '''
492+
a = b {
493+
c: ->
494+
d e,
495+
if f
496+
g {},
497+
if h
498+
i {}
499+
}
500+
'''
501+
tokens = CoffeeScript.tokens source
502+
[..., closeCurly1, callEnd1, outdent1, outdent2, callEnd2, outdent3, outdent4,
503+
callEnd3, outdent5, outdent6, closeCurly2, callEnd4, terminator] = tokens
504+
eq closeCurly1[0], '}'
505+
assertAtCloseCurly = (token) ->
506+
eq token[2].first_line, closeCurly1[2].last_line
507+
eq token[2].first_column, closeCurly1[2].last_column
508+
eq token[2].last_line, closeCurly1[2].last_line
509+
eq token[2].last_column, closeCurly1[2].last_column
510+
511+
for token in [outdent1, outdent2, outdent3, outdent4, outdent5, outdent6]
512+
eq token[0], 'OUTDENT'
513+
assertAtCloseCurly(token)
514+
for token in [callEnd1, callEnd2, callEnd3]
515+
eq token[0], 'CALL_END'
516+
assertAtCloseCurly(token)
517+
518+
test "Verify real CALL_END tokens have the right position", ->
519+
source = '''
520+
a()
521+
'''
522+
tokens = CoffeeScript.tokens source
523+
[identifier, callStart, callEnd, terminator] = tokens
524+
startIndex = identifier[2].first_column
525+
eq identifier[2].last_column, startIndex
526+
eq callStart[2].first_column, startIndex + 1
527+
eq callStart[2].last_column, startIndex + 1
528+
eq callEnd[2].first_column, startIndex + 2
529+
eq callEnd[2].last_column, startIndex + 2
530+
490531
test "Verify all tokens get a location", ->
491532
doesNotThrow ->
492533
tokens = CoffeeScript.tokens testScript

0 commit comments

Comments
 (0)