Skip to content

Commit 81ad9c3

Browse files
Merge branch 'master' into sort-character-class-elements
2 parents 572d35f + fd683c5 commit 81ad9c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3063
-2072
lines changed

.changeset/early-guests-hug.md

+5
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/prefer-unicode-codepoint-escapes` rule

.changeset/friendly-walls-reply.md

+5
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/no-dupe-characters-character-class` rule

.changeset/gold-baboons-clap.md

+5
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/no-useless-character-class` rule

.changeset/large-dingos-allow.md

+5
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/unicode-escape` rule

.changeset/neat-rats-sneeze.md

+5
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/no-contradiction-with-assertion`

.changeset/pre.json

+3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
"clever-jobs-clap",
1111
"curvy-shoes-kiss",
1212
"cyan-waves-notice",
13+
"early-guests-hug",
1314
"heavy-rings-reply",
1415
"hungry-spoons-mix",
1516
"khaki-colts-tell",
17+
"large-dingos-allow",
1618
"loud-flowers-search",
1719
"lovely-brooms-explode",
20+
"neat-rats-sneeze",
1821
"short-dingos-dance",
1922
"six-squids-look",
2023
"small-doors-lie",

.changeset/rich-ways-exercise.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-regexp": minor
3+
---
4+
5+
Improve `regexp/sort-alternatives` rule to add support for string alternatives and v flag

.changeset/sour-feet-explain.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-regexp": minor
3+
---
4+
5+
Add `regexp/require-unicode-sets-regexp` rule

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/dist
44
/docs/.vitepress/dist
55
/docs/.vitepress/build-system/shim/eslint.mjs
6+
/docs/.vitepress/cache
67
/node_modules
78
/assets
89
!/docs/.vitepress

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,4 @@ dist
105105

106106
## Repo
107107
docs/.vitepress/build-system/shim/eslint.mjs
108+
docs/.vitepress/cache

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# eslint-plugin-regexp
22

3+
## 2.0.0-next.6
4+
5+
### Minor Changes
6+
7+
- Add support for v flag to `regexp/prefer-unicode-codepoint-escapes` rule ([#592](https://github.com/ota-meshi/eslint-plugin-regexp/pull/592))
8+
9+
- Add support for v flag to `regexp/unicode-escape` rule ([#592](https://github.com/ota-meshi/eslint-plugin-regexp/pull/592))
10+
11+
### Patch Changes
12+
13+
- Add support for `v` flag to `regexp/no-contradiction-with-assertion` ([#606](https://github.com/ota-meshi/eslint-plugin-regexp/pull/606))
14+
315
## 2.0.0-next.5
416

517
### Minor Changes

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ The `plugin:regexp/all` config enables all rules. It's meant for testing, not fo
166166
| [prefer-regexp-exec](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-regexp-exec.html) | enforce that `RegExp#exec` is used instead of `String#match` if no global flag is provided | | | | |
167167
| [prefer-regexp-test](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-regexp-test.html) | enforce that `RegExp#test` is used instead of `String#match` and `RegExp#exec` | | | 🔧 | |
168168
| [require-unicode-regexp](https://ota-meshi.github.io/eslint-plugin-regexp/rules/require-unicode-regexp.html) | enforce the use of the `u` flag | | | 🔧 | |
169+
| [require-unicode-sets-regexp](https://ota-meshi.github.io/eslint-plugin-regexp/rules/require-unicode-sets-regexp.html) | enforce the use of the `v` flag | | | 🔧 | |
169170
| [sort-alternatives](https://ota-meshi.github.io/eslint-plugin-regexp/rules/sort-alternatives.html) | sort alternatives if order doesn't matter | | | 🔧 | |
170171
| [use-ignore-case](https://ota-meshi.github.io/eslint-plugin-regexp/rules/use-ignore-case.html) | use the `i` flag if it simplifies the pattern || | 🔧 | |
171172

docs/.vitepress/config.ts renamed to docs/.vitepress/config.mts

+13-12
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@ import { defineConfig } from "vitepress"
33
import { BUNDLED_LANGUAGES } from "shiki"
44
import path from "path"
55
import { fileURLToPath } from "url"
6-
import { rules } from "../../lib/utils/rules"
7-
import type { RuleModule } from "../../lib/types"
6+
import { rules } from "../../lib/utils/rules.js"
7+
import type { RuleModule } from "../../lib/types.js"
88

99
// Pre-build cjs packages that cannot be bundled well.
1010
import "./build-system/build"
1111

12-
const dirname = path.dirname(
13-
fileURLToPath(
14-
// @ts-expect-error -- Cannot change `module` option
15-
import.meta.url,
16-
),
17-
)
12+
const dirname = path.dirname(fileURLToPath(import.meta.url))
1813

1914
// Include `json5` as alias for jsonc
2015
const jsonc = BUNDLED_LANGUAGES.find((lang) => lang.id === "jsonc")
@@ -67,6 +62,12 @@ export default defineConfig({
6762

6863
lastUpdated: true,
6964
themeConfig: {
65+
search: {
66+
provider: "local",
67+
options: {
68+
detailedView: true,
69+
},
70+
},
7071
editLink: {
7172
pattern:
7273
"https://github.com/ota-meshi/eslint-plugin-regexp/edit/master/docs/:path",
@@ -92,17 +93,17 @@ export default defineConfig({
9293
},
9394
{
9495
text: "Possible Errors",
95-
collapsible: false,
96+
collapsed: false,
9697
items: categories["Possible Errors"].map(ruleToSidebarItem),
9798
},
9899
{
99100
text: "Best Practices",
100-
collapsible: false,
101+
collapsed: false,
101102
items: categories["Best Practices"].map(ruleToSidebarItem),
102103
},
103104
{
104105
text: "Stylistic Issues",
105-
collapsible: false,
106+
collapsed: false,
106107
items: categories["Stylistic Issues"].map(
107108
ruleToSidebarItem,
108109
),
@@ -111,7 +112,7 @@ export default defineConfig({
111112
? [
112113
{
113114
text: "Deprecated",
114-
collapsible: false,
115+
collapsed: false,
115116
items: categories.deprecated.map(
116117
ruleToSidebarItem,
117118
),

docs/rules/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ sidebarDepth: 0
7373
| [prefer-regexp-exec](prefer-regexp-exec.md) | enforce that `RegExp#exec` is used instead of `String#match` if no global flag is provided | | | | |
7474
| [prefer-regexp-test](prefer-regexp-test.md) | enforce that `RegExp#test` is used instead of `String#match` and `RegExp#exec` | | | 🔧 | |
7575
| [require-unicode-regexp](require-unicode-regexp.md) | enforce the use of the `u` flag | | | 🔧 | |
76+
| [require-unicode-sets-regexp](require-unicode-sets-regexp.md) | enforce the use of the `v` flag | | | 🔧 | |
7677
| [sort-alternatives](sort-alternatives.md) | sort alternatives if order doesn't matter | | | 🔧 | |
7778
| [use-ignore-case](use-ignore-case.md) | use the `i` flag if it simplifies the pattern || | 🔧 | |
7879

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
pageClass: "rule-details"
3+
sidebarDepth: 0
4+
title: "regexp/require-unicode-sets-regexp"
5+
description: "enforce the use of the `v` flag"
6+
---
7+
# regexp/require-unicode-sets-regexp
8+
9+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
10+
11+
<!-- end auto-generated rule header -->
12+
13+
> enforce the use of the `v` flag
14+
15+
## :book: Rule Details
16+
17+
This rule reports regular expressions without the `v` flag.
18+
19+
It will automatically replace the `v` flag to regular expressions where it is already uses the 'u' flag and statically guaranteed to be safe to do so. In all other cases, the developer has to check that adding the `v` flag doesn't cause the regex to behave incorrectly.
20+
21+
If you want to automatically add the `v` flag to legacy regular expressions that don't use the `u` flag, use them together with the [regexp/require-unicode-regexp] rule.
22+
23+
<eslint-code-block fix>
24+
25+
```js
26+
/* eslint regexp/require-unicode-sets-regexp: "error" */
27+
28+
/* ✓ GOOD */
29+
var foo = /foo/v;
30+
var foo = /a\s+b/v;
31+
32+
/* ✗ BAD */
33+
var foo = /foo/;
34+
var foo = RegExp("a\\s+b");
35+
var foo = /[a-z]/i;
36+
var foo = /\S/;
37+
var foo = /foo/u;
38+
var foo = RegExp("a\\s+b", 'u');
39+
var foo = /[a-z]/iu;
40+
var foo = /\S/u;
41+
```
42+
43+
</eslint-code-block>
44+
45+
## :wrench: Options
46+
47+
Nothing.
48+
49+
## :couple: Related rules
50+
51+
- [regexp/require-unicode-regexp]
52+
53+
[regexp/require-unicode-regexp]: ./require-unicode-regexp.md
54+
55+
## :books: Further reading
56+
57+
- [require-unicode-regexp]
58+
59+
[require-unicode-regexp]: https://eslint.org/docs/rules/require-unicode-regexp
60+
61+
## :rocket: Version
62+
63+
:exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
64+
65+
## :mag: Implementation
66+
67+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/lib/rules/require-unicode-sets-regexp.ts)
68+
- [Test source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/tests/lib/rules/require-unicode-sets-regexp.ts)

docs/rules/sort-alternatives.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ This rule will sort alternatives to improve readability and maintainability.
1919

2020
The primary target of this rule are lists of words and/or numbers. These lists are somewhat common, and sorting them makes it easy for readers to check whether a particular word or number is included.
2121

22-
This rule will only sort alternatives if reordering the alternatives doesn't affect the pattern.
22+
This rule will only sort alternatives if reordering the alternatives doesn't affect the pattern.\
23+
However, character classes containing strings are ensured to match the longest string, so they can always be sorted.
2324

2425
<eslint-code-block fix>
2526

@@ -29,11 +30,13 @@ This rule will only sort alternatives if reordering the alternatives doesn't aff
2930
/* ✓ GOOD */
3031
var foo = /\b(1|2|3)\b/;
3132
var foo = /\b(alpha|beta|gamma)\b/;
33+
var foo = /[\q{blue|green|red}]/v;
3234

3335
/* ✗ BAD */
3436
var foo = /\b(2|1|3)\b/;
3537
var foo = /__(?:Foo|Bar)__/;
3638
var foo = /\((?:TM|R|C)\)/;
39+
var foo = /[\q{red|green|blue}]/v;
3740
```
3841

3942
</eslint-code-block>

lib/rules/no-contradiction-with-assertion.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ function* getNextElements(
8989

9090
if (
9191
parent.type === "CharacterClass" ||
92-
parent.type === "CharacterClassRange"
92+
parent.type === "CharacterClassRange" ||
93+
parent.type === "ExpressionCharacterClass" ||
94+
parent.type === "ClassIntersection" ||
95+
parent.type === "ClassSubtraction" ||
96+
parent.type === "StringAlternative"
9397
) {
9498
return
9599
}
@@ -103,10 +107,8 @@ function* getNextElements(
103107
}
104108
}
105109

106-
// FIXME: TS Error
107-
// @ts-expect-error -- FIXME
108110
const elements = parent.elements
109-
const index = elements.indexOf(element) as number
111+
const index = elements.indexOf(element)
110112
const inc = dir === "ltr" ? 1 : -1
111113
for (let i = index + inc; i >= 0 && i < elements.length; i += inc) {
112114
const e = elements[i]

0 commit comments

Comments
 (0)