Skip to content

Commit 3530337

Browse files
authored
feat: update the indent rule for class static blocks (#15324)
Updates the `indent` rule to apply to class static blocks. Adds `StaticBlock.body` option that enforces indentation level for the body of a class static block. Refs #15016
1 parent f03cd14 commit 3530337

File tree

3 files changed

+882
-0
lines changed

3 files changed

+882
-0
lines changed

docs/rules/indent.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ This rule has an object option:
7979
* `"FunctionExpression"` takes an object to define rules for function expressions.
8080
* `parameters` (default: 1) enforces indentation level for parameters in a function expression. This can either be a number indicating indentation level, or the string `"first"` indicating that all parameters of the expression must be aligned with the first parameter. This can also be set to `"off"` to disable checking for FunctionExpression parameters.
8181
* `body` (default: 1) enforces indentation level for the body of a function expression.
82+
* `"StaticBlock"` takes an object to define rules for class static blocks.
83+
* `body` (default: 1) enforces indentation level for the body of a class static block.
8284
* `"CallExpression"` takes an object to define rules for function call expressions.
8385
* `arguments` (default: 1) enforces indentation level for arguments in a call expression. This can either be a number indicating indentation level, or the string `"first"` indicating that all arguments of the expression must be aligned with the first argument. This can also be set to `"off"` to disable checking for CallExpression arguments.
8486
* `"ArrayExpression"` (default: 1) enforces indentation level for elements in arrays. It can also be set to the string `"first"`, indicating that all the elements in the array should be aligned with the first element. This can also be set to `"off"` to disable checking for array elements.
@@ -484,6 +486,56 @@ var foo = function(bar, baz,
484486
}
485487
```
486488

489+
### StaticBlock
490+
491+
Examples of **incorrect** code for this rule with the `2, { "StaticBlock": {"body": 1} }` option:
492+
493+
```js
494+
/*eslint indent: ["error", 2, { "StaticBlock": {"body": 1} }]*/
495+
496+
class C {
497+
static {
498+
foo();
499+
}
500+
}
501+
```
502+
503+
Examples of **correct** code for this rule with the `2, { "StaticBlock": {"body": 1} }` option:
504+
505+
```js
506+
/*eslint indent: ["error", 2, { "StaticBlock": {"body": 1} }]*/
507+
508+
class C {
509+
static {
510+
foo();
511+
}
512+
}
513+
```
514+
515+
Examples of **incorrect** code for this rule with the `2, { "StaticBlock": {"body": 2} }` option:
516+
517+
```js
518+
/*eslint indent: ["error", 2, { "StaticBlock": {"body": 2} }]*/
519+
520+
class C {
521+
static {
522+
foo();
523+
}
524+
}
525+
```
526+
527+
Examples of **correct** code for this rule with the `2, { "StaticBlock": {"body": 2} }` option:
528+
529+
```js
530+
/*eslint indent: ["error", 2, { "StaticBlock": {"body": 2} }]*/
531+
532+
class C {
533+
static {
534+
foo();
535+
}
536+
}
537+
```
538+
487539
### CallExpression
488540

489541
Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option:

lib/rules/indent.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const KNOWN_NODES = new Set([
6868
"ReturnStatement",
6969
"SequenceExpression",
7070
"SpreadElement",
71+
"StaticBlock",
7172
"Super",
7273
"SwitchCase",
7374
"SwitchStatement",
@@ -583,6 +584,16 @@ module.exports = {
583584
},
584585
additionalProperties: false
585586
},
587+
StaticBlock: {
588+
type: "object",
589+
properties: {
590+
body: {
591+
type: "integer",
592+
minimum: 0
593+
}
594+
},
595+
additionalProperties: false
596+
},
586597
CallExpression: {
587598
type: "object",
588599
properties: {
@@ -646,6 +657,9 @@ module.exports = {
646657
parameters: DEFAULT_PARAMETER_INDENT,
647658
body: DEFAULT_FUNCTION_BODY_INDENT
648659
},
660+
StaticBlock: {
661+
body: DEFAULT_FUNCTION_BODY_INDENT
662+
},
649663
CallExpression: {
650664
arguments: DEFAULT_PARAMETER_INDENT
651665
},
@@ -1397,6 +1411,13 @@ module.exports = {
13971411
}
13981412
},
13991413

1414+
StaticBlock(node) {
1415+
const openingCurly = sourceCode.getFirstToken(node, { skip: 1 }); // skip the `static` token
1416+
const closingCurly = sourceCode.getLastToken(node);
1417+
1418+
addElementListIndent(node.body, openingCurly, closingCurly, options.StaticBlock.body);
1419+
},
1420+
14001421
SwitchStatement(node) {
14011422
const openingCurly = sourceCode.getTokenAfter(node.discriminant, astUtils.isOpeningBraceToken);
14021423
const closingCurly = sourceCode.getLastToken(node);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy