Skip to content

Commit 9fef66f

Browse files
empty regular expressions with flags still need to be compiled to /(?:)/
1 parent 1627922 commit 9fef66f

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

lib/coffee-script/lexer.js

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

src/lexer.coffee

+6-6
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ exports.Lexer = class Lexer
198198
prev = last @tokens
199199
return 0 if prev and (prev[0] in (if prev.spaced then NOT_REGEX else NOT_SPACED_REGEX))
200200
return 0 unless match = REGEX.exec @chunk
201-
[regex] = match
202-
if regex.match /^\/\*/ then @error 'regular expressions cannot begin with `*`'
201+
[match, regex, flags] = match
202+
if regex[..1] is '/*' then @error 'regular expressions cannot begin with `*`'
203203
if regex is '//' then regex = '/(?:)/'
204-
@token 'REGEX', regex
205-
regex.length
204+
@token 'REGEX', "#{regex}#{flags}"
205+
match.length
206206

207207
# Matches multiline extended regular expressions.
208208
heregexToken: (match) ->
@@ -607,7 +607,7 @@ JSTOKEN = /^`[^\\`]*(?:\\.[^\\`]*)*`/
607607

608608
# Regex-matching-regexes.
609609
REGEX = /// ^
610-
/ (?! [\s=] ) # disallow leading whitespace or equals signs
610+
(/ (?! [\s=] ) # disallow leading whitespace or equals signs
611611
[^ [ / \n \\ ]* # every other thing
612612
(?:
613613
(?: \\[\s\S] # anything escaped
@@ -617,7 +617,7 @@ REGEX = /// ^
617617
]
618618
) [^ [ / \n \\ ]*
619619
)*
620-
/ [imgy]{0,4} (?!\w)
620+
/) ([imgy]{0,4}) (?!\w)
621621
///
622622

623623
HEREGEX = /// ^ /{3} ([\s\S]+?) /{3} ([imgy]{0,4}) (?!\w) ///

test/regexps.coffee

+6
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,9 @@ test "an empty heregex will compile to an empty, non-capturing group", ->
5555

5656
test "#1724: regular expressions beginning with `*`", ->
5757
throws -> CoffeeScript.compile '/// * ///'
58+
59+
test "empty regular expressions with flags", ->
60+
fn = (x) -> x
61+
a = "" + //i
62+
fn ""
63+
eq '/(?:)/i', a

0 commit comments

Comments
 (0)