Skip to content

Commit 936fc19

Browse files
authored
Add no-regexp-unicode-property-escapes-2023 rule (#65)
* Add no-regexp-unicode-property-escapes-2023 rule * refactor * chore: use node-fetch * chore: refactor * chore: use fetch().text() * Update package.json * add es2024 to update-unicode-properties
1 parent a59d0a4 commit 936fc19

15 files changed

+321
-27
lines changed

.eslintrc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ ignorePatterns:
1010
- "!.vuepress"
1111
- /docs/.vuepress/dist
1212

13+
globals:
14+
fetch: readonly
15+
1316
overrides:
1417
- files: lib/rules/**/*.js
1518
rules:

docs/rules/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ There is a config that enables the rules in this category: `plugin:es-x/no-new-i
1616
| [es-x/no-array-prototype-tospliced](./no-array-prototype-tospliced.md) | disallow the `Array.prototype.toSpliced` method. | |
1717
| [es-x/no-array-prototype-with](./no-array-prototype-with.md) | disallow the `Array.prototype.with` method. | |
1818
| [es-x/no-hashbang](./no-hashbang.md) | disallow Hashbang comments. | |
19+
| [es-x/no-regexp-unicode-property-escapes-2023](./no-regexp-unicode-property-escapes-2023.md) | disallow the new values of RegExp Unicode property escape sequences in ES2023. | |
1920

2021
## ES2023 Intl API
2122

docs/rules/no-regexp-unicode-property-escapes-2022.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ since: "v6.0.0"
1111

1212
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.
1313

14-
For example, the following patterns are valid in ES2022, but syntax error in ES2020 environments:
14+
For example, the following patterns are valid in ES2022, but syntax error in ES2021 environments:
1515

1616
- `\p{Script=Cpmn}`
1717
- `\p{Script=Cypro_Minoan}`
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: "es-x/no-regexp-unicode-property-escapes-2023"
3+
description: "disallow the new values of RegExp Unicode property escape sequences in ES2023"
4+
---
5+
6+
# es-x/no-regexp-unicode-property-escapes-2023
7+
> disallow the new values of RegExp Unicode property escape sequences in ES2023
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-esnext`
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 ES2023.
13+
14+
For example, the following patterns are valid in ES2023, but syntax error in ES2022 environments:
15+
16+
- `\p{Script=Kawi}`
17+
- `\p{Script=Nag_Mundari}`
18+
- `\p{Script=Nagm}`
19+
20+
## 💡 Examples
21+
22+
⛔ Examples of **incorrect** code for this rule:
23+
24+
<eslint-playground type="bad">
25+
26+
```js
27+
/*eslint es-x/no-regexp-unicode-property-escapes-2023: error */
28+
const r1 = /\p{Script=Kawi}/u
29+
const r2 = /\p{Script=Nag_Mundari}/u
30+
const r2 = /\p{Script=Nagm}/u
31+
```
32+
33+
</eslint-playground>
34+
35+
## 📚 References
36+
37+
- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-regexp-unicode-property-escapes-2023.js)
38+
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-regexp-unicode-property-escapes-2023.js)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ module.exports = {
1313
"es-x/no-array-prototype-tospliced": "error",
1414
"es-x/no-array-prototype-with": "error",
1515
"es-x/no-hashbang": "error",
16+
"es-x/no-regexp-unicode-property-escapes-2023": "error",
1617
},
1718
}

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ module.exports = {
227227
"no-regexp-unicode-property-escapes-2020": require("./rules/no-regexp-unicode-property-escapes-2020"),
228228
"no-regexp-unicode-property-escapes-2021": require("./rules/no-regexp-unicode-property-escapes-2021"),
229229
"no-regexp-unicode-property-escapes-2022": require("./rules/no-regexp-unicode-property-escapes-2022"),
230+
"no-regexp-unicode-property-escapes-2023": require("./rules/no-regexp-unicode-property-escapes-2023"),
230231
"no-regexp-y-flag": require("./rules/no-regexp-y-flag"),
231232
"no-rest-parameters": require("./rules/no-rest-parameters"),
232233
"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.es2023.has(value)
16+
}
17+
18+
function isNewBinaryUnicodeProperty(key) {
19+
return binPropertySets.es2023.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 ES2023",
27+
category: "ES2023",
28+
recommended: false,
29+
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-regexp-unicode-property-escapes-2023.html",
30+
},
31+
fixable: null,
32+
messages: {
33+
forbidden: "ES2023 '{{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+
}

lib/util/unicode-properties.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
"use strict"
33

