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 the indent rule for class static blocks #15324

Merged
merged 1 commit into from Nov 19, 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
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