diff --git a/lib/coffee-script/lexer.js b/lib/coffee-script/lexer.js index bab43089..0f97eab4 100644 --- a/lib/coffee-script/lexer.js +++ b/lib/coffee-script/lexer.js @@ -275,7 +275,7 @@ var body, flags, flagsOffset, heregex, plusToken, prev, re, tag, token, tokens, value, _i, _len, _ref2, _ref3, _ref4; heregex = match[0], body = match[1], flags = match[2]; if (0 > body.indexOf('#{')) { - re = body.replace(HEREGEX_OMIT, '$1$2').replace(/\//g, '\\/'); + re = this.escapeLines(body.replace(HEREGEX_OMIT, '$1$2').replace(/\//g, '\\/'), true); if (re.match(/^\*/)) { this.error('regular expressions cannot begin with `*`'); } @@ -860,7 +860,7 @@ HEREGEX = /^\/{3}((?:\\?[\s\S])+?)\/{3}([imgy]{0,4})(?!\w)/; - HEREGEX_OMIT = /((?:\\\\)+)|\\([^\n\S]|\/)|\s+(?:#.*)?/g; + HEREGEX_OMIT = /((?:\\\\)+)|\\(\s|\/)|\s+(?:#.*)?/g; MULTILINER = /\n/g; diff --git a/src/lexer.coffee b/src/lexer.coffee index 4371b3cd..ed995a19 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -255,7 +255,7 @@ exports.Lexer = class Lexer heregexToken: (match) -> [heregex, body, flags] = match if 0 > body.indexOf '#{' - re = body.replace(HEREGEX_OMIT, '$1$2').replace(/\//g, '\\/') + re = @escapeLines body.replace(HEREGEX_OMIT, '$1$2').replace(/\//g, '\\/'), yes if re.match /^\*/ then @error 'regular expressions cannot begin with `*`' @token 'REGEX', "/#{ re or '(?:)' }/#{flags}", 0, heregex.length return heregex.length @@ -810,7 +810,7 @@ HEREGEX = /// ^ /{3} ((?:\\?[\s\S])+?) /{3} ([imgy]{0,4}) (?!\w) /// HEREGEX_OMIT = /// ((?:\\\\)+) # consume (and preserve) an even number of backslashes - | \\([^\n\S]|/) # preserve escaped spaces and "de-escape" slashes + | \\(\s|/) # preserve escaped whitespace and "de-escape" slashes | \s+(?:#.*)? # remove whitespace and comments ///g diff --git a/test/regexps.coffee b/test/regexps.coffee index 3511709f..ff4f32b3 100644 --- a/test/regexps.coffee +++ b/test/regexps.coffee @@ -72,3 +72,8 @@ test "#2238: don't escape already escaped slashes", -> test "escaped slashes don't close heregex", -> eq /// \/// ///.source, /\/\/\//.source eq /// \\\////.source, /\\\//.source + +test "escaped linebreaks", -> + eq /// \n\ + \ + ///.source, /\n\n\n/.source