Skip to content

Add no-regexp-unicode-property-escapes-(2020 - 2022) rule #26

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

Merged
merged 6 commits into from
Jan 26, 2023
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
24 changes: 24 additions & 0 deletions .github/workflows/cron.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: cron
on:
schedule:
- cron: 0 0 * * 0

jobs:
check-resource-update:
name: check-resource-update
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 14
- name: Install Packages
run: npm install
- name: Update
run: npm run resource-update:unicode-properties
- name: Check changes
run: |
git add --all && \
git diff-index --cached HEAD --stat --exit-code
3 changes: 3 additions & 0 deletions docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ There are multiple configs that enable all rules in this category: `plugin:es-x/
| [es-x/no-object-hasown](./no-object-hasown.md) | disallow the `Object.hasOwn` method. | |
| [es-x/no-private-in](./no-private-in.md) | disallow `#x in obj`. | |
| [es-x/no-regexp-d-flag](./no-regexp-d-flag.md) | disallow RegExp `d` flag. | |
| [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. | |
| [es-x/no-top-level-await](./no-top-level-await.md) | disallow top-level `await`. | |

## ES2021
Expand All @@ -38,6 +39,7 @@ There are multiple configs that enable all rules in this category: `plugin:es-x/
| [es-x/no-logical-assignment-operators](./no-logical-assignment-operators.md) | disallow logical assignment operators. | 🔧 |
| [es-x/no-numeric-separators](./no-numeric-separators.md) | disallow numeric separators. | 🔧 |
| [es-x/no-promise-any](./no-promise-any.md) | disallow `Promise.any` function and `AggregateError` class. | |
| [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. | |
| [es-x/no-string-prototype-replaceall](./no-string-prototype-replaceall.md) | disallow the `String.prototype.replaceAll` method. | |
| [es-x/no-weakrefs](./no-weakrefs.md) | disallow the `WeakRef` and `FinalizationRegistry` class. | |

Expand All @@ -55,6 +57,7 @@ There are multiple configs that enable all rules in this category: `plugin:es-x/
| [es-x/no-nullish-coalescing-operators](./no-nullish-coalescing-operators.md) | disallow nullish coalescing operators. | |
| [es-x/no-optional-chaining](./no-optional-chaining.md) | disallow optional chaining. | |
| [es-x/no-promise-all-settled](./no-promise-all-settled.md) | disallow `Promise.allSettled` function. | |
| [es-x/no-regexp-unicode-property-escapes-2020](./no-regexp-unicode-property-escapes-2020.md) | disallow the new values of RegExp Unicode property escape sequences in ES2020. | |
| [es-x/no-string-prototype-matchall](./no-string-prototype-matchall.md) | disallow the `String.prototype.matchAll` method. | |

## ES2019
Expand Down
42 changes: 42 additions & 0 deletions docs/rules/no-regexp-unicode-property-escapes-2020.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: "es-x/no-regexp-unicode-property-escapes-2020"
description: "disallow the new values of RegExp Unicode property escape sequences in ES2020"
---

# es-x/no-regexp-unicode-property-escapes-2020
> disallow the new values of RegExp Unicode property escape sequences in ES2020

- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
- ✅ The following configurations enable this rule: `plugin:es-x/no-new-in-es2020`, `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`, and `plugin:es-x/restrict-to-es2019`

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 ES2020.

For example, the following patterns are valid in ES2020, but syntax error in ES2019 environments:

- `\p{Script=Elym}`
- `\p{Script=Elymaic}`
- `\p{Script=Hmnp}`
- `\p{Script=Nand}`
- `\p{Script=Nandinagari}`
- `\p{Script=Nyiakeng_Puachue_Hmong}`
- `\p{Script=Wancho}`
- `\p{Script=Wcho}`

## 💡 Examples

⛔ Examples of **incorrect** code for this rule:

<eslint-playground type="bad">

```js
/*eslint es-x/no-regexp-unicode-property-escapes-2020: error */
const r1 = /\p{Script=Elym}/u
const r2 = /\p{Script=Elymaic}/u
```

</eslint-playground>

## 📚 References

- [Rule source](https://github.com/ota-meshi/eslint-plugin-es-x/blob/master/lib/rules/no-regexp-unicode-property-escapes-2020.js)
- [Test source](https://github.com/ota-meshi/eslint-plugin-es-x/blob/master/tests/lib/rules/no-regexp-unicode-property-escapes-2020.js)
47 changes: 47 additions & 0 deletions docs/rules/no-regexp-unicode-property-escapes-2021.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: "es-x/no-regexp-unicode-property-escapes-2021"
description: "disallow the new values of RegExp Unicode property escape sequences in ES2021"
---

# es-x/no-regexp-unicode-property-escapes-2021
> disallow the new values of RegExp Unicode property escape sequences in ES2021

- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
- ✅ 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`

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.

For example, the following patterns are valid in ES2021, but syntax error in ES2020 environments:

- `\p{EBase}`
- `\p{EComp}`
- `\p{EMod}`
- `\p{EPres}`
- `\p{ExtPict}`
- `\p{Script=Chorasmian}`
- `\p{Script=Chrs}`
- `\p{Script=Diak}`
- `\p{Script=Dives_Akuru}`
- `\p{Script=Khitan_Small_Script}`
- `\p{Script=Kits}`
- `\p{Script=Yezi}`
- `\p{Script=Yezidi}`

## 💡 Examples

⛔ Examples of **incorrect** code for this rule:

<eslint-playground type="bad">

```js
/*eslint es-x/no-regexp-unicode-property-escapes-2021: error */
const r1 = /\p{EBase}/u
const r2 = /\p{Script=Chorasmian}/u
```

</eslint-playground>

## 📚 References

- [Rule source](https://github.com/ota-meshi/eslint-plugin-es-x/blob/master/lib/rules/no-regexp-unicode-property-escapes-2021.js)
- [Test source](https://github.com/ota-meshi/eslint-plugin-es-x/blob/master/tests/lib/rules/no-regexp-unicode-property-escapes-2021.js)
43 changes: 43 additions & 0 deletions docs/rules/no-regexp-unicode-property-escapes-2022.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
title: "es-x/no-regexp-unicode-property-escapes-2022"
description: "disallow the new values of RegExp Unicode property escape sequences in ES2022"
---

# es-x/no-regexp-unicode-property-escapes-2022
> disallow the new values of RegExp Unicode property escape sequences in ES2022

- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
- ✅ 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`

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.

For example, the following patterns are valid in ES2022, but syntax error in ES2020 environments:

- `\p{Script=Cpmn}`
- `\p{Script=Cypro_Minoan}`
- `\p{Script=Old_Uyghur}`
- `\p{Script=Ougr}`
- `\p{Script=Tangsa}`
- `\p{Script=Tnsa}`
- `\p{Script=Toto}`
- `\p{Script=Vith}`
- `\p{Script=Vithkuqi}`

## 💡 Examples

⛔ Examples of **incorrect** code for this rule:

<eslint-playground type="bad">

```js
/*eslint es-x/no-regexp-unicode-property-escapes-2022: error */
const r1 = /\p{Script=Cpmn}/u
const r2 = /\p{Script=Cypro_Minoan}/u
```

</eslint-playground>

## 📚 References

- [Rule source](https://github.com/ota-meshi/eslint-plugin-es-x/blob/master/lib/rules/no-regexp-unicode-property-escapes-2022.js)
- [Test source](https://github.com/ota-meshi/eslint-plugin-es-x/blob/master/tests/lib/rules/no-regexp-unicode-property-escapes-2022.js)
1 change: 1 addition & 0 deletions lib/configs/no-new-in-es2020.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
"es-x/no-nullish-coalescing-operators": "error",
"es-x/no-optional-chaining": "error",
"es-x/no-promise-all-settled": "error",
"es-x/no-regexp-unicode-property-escapes-2020": "error",
"es-x/no-string-prototype-matchall": "error",
},
}
1 change: 1 addition & 0 deletions lib/configs/no-new-in-es2021.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
"es-x/no-logical-assignment-operators": "error",
"es-x/no-numeric-separators": "error",
"es-x/no-promise-any": "error",
"es-x/no-regexp-unicode-property-escapes-2021": "error",
"es-x/no-string-prototype-replaceall": "error",
"es-x/no-weakrefs": "error",
},
Expand Down
1 change: 1 addition & 0 deletions lib/configs/no-new-in-es2022.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
"es-x/no-object-hasown": "error",
"es-x/no-private-in": "error",
"es-x/no-regexp-d-flag": "error",
"es-x/no-regexp-unicode-property-escapes-2022": "error",
"es-x/no-top-level-await": "error",
},
}
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ module.exports = {
"no-regexp-u-flag": require("./rules/no-regexp-u-flag"),
"no-regexp-unicode-property-escapes": require("./rules/no-regexp-unicode-property-escapes"),
"no-regexp-unicode-property-escapes-2019": require("./rules/no-regexp-unicode-property-escapes-2019"),
"no-regexp-unicode-property-escapes-2020": require("./rules/no-regexp-unicode-property-escapes-2020"),
"no-regexp-unicode-property-escapes-2021": require("./rules/no-regexp-unicode-property-escapes-2021"),
"no-regexp-unicode-property-escapes-2022": require("./rules/no-regexp-unicode-property-escapes-2022"),
"no-regexp-y-flag": require("./rules/no-regexp-y-flag"),
"no-rest-parameters": require("./rules/no-rest-parameters"),
"no-rest-spread-properties": require("./rules/no-rest-spread-properties"),
Expand Down
64 changes: 16 additions & 48 deletions lib/rules/no-regexp-lookbehind-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,7 @@
*/
"use strict"

const { RegExpValidator } = require("@eslint-community/regexpp")
const { getRegExpCalls } = require("../utils")

/**
* Verify a given regular expression.
* @param {RuleContext} context The rule context to report.
* @param {Node} node The AST node to report.
* @param {string} pattern The pattern part of a RegExp.
* @param {string} flags The flags part of a RegExp.
* @returns {void}
*/
function verify(context, node, pattern, flags) {
try {
let found = false

new RegExpValidator({
onLookaroundAssertionEnter(_start, kind) {
if (kind === "lookbehind") {
found = true
}
},
}).validatePattern(pattern, 0, pattern.length, flags.includes("u"))

if (found) {
context.report({ node, messageId: "forbidden" })
}
} catch (error) {
//istanbul ignore else
if (error.message.startsWith("Invalid regular expression:")) {
return
}
//istanbul ignore next
throw error
}
}
const { defineRegExpHandler } = require("../util/define-regexp-handler")

module.exports = {
meta: {
Expand All @@ -56,18 +22,20 @@ module.exports = {
type: "problem",
},
create(context) {
return {
"Literal[regex]"(node) {
const { pattern, flags } = node.regex
verify(context, node, pattern || "", flags || "")
},

"Program:exit"() {
const scope = context.getScope()
for (const { node, pattern, flags } of getRegExpCalls(scope)) {
verify(context, node, pattern || "", flags || "")
}
},
}
return defineRegExpHandler(context, (node) => {
let found = false
return {
onLookaroundAssertionEnter(_start, kind) {
if (kind === "lookbehind") {
found = true
}
},
onExit() {
if (found) {
context.report({ node, messageId: "forbidden" })
}
},
}
})
},
}
74 changes: 21 additions & 53 deletions lib/rules/no-regexp-named-capture-groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,7 @@
*/
"use strict"

const { RegExpValidator } = require("@eslint-community/regexpp")
const { getRegExpCalls } = require("../utils")

/**
* Verify a given regular expression.
* @param {RuleContext} context The rule context to report.
* @param {Node} node The AST node to report.
* @param {string} pattern The pattern part of a RegExp.
* @param {string} flags The flags part of a RegExp.
* @returns {void}
*/
function verify(context, node, pattern, flags) {
try {
let found = false

new RegExpValidator({
onCapturingGroupEnter(_start, name) {
if (name) {
found = true
}
},
onBackreference(_start, _end, ref) {
if (typeof ref === "string") {
found = true
}
},
}).validatePattern(pattern, 0, pattern.length, flags.includes("u"))

if (found) {
context.report({ node, messageId: "forbidden" })
}
} catch (error) {
//istanbul ignore else
if (error.message.startsWith("Invalid regular expression:")) {
return
}
//istanbul ignore next
throw error
}
}
const { defineRegExpHandler } = require("../util/define-regexp-handler")

module.exports = {
meta: {
Expand All @@ -61,18 +22,25 @@ module.exports = {
type: "problem",
},
create(context) {
return {
"Literal[regex]"(node) {
const { pattern, flags } = node.regex
verify(context, node, pattern || "", flags || "")
},

"Program:exit"() {
const scope = context.getScope()
for (const { node, pattern, flags } of getRegExpCalls(scope)) {
verify(context, node, pattern || "", flags || "")
}
},
}
return defineRegExpHandler(context, (node) => {
let found = false
return {
onCapturingGroupEnter(_start, name) {
if (name) {
found = true
}
},
onBackreference(_start, _end, ref) {
if (typeof ref === "string") {
found = true
}
},
onExit() {
if (found) {
context.report({ node, messageId: "forbidden" })
}
},
}
})
},
}
Loading