diff --git a/docs/rules/max-depth.md b/docs/rules/max-depth.md index b853d026713..ae8121e6930 100644 --- a/docs/rules/max-depth.md +++ b/docs/rules/max-depth.md @@ -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 @@ -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 @@ -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) diff --git a/lib/rules/max-depth.js b/lib/rules/max-depth.js index 415598b2929..bdacc66afb1 100644 --- a/lib/rules/max-depth.js +++ b/lib/rules/max-depth.js @@ -118,6 +118,7 @@ module.exports = { FunctionDeclaration: startFunction, FunctionExpression: startFunction, ArrowFunctionExpression: startFunction, + StaticBlock: startFunction, IfStatement(node) { if (node.parent.type !== "IfStatement") { @@ -146,6 +147,7 @@ module.exports = { "FunctionDeclaration:exit": endFunction, "FunctionExpression:exit": endFunction, "ArrowFunctionExpression:exit": endFunction, + "StaticBlock:exit": endFunction, "Program:exit": endFunction }; diff --git a/tests/lib/rules/max-depth.js b/tests/lib/rules/max-depth.js index 0b4f846dd49..b35b60301c5 100644 --- a/tests/lib/rules/max-depth.js +++ b/tests/lib/rules/max-depth.js @@ -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" }] }, @@ -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 + }] + } ] });