Skip to content

Commit b5ec405

Browse files
committed
support the v regular expression literal flag
1 parent a7659c3 commit b5ec405

File tree

6 files changed

+19
-1
lines changed

6 files changed

+19
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434

3535
This fix was contributed by [@fz6m](https://github.com/fz6m).
3636

37+
* Add support for the `v` flag in regular expression literals
38+
39+
People are currently working on adding a `v` flag to JavaScript regular expresions. You can read more about this flag here: https://v8.dev/features/regexp-v-flag. This release adds support for parsing this flag, so esbuild will now no longer consider regular expression literals with this flag to be a syntax error. If the target is set to something other than `esnext`, esbuild will transform regular expression literals containing this flag into a `new RegExp()` constructor call so the resulting code doesn't have a syntax error. This enables you to provide a polyfill for `RegExp` that implements the `v` flag to get your code to work at run-time. While esbuild doesn't typically adopt proposals until they're already shipping in a real JavaScript run-time, I'm adding it now because a) esbuild's implementation doesn't need to change as the proposal evolves, b) this isn't really new syntax since regular expression literals already have flags, and c) esbuild's implementation is a trivial pass-through anyway.
40+
3741
## 0.16.8
3842

3943
* Allow plugins to resolve injected files ([#2754](https://github.com/evanw/esbuild/issues/2754))

internal/compat/js_table.go

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ const (
9797
RegexpLookbehindAssertions
9898
RegexpMatchIndices
9999
RegexpNamedCaptureGroups
100+
RegexpSetNotation
100101
RegexpStickyAndUnicodeFlags
101102
RegexpUnicodePropertyEscapes
102103
RestArgument
@@ -152,6 +153,7 @@ var StringToJSFeature = map[string]JSFeature{
152153
"regexp-lookbehind-assertions": RegexpLookbehindAssertions,
153154
"regexp-match-indices": RegexpMatchIndices,
154155
"regexp-named-capture-groups": RegexpNamedCaptureGroups,
156+
"regexp-set-notation": RegexpSetNotation,
155157
"regexp-sticky-and-unicode-flags": RegexpStickyAndUnicodeFlags,
156158
"regexp-unicode-property-escapes": RegexpUnicodePropertyEscapes,
157159
"rest-argument": RestArgument,
@@ -633,6 +635,7 @@ var jsTable = map[JSFeature]map[Engine][]versionRange{
633635
Opera: {{start: v{51, 0, 0}}},
634636
Safari: {{start: v{11, 1, 0}}},
635637
},
638+
RegexpSetNotation: {},
636639
RegexpStickyAndUnicodeFlags: {
637640
Chrome: {{start: v{50, 0, 0}}},
638641
Deno: {{start: v{1, 0, 0}}},

internal/js_lexer/js_lexer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2042,7 +2042,7 @@ func (lexer *Lexer) ScanRegExp() {
20422042
bits := uint32(0)
20432043
for js_ast.IsIdentifierContinue(lexer.codePoint) {
20442044
switch lexer.codePoint {
2045-
case 'd', 'g', 'i', 'm', 's', 'u', 'y':
2045+
case 'd', 'g', 'i', 'm', 's', 'u', 'v', 'y':
20462046
bit := uint32(1) << uint32(lexer.codePoint-'a')
20472047
if (bit & bits) != 0 {
20482048
// Reject duplicate flags

internal/js_parser/js_parser.go

+6
Original file line numberDiff line numberDiff line change
@@ -11643,6 +11643,12 @@ pattern:
1164311643
}
1164411644
feature = compat.RegexpMatchIndices
1164511645

11646+
case 'v':
11647+
if !p.options.unsupportedJSFeatures.Has(compat.RegexpSetNotation) {
11648+
continue // This is from a proposal: https://github.com/tc39/proposal-regexp-v-flag
11649+
}
11650+
feature = compat.RegexpSetNotation
11651+
1164611652
default:
1164711653
// Unknown flags are never supported
1164811654
}

scripts/compat-table.js

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ mergeVersions('ClassStaticField', { es2022: true })
204204
mergeVersions('TopLevelAwait', { es2022: true })
205205
mergeVersions('ArbitraryModuleNamespaceNames', { es2022: true })
206206
mergeVersions('RegexpMatchIndices', { es2022: true })
207+
mergeVersions('RegexpSetNotation', {})
207208
mergeVersions('ImportAssertions', {})
208209

209210
// Manually copied from https://caniuse.com/?search=export%20*%20as

scripts/js-api-tests.js

+4
Original file line numberDiff line numberDiff line change
@@ -5453,6 +5453,10 @@ let transformTests = {
54535453
// RegExpMatchIndices
54545454
check('es2022', `x1 = /y/d`, `x1 = /y/d;\n`),
54555455
check('es2021', `x2 = /y/d`, `x2 = new RegExp("y", "d");\n`),
5456+
5457+
// RegExpSetNotation
5458+
check('esnext', `x1 = /[\\p{White_Space}&&\\p{ASCII}]/v`, `x1 = /[\\p{White_Space}&&\\p{ASCII}]/v;\n`),
5459+
check('es2022', `x2 = /[\\p{White_Space}&&\\p{ASCII}]/v`, `x2 = new RegExp("[\\\\p{White_Space}&&\\\\p{ASCII}]", "v");\n`),
54565460
])
54575461
},
54585462

0 commit comments

Comments
 (0)