Skip to content
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
30 changes: 27 additions & 3 deletions lib/coffeescript/lexer.js

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

21 changes: 18 additions & 3 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ exports.Lexer = class Lexer
return id.length
if id is 'do' and regExSuper = /^(\s*super)(?!\(\))/.exec @chunk[3...]
@token 'SUPER', 'super'
@token 'CALL_START', '('
@token 'CALL_START', '('
@token 'CALL_END', ')'
[input, sup] = regExSuper
return sup.length + 3
Expand Down Expand Up @@ -820,8 +820,23 @@ exports.Lexer = class Lexer
[tag, value] = token
switch tag
when 'TOKENS'
# Optimize out empty interpolations (an empty pair of parentheses).
continue if value.length is 2
if value.length is 2
# Optimize out empty interpolations (an empty pair of parentheses).
continue unless value[0].comments or value[1].comments
# There are comments (and nothing else) in this interpolation.
if @csxDepth is 0
# This is an interpolated string, not a CSX tag; and for whatever
# reason `` `a${/*test*/}b` `` is invalid JS. So compile to
# `` `a${/*test*/''}b` `` instead.
placeholderToken = @makeToken 'STRING', "''"
else
placeholderToken = @makeToken 'JS', ''
# Use the same location data as the first parenthesis.
placeholderToken[2] = value[0][2]
for val in value when val.comments
placeholderToken.comments ?= []
placeholderToken.comments.push val.comments...
value.splice 1, 0, placeholderToken
# Push all the tokens in the fake `'TOKENS'` token. These already have
# sane location data.
locationToken = value[0]
Expand Down
20 changes: 20 additions & 0 deletions test/comments.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,26 @@ test "Empty lines between comments are preserved", ->
3;
}'''

test "Block comment in an interpolated string", ->
eqJS '"a#{### Comment ###}b"', "`a${/* Comment */''}b`;"
eqJS '"a#{### 1 ###}b#{### 2 ###}c"', "`a${/* 1 */''}b${/* 2 */''}c`;"

test "#4629: Block comment in CSX interpolation", ->
eqJS '<div>{### Comment ###}</div>', '<div>{/* Comment */}</div>;'
eqJS '''
<div>
{###
Multiline
Comment
###}
</div>''', '''
<div>
{/*
Multiline
Comment
*/}
</div>;'''

test "Line comment in an interpolated string", ->
eqJS '''
"a#{# Comment
Expand Down