Skip to content
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

Change default configuration of prefer-d #715

Merged
merged 2 commits into from
Mar 24, 2024
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/shaggy-lemons-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-regexp": minor
---

Change default configuration of `prefer-d` to ignore digits inside character classes.
4 changes: 2 additions & 2 deletions docs/rules/prefer-d.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ This option control how character class element equivalent to `\d` will be treat
*Note:* This option does not affect character classes equivalent to `\d`. E.g. `[\d]`, `[0-9]`, and `[0123456789]` are unaffected.
It also does not affect expression non-nested operands equivalent to `\d`. E.g. `[\d&&x]`, and `[\d--x]` are unaffected.

- `insideCharacterClass: "d"` (*default*)
- `insideCharacterClass: "d"`

Character class element equivalent to `\d` will be reported and replaced with `\d`.

Expand Down Expand Up @@ -98,7 +98,7 @@ It also does not affect expression non-nested operands equivalent to `\d`. E.g.

</eslint-code-block>

- `insideCharacterClass: "ignore"`
- `insideCharacterClass: "ignore"` (*default*)

Character class element will not be reported.

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default createRule("prefer-d", {
},
create(context) {
const insideCharacterClass: "ignore" | "range" | "d" =
context.options[0]?.insideCharacterClass ?? "d"
context.options[0]?.insideCharacterClass ?? "ignore"

function createVisitor({
node,
Expand Down
2 changes: 0 additions & 2 deletions tests/lib/eslint-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ describe("Integration with eslint-plugin-regexp", async () => {
"regexp/no-dupe-characters-character-class",
"regexp/prefer-w",
"regexp/prefer-d",
"regexp/prefer-d",
"regexp/use-ignore-case",
],
)
Expand Down Expand Up @@ -75,7 +74,6 @@ describe("Integration with eslint-plugin-regexp", async () => {
"regexp/no-dupe-characters-character-class",
"regexp/prefer-w",
"regexp/prefer-d",
"regexp/prefer-d",
"regexp/use-ignore-case",
],
)
Expand Down
3 changes: 3 additions & 0 deletions tests/lib/rules/__snapshots__/prefer-d.ts.eslintsnap
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Output:


Test: prefer-d >> invalid
Options:
- insideCharacterClass: d

Code:
1 | /[^0-9\w]/
| ^~~ [1]
Expand Down
6 changes: 5 additions & 1 deletion tests/lib/rules/prefer-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ tester.run("prefer-d", rule as any, {
valid: [
String.raw`/\d/`,
String.raw`/[1-9]/`,
"/[^0-9\\w]/",
{
code: String.raw`/[0-9a-z]/`,
options: [{ insideCharacterClass: "ignore" }],
Expand Down Expand Up @@ -40,7 +41,10 @@ tester.run("prefer-d", rule as any, {
invalid: [
"/[0-9]/",
"/[^0-9]/",
"/[^0-9\\w]/",
{
code: "/[^0-9\\w]/",
options: [{ insideCharacterClass: "d" }],
},
`
const s = "[0-9]"
new RegExp(s)
Expand Down
Loading