Skip to content

Commit e6fb863

Browse files
authored
fix: also include generated } tokens when fixing closing token positions (#10)
Closes decaffeinate/decaffeinate#458 Closes decaffeinate/decaffeinate#461 Closes decaffeinate/decaffeinate#504 Just like OUTDENT and CALL_END tokens, close-curly-brace tokens can be generated without having a real location, and if that position overlaps with a later token, it can cause the AST to have bad location data. Just like the other two token types, we now give `}` tokens the position of the previous real token, which makes all AST nodes have reasonable locations.
1 parent 2a1fe8a commit e6fb863

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

lib/coffee-script/rewriter.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rewriter.coffee

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@ class exports.Rewriter
375375
fixOutdentLocationData: ->
376376
@scanTokens (token, i, tokens) ->
377377
return 1 unless token[0] is 'OUTDENT' or
378-
(token.generated and token[0] is 'CALL_END')
378+
(token.generated and token[0] is 'CALL_END') or
379+
(token.generated and token[0] is '}')
379380
prevLocationData = tokens[i - 1][2]
380381
token[2] =
381382
first_line: prevLocationData.last_line

test/location.coffee

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,27 @@ test "Verify OUTDENT and CALL_END tokens are located at the end of the previous
515515
eq token[0], 'CALL_END'
516516
assertAtCloseCurly(token)
517517

518+
test "Verify generated } tokens are located at the end of the previous token", ->
519+
source = '''
520+
a(b, ->
521+
c: () ->
522+
if d
523+
e
524+
)
525+
'''
526+
tokens = CoffeeScript.tokens source
527+
[..., identifier, outdent1, outdent2, closeCurly, outdent3, callEnd,
528+
terminator] = tokens
529+
eq identifier[0], 'IDENTIFIER'
530+
assertAtIdentifier = (token) ->
531+
eq token[2].first_line, identifier[2].last_line
532+
eq token[2].first_column, identifier[2].last_column
533+
eq token[2].last_line, identifier[2].last_line
534+
eq token[2].last_column, identifier[2].last_column
535+
536+
for token in [outdent1, outdent2, closeCurly, outdent3]
537+
assertAtIdentifier(token)
538+
518539
test "Verify real CALL_END tokens have the right position", ->
519540
source = '''
520541
a()

0 commit comments

Comments
 (0)