Skip to content

Commit 6fb79b5

Browse files
committed
Add no-regexp-unicode-property-escapes-2021 rule
1 parent 7cdb9da commit 6fb79b5

File tree

6 files changed

+202
-0
lines changed

6 files changed

+202
-0
lines changed

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ There are multiple configs that enable all rules in this category: `plugin:es-x/
3838
| [es-x/no-logical-assignment-operators](./no-logical-assignment-operators.md) | disallow logical assignment operators. | 🔧 |
3939
| [es-x/no-numeric-separators](./no-numeric-separators.md) | disallow numeric separators. | 🔧 |
4040
| [es-x/no-promise-any](./no-promise-any.md) | disallow `Promise.any` function and `AggregateError` class. | |
41+
| [es-x/no-regexp-unicode-property-escapes-2021](./no-regexp-unicode-property-escapes-2021.md) | disallow the new values of RegExp Unicode property escape sequences in ES2021. | |
4142
| [es-x/no-string-prototype-replaceall](./no-string-prototype-replaceall.md) | disallow the `String.prototype.replaceAll` method. | |
4243
| [es-x/no-weakrefs](./no-weakrefs.md) | disallow the `WeakRef` and `FinalizationRegistry` class. | |
4344

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: "es-x/no-regexp-unicode-property-escapes-2021"
3+
description: "disallow the new values of RegExp Unicode property escape sequences in ES2021"
4+
---
5+
6+
# es-x/no-regexp-unicode-property-escapes-2021
7+
> disallow the new values of RegExp Unicode property escape sequences in ES2021
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-es2021`, `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`, and `plugin:es-x/restrict-to-es2020`
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 ES2021.
13+
14+
For example, the following patterns are valid in ES2021, but syntax error in ES2020 environments:
15+
16+
- `\p{EBase}`
17+
- `\p{EComp}`
18+
- `\p{EMod}`
19+
- `\p{EPres}`
20+
- `\p{ExtPict}`
21+
- `\p{Script=Chorasmian}`
22+
- `\p{Script=Chrs}`
23+
- `\p{Script=Diak}`
24+
- `\p{Script=Dives_Akuru}`
25+
- `\p{Script=Khitan_Small_Script}`
26+
- `\p{Script=Kits}`
27+
- `\p{Script=Yezi}`
28+
- `\p{Script=Yezidi}`
29+
30+
## 💡 Examples
31+
32+
⛔ Examples of **incorrect** code for this rule:
33+
34+
<eslint-playground type="bad">
35+
36+
```js
37+
/*eslint es-x/no-regexp-unicode-property-escapes-2021: error */
38+
const r1 = /\p{EBase}/u
39+
const r2 = /\p{Script=Chorasmian}/u
40+
```
41+
42+
</eslint-playground>
43+
44+
## 📚 References
45+
46+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-es-x/blob/master/lib/rules/no-regexp-unicode-property-escapes-2021.js)
47+
- [Test source](https://github.com/ota-meshi/eslint-plugin-es-x/blob/master/tests/lib/rules/no-regexp-unicode-property-escapes-2021.js)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010
"es-x/no-logical-assignment-operators": "error",
1111
"es-x/no-numeric-separators": "error",
1212
"es-x/no-promise-any": "error",
13+
"es-x/no-regexp-unicode-property-escapes-2021": "error",
1314
"es-x/no-string-prototype-replaceall": "error",
1415
"es-x/no-weakrefs": "error",
1516
},

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ module.exports = {
190190
"no-regexp-unicode-property-escapes": require("./rules/no-regexp-unicode-property-escapes"),
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"),
193+
"no-regexp-unicode-property-escapes-2021": require("./rules/no-regexp-unicode-property-escapes-2021"),
193194
"no-regexp-y-flag": require("./rules/no-regexp-y-flag"),
194195
"no-rest-parameters": require("./rules/no-rest-parameters"),
195196
"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.es2021.has(value)
16+
}
17+
18+
function isNewBinaryUnicodeProperty(key) {
19+
return binPropertySets.es2021.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 ES2021",
27+
category: "ES2021",
28+
recommended: false,
29+
url: "http://ota-meshi.github.io/eslint-plugin-es-x/rules/no-regexp-unicode-property-escapes-2021.html",
30+
},
31+
fixable: null,
32+
messages: {
33+
forbidden: "ES2021 '{{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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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-2021.js")
9+
10+
if (!RuleTester.isSupported(2021)) {
11+
//eslint-disable-next-line no-console
12+
console.log("Skip the tests of no-regexp-unicode-property-escapes-2021.")
13+
return
14+
}
15+
16+
new RuleTester().run("no-regexp-unicode-property-escapes-2021", rule, {
17+
valid: [
18+
String.raw`/\p{Letter}/u`,
19+
String.raw`/\P{Letter}/u`,
20+
String.raw`/\p{Script=Hiragana}/u`,
21+
String.raw`/\P{Script=Hiragana}/u`,
22+
String.raw`/\p{Extended_Pictographic}/`,
23+
String.raw`/\P{Extended_Pictographic}/`,
24+
String.raw`/\p{Script=Dogr}/`,
25+
String.raw`/\P{Script=Dogr}/`,
26+
String.raw`new RegExp('\\p{Extended_Pictographic}')`,
27+
String.raw`new RegExp('\\\\p{Extended_Pictographic}', 'u')`,
28+
String.raw`/\p{Extended_Pictographic}/u`,
29+
String.raw`/\p{Script=Dogr}/u`,
30+
String.raw`/\p{Script=Elym}/u`,
31+
],
32+
invalid: [
33+
{
34+
code: String.raw`/\p{EBase}/u`,
35+
errors: ["ES2021 '\\p{EBase}' is forbidden."],
36+
},
37+
{
38+
code: String.raw`/\p{EComp}/u`,
39+
errors: ["ES2021 '\\p{EComp}' is forbidden."],
40+
},
41+
{
42+
code: String.raw`/\p{EMod}/u`,
43+
errors: ["ES2021 '\\p{EMod}' is forbidden."],
44+
},
45+
{
46+
code: String.raw`/\p{EPres}/u`,
47+
errors: ["ES2021 '\\p{EPres}' is forbidden."],
48+
},
49+
{
50+
code: String.raw`/\p{ExtPict}/u`,
51+
errors: ["ES2021 '\\p{ExtPict}' is forbidden."],
52+
},
53+
{
54+
code: String.raw`/\p{Script=Chorasmian}/u`,
55+
errors: ["ES2021 '\\p{Script=Chorasmian}' is forbidden."],
56+
},
57+
{
58+
code: String.raw`/\p{Script=Chrs}/u`,
59+
errors: ["ES2021 '\\p{Script=Chrs}' is forbidden."],
60+
},
61+
{
62+
code: String.raw`/\p{Script=Diak}/u`,
63+
errors: ["ES2021 '\\p{Script=Diak}' is forbidden."],
64+
},
65+
{
66+
code: String.raw`/\p{Script=Dives_Akuru}/u`,
67+
errors: ["ES2021 '\\p{Script=Dives_Akuru}' is forbidden."],
68+
},
69+
{
70+
code: String.raw`/\p{Script=Khitan_Small_Script}/u`,
71+
errors: ["ES2021 '\\p{Script=Khitan_Small_Script}' is forbidden."],
72+
},
73+
{
74+
code: String.raw`/\p{Script=Kits}/u`,
75+
errors: ["ES2021 '\\p{Script=Kits}' is forbidden."],
76+
},
77+
{
78+
code: String.raw`/\p{Script=Yezi}/u`,
79+
errors: ["ES2021 '\\p{Script=Yezi}' is forbidden."],
80+
},
81+
{
82+
code: String.raw`/\p{Script=Yezidi}/u`,
83+
errors: ["ES2021 '\\p{Script=Yezidi}' is forbidden."],
84+
},
85+
],
86+
})

0 commit comments

Comments
 (0)