Skip to content

Commit 40c3511

Browse files
[CS2] Fix #4629: interpolations (whether in strings or CSX tags) with only comments (#4659)
* Fix #4629: interpolations (whether in strings or CSX tags) that contain nothing but comments should not be optimized out * Template literals need an expression inside their interpolations, so if we only have a comment to put in there, toss in an empty string as well
1 parent a3b08e1 commit 40c3511

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

lib/coffeescript/lexer.js

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

src/lexer.coffee

+18-3
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ exports.Lexer = class Lexer
139139
return id.length
140140
if id is 'do' and regExSuper = /^(\s*super)(?!\(\))/.exec @chunk[3...]
141141
@token 'SUPER', 'super'
142-
@token 'CALL_START', '('
142+
@token 'CALL_START', '('
143143
@token 'CALL_END', ')'
144144
[input, sup] = regExSuper
145145
return sup.length + 3
@@ -820,8 +820,23 @@ exports.Lexer = class Lexer
820820
[tag, value] = token
821821
switch tag
822822
when 'TOKENS'
823-
# Optimize out empty interpolations (an empty pair of parentheses).
824-
continue if value.length is 2
823+
if value.length is 2
824+
# Optimize out empty interpolations (an empty pair of parentheses).
825+
continue unless value[0].comments or value[1].comments
826+
# There are comments (and nothing else) in this interpolation.
827+
if @csxDepth is 0
828+
# This is an interpolated string, not a CSX tag; and for whatever
829+
# reason `` `a${/*test*/}b` `` is invalid JS. So compile to
830+
# `` `a${/*test*/''}b` `` instead.
831+
placeholderToken = @makeToken 'STRING', "''"
832+
else
833+
placeholderToken = @makeToken 'JS', ''
834+
# Use the same location data as the first parenthesis.
835+
placeholderToken[2] = value[0][2]
836+
for val in value when val.comments
837+
placeholderToken.comments ?= []
838+
placeholderToken.comments.push val.comments...
839+
value.splice 1, 0, placeholderToken
825840
# Push all the tokens in the fake `'TOKENS'` token. These already have
826841
# sane location data.
827842
locationToken = value[0]

test/comments.coffee

+20
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,26 @@ test "Empty lines between comments are preserved", ->
721721
3;
722722
}'''
723723

724+
test "Block comment in an interpolated string", ->
725+
eqJS '"a#{### Comment ###}b"', "`a${/* Comment */''}b`;"
726+
eqJS '"a#{### 1 ###}b#{### 2 ###}c"', "`a${/* 1 */''}b${/* 2 */''}c`;"
727+
728+
test "#4629: Block comment in CSX interpolation", ->
729+
eqJS '<div>{### Comment ###}</div>', '<div>{/* Comment */}</div>;'
730+
eqJS '''
731+
<div>
732+
{###
733+
Multiline
734+
Comment
735+
###}
736+
</div>''', '''
737+
<div>
738+
{/*
739+
Multiline
740+
Comment
741+
*/}
742+
</div>;'''
743+
724744
test "Line comment in an interpolated string", ->
725745
eqJS '''
726746
"a#{# Comment

0 commit comments

Comments
 (0)