forked from eslint/eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new rule
no-empty-static-block
(eslint#16325)
* feat: add `no-empty-static-block` * docs: add docs for `no-empty-static-block` * chore: add `no-empty-static-block` * test: add tests for multiple static blocks * Add `further_reading` * test: specify `ecmaVersion` at `RuleTester` * docs: remove options section * fix: `disallows` -> `disallow` * fix: remove `loc` parameters * feat: ignore comment that isn't inside block * fix: address review
- Loading branch information
1 parent
ce93b42
commit 978799b
Showing
6 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
title: no-empty-static-block | ||
layout: doc | ||
rule_type: suggestion | ||
related_rules: | ||
- no-empty | ||
- no-empty-function | ||
further_reading: | ||
- https://github.com/tc39/proposal-class-static-block | ||
--- | ||
|
||
Empty static blocks, while not technically errors, usually occur due to refactoring that wasn't completed. They can cause confusion when reading code. | ||
|
||
## Rule Details | ||
|
||
This rule disallows empty static blocks. This rule ignores static blocks which contain a comment. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
::: incorrect | ||
|
||
```js | ||
/*eslint no-empty-static-block: "error"*/ | ||
|
||
class Foo { | ||
static {} | ||
} | ||
``` | ||
|
||
::: | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
:::correct | ||
|
||
```js | ||
/*eslint no-empty-static-block: "error"*/ | ||
|
||
class Foo { | ||
static { | ||
bar(); | ||
} | ||
} | ||
|
||
class Foo { | ||
static { | ||
// comment | ||
} | ||
} | ||
``` | ||
|
||
::: | ||
|
||
## When Not To Use It | ||
|
||
This rule should not be used in environments prior to ES2022. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* @fileoverview Rule to disallow empty static blocks. | ||
* @author Sosuke Suzuki | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
/** @type {import('../shared/types').Rule} */ | ||
module.exports = { | ||
meta: { | ||
type: "suggestion", | ||
|
||
docs: { | ||
description: "Disallow empty static blocks", | ||
recommended: false, | ||
url: "https://eslint.org/docs/rules/no-empty-static-block" | ||
}, | ||
|
||
schema: [], | ||
|
||
messages: { | ||
unexpected: "Unexpected empty static block." | ||
} | ||
}, | ||
|
||
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
return { | ||
StaticBlock(node) { | ||
if (node.body.length === 0) { | ||
const closingBrace = sourceCode.getLastToken(node); | ||
|
||
if (sourceCode.getCommentsBefore(closingBrace).length === 0) { | ||
context.report({ | ||
node, | ||
messageId: "unexpected" | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @fileoverview Tests for no-empty-static-block rule. | ||
* @author Sosuke Suzuki | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require("../../../lib/rules/no-empty-static-block"), | ||
{ RuleTester } = require("../../../lib/rule-tester"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { ecmaVersion: 2022 } | ||
}); | ||
|
||
ruleTester.run("no-empty-static-block", rule, { | ||
valid: [ | ||
"class Foo { static { bar(); } }", | ||
"class Foo { static { /* comments */ } }", | ||
"class Foo { static {\n// comment\n} }", | ||
"class Foo { static { bar(); } static { bar(); } }" | ||
], | ||
invalid: [ | ||
{ | ||
code: "class Foo { static {} }", | ||
errors: [{ messageId: "unexpected" }] | ||
}, | ||
{ | ||
code: "class Foo { static { } }", | ||
errors: [{ messageId: "unexpected" }] | ||
}, | ||
{ | ||
code: "class Foo { static { \n\n } }", | ||
errors: [{ messageId: "unexpected" }] | ||
}, | ||
{ | ||
code: "class Foo { static { bar(); } static {} }", | ||
errors: [{ messageId: "unexpected" }] | ||
}, | ||
{ | ||
code: "class Foo { static // comment\n {} }", | ||
errors: [{ messageId: "unexpected" }] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters