diff --git a/lib/coffee-script/lexer.js b/lib/coffee-script/lexer.js index 2c486d83..41de5487 100644 --- a/lib/coffee-script/lexer.js +++ b/lib/coffee-script/lexer.js @@ -193,14 +193,13 @@ }; Lexer.prototype.heredocToken = function() { - var doc, heredoc, match, quote, trimmed; + var doc, heredoc, match, quote; if (!(match = HEREDOC.exec(this.chunk))) { return 0; } heredoc = match[0]; quote = heredoc.charAt(0); - trimmed = match[2].replace(/(([^\\]|\\\\)\s*)\n[^\n\S]*$/, '$1'); - doc = this.sanitizeHeredoc(trimmed, { + doc = this.sanitizeHeredoc(match[2], { quote: quote, indent: null }); @@ -847,7 +846,7 @@ NUMBER = /^0b[01]+|^0o[0-7]+|^0x[\da-f]+|^\d*\.?\d+(?:e[+-]?\d+)?/i; - HEREDOC = /^("""|''')(([\s\S]*?([^\\]|\\\\))?)\1/; + HEREDOC = /^("""|''')((?:\\[\s\S]|[^\\])*?)(?:\n[^\n\S]*)?\1/; OPERATOR = /^(?:[-=]>|[-+*\/%<>&|^!?=]=|>>>=?|([-+:])\1|([&|<>])\2=?|\?(\.|::)|\.{2,3})/; diff --git a/src/lexer.coffee b/src/lexer.coffee index 72b63143..985c4042 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -205,9 +205,7 @@ exports.Lexer = class Lexer return 0 unless match = HEREDOC.exec @chunk heredoc = match[0] quote = heredoc.charAt 0 - # Trim last newline if it's not escaped - trimmed = match[2].replace /(([^\\]|\\\\)\s*)\n[^\n\S]*$/, '$1' - doc = @sanitizeHeredoc trimmed, quote: quote, indent: null + doc = @sanitizeHeredoc match[2], quote: quote, indent: null if quote is '"' and 0 <= doc.indexOf '#{' @interpolateString doc, heredoc: yes, strOffset: 3, lexedLength: heredoc.length else @@ -772,7 +770,7 @@ NUMBER = /// ^ \d*\.?\d+ (?:e[+-]?\d+)? # decimal ///i -HEREDOC = /// ^ ("""|''') (( [\s\S]*? ([^\\]|\\\\) )?) \1 /// +HEREDOC = /// ^ ("""|''') ((?: \\[\s\S] | [^\\] )*?) (?:\n[^\n\S]*)? \1 /// OPERATOR = /// ^ ( ?: [-=]> # function diff --git a/test/compilation.coffee b/test/compilation.coffee index 7190edf6..b43e0b67 100644 --- a/test/compilation.coffee +++ b/test/compilation.coffee @@ -70,6 +70,7 @@ test "#1050", -> test "#1273: escaping quotes at the end of heredocs", -> cantCompile '"""\\"""' # """\""" + cantCompile '"""\\\\\\"""' # """\\\""" test "#1106: __proto__ compilation", -> object = eq diff --git a/test/strings.coffee b/test/strings.coffee index 803981ac..708c6b14 100644 --- a/test/strings.coffee +++ b/test/strings.coffee @@ -207,6 +207,7 @@ eq '""Hello, World\\""', """ test "#1273, escaping quotes at the end of heredocs.", -> # """\""" no longer compiles eq """\\""", '\\' + eq """\\\"""", '\\\"' a = """ basic heredoc