Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a61b6ee

Browse files
committed
Fixed leading whitespace before interpolation in simple strings
1 parent b11d956 commit a61b6ee

3 files changed

Lines changed: 44 additions & 38 deletions

File tree

lib/coffee-script/lexer.js

Lines changed: 21 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lexer.coffee

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,15 @@ exports.Lexer = class Lexer
186186
# Matches strings, including multi-line strings. Ensures that quotation marks
187187
# are balanced within the string's contents, and within nested interpolations.
188188
stringToken: ->
189-
switch @chunk.charAt 0
190-
when "'"
191-
return 0 unless match = SIMPLESTR.exec @chunk
192-
string = match[0]
193-
@token 'STRING', @escapeLines(string), 0, string.length
194-
when '"'
195-
return 0 unless string = @balancedString @chunk, '"'
196-
if 0 < string.indexOf '#{', 1
197-
@interpolateString string[1...-1], strOffset: 1, lexedLength: string.length
198-
else
199-
@token 'STRING', @escapeLines(string), 0, string.length
200-
else
201-
return 0
189+
switch quote = @chunk.charAt 0
190+
when "'" then [string] = SIMPLESTR.exec @chunk
191+
when '"' then string = @balancedString @chunk, '"'
192+
return 0 unless string
193+
trimmed = @removeNewlines string[1...-1]
194+
if quote is '"' and 0 < string.indexOf '#{', 1
195+
@interpolateString trimmed, strOffset: 1, lexedLength: string.length
196+
else
197+
@token 'STRING', quote + @escapeLines(trimmed) + quote, 0, string.length
202198
if octalEsc = /^(?:\\.|[^\\])*\\(?:0[0-7]|[1-7])/.test string
203199
@error "octal escape sequences #{string} are not allowed"
204200
string.length
@@ -686,6 +682,11 @@ exports.Lexer = class Lexer
686682
@tag() in ['\\', '.', '?.', '?::', 'UNARY', 'MATH', '+', '-', 'SHIFT', 'RELATION'
687683
'COMPARE', 'LOGIC', 'THROW', 'EXTENDS']
688684

685+
# Remove newlines from beginning and (non escaped) from end of string literals.
686+
removeNewlines: (str) ->
687+
str.replace(/^\s*\n\s*/, '')
688+
.replace(/([^\\]|\\\\)\s*\n\s*$/, '$1')
689+
689690
# Converts newlines for string literals.
690691
escapeLines: (str, heredoc) ->
691692
# Ignore escaped backslashes and remove escaped newlines
@@ -695,9 +696,7 @@ exports.Lexer = class Lexer
695696
str.replace MULTILINER, '\\n'
696697
else
697698
# Trim leading and trailing whitespace, string includes quotes
698-
str.replace(/^(.)\s*\n\s*/, '$1')
699-
.replace(/\s*\n\s*(.)$/, '$1')
700-
.replace(/\s*\n\s*/g, ' ')
699+
str.replace /\s*\n\s*/g, ' '
701700

702701
# Constructs a string token by escaping quotes and newlines.
703702
makeString: (body, quote, heredoc) ->

test/strings.coffee

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ test "#3229, multiline strings", ->
8181
'string ' + "inside
8282
interpolation"
8383
}", "a string inside interpolation"
84+
eq "
85+
#{1}
86+
", '1'
8487

8588
# Handle escaped backslashes correctly.
8689
eq '\\', `'\\'`
@@ -155,6 +158,11 @@ test "#3249, escape newlines in heredocs with backslashes", ->
155158
too #{3}\
156159
!
157160
""", 'interpolation 1\n follows 2 too 3!'
161+
eq """
162+
163+
#{1} #{2}
164+
165+
""", '\n1 2\n'
158166

159167
# TODO: uncomment when #2388 is fixed
160168
# eq """a heredoc #{

0 commit comments

Comments
 (0)