From a25d396ebeb7db0d878ba8dc06fb42b0682d5ab1 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sun, 1 Oct 2023 16:01:09 +0200 Subject: [PATCH 1/2] Add support for `v` flag to `regexp/match-any` --- lib/rules/match-any.ts | 49 ++++++++++++++++++++---------------- tests/lib/rules/match-any.ts | 22 +++++++++++++++- 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/lib/rules/match-any.ts b/lib/rules/match-any.ts index 6e694bdd1..6b61554e2 100644 --- a/lib/rules/match-any.ts +++ b/lib/rules/match-any.ts @@ -2,12 +2,13 @@ import type { RegExpVisitor } from "@eslint-community/regexpp/visitor" import type { Rule } from "eslint" import type { CharacterClass, - Node as RegExpNode, + ExpressionCharacterClass, + Node, } from "@eslint-community/regexpp/ast" import type { RegExpContext } from "../utils" import { createRule, defineRegexpVisitor } from "../utils" import { isRegexpLiteral } from "../utils/ast-utils/utils" -import { matchesAllCharacters } from "regexp-ast-analysis" +import { matchesAllCharacters, hasStrings } from "regexp-ast-analysis" import { mention } from "../utils/mention" const OPTION_SS1 = "[\\s\\S]" as const @@ -72,7 +73,7 @@ export default createRule("match-any", { function fix( fixer: Rule.RuleFixer, { node, flags, patternSource }: RegExpContext, - regexpNode: RegExpNode, + regexpNode: Node, ): null | Rule.Fix | Rule.Fix[] { if (!preference) { return null @@ -134,6 +135,28 @@ export default createRule("match-any", { ): RegExpVisitor.Handlers { const { node, flags, getRegexpLocation } = regexpContext + function onClass( + ccNode: CharacterClass | ExpressionCharacterClass, + ) { + if ( + matchesAllCharacters(ccNode, flags) && + !hasStrings(ccNode, flags) && + !allows.has(ccNode.raw as never) + ) { + context.report({ + node, + loc: getRegexpLocation(ccNode), + messageId: "unexpected", + data: { + expr: mention(ccNode), + }, + fix(fixer) { + return fix(fixer, regexpContext, ccNode) + }, + }) + } + } + return { onCharacterSetEnter(csNode) { if ( @@ -154,24 +177,8 @@ export default createRule("match-any", { }) } }, - onCharacterClassEnter(ccNode: CharacterClass) { - if ( - matchesAllCharacters(ccNode, flags) && - !allows.has(ccNode.raw as never) - ) { - context.report({ - node, - loc: getRegexpLocation(ccNode), - messageId: "unexpected", - data: { - expr: mention(ccNode), - }, - fix(fixer) { - return fix(fixer, regexpContext, ccNode) - }, - }) - } - }, + onCharacterClassEnter: onClass, + onExpressionCharacterClassEnter: onClass, } } diff --git a/tests/lib/rules/match-any.ts b/tests/lib/rules/match-any.ts index c2f103a3f..bd8c84a83 100644 --- a/tests/lib/rules/match-any.ts +++ b/tests/lib/rules/match-any.ts @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/match-any" const tester = new RuleTester({ parserOptions: { - ecmaVersion: 2020, + ecmaVersion: "latest", sourceType: "module", }, }) @@ -41,6 +41,7 @@ tester.run("match-any", rule as any, { "/[^\\p{ASCII}\\P{ASCII}]/u", "/[^\\P{ASCII}\\p{ASCII}]/u", "/[^\\s\\S\\0-\\uFFFF]/", + String.raw`/[\S\s\q{abc}]/v`, ], invalid: [ { @@ -55,6 +56,25 @@ tester.run("match-any", rule as any, { }, ], }, + { + code: String.raw`/[\S\s]/v`, + output: String.raw`/[\s\S]/v`, + errors: ["Unexpected using '[\\S\\s]' to match any character."], + }, + { + code: String.raw`/[\S\s\q{a|b|c}]/v`, + output: String.raw`/[\s\S]/v`, + errors: [ + "Unexpected using '[\\S\\s\\q{a|b|c}]' to match any character.", + ], + }, + { + code: String.raw`/[[\S\s\q{abc}]--\q{abc}]/v`, + output: String.raw`/[\s\S]/v`, + errors: [ + "Unexpected using '[[\\S\\s\\q{abc}]--\\q{abc}]' to match any character.", + ], + }, { code: "/[^]/", output: "/[\\s\\S]/", From f1f4010428794c2578f38c0fd0ec790e595d2898 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Sun, 1 Oct 2023 16:02:50 +0200 Subject: [PATCH 2/2] Create lemon-goats-look.md --- .changeset/lemon-goats-look.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/lemon-goats-look.md diff --git a/.changeset/lemon-goats-look.md b/.changeset/lemon-goats-look.md new file mode 100644 index 000000000..793939e44 --- /dev/null +++ b/.changeset/lemon-goats-look.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-regexp": minor +--- + +Add support for `v` flag to `regexp/match-any`