Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: update max-depth for class static blocks (#15316)
Updates the `max-depth` rule to treat class static blocks as separate contexts when calculating depth.

Refs #15016
  • Loading branch information
mdjermanovic committed Nov 21, 2021
1 parent 6487df3 commit b171cd7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 4 deletions.
44 changes: 42 additions & 2 deletions docs/rules/max-depth.md
Expand Up @@ -20,7 +20,6 @@ Examples of **incorrect** code for this rule with the default `{ "max": 4 }` opt

```js
/*eslint max-depth: ["error", 4]*/
/*eslint-env es6*/

function foo() {
for (;;) { // Nested 1 deep
Expand All @@ -40,7 +39,6 @@ Examples of **correct** code for this rule with the default `{ "max": 4 }` optio

```js
/*eslint max-depth: ["error", 4]*/
/*eslint-env es6*/

function foo() {
for (;;) { // Nested 1 deep
Expand All @@ -54,6 +52,48 @@ function foo() {
}
```

Note that class static blocks do not count as nested blocks, and that the depth in them is calculated separately from the enclosing context.

Examples of **incorrect** code for this rule with `{ "max": 2 }` option:

```js
/*eslint max-depth: ["error", 2]*/

function foo() {
if (true) { // Nested 1 deep
class C {
static {
if (true) { // Nested 1 deep
if (true) { // Nested 2 deep
if (true) { // Nested 3 deep
}
}
}
}
}
}
}
```

Examples of **correct** code for this rule with `{ "max": 2 }` option:

```js
/*eslint max-depth: ["error", 2]*/

function foo() {
if (true) { // Nested 1 deep
class C {
static {
if (true) { // Nested 1 deep
if (true) { // Nested 2 deep
}
}
}
}
}
}
```

## Related Rules

* [complexity](complexity.md)
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/max-depth.js
Expand Up @@ -118,6 +118,7 @@ module.exports = {
FunctionDeclaration: startFunction,
FunctionExpression: startFunction,
ArrowFunctionExpression: startFunction,
StaticBlock: startFunction,

IfStatement(node) {
if (node.parent.type !== "IfStatement") {
Expand Down Expand Up @@ -146,6 +147,7 @@ module.exports = {
"FunctionDeclaration:exit": endFunction,
"FunctionExpression:exit": endFunction,
"ArrowFunctionExpression:exit": endFunction,
"StaticBlock:exit": endFunction,
"Program:exit": endFunction
};

Expand Down
60 changes: 58 additions & 2 deletions tests/lib/rules/max-depth.js
Expand Up @@ -26,7 +26,18 @@ ruleTester.run("max-depth", rule, {
"function foo() { if (true) { if (false) { if (true) { } } } }",

// object property options
{ code: "function foo() { if (true) { if (false) { if (true) { } } } }", options: [{ max: 3 }] }
{ code: "function foo() { if (true) { if (false) { if (true) { } } } }", options: [{ max: 3 }] },

{ code: "class C { static { if (1) { if (2) {} } } }", options: [2], parserOptions: { ecmaVersion: 2022 } },
{ code: "class C { static { if (1) { if (2) {} } if (1) { if (2) {} } } }", options: [2], parserOptions: { ecmaVersion: 2022 } },
{ code: "class C { static { if (1) { if (2) {} } } static { if (1) { if (2) {} } } }", options: [2], parserOptions: { ecmaVersion: 2022 } },
{ code: "if (1) { class C { static { if (1) { if (2) {} } } } }", options: [2], parserOptions: { ecmaVersion: 2022 } },
{ code: "function foo() { if (1) { class C { static { if (1) { if (2) {} } } } } }", options: [2], parserOptions: { ecmaVersion: 2022 } },
{
code: "function foo() { if (1) { if (2) { class C { static { if (1) { if (2) {} } if (1) { if (2) {} } } } } } if (1) { if (2) {} } }",
options: [2],
parserOptions: { ecmaVersion: 2022 }
}
],
invalid: [
{ code: "function foo() { if (true) { if (false) { if (true) { } } } }", options: [2], errors: [{ messageId: "tooDeeply", data: { depth: 3, maxDepth: 2 }, type: "IfStatement" }] },
Expand All @@ -41,6 +52,51 @@ ruleTester.run("max-depth", rule, {
{ code: "function foo() { if (true) { if (false) { if (true) { } } } }", options: [{ max: 2 }], errors: [{ messageId: "tooDeeply", data: { depth: 3, maxDepth: 2 }, type: "IfStatement" }] },

{ code: "function foo() { if (a) { if (b) { if (c) { if (d) { if (e) {} } } } } }", options: [{}], errors: [{ messageId: "tooDeeply", data: { depth: 5, maxDepth: 4 } }] },
{ code: "function foo() { if (true) {} }", options: [{ max: 0 }], errors: [{ messageId: "tooDeeply", data: { depth: 1, maxDepth: 0 } }] }
{ code: "function foo() { if (true) {} }", options: [{ max: 0 }], errors: [{ messageId: "tooDeeply", data: { depth: 1, maxDepth: 0 } }] },

{
code: "class C { static { if (1) { if (2) { if (3) {} } } } }",
options: [2],
parserOptions: { ecmaVersion: 2022 },
errors: [{
messageId: "tooDeeply",
data: { depth: 3, maxDepth: 2 },
line: 1,
column: 38
}]
},
{
code: "if (1) { class C { static { if (1) { if (2) { if (3) {} } } } } }",
options: [2],
parserOptions: { ecmaVersion: 2022 },
errors: [{
messageId: "tooDeeply",
data: { depth: 3, maxDepth: 2 },
line: 1,
column: 47
}]
},
{
code: "function foo() { if (1) { class C { static { if (1) { if (2) { if (3) {} } } } } } }",
options: [2],
parserOptions: { ecmaVersion: 2022 },
errors: [{
messageId: "tooDeeply",
data: { depth: 3, maxDepth: 2 },
line: 1,
column: 64
}]
},
{
code: "function foo() { if (1) { class C { static { if (1) { if (2) {} } } } if (2) { if (3) {} } } }",
options: [2],
parserOptions: { ecmaVersion: 2022 },
errors: [{
messageId: "tooDeeply",
data: { depth: 3, maxDepth: 2 },
line: 1,
column: 80
}]
}
]
});

0 comments on commit b171cd7

Please sign in to comment.