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
feat: Add new rule no-empty-static-block
#16325
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3f3ba34
feat: add `no-empty-static-block`
sosukesuzuki b043a11
docs: add docs for `no-empty-static-block`
sosukesuzuki 7e6e412
chore: add `no-empty-static-block`
sosukesuzuki 14f6e6a
test: add tests for multiple static blocks
sosukesuzuki 690f40d
Add `further_reading`
sosukesuzuki 27d1256
test: specify `ecmaVersion` at `RuleTester`
sosukesuzuki d3fc888
docs: remove options section
sosukesuzuki ff14329
fix: `disallows` -> `disallow`
sosukesuzuki 5943523
fix: remove `loc` parameters
sosukesuzuki c697d95
feat: ignore comment that isn't inside block
sosukesuzuki 8f26be5
fix: address review
sosukesuzuki File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,49 @@ | ||
/** | ||
* @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) { | ||
const [blockOpenToken] = sourceCode.getFirstTokens(node, { | ||
filter: token => token.type === "Punctuator" && token.value === "{" | ||
}); | ||
const innerComments = | ||
sourceCode.getCommentsInside(node).filter(commentToken => blockOpenToken.range[1] < commentToken.range[0]); | ||
|
||
if (node.body.length === 0 && innerComments.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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should move token calculations into
if (node.body.length === 0) {}
, for performance reasons.It could be something like this: