Skip to content

Add support for v flag to regexp/match-any #628

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

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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