Skip to content

Commit

Permalink
Add no-regexp-v-flag rule (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Jul 22, 2023
1 parent 161ea58 commit c7bc9f8
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ There is a config that enables the rules in this category: `plugin:es-x/no-new-i
| Rule ID | Description | |
|:--------|:------------|:--:|
| [es-x/no-atomics-waitasync](./no-atomics-waitasync.md) | disallow the `Atomics.waitAsync` method. | |
| [es-x/no-regexp-v-flag](./no-regexp-v-flag.md) | disallow RegExp `v` flag. | |
| [es-x/no-string-prototype-iswellformed-towellformed](./no-string-prototype-iswellformed-towellformed.md) | disallow the `String.prototype.{isWellFormed,toWellFormed}` methods. | |

## ES2023
Expand Down
31 changes: 31 additions & 0 deletions docs/rules/no-regexp-v-flag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: "es-x/no-regexp-v-flag"
description: "disallow RegExp `v` flag"
---

# es-x/no-regexp-v-flag
> disallow RegExp `v` flag
- ❗ <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-esnext`

This rule reports ES2024 [RegExp `v` flag](https://github.com/tc39/proposal-regexp-v-flag) as errors.

## 💡 Examples

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

<eslint-playground type="bad">

```js
/*eslint es-x/no-regexp-v-flag: error */
const r1 = /^\p{RGI_Emoji}$/v
const r2 = /[\p{Decimal_Number}--[0-9]]/v
```

</eslint-playground>

## 📚 References

- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-regexp-v-flag.js)
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-regexp-v-flag.js)
1 change: 1 addition & 0 deletions lib/configs/no-new-in-esnext.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
plugins: ["es-x"],
rules: {
"es-x/no-atomics-waitasync": "error",
"es-x/no-regexp-v-flag": "error",
"es-x/no-string-prototype-iswellformed-towellformed": "error",
},
}
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ module.exports = {
"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-unicode-property-escapes-2023": require("./rules/no-regexp-unicode-property-escapes-2023"),
"no-regexp-v-flag": require("./rules/no-regexp-v-flag"),
"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
39 changes: 39 additions & 0 deletions lib/rules/no-regexp-v-flag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict"

const { getRegExpCalls } = require("../utils")

module.exports = {
meta: {
docs: {
description: "disallow RegExp `v` flag.",
category: "ES2024",
recommended: false,
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-regexp-v-flag.html",
},
fixable: null,
messages: {
forbidden: "ES2024 RegExp 'v' flag is forbidden.",
},
schema: [],
type: "problem",
},
create(context) {
return {
"Literal[regex]"(node) {
if (node.regex.flags.includes("v")) {
context.report({ node, messageId: "forbidden" })
}
},

"Program:exit"() {
const scope = context.getScope()

for (const { node, flags } of getRegExpCalls(scope)) {
if (flags && flags.includes("v")) {
context.report({ node, messageId: "forbidden" })
}
}
},
}
},
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"dependencies": {
"@eslint-community/eslint-utils": "^4.1.2",
"@eslint-community/regexpp": "^4.5.0"
"@eslint-community/regexpp": "^4.6.0"
},
"devDependencies": {
"@typescript-eslint/parser": "^5.14.0",
Expand Down
13 changes: 10 additions & 3 deletions scripts/new-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
const cp = require("child_process")
const fs = require("fs")
const path = require("path")
const { categories } = require("./rules")
const logger = console

const maxESVersion = Math.max(
...Object.keys(categories).map((esVersion) =>
/^ES\d+$/u.test(esVersion) ? Number(esVersion.slice(2)) : 0,
),
)

// main
;((ruleId) => {
if (ruleId == null) {
Expand All @@ -33,8 +40,8 @@ const logger = console
module.exports = {
meta: {
docs: {
description: "",
category: "",
description: "disallow ....",
category: "ES${maxESVersion}",
recommended: false,
url: "",
},
Expand All @@ -58,7 +65,7 @@ module.exports = {
const RuleTester = require("../../tester")
const rule = require("../../../lib/rules/${ruleId}.js")
if (!RuleTester.isSupported(2022)) {
if (!RuleTester.isSupported(${maxESVersion})) {
//eslint-disable-next-line no-console
console.log("Skip the tests of ${ruleId}.")
return
Expand Down
43 changes: 43 additions & 0 deletions tests/lib/rules/no-regexp-v-flag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict"

const RuleTester = require("../../tester")
const rule = require("../../../lib/rules/no-regexp-v-flag.js")

if (!RuleTester.isSupported(2024)) {
//eslint-disable-next-line no-console
console.log("Skip the tests of no-regexp-v-flag.")
return
}

new RuleTester().run("no-regexp-v-flag", rule, {
valid: [
"/foo/gimsu",
"a\n/b/y",
"new RegExp('foo')",
"new RegExp('foo', 'gimsu')",
"new RegExp('foo', flags)",
"const flags = 'gimsu'; new RegExp('foo', flags)",
],
invalid: [
{
code: "/foo/v",
errors: ["ES2024 RegExp 'v' flag is forbidden."],
},
{
code: "/foo/gimsyv",
errors: ["ES2024 RegExp 'v' flag is forbidden."],
},
{
code: "new RegExp('foo', 'v')",
errors: ["ES2024 RegExp 'v' flag is forbidden."],
},
{
code: "new RegExp('foo', 'gimsyv')",
errors: ["ES2024 RegExp 'v' flag is forbidden."],
},
{
code: "const pattern = 'foo', flags = 'gimsyv', regex = new RegExp(pattern, flags)",
errors: ["ES2024 RegExp 'v' flag is forbidden."],
},
],
})
2 changes: 2 additions & 0 deletions tests/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const semver = require("semver")
const eslintVersion = new Linter().version
const ecmaVersion =
/*eslint-disable prettier/prettier */
semver.gte(eslintVersion, "8.44.0") ? 2024 :
semver.gte(eslintVersion, "8.23.0") ? 2023 :
semver.gte(eslintVersion, "8.0.0") ? 2022 :
semver.gte(eslintVersion, "7.8.0") ? 2021 :
Expand All @@ -37,5 +38,6 @@ RuleTester.setDefaultConfig({
})
RuleTester.isSupported = (targetEcmaVersion) => targetEcmaVersion <= ecmaVersion
RuleTester.eslintVersion = eslintVersion
RuleTester.supportedEcmaVersion = ecmaVersion

module.exports = RuleTester

0 comments on commit c7bc9f8

Please sign in to comment.