Skip to content

Commit e9cb2e9

Browse files
committed
WIP: Add no-regexp-unicode-property-escapes-2022 rule
1 parent 044a753 commit e9cb2e9

File tree

6 files changed

+185
-0
lines changed

6 files changed

+185
-0
lines changed

docs/rules/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ There are multiple configs that enable all rules in this category: `plugin:es-x/
2727
| [es-x/no-object-hasown](./no-object-hasown.md) | disallow the `Object.hasOwn` method. | |
2828
| [es-x/no-private-in](./no-private-in.md) | disallow `#x in obj`. | |
2929
| [es-x/no-regexp-d-flag](./no-regexp-d-flag.md) | disallow RegExp `d` flag. | |
30+
| [es-x/no-regexp-unicode-property-escapes-2022](./no-regexp-unicode-property-escapes-2022.md) | disallow the new values of RegExp Unicode property escape sequences in ES2022. | |
3031
| [es-x/no-top-level-await](./no-top-level-await.md) | disallow top-level `await`. | |
3132

3233
## ES2021
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: "es-x/no-regexp-unicode-property-escapes-2022"
3+
description: "disallow the new values of RegExp Unicode property escape sequences in ES2022"
4+
---
5+
6+
# es-x/no-regexp-unicode-property-escapes-2022
7+
> disallow the new values of RegExp Unicode property escape sequences in ES2022
8+
9+
- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
10+
- ✅ The following configurations enable this rule: `plugin:es-x/no-new-in-es2022`, `plugin:es-x/restrict-to-es3`, `plugin:es-x/restrict-to-es5`, `plugin:es-x/restrict-to-es2015`, `plugin:es-x/restrict-to-es2016`, `plugin:es-x/restrict-to-es2017`, `plugin:es-x/restrict-to-es2018`, `plugin:es-x/restrict-to-es2019`, `plugin:es-x/restrict-to-es2020`, and `plugin:es-x/restrict-to-es2021`
11+
12+
This rule reports the new values of ES2018 [RegExp Unicode property escape sequences](https://github.com/tc39/proposal-regexp-unicode-property-escapes#readme) which were added in ES2022.
13+
14+
For example, the following patterns are valid in ES2022, but syntax error in ES2020 environments:
15+
16+
- `\p{Script=Cpmn}`
17+
- `\p{Script=Cypro_Minoan}`
18+
- `\p{Script=Old_Uyghur}`
19+
- `\p{Script=Ougr}`
20+
- `\p{Script=Tangsa}`
21+
- `\p{Script=Tnsa}`
22+
- `\p{Script=Toto}`
23+
- `\p{Script=Vith}`
24+
- `\p{Script=Vithkuqi}`
25+
26+
## 💡 Examples
27+
28+
⛔ Examples of **incorrect** code for this rule:
29+
30+
<eslint-playground type="bad">
31+
32+
```js
33+
/*eslint es-x/no-regexp-unicode-property-escapes-2022: error */
34+
const r1 = /\p{Script=Cpmn}/u
35+
const r2 = /\p{Script=Cypro_Minoan}/u
36+
```
37+
38+
</eslint-playground>
39+
40+
## 📚 References
41+
42+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-es-x/blob/master/lib/rules/no-regexp-unicode-property-escapes-2022.js)
43+
- [Test source](https://github.com/ota-meshi/eslint-plugin-es-x/blob/master/tests/lib/rules/no-regexp-unicode-property-escapes-2022.js)

lib/configs/no-new-in-es2022.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = {
1515
"es-x/no-object-hasown": "error",
1616
"es-x/no-private-in": "error",
1717
"es-x/no-regexp-d-flag": "error",
18+
"es-x/no-regexp-unicode-property-escapes-2022": "error",
1819
"es-x/no-top-level-await": "error",
1920
},
2021
}

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ module.exports = {
191191
"no-regexp-unicode-property-escapes-2019": require("./rules/no-regexp-unicode-property-escapes-2019"),
192192
"no-regexp-unicode-property-escapes-2020": require("./rules/no-regexp-unicode-property-escapes-2020"),
193193
"no-regexp-unicode-property-escapes-2021": require("./rules/no-regexp-unicode-property-escapes-2021"),
194+
"no-regexp-unicode-property-escapes-2022": require("./rules/no-regexp-unicode-property-escapes-2022"),
194195
"no-regexp-y-flag": require("./rules/no-regexp-y-flag"),
195196
"no-rest-parameters": require("./rules/no-rest-parameters"),
196197
"no-rest-spread-properties": require("./rules/no-rest-spread-properties"),
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @author Yosuke Ota
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
"use strict"
6+
7+
const { defineRegExpHandler } = require("../util/define-regexp-handler")
8+
const {
9+
scNameSet,
10+
scValueSets,
11+
binPropertySets,
12+
} = require("../util/unicode-properties")
13+
14+
function isNewUnicodePropertyKeyValuePair(key, value) {
15+
return scNameSet.has(key) && scValueSets.es2022.has(value)
16+
}
17+
18+
function isNewBinaryUnicodeProperty(key) {
19+
return binPropertySets.es2022.has(key)
20+
}
21+
22+
module.exports = {
23+
meta: {
24+
docs: {
25+
description:
26+
"disallow the new values of RegExp Unicode property escape sequences in ES2022",
27+
category: "ES2022",
28+
recommended: false,
29+
url: "http://ota-meshi.github.io/eslint-plugin-es-x/rules/no-regexp-unicode-property-escapes-2022.html",
30+
},
31+
fixable: null,
32+
messages: {
33+
forbidden: "ES2022 '{{value}}' is forbidden.",
34+
},
35+
schema: [],
36+
type: "problem",
37+
},
38+
create(context) {
39+
return defineRegExpHandler(context, (node, { pattern }) => {
40+
let foundValue = ""
41+
return {
42+
onUnicodePropertyCharacterSet(start, end, _kind, key, value) {
43+
if (foundValue) {
44+
return
45+
}
46+
if (
47+
value
48+
? isNewUnicodePropertyKeyValuePair(key, value)
49+
: isNewBinaryUnicodeProperty(key)
50+
) {
51+
foundValue = pattern.slice(start, end)
52+
}
53+
},
54+
onExit() {
55+
if (foundValue) {
56+
context.report({
57+
node,
58+
messageId: "forbidden",
59+
data: { value: foundValue },
60+
})
61+
}
62+
},
63+
}
64+
})
65+
},
66+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @author Yosuke Ota
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
"use strict"
6+
7+
const RuleTester = require("../../tester")
8+
const rule = require("../../../lib/rules/no-regexp-unicode-property-escapes-2022.js")
9+
10+
if (!RuleTester.isSupported(2022)) {
11+
//eslint-disable-next-line no-console
12+
console.log("Skip the tests of no-regexp-unicode-property-escapes-2022.")
13+
return
14+
}
15+
16+
new RuleTester().run("no-regexp-unicode-property-escapes-2022", rule, {
17+
valid: [
18+
String.raw`/\p{Script=Cpmn}/u`,
19+
String.raw`/\p{Letter}/u`,
20+
String.raw`/\P{Letter}/u`,
21+
String.raw`/\p{Script=Hiragana}/u`,
22+
String.raw`/\P{Script=Hiragana}/u`,
23+
String.raw`/\p{Extended_Pictographic}/`,
24+
String.raw`/\P{Extended_Pictographic}/`,
25+
String.raw`/\p{Script=Dogr}/`,
26+
String.raw`/\P{Script=Dogr}/`,
27+
String.raw`new RegExp('\\p{Extended_Pictographic}')`,
28+
String.raw`new RegExp('\\\\p{Extended_Pictographic}', 'u')`,
29+
String.raw`/\p{Extended_Pictographic}/u`,
30+
String.raw`/\p{Script=Dogr}/u`,
31+
String.raw`/\p{Script=Elym}/u`,
32+
String.raw`/\p{EBase}/u`,
33+
String.raw`/\p{Script=Chorasmian}/u`,
34+
],
35+
invalid: [
36+
{
37+
code: String.raw`/\p{Script=Cpmn}/u`,
38+
errors: ["ES2022 '\\p{Script=Cpmn}' is forbidden."],
39+
},
40+
{
41+
code: String.raw`/\p{Script=Cypro_Minoan}/u`,
42+
errors: ["ES2022 '\\p{Script=Cypro_Minoan}' is forbidden."],
43+
},
44+
{
45+
code: String.raw`/\p{Script=Old_Uyghur}/u`,
46+
errors: ["ES2022 '\\p{Script=Old_Uyghur}' is forbidden."],
47+
},
48+
{
49+
code: String.raw`/\p{Script=Ougr}/u`,
50+
errors: ["ES2022 '\\p{Script=Ougr}' is forbidden."],
51+
},
52+
{
53+
code: String.raw`/\p{Script=Tangsa}/u`,
54+
errors: ["ES2022 '\\p{Script=Tangsa}' is forbidden."],
55+
},
56+
{
57+
code: String.raw`/\p{Script=Tnsa}/u`,
58+
errors: ["ES2022 '\\p{Script=Tnsa}' is forbidden."],
59+
},
60+
{
61+
code: String.raw`/\p{Script=Toto}/u`,
62+
errors: ["ES2022 '\\p{Script=Toto}' is forbidden."],
63+
},
64+
{
65+
code: String.raw`/\p{Script=Vith}/u`,
66+
errors: ["ES2022 '\\p{Script=Vith}' is forbidden."],
67+
},
68+
{
69+
code: String.raw`/\p{Script=Vithkuqi}/u`,
70+
errors: ["ES2022 '\\p{Script=Vithkuqi}' is forbidden."],
71+
},
72+
],
73+
})

0 commit comments

Comments
 (0)