12
12
{Rewriter , INVERSES } = require ' ./rewriter'
13
13
14
14
# Import the helpers we need.
15
- {count, starts, compact, repeat, invertLiterate,
15
+ {count, starts, compact, repeat, invertLiterate, merge,
16
16
locationDataToString, throwSyntaxError} = require ' ./helpers'
17
17
18
18
# The Lexer Class
@@ -330,7 +330,6 @@ exports.Lexer = class Lexer
330
330
when match = REGEX .exec @chunk
331
331
[regex , body , closed ] = match
332
332
@ validateEscapes body, isRegex : yes , offsetInChunk : 1
333
- body = @ formatRegex body, delimiter : ' /'
334
333
index = regex .length
335
334
prev = @ prev ()
336
335
if prev
@@ -349,13 +348,17 @@ exports.Lexer = class Lexer
349
348
when not VALID_FLAGS .test flags
350
349
@ error " invalid regular expression flags #{ flags} " , offset : index, length : flags .length
351
350
when regex or tokens .length is 1
352
- body ?= @ formatHeregex tokens[0 ][1 ]
351
+ if body
352
+ body = @ formatRegex body, { flags, delimiter : ' /' }
353
+ else
354
+ body = @ formatHeregex tokens[0 ][1 ], { flags }
353
355
@ token ' REGEX' , " #{ @ makeDelimitedLiteral body, delimiter : ' /' }#{ flags} " , 0 , end, origin
354
356
else
355
357
@ token ' REGEX_START' , ' (' , 0 , 0 , origin
356
358
@ token ' IDENTIFIER' , ' RegExp' , 0 , 0
357
359
@ token ' CALL_START' , ' (' , 0 , 0
358
- @ mergeInterpolationTokens tokens, {delimiter : ' "' , double : yes }, @formatHeregex
360
+ @ mergeInterpolationTokens tokens, {delimiter : ' "' , double : yes }, (str ) =>
361
+ @ formatHeregex str, { flags }
359
362
if flags
360
363
@ token ' ,' , ' ,' , index - 1 , 0
361
364
@ token ' STRING' , ' "' + flags + ' "' , index - 1 , flags .length
@@ -792,8 +795,8 @@ exports.Lexer = class Lexer
792
795
formatString : (str , options ) ->
793
796
@ replaceUnicodeCodePointEscapes str .replace (STRING_OMIT, ' $1' ), options
794
797
795
- formatHeregex : (str ) ->
796
- @ formatRegex str .replace (HEREGEX_OMIT, ' $1$2' ), delimiter : ' ///'
798
+ formatHeregex : (str , options ) ->
799
+ @ formatRegex str .replace (HEREGEX_OMIT, ' $1$2' ), merge (options, delimiter : ' ///' )
797
800
798
801
formatRegex : (str , options ) ->
799
802
@ replaceUnicodeCodePointEscapes str, options
@@ -808,8 +811,9 @@ exports.Lexer = class Lexer
808
811
low = (codePoint - 0x10000 ) % 0x400 + 0xDC00
809
812
" #{ toUnicodeEscape (high)}#{ toUnicodeEscape (low)} "
810
813
811
- # Replace \u{...} with \uxxxx[\uxxxx] in strings and regexes
814
+ # Replace \u{...} with \uxxxx[\uxxxx] in regexes without `u` flag
812
815
replaceUnicodeCodePointEscapes : (str , options ) ->
816
+ shouldReplace = options .flags ? and ' u' not in options .flags
813
817
str .replace UNICODE_CODE_POINT_ESCAPE, (match , escapedBackslash , codePointHex , offset ) =>
814
818
return escapedBackslash if escapedBackslash
815
819
@@ -818,6 +822,7 @@ exports.Lexer = class Lexer
818
822
@ error " unicode code point escapes greater than \\ u{10ffff} are not allowed" ,
819
823
offset : offset + options .delimiter .length
820
824
length : codePointHex .length + 4
825
+ return match unless shouldReplace
821
826
822
827
@ unicodeCodePointToUnicodeEscapes codePointDecimal
823
828
0 commit comments