Skip to content

Fix location data for implicit CALL_END tokens #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/coffee-script/rewriter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/rewriter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ class exports.Rewriter
# location corresponding to the last "real" token under the node.
fixOutdentLocationData: ->
@scanTokens (token, i, tokens) ->
return 1 unless token[0] is 'OUTDENT'
return 1 unless token[0] is 'OUTDENT' or
(token.generated and token[0] is 'CALL_END')
prevLocationData = tokens[i - 1][2]
token[2] =
first_line: prevLocationData.last_line
Expand Down
41 changes: 41 additions & 0 deletions test/location.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,47 @@ test "Verify OUTDENT tokens are located at the end of the previous token", ->
eq outdent[2].last_line, number[2].last_line
eq outdent[2].last_column, number[2].last_column

test "Verify OUTDENT and CALL_END tokens are located at the end of the previous token", ->
source = '''
a = b {
c: ->
d e,
if f
g {},
if h
i {}
}
'''
tokens = CoffeeScript.tokens source
[..., closeCurly1, callEnd1, outdent1, outdent2, callEnd2, outdent3, outdent4,
callEnd3, outdent5, outdent6, closeCurly2, callEnd4, terminator] = tokens
eq closeCurly1[0], '}'
assertAtCloseCurly = (token) ->
eq token[2].first_line, closeCurly1[2].last_line
eq token[2].first_column, closeCurly1[2].last_column
eq token[2].last_line, closeCurly1[2].last_line
eq token[2].last_column, closeCurly1[2].last_column

for token in [outdent1, outdent2, outdent3, outdent4, outdent5, outdent6]
eq token[0], 'OUTDENT'
assertAtCloseCurly(token)
for token in [callEnd1, callEnd2, callEnd3]
eq token[0], 'CALL_END'
assertAtCloseCurly(token)

test "Verify real CALL_END tokens have the right position", ->
source = '''
a()
'''
tokens = CoffeeScript.tokens source
[identifier, callStart, callEnd, terminator] = tokens
startIndex = identifier[2].first_column
eq identifier[2].last_column, startIndex
eq callStart[2].first_column, startIndex + 1
eq callStart[2].last_column, startIndex + 1
eq callEnd[2].first_column, startIndex + 2
eq callEnd[2].last_column, startIndex + 2

test "Verify all tokens get a location", ->
doesNotThrow ->
tokens = CoffeeScript.tokens testScript
Expand Down