Skip to content

Commit 0f0971f

Browse files
authored
feat: update semi rule for class static blocks (#15286)
Updates `omitLastInOneLineBlock` option to apply to class static blocks. Refs #15016
1 parent a2e6328 commit 0f0971f

File tree

3 files changed

+395
-11
lines changed

3 files changed

+395
-11
lines changed

docs/rules/semi.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ Examples of additional **correct** code for this rule with the `"always", { "omi
172172
if (foo) { bar() }
173173

174174
if (foo) { bar(); baz() }
175+
176+
function f() { bar(); baz() }
177+
178+
class C {
179+
foo() { bar(); baz() }
180+
181+
static { bar(); baz() }
182+
}
175183
```
176184

177185
#### beforeStatementContinuationChars

lib/rules/semi.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -306,22 +306,31 @@ module.exports = {
306306
}
307307

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

317319
if (!nextToken || nextToken.value !== "}") {
318320
return false;
319321
}
320-
return (
321-
!!parent &&
322-
parent.type === "BlockStatement" &&
323-
parent.loc.start.line === parent.loc.end.line
324-
);
322+
323+
if (parent.type === "BlockStatement") {
324+
return parent.loc.start.line === parent.loc.end.line;
325+
}
326+
327+
if (parent.type === "StaticBlock") {
328+
const openingBrace = sourceCode.getFirstToken(parent, { skip: 1 }); // skip the `static` token
329+
330+
return openingBrace.loc.start.line === parent.loc.end.line;
331+
}
332+
333+
return false;
325334
}
326335

327336
/**
@@ -343,7 +352,7 @@ module.exports = {
343352
report(node);
344353
}
345354
} else {
346-
const oneLinerBlock = (exceptOneLine && isOneLinerBlock(node));
355+
const oneLinerBlock = (exceptOneLine && isLastInOneLinerBlock(node));
347356

348357
if (isSemi && oneLinerBlock) {
349358
report(node, true);

0 commit comments

Comments
 (0)