Prevent duplicate regex flags (#7617)
This commit is contained in:
parent
e80488f1d5
commit
840ba187a7
@ -20,6 +20,8 @@ import {
|
||||
} from "../util/whitespace";
|
||||
import State from "./state";
|
||||
|
||||
const VALID_REGEX_FLAGS = "gmsiyu";
|
||||
|
||||
// The following character codes are forbidden from being
|
||||
// an immediate sibling of NumericLiteralSeparator _
|
||||
|
||||
@ -834,19 +836,24 @@ export default class Tokenizer extends LocationParser {
|
||||
const content = this.input.slice(start, this.state.pos);
|
||||
++this.state.pos;
|
||||
|
||||
const validFlags = /^[gmsiyu]$/;
|
||||
let mods = "";
|
||||
|
||||
while (this.state.pos < this.input.length) {
|
||||
const char = this.input[this.state.pos];
|
||||
const charCode = this.fullCharCodeAtPos();
|
||||
if (validFlags.test(char)) {
|
||||
|
||||
if (VALID_REGEX_FLAGS.indexOf(char) > -1) {
|
||||
if (mods.indexOf(char) > -1) {
|
||||
this.raise(this.state.pos + 1, "Duplicate regular expression flag");
|
||||
}
|
||||
|
||||
++this.state.pos;
|
||||
mods += char;
|
||||
} else if (
|
||||
isIdentifierChar(charCode) ||
|
||||
charCode === charCodes.backslash
|
||||
) {
|
||||
this.raise(this.state.pos, "Invalid regular expression flag");
|
||||
this.raise(this.state.pos + 1, "Invalid regular expression flag");
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Invalid regular expression flag (1:16)"
|
||||
"throws": "Invalid regular expression flag (1:17)"
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Invalid regular expression flag (1:15)"
|
||||
"throws": "Invalid regular expression flag (1:16)"
|
||||
}
|
||||
|
||||
1
packages/babylon/test/fixtures/es2015/regex/duplicate-flags/input.js
vendored
Normal file
1
packages/babylon/test/fixtures/es2015/regex/duplicate-flags/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
/./gii;
|
||||
3
packages/babylon/test/fixtures/es2015/regex/duplicate-flags/options.json
vendored
Normal file
3
packages/babylon/test/fixtures/es2015/regex/duplicate-flags/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "Duplicate regular expression flag (1:6)"
|
||||
}
|
||||
@ -1946,8 +1946,6 @@ language/global-code/new.target-arrow.js(default)
|
||||
language/global-code/new.target-arrow.js(strict mode)
|
||||
language/import/dup-bound-names.js(default)
|
||||
language/import/dup-bound-names.js(strict mode)
|
||||
language/literals/regexp/early-err-dup-flag.js(default)
|
||||
language/literals/regexp/early-err-dup-flag.js(strict mode)
|
||||
language/literals/regexp/early-err-pattern.js(default)
|
||||
language/literals/regexp/early-err-pattern.js(strict mode)
|
||||
language/literals/regexp/invalid-braced-quantifier-exact.js(default)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user