Skip to content

Commit eecb6e3

Browse files
authored
Fix false positives for character classes with no matching characters in regexp/match-any rule. (#88)
1 parent bcab99d commit eecb6e3

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

lib/rules/match-any.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ export default createRule("match-any", {
160160
}
161161
return
162162
}
163-
if (characterClassData && !characterClassData.reported) {
163+
if (
164+
characterClassData &&
165+
!characterClassData.reported &&
166+
!characterClassData.node.negate
167+
) {
164168
const key = getCharacterSetKey(csNode)
165169
const alreadyCharSet = characterClassData.charSets.get(
166170
key,
@@ -213,7 +217,8 @@ export default createRule("match-any", {
213217
) {
214218
if (
215219
characterClassData &&
216-
!characterClassData.reported
220+
!characterClassData.reported &&
221+
!characterClassData.node.negate
217222
) {
218223
const ccNode = characterClassData.node
219224
context.report({

tests/lib/rules/match-any.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ tester.run("match-any", rule as any, {
2828
code: "/[\\s\\S][\\S\\s][^]./s",
2929
options: [{ allows: ["[\\s\\S]", "[\\S\\s]", "[^]", "dotAll"] }],
3030
},
31+
"/[^\\S\\s]/",
32+
{
33+
code: "/[^\\s\\S]/",
34+
options: [{ allows: ["[^]"] }],
35+
},
36+
"/[^\\d\\D]/",
37+
"/[^\\D\\d]/",
38+
"/[^\\w\\W]/",
39+
"/[^\\W\\w]/",
40+
"/[^\\0-\\uFFFF]/",
41+
"/[^\\p{ASCII}\\P{ASCII}]/u",
42+
"/[^\\P{ASCII}\\p{ASCII}]/u",
43+
"/[^\\s\\S\\0-\\uFFFF]/",
3144
],
3245
invalid: [
3346
{
@@ -134,11 +147,6 @@ tester.run("match-any", rule as any, {
134147
'Unexpected using "." to match any character.',
135148
],
136149
},
137-
{
138-
code: "/[^\\s\\S]/",
139-
output: "/[\\s\\S]/",
140-
errors: ['Unexpected using "[^\\s\\S]" to match any character.'],
141-
},
142150
{
143151
code: "/[\\p{ASCII}\\P{ASCII}]/u",
144152
output: "/[\\s\\S]/u",

0 commit comments

Comments
 (0)