Skip to content
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

Add es/no-regexp-d-flag rule #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions docs/rules/no-regexp-d-flag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# es/no-regexp-d-flag
> disallow RegExp `d` flag

- ✅ The following configurations enable this rule: `plugin:es/no-new-in-esnext`

This rule reports ES2022 [RegExp `d` flag](https://github.com/tc39/proposal-regexp-match-indices#readme) as errors.

## Examples

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

<eslint-playground type="bad" code="/*eslint es/no-regexp-d-flag: error */
const r1 = /./d
" />

## 📚 References

- [Rule source](https://github.com/mysticatea/eslint-plugin-es/blob/v4.1.0/lib/rules/no-regexp-d-flag.js)
- [Test source](https://github.com/mysticatea/eslint-plugin-es/blob/v4.1.0/tests/lib/rules/no-regexp-d-flag.js)
44 changes: 44 additions & 0 deletions lib/rules/no-regexp-d-flag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
"use strict"

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

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

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

for (const { node, flags } of getRegExpCalls(scope)) {
if (flags && flags.includes("d")) {
context.report({ node, messageId: "forbidden" })
}
}
},
}
},
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"@mysticatea/eslint-plugin": "^13.0.0",
"@typescript-eslint/parser": "^4.8.2",
"@vuepress/plugin-pwa": "^1.2.0",
"acorn": "^7.1.0",
"acorn": "^8.5.0",
"codecov": "^3.8.1",
"eslint": "^7.10.0",
"eslint": "^8.0.0-0",
"eslint4b": "^7.10.0",
"espree": "^7.0.0",
"espree": "^9.0.0",
"globals": "^12.0.0",
"mocha": "^6.2.0",
"npm-run-all": "^4.1.5",
Expand Down
47 changes: 47 additions & 0 deletions tests/lib/rules/no-regexp-d-flag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
"use strict"

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

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

new RuleTester().run("no-regexp-d-flag", rule, {
valid: [
"/foo/gimuys",
"a\n/b/d",
"new RegExp('foo', 'gimuys')",
"new RegExp('foo')",
"new RegExp('foo', flags)",
"const flags = 'gimuys'; new RegExp('foo', flags)",
],
invalid: [
{
code: "/foo/d",
errors: ["ES2022 RegExp 'd' flag is forbidden."],
},
{
code: "/foo/gimsuyd",
errors: ["ES2022 RegExp 'd' flag is forbidden."],
},
{
code: "new RegExp('foo', 'd')",
errors: ["ES2022 RegExp 'd' flag is forbidden."],
},
{
code: "new RegExp('foo', 'gimsuyd')",
errors: ["ES2022 RegExp 'd' flag is forbidden."],
},
{
code: "const flags = 'd'; new RegExp('foo', flags)",
errors: ["ES2022 RegExp 'd' flag is forbidden."],
},
],
})
1 change: 1 addition & 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 @mysticatea/prettier */
semver.gte(eslintVersion, "8.0.0-0") ? 2022 :
semver.gte(eslintVersion, "7.8.0") ? 2021 :
semver.gte(eslintVersion, "6.2.0") ? 2020 :
semver.gte(eslintVersion, "5.0.0") ? 2019 :
Expand Down