diff --git a/src/lexer.ls b/src/lexer.ls index 9b6a289ad..82429002c 100644 --- a/src/lexer.ls +++ b/src/lexer.ls @@ -405,7 +405,7 @@ exports <<< if t.0 is 'TOKENS' tokens.push ...t.1 else - val = t.1.replace HEREGEX_OMIT, '' + val = deheregex t.1 continue if one and not val one = tokens.push t <<< ['STRNUM' @string '\'' enslash val] tokens.push ['+-' '+' tokens[*-1].2, tokens[*-1].3] @@ -415,7 +415,7 @@ exports <<< if dynaflag then tokens.push ...dynaflag else @token 'STRNUM' "'#flag'" @token (if flag is '$' then ')' else ')CALL'), '' else - @regex reslash(parts.0.1.replace HEREGEX_OMIT, ''), flag + @regex reslash(deheregex parts.0.1), flag parts.size + flag.length # Matches a word literal, or ignores a sequence of whitespaces. @@ -944,6 +944,12 @@ reslash = (.replace /(\\.)|\//g -> &1 or \\\/) # Transforms hyphenated-words to camelCase. camelize = (.replace /-[a-z]/ig -> it.char-at 1 .to-upper-case!) +# ESifies a heregex. +deheregex = (.replace do + /\s+(?:#.*)?|(\\[\s\S])/g + (, bs || '') -> if \\n is bs.charAt 1 then \\\n else bs +) + # Deletes the first character if newline. function lchomp then it.slice 1 + it.last-index-of '\n' 0 diff --git a/test/regex.ls b/test/regex.ls index d7fc67786..20d8804d4 100644 --- a/test/regex.ls +++ b/test/regex.ls @@ -57,6 +57,13 @@ eq '\\\\#{}\\\\\\\"', // \\ \" //.source +# [coffee#3059](https://github.com/jashkenas/coffee-script/pull/3059) +# Keep escaped whitespaces. +ok //^ + a \ b \ c \ + d +$//test 'a b\u3000c\nd' + eq '(?:)' ////source eq // _ #{if 1 then \g else \m}//? + '', '/_/g'