44
class DataSet {
5-
constructor(raw2018, raw2019, raw2020, raw2021, raw2022, raw2023) {
5+
constructor(raw2018, raw2019, raw2020, raw2021, raw2022, raw2023, raw2024) {
66
this._raw2018 = raw2018
77
this._raw2019 = raw2019
88
this._raw2020 = raw2020
99
this._raw2021 = raw2021
1010
this._raw2022 = raw2022
1111
this._raw2023 = raw2023
12+
this._raw2024 = raw2024
1213
}
1314

1415
get es2018() {
@@ -46,6 +47,12 @@ class DataSet {
4647
this._set2023 || (this._set2023 = new Set(this._raw2023.split(" ")))
4748
)
4849
}
50+
51+
get es2024() {
52+
return (
53+
this._set2024 || (this._set2024 = new Set(this._raw2024.split(" ")))
54+
)
55+
}
4956
}
5057

5158
const gcNameSet = new Set(["General_Category", "gc"])
@@ -57,13 +64,15 @@ const gcValueSets = new DataSet(
5764
"",
5865
"",
5966
"",
67+
"",
6068
)
6169
const scValueSets = new DataSet(
6270
"Adlam Adlm Aghb Ahom Anatolian_Hieroglyphs Arab Arabic Armenian Armi Armn Avestan Avst Bali Balinese Bamu Bamum Bass Bassa_Vah Batak Batk Beng Bengali Bhaiksuki Bhks Bopo Bopomofo Brah Brahmi Brai Braille Bugi Buginese Buhd Buhid Cakm Canadian_Aboriginal Cans Cari Carian Caucasian_Albanian Chakma Cham Cher Cherokee Common Copt Coptic Cprt Cuneiform Cypriot Cyrillic Cyrl Deseret Deva Devanagari Dsrt Dupl Duployan Egyp Egyptian_Hieroglyphs Elba Elbasan Ethi Ethiopic Geor Georgian Glag Glagolitic Gonm Goth Gothic Gran Grantha Greek Grek Gujarati Gujr Gurmukhi Guru Han Hang Hangul Hani Hano Hanunoo Hatr Hatran Hebr Hebrew Hira Hiragana Hluw Hmng Hung Imperial_Aramaic Inherited Inscriptional_Pahlavi Inscriptional_Parthian Ital Java Javanese Kaithi Kali Kana Kannada Katakana Kayah_Li Khar Kharoshthi Khmer Khmr Khoj Khojki Khudawadi Knda Kthi Lana Lao Laoo Latin Latn Lepc Lepcha Limb Limbu Lina Linb Linear_A Linear_B Lisu Lyci Lycian Lydi Lydian Mahajani Mahj Malayalam Mand Mandaic Mani Manichaean Marc Marchen Masaram_Gondi Meetei_Mayek Mend Mende_Kikakui Merc Mero Meroitic_Cursive Meroitic_Hieroglyphs Miao Mlym Modi Mong Mongolian Mro Mroo Mtei Mult Multani Myanmar Mymr Nabataean Narb Nbat New_Tai_Lue Newa Nko Nkoo Nshu Nushu Ogam Ogham Ol_Chiki Olck Old_Hungarian Old_Italic Old_North_Arabian Old_Permic Old_Persian Old_South_Arabian Old_Turkic Oriya Orkh Orya Osage Osge Osma Osmanya Pahawh_Hmong Palm Palmyrene Pau_Cin_Hau Pauc Perm Phag Phags_Pa Phli Phlp Phnx Phoenician Plrd Prti Psalter_Pahlavi Qaac Qaai Rejang Rjng Runic Runr Samaritan Samr Sarb Saur Saurashtra Sgnw Sharada Shavian Shaw Shrd Sidd Siddham SignWriting Sind Sinh Sinhala Sora Sora_Sompeng Soyo Soyombo Sund Sundanese Sylo Syloti_Nagri Syrc Syriac Tagalog Tagb Tagbanwa Tai_Le Tai_Tham Tai_Viet Takr Takri Tale Talu Tamil Taml Tang Tangut Tavt Telu Telugu Tfng Tglg Thaa Thaana Thai Tibetan Tibt Tifinagh Tirh Tirhuta Ugar Ugaritic Vai Vaii Wara Warang_Citi Xpeo Xsux Yi Yiii Zanabazar_Square Zanb Zinh Zyyy",
6371
"Dogr Dogra Gong Gunjala_Gondi Hanifi_Rohingya Maka Makasar Medefaidrin Medf Old_Sogdian Rohg Sogd Sogdian Sogo",
6472
"Elym Elymaic Hmnp Nand Nandinagari Nyiakeng_Puachue_Hmong Wancho Wcho",
6573
"Chorasmian Chrs Diak Dives_Akuru Khitan_Small_Script Kits Yezi Yezidi",
6674
"Cpmn Cypro_Minoan Old_Uyghur Ougr Tangsa Tnsa Toto Vith Vithkuqi",
75+
"Hrkt Katakana_Or_Hiragana Kawi Nag_Mundari Nagm Unknown Zzzz",
6776
"",
6877
)
6978
const binPropertySets = new DataSet(
@@ -73,6 +82,7 @@ const binPropertySets = new DataSet(
7382
"EBase EComp EMod EPres ExtPict",
7483
"",
7584
"",
85+
"",
7686
)
7787
module.exports = {
7888
gcNameSet,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"dependencies": {
1616
"@eslint-community/eslint-utils": "^4.1.2",
17-
"@eslint-community/regexpp": "^4.2.0"
17+
"@eslint-community/regexpp": "^4.5.0"
1818
},
1919
"devDependencies": {
2020
"@typescript-eslint/parser": "^5.14.0",

scripts/fetch-lines.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict"
2+
3+
module.exports = { fetchLines }
4+
5+
async function* fetchLines(url) {
6+
const response = await fetch(url)
7+
yield* (await response.text()).split("\n")
8+
}

0 commit comments

Comments
 (0)