Skip to content

Commit

Permalink
feat: ignore comment that isn't inside block
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Oct 31, 2022
1 parent 5943523 commit c697d95
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
13 changes: 4 additions & 9 deletions lib/rules/no-empty-static-block.js
Expand Up @@ -4,12 +4,6 @@
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const astUtils = require("./utils/ast-utils");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -37,10 +31,11 @@ module.exports = {

return {
StaticBlock(node) {
const innerComments = sourceCode.getTokens(node, {
includeComments: true,
filter: astUtils.isCommentToken
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({
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/rules/no-empty-static-block.js
Expand Up @@ -42,6 +42,10 @@ ruleTester.run("no-empty-static-block", rule, {
{
code: "class Foo { static { bar(); } static {} }",
errors: [{ messageId: "unexpected" }]
},
{
code: "class Foo { static // comment\n {} }",
errors: [{ messageId: "unexpected" }]
}
]
});

0 comments on commit c697d95

Please sign in to comment.