diff --git a/src/regexp.js b/src/regexp.js index 5e8143451..f2f57c16c 100644 --- a/src/regexp.js +++ b/src/regexp.js @@ -96,15 +96,16 @@ export class RegExpValidationState { this.parser.raise(this.start, `Invalid regular expression: /${this.source}/: ${message}`) } - // Node.js 0.12/0.10 don't support String.prototype.codePointAt(). - codePointAt(i) { + // If u flag is given, this returns the code point at the index (it combines a surrogate pair). + // Otherwise, this returns the code unit of the index (can be a part of a surrogate pair). + at(i) { const s = this.source const l = s.length if (i >= l) { return -1 } const c = s.charCodeAt(i) - if (c <= 0xD7FF || c >= 0xE000 || i + 1 >= l) { + if (!this.switchU || c <= 0xD7FF || c >= 0xE000 || i + 1 >= l) { return c } return (c << 10) + s.charCodeAt(i + 1) - 0x35FDC00 @@ -117,18 +118,18 @@ export class RegExpValidationState { return l } const c = s.charCodeAt(i) - if (c <= 0xD7FF || c >= 0xE000 || i + 1 >= l) { + if (!this.switchU || c <= 0xD7FF || c >= 0xE000 || i + 1 >= l) { return i + 1 } return i + 2 } current() { - return this.codePointAt(this.pos) + return this.at(this.pos) } lookahead() { - return this.codePointAt(this.nextIndex(this.pos)) + return this.at(this.nextIndex(this.pos)) } advance() { diff --git a/test/tests-regexp.js b/test/tests-regexp.js index 6b9b2656d..8601bcb3b 100644 --- a/test/tests-regexp.js +++ b/test/tests-regexp.js @@ -943,6 +943,11 @@ testFail("/[🌷-🌸]/", "Invalid regular expression: /[🌷-🌸]/: Range out testFail("/[🌷-🌸]/", "Invalid regular expression: /[🌷-🌸]/: Range out of order in character class (1:1)", { ecmaVersion: 2015 }) testFail("/[🌷-🌸]/u", "Invalid regular expression flag (1:1)", { ecmaVersion: 5 }) test("/[🌷-🌸]/u", {}, { ecmaVersion: 2015 }) +testFail("/[\\u0000-🌸-\\u0000]/", "Invalid regular expression: /[\\u0000-🌸-\\u0000]/: Range out of order in character class (1:1)", { ecmaVersion: 2015 }) +testFail("/[\\u0000-\\ud83c\\udf38-\\u0000]/", "Invalid regular expression: /[\\u0000-\\ud83c\\udf38-\\u0000]/: Range out of order in character class (1:1)", { ecmaVersion: 2015 }) +test("/[\\u0000-🌸-\\u0000]/u", {}, { ecmaVersion: 2015 }) +test("/[\\u0000-\\u{1f338}-\\u0000]/u", {}, { ecmaVersion: 2015 }) +test("/[\\u0000-\\ud83c\\udf38-\\u0000]/u", {}, { ecmaVersion: 2015 }) testFail("/[🌸-🌷]/", "Invalid regular expression: /[🌸-🌷]/: Range out of order in character class (1:1)", { ecmaVersion: 5 }) testFail("/[🌸-🌷]/", "Invalid regular expression: /[🌸-🌷]/: Range out of order in character class (1:1)", { ecmaVersion: 2015 }) testFail("/[🌸-🌷]/u", "Invalid regular expression flag (1:1)", { ecmaVersion: 5 })