diff --git a/docs/rules/no-class-static-block.md b/docs/rules/no-class-static-block.md new file mode 100644 index 00000000..6a37d764 --- /dev/null +++ b/docs/rules/no-class-static-block.md @@ -0,0 +1,28 @@ +# es/no-class-static-block +> disallow class static block + +- ✅ The following configurations enable this rule: `plugin:es/no-new-in-esnext` + +This rule reports ES2022 class static blocks as errors. + +## Examples + +⛔ Examples of **incorrect** code for this rule: + + + +## 📚 References + +- [Rule source](https://github.com/mysticatea/eslint-plugin-es/blob/v4.1.0/lib/rules/no-class-static-block.js) +- [Test source](https://github.com/mysticatea/eslint-plugin-es/blob/v4.1.0/tests/lib/rules/no-class-static-block.js) diff --git a/lib/index.js b/lib/index.js index 5091fc9e..1cd096d6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -79,6 +79,7 @@ module.exports = { "no-binary-numeric-literals": require("./rules/no-binary-numeric-literals"), "no-block-scoped-functions": require("./rules/no-block-scoped-functions"), "no-block-scoped-variables": require("./rules/no-block-scoped-variables"), + "no-class-static-block": require("./rules/no-class-static-block"), "no-classes": require("./rules/no-classes"), "no-computed-properties": require("./rules/no-computed-properties"), "no-date-now": require("./rules/no-date-now"), diff --git a/lib/rules/no-class-static-block.js b/lib/rules/no-class-static-block.js new file mode 100644 index 00000000..0ca94e48 --- /dev/null +++ b/lib/rules/no-class-static-block.js @@ -0,0 +1,30 @@ +/** + * @author Yosuke Ota + * See LICENSE file in root directory for full license. + */ +"use strict" + +module.exports = { + meta: { + docs: { + description: "disallow class static block.", + category: "ES2022", + recommended: false, + url: + "http://mysticatea.github.io/eslint-plugin-es/rules/no-class-static-block.html", + }, + fixable: null, + messages: { + forbidden: "ES2022 class static block is forbidden.", + }, + schema: [], + type: "problem", + }, + create(context) { + return { + StaticBlock(node) { + context.report({ node, messageId: "forbidden" }) + }, + } + }, +} diff --git a/package.json b/package.json index 5b10b719..e05812cd 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tests/lib/rules/no-class-static-block.js b/tests/lib/rules/no-class-static-block.js new file mode 100644 index 00000000..cd5f2638 --- /dev/null +++ b/tests/lib/rules/no-class-static-block.js @@ -0,0 +1,28 @@ +/** + * @author Yosuke Ota + * See LICENSE file in root directory for full license. + */ +"use strict" + +const RuleTester = require("../../tester") +const rule = require("../../../lib/rules/no-class-static-block.js") + +if (!RuleTester.isSupported(2022)) { + //eslint-disable-next-line no-console + console.log("Skip the tests of no-class-static-block.") + return +} + +new RuleTester().run("no-class-static-block", rule, { + valid: ["class A { static f() {} }", "class A { static get f() {} }"], + invalid: [ + { + code: "class A { static {}; }", + errors: ["ES2022 class static block is forbidden."], + }, + { + code: "(class { static {} })", + errors: ["ES2022 class static block is forbidden."], + }, + ], +}) diff --git a/tests/tester.js b/tests/tester.js index f738a4c2..0bedf3cf 100644 --- a/tests/tester.js +++ b/tests/tester.js @@ -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 :