Skip to content

Commit 26200f4

Browse files
author
Marc Häfner
committed
Improve HEREDOC regexp
* Exclude trailing blank line from the match group * Fix backslash handling
1 parent 13f2054 commit 26200f4

File tree

4 files changed

+7
-8
lines changed

4 files changed

+7
-8
lines changed

lib/coffee-script/lexer.js

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

src/lexer.coffee

+2-4
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,7 @@ exports.Lexer = class Lexer
205205
return 0 unless match = HEREDOC.exec @chunk
206206
heredoc = match[0]
207207
quote = heredoc.charAt 0
208-
# Trim last newline if it's not escaped
209-
trimmed = match[2].replace /(([^\\]|\\\\)\s*)\n[^\n\S]*$/, '$1'
210-
doc = @sanitizeHeredoc trimmed, quote: quote, indent: null
208+
doc = @sanitizeHeredoc match[2], quote: quote, indent: null
211209
if quote is '"' and 0 <= doc.indexOf '#{'
212210
@interpolateString doc, heredoc: yes, strOffset: 3, lexedLength: heredoc.length
213211
else
@@ -772,7 +770,7 @@ NUMBER = ///
772770
^ \d*\.?\d+ (?:e[+-]?\d+)? # decimal
773771
///i
774772

775-
HEREDOC = /// ^ ("""|''') (( [\s\S]*? ([^\\]|\\\\) )?) \1 ///
773+
HEREDOC = /// ^ ("""|''') ((?: \\[\s\S] | [^\\] )*?) (?:\n[^\n\S]*)? \1 ///
776774

777775
OPERATOR = /// ^ (
778776
?: [-=]> # function

test/compilation.coffee

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ test "#1050", ->
7070

7171
test "#1273: escaping quotes at the end of heredocs", ->
7272
cantCompile '"""\\"""' # """\"""
73+
cantCompile '"""\\\\\\"""' # """\\\"""
7374

7475
test "#1106: __proto__ compilation", ->
7576
object = eq

test/strings.coffee

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ eq '""Hello, World\\""', """
207207
test "#1273, escaping quotes at the end of heredocs.", ->
208208
# """\""" no longer compiles
209209
eq """\\""", '\\'
210+
eq """\\\"""", '\\\"'
210211

211212
a = """
212213
basic heredoc

0 commit comments

Comments
 (0)