Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking: support new regex d flag (fixes #14640) #14653

Merged
merged 5 commits into from Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 8 additions & 17 deletions lib/rules/no-empty-character-class.js
Expand Up @@ -12,16 +12,13 @@
/*
* plain-English description of the following regexp:
* 0. `^` fix the match at the beginning of the string
* 1. `\/`: the `/` that begins the regexp
* 2. `([^\\[]|\\.|\[([^\\\]]|\\.)+\])*`: regexp contents; 0 or more of the following
* 2.0. `[^\\[]`: any character that's not a `\` or a `[` (anything but escape sequences and character classes)
* 2.1. `\\.`: an escape sequence
* 2.2. `\[([^\\\]]|\\.)+\]`: a character class that isn't empty
* 3. `\/` the `/` that ends the regexp
* 4. `[gimuy]*`: optional regexp flags
* 5. `$`: fix the match at the end of the string
* 1. `([^\\[]|\\.|\[([^\\\]]|\\.)+\])*`: regexp contents; 0 or more of the following
* 1.0. `[^\\[]`: any character that's not a `\` or a `[` (anything but escape sequences and character classes)
* 1.1. `\\.`: an escape sequence
* 1.2. `\[([^\\\]]|\\.)+\]`: a character class that isn't empty
* 2. `$`: fix the match at the end of the string
*/
const regex = /^\/([^\\[]|\\.|\[([^\\\]]|\\.)+\])*\/[gimuys]*$/u;
const regex = /^([^\\[]|\\.|\[([^\\\]]|\\.)+\])*$/u;

//------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -46,18 +43,12 @@ module.exports = {
},

create(context) {
const sourceCode = context.getSourceCode();

return {

Literal(node) {
const token = sourceCode.getFirstToken(node);

if (token.type === "RegularExpression" && !regex.test(token.value)) {
"Literal[regex]"(node) {
if (!regex.test(node.regex.pattern)) {
context.report({ node, messageId: "unexpected" });
}
}

};

}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-invalid-regexp.js
Expand Up @@ -10,7 +10,7 @@

const RegExpValidator = require("regexpp").RegExpValidator;
const validator = new RegExpValidator();
const validFlags = /[gimuys]/gu;
const validFlags = /[dgimsuy]/gu;
const undefined1 = void 0;

//------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -56,7 +56,7 @@
"eslint-scope": "^5.1.1",
"eslint-utils": "^2.1.0",
"eslint-visitor-keys": "^2.0.0",
"espree": "^7.3.1",
"espree": "^8.0.0",
"esquery": "^1.4.0",
"esutils": "^2.0.2",
"fast-deep-equal": "^3.1.3",
Expand All @@ -76,7 +76,7 @@
"natural-compare": "^1.4.0",
"optionator": "^0.9.1",
"progress": "^2.0.0",
"regexpp": "^3.1.0",
"regexpp": "^3.2.0",
"semver": "^7.2.1",
"strip-ansi": "^6.0.0",
"strip-json-comments": "^3.1.0",
Expand Down
4 changes: 3 additions & 1 deletion tests/lib/rules/no-empty-character-class.js
Expand Up @@ -32,6 +32,7 @@ ruleTester.run("no-empty-character-class", rule, {
"var foo = /\\s*:\\s*/gim;",
{ code: "var foo = /[\\]]/uy;", parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = /[\\]]/s;", parserOptions: { ecmaVersion: 2018 } },
{ code: "var foo = /[\\]]/d;", parserOptions: { ecmaVersion: 2022 } },
"var foo = /\\[]/"
],
invalid: [
Expand All @@ -41,6 +42,7 @@ ruleTester.run("no-empty-character-class", rule, {
{ code: "if (/^abc[]/.test(foo)) {}", errors: [{ messageId: "unexpected", type: "Literal" }] },
{ code: "var foo = /[]]/;", errors: [{ messageId: "unexpected", type: "Literal" }] },
{ code: "var foo = /\\[[]/;", errors: [{ messageId: "unexpected", type: "Literal" }] },
{ code: "var foo = /\\[\\[\\]a-z[]/;", errors: [{ messageId: "unexpected", type: "Literal" }] }
{ code: "var foo = /\\[\\[\\]a-z[]/;", errors: [{ messageId: "unexpected", type: "Literal" }] },
{ code: "var foo = /[]]/d;", parserOptions: { ecmaVersion: 2022 }, errors: [{ messageId: "unexpected", type: "Literal" }] }
]
});
3 changes: 3 additions & 0 deletions tests/lib/rules/no-invalid-regexp.js
Expand Up @@ -59,6 +59,9 @@ ruleTester.run("no-invalid-regexp", rule, {
"new RegExp('(?<𝒜>.)', 'g');",
"new RegExp('\\\\p{Script=Nandinagari}', 'u');",

// ES2022
"new RegExp('a+(?<Z>z)?', 'd')",

// allowConstructorFlags
{
code: "new RegExp('.', 'g')",
Expand Down