Skip to content

Commit fb15338

Browse files
Add support for v flag to regexp/match-any (#628)
* Add support for `v` flag to `regexp/match-any` * Create lemon-goats-look.md
1 parent fc58310 commit fb15338

File tree

3 files changed

+54
-22
lines changed

3 files changed

+54
-22
lines changed

.changeset/lemon-goats-look.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-regexp": minor
3+
---
4+
5+
Add support for `v` flag to `regexp/match-any`

lib/rules/match-any.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
22
import type { Rule } from "eslint"
33
import type {
44
CharacterClass,
5-
Node as RegExpNode,
5+
ExpressionCharacterClass,
6+
Node,
67
} from "@eslint-community/regexpp/ast"
78
import type { RegExpContext } from "../utils"
89
import { createRule, defineRegexpVisitor } from "../utils"
910
import { isRegexpLiteral } from "../utils/ast-utils/utils"
10-
import { matchesAllCharacters } from "regexp-ast-analysis"
11+
import { matchesAllCharacters, hasStrings } from "regexp-ast-analysis"
1112
import { mention } from "../utils/mention"
1213

1314
const OPTION_SS1 = "[\\s\\S]" as const
@@ -72,7 +73,7 @@ export default createRule("match-any", {
7273
function fix(
7374
fixer: Rule.RuleFixer,
7475
{ node, flags, patternSource }: RegExpContext,
75-
regexpNode: RegExpNode,
76+
regexpNode: Node,
7677
): null | Rule.Fix | Rule.Fix[] {
7778
if (!preference) {
7879
return null
@@ -134,6 +135,28 @@ export default createRule("match-any", {
134135
): RegExpVisitor.Handlers {
135136
const { node, flags, getRegexpLocation } = regexpContext
136137

138+
function onClass(
139+
ccNode: CharacterClass | ExpressionCharacterClass,
140+
) {
141+
if (
142+
matchesAllCharacters(ccNode, flags) &&
143+
!hasStrings(ccNode, flags) &&
144+
!allows.has(ccNode.raw as never)
145+
) {
146+
context.report({
147+
node,
148+
loc: getRegexpLocation(ccNode),
149+
messageId: "unexpected",
150+
data: {
151+
expr: mention(ccNode),
152+
},
153+
fix(fixer) {
154+
return fix(fixer, regexpContext, ccNode)
155+
},
156+
})
157+
}
158+
}
159+
137160
return {
138161
onCharacterSetEnter(csNode) {
139162
if (
@@ -154,24 +177,8 @@ export default createRule("match-any", {
154177
})
155178
}
156179
},
157-
onCharacterClassEnter(ccNode: CharacterClass) {
158-
if (
159-
matchesAllCharacters(ccNode, flags) &&
160-
!allows.has(ccNode.raw as never)
161-
) {
162-
context.report({
163-
node,
164-
loc: getRegexpLocation(ccNode),
165-
messageId: "unexpected",
166-
data: {
167-
expr: mention(ccNode),
168-
},
169-
fix(fixer) {
170-
return fix(fixer, regexpContext, ccNode)
171-
},
172-
})
173-
}
174-
},
180+
onCharacterClassEnter: onClass,
181+
onExpressionCharacterClassEnter: onClass,
175182
}
176183
}
177184

tests/lib/rules/match-any.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import rule from "../../../lib/rules/match-any"
33

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
@@ -41,6 +41,7 @@ tester.run("match-any", rule as any, {
4141
"/[^\\p{ASCII}\\P{ASCII}]/u",
4242
"/[^\\P{ASCII}\\p{ASCII}]/u",
4343
"/[^\\s\\S\\0-\\uFFFF]/",
44+
String.raw`/[\S\s\q{abc}]/v`,
4445
],
4546
invalid: [
4647
{
@@ -55,6 +56,25 @@ tester.run("match-any", rule as any, {
5556
},
5657
],
5758
},
59+
{
60+
code: String.raw`/[\S\s]/v`,
61+
output: String.raw`/[\s\S]/v`,
62+
errors: ["Unexpected using '[\\S\\s]' to match any character."],
63+
},
64+
{
65+
code: String.raw`/[\S\s\q{a|b|c}]/v`,
66+
output: String.raw`/[\s\S]/v`,
67+
errors: [
68+
"Unexpected using '[\\S\\s\\q{a|b|c}]' to match any character.",
69+
],
70+
},
71+
{
72+
code: String.raw`/[[\S\s\q{abc}]--\q{abc}]/v`,
73+
output: String.raw`/[\s\S]/v`,
74+
errors: [
75+
"Unexpected using '[[\\S\\s\\q{abc}]--\\q{abc}]' to match any character.",
76+
],
77+
},
5878
{
5979
code: "/[^]/",
6080
output: "/[\\s\\S]/",

0 commit comments

Comments
 (0)