Skip to content

Commit

Permalink
feat: update the indent rule for class static blocks (#15324)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mdjermanovic committed Nov 19, 2021
1 parent f03cd14 commit 3530337
Show file tree
Hide file tree
Showing 3 changed files with 882 additions and 0 deletions.
52 changes: 52 additions & 0 deletions docs/rules/indent.md
Expand Up @@ -79,6 +79,8 @@ This rule has an object option:
* `"FunctionExpression"` takes an object to define rules for function expressions.
* `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.
* `body` (default: 1) enforces indentation level for the body of a function expression.
* `"StaticBlock"` takes an object to define rules for class static blocks.
* `body` (default: 1) enforces indentation level for the body of a class static block.
* `"CallExpression"` takes an object to define rules for function call expressions.
* `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.
* `"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.
Expand Down Expand Up @@ -484,6 +486,56 @@ var foo = function(bar, baz,
}
```

### StaticBlock

Examples of **incorrect** code for this rule with the `2, { "StaticBlock": {"body": 1} }` option:

```js
/*eslint indent: ["error", 2, { "StaticBlock": {"body": 1} }]*/

class C {
static {
foo();
}
}
```

Examples of **correct** code for this rule with the `2, { "StaticBlock": {"body": 1} }` option:

```js
/*eslint indent: ["error", 2, { "StaticBlock": {"body": 1} }]*/

class C {
static {
foo();
}
}
```

Examples of **incorrect** code for this rule with the `2, { "StaticBlock": {"body": 2} }` option:

```js
/*eslint indent: ["error", 2, { "StaticBlock": {"body": 2} }]*/

class C {
static {
foo();
}
}
```

Examples of **correct** code for this rule with the `2, { "StaticBlock": {"body": 2} }` option:

```js
/*eslint indent: ["error", 2, { "StaticBlock": {"body": 2} }]*/

class C {
static {
foo();
}
}
```

### CallExpression

Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option:
Expand Down
21 changes: 21 additions & 0 deletions lib/rules/indent.js
Expand Up @@ -68,6 +68,7 @@ const KNOWN_NODES = new Set([
"ReturnStatement",
"SequenceExpression",
"SpreadElement",
"StaticBlock",
"Super",
"SwitchCase",
"SwitchStatement",
Expand Down Expand Up @@ -583,6 +584,16 @@ module.exports = {
},
additionalProperties: false
},
StaticBlock: {
type: "object",
properties: {
body: {
type: "integer",
minimum: 0
}
},
additionalProperties: false
},
CallExpression: {
type: "object",
properties: {
Expand Down Expand Up @@ -646,6 +657,9 @@ module.exports = {
parameters: DEFAULT_PARAMETER_INDENT,
body: DEFAULT_FUNCTION_BODY_INDENT
},
StaticBlock: {
body: DEFAULT_FUNCTION_BODY_INDENT
},
CallExpression: {
arguments: DEFAULT_PARAMETER_INDENT
},
Expand Down Expand Up @@ -1397,6 +1411,13 @@ module.exports = {
}
},

StaticBlock(node) {
const openingCurly = sourceCode.getFirstToken(node, { skip: 1 }); // skip the `static` token
const closingCurly = sourceCode.getLastToken(node);

addElementListIndent(node.body, openingCurly, closingCurly, options.StaticBlock.body);
},

SwitchStatement(node) {
const openingCurly = sourceCode.getTokenAfter(node.discriminant, astUtils.isOpeningBraceToken);
const closingCurly = sourceCode.getLastToken(node);
Expand Down

0 comments on commit 3530337

Please sign in to comment.