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-class-static-block rule #80

Open
wants to merge 2 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
28 changes: 28 additions & 0 deletions docs/rules/no-class-static-block.md
Original file line number Diff line number Diff line change
@@ -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:

<eslint-playground type="bad" code="/*eslint es/no-class-static-block: error */
class A {
static {
// ...
}
}
const B = class {
static {
// ...
}
}
" />

## 📚 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)
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
30 changes: 30 additions & 0 deletions lib/rules/no-class-static-block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @author Yosuke Ota <https://github.com/ota-meshi>
* 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" })
},
}
},
}
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
28 changes: 28 additions & 0 deletions tests/lib/rules/no-class-static-block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @author Yosuke Ota <https://github.com/ota-meshi>
* 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."],
},
],
})
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