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

feat: update semi rule for class static blocks #15286

Merged
merged 1 commit into from Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/rules/semi.md
Expand Up @@ -172,6 +172,14 @@ Examples of additional **correct** code for this rule with the `"always", { "omi
if (foo) { bar() }

if (foo) { bar(); baz() }

function f() { bar(); baz() }

class C {
foo() { bar(); baz() }

static { bar(); baz() }
}
```

#### beforeStatementContinuationChars
Expand Down
27 changes: 18 additions & 9 deletions lib/rules/semi.js
Expand Up @@ -306,22 +306,31 @@ module.exports = {
}

/**
* Checks a node to see if it's in a one-liner block statement.
* Checks a node to see if it's the last item in a one-liner block.
* Block is any `BlockStatement` or `StaticBlock` node. Block is a one-liner if its
* braces (and consequently everything between them) are on the same line.
* @param {ASTNode} node The node to check.
* @returns {boolean} whether the node is in a one-liner block statement.
* @returns {boolean} whether the node is the last item in a one-liner block.
*/
function isOneLinerBlock(node) {
function isLastInOneLinerBlock(node) {
const parent = node.parent;
const nextToken = sourceCode.getTokenAfter(node);

if (!nextToken || nextToken.value !== "}") {
return false;
}
return (
!!parent &&
parent.type === "BlockStatement" &&
parent.loc.start.line === parent.loc.end.line
);

if (parent.type === "BlockStatement") {
return parent.loc.start.line === parent.loc.end.line;
}

if (parent.type === "StaticBlock") {
const openingBrace = sourceCode.getFirstToken(parent, { skip: 1 }); // skip the `static` token

return openingBrace.loc.start.line === parent.loc.end.line;
}

return false;
}

/**
Expand All @@ -343,7 +352,7 @@ module.exports = {
report(node);
}
} else {
const oneLinerBlock = (exceptOneLine && isOneLinerBlock(node));
const oneLinerBlock = (exceptOneLine && isLastInOneLinerBlock(node));

if (isSemi && oneLinerBlock) {
report(node, true);
Expand Down