Skip to content

Commit

Permalink
Prevent duplicate regex flags (#7617)
Browse files Browse the repository at this point in the history
  • Loading branch information
existentialism authored Mar 22, 2018
1 parent e80488f commit 840ba18
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 7 deletions.
13 changes: 10 additions & 3 deletions packages/babylon/src/tokenizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 _

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Invalid regular expression flag (1:16)"
"throws": "Invalid regular expression flag (1:17)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Invalid regular expression flag (1:15)"
"throws": "Invalid regular expression flag (1:16)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/./gii;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Duplicate regular expression flag (1:6)"
}
2 changes: 0 additions & 2 deletions scripts/tests/test262/test262_whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 840ba18

Please sign in to comment.