Skip to content

Commit

Permalink
Add support for v flag to regexp/match-any (#628)
Browse files Browse the repository at this point in the history
* Add support for `v` flag to `regexp/match-any`

* Create lemon-goats-look.md
  • Loading branch information
RunDevelopment authored Oct 1, 2023
1 parent fc58310 commit fb15338
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-goats-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-regexp": minor
---

Add support for `v` flag to `regexp/match-any`
49 changes: 28 additions & 21 deletions lib/rules/match-any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand All @@ -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,
}
}

Expand Down
22 changes: 21 additions & 1 deletion tests/lib/rules/match-any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/match-any"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})
Expand Down Expand Up @@ -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: [
{
Expand All @@ -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]/",
Expand Down

0 comments on commit fb15338

Please sign in to comment.