From d7a799eeddfaa32cfcd8c3699aa90a62a4fbeaa5 Mon Sep 17 00:00:00 2001 From: Arend van Beelen jr Date: Tue, 19 Sep 2023 16:59:02 +0200 Subject: [PATCH] Change default complexity (#337) --- .../nursery/no_excessive_complexity.rs | 19 ++++++++++++------- .../complexEventHandler.ts.snap | 2 +- .../src/configuration/linter/rules.rs | 2 +- editors/vscode/configuration_schema.json | 2 +- .../@biomejs/backend-jsonrpc/src/workspace.ts | 2 +- .../@biomejs/biome/configuration_schema.json | 2 +- .../src/content/docs/linter/rules/index.mdx | 2 +- .../linter/rules/no-excessive-complexity.md | 19 ++++++++++++------- 8 files changed, 30 insertions(+), 20 deletions(-) diff --git a/crates/biome_js_analyze/src/analyzers/nursery/no_excessive_complexity.rs b/crates/biome_js_analyze/src/analyzers/nursery/no_excessive_complexity.rs index 2378a7928e46..103adb918265 100644 --- a/crates/biome_js_analyze/src/analyzers/nursery/no_excessive_complexity.rs +++ b/crates/biome_js_analyze/src/analyzers/nursery/no_excessive_complexity.rs @@ -24,7 +24,7 @@ const MAX_FUNCTION_DEPTH: usize = 10; const MAX_SCORE: u8 = u8::MAX; declare_rule! { - /// Disallow functions that exceed a given complexity score. + /// Disallow functions that exceed a given Cognitive Complexity score. /// /// The more complexity a function contains, the harder it is to understand /// later on. @@ -34,7 +34,10 @@ declare_rule! { /// side-effects when making changes. /// /// This rule calculates a complexity score for every function and disallows - /// those that exceed a configured complexity threshold (default: 10). + /// those that exceed a configured complexity threshold (default: 15). + /// + /// The complexity score is calculated based on the Cognitive Complexity + /// algorithm: http://redirect.sonarsource.com/doc/cognitive-complexity.html /// /// Source: /// @@ -48,9 +51,11 @@ declare_rule! { /// function tooComplex() { /// for (let x = 0; x < 10; x++) { /// for (let y = 0; y < 10; y++) { - /// if (x % 2 === 0) { - /// if (y % 2 === 0) { - /// console.log(x > y ? `${x} > ${y}` : `${y} > ${x}`); + /// for (let z = 0; z < 10; z++) { + /// if (x % 2 === 0) { + /// if (y % 2 === 0) { + /// console.log(x > y ? `${x} > ${y}` : `${y} > ${x}`); + /// } /// } /// } /// } @@ -71,7 +76,7 @@ declare_rule! { /// } /// ``` /// - /// The allowed values range from 1 through 254. The default is 10. + /// The allowed values range from 1 through 254. The default is 15. /// pub(crate) NoExcessiveComplexity { version: "1.0.0", @@ -379,7 +384,7 @@ pub struct ComplexityOptions { impl Default for ComplexityOptions { fn default() -> Self { Self { - max_allowed_complexity: 10, + max_allowed_complexity: 15, } } } diff --git a/crates/biome_js_analyze/tests/specs/nursery/noExcessiveComplexity/complexEventHandler.ts.snap b/crates/biome_js_analyze/tests/specs/nursery/noExcessiveComplexity/complexEventHandler.ts.snap index 6bab782aeb28..e80bb7607c85 100644 --- a/crates/biome_js_analyze/tests/specs/nursery/noExcessiveComplexity/complexEventHandler.ts.snap +++ b/crates/biome_js_analyze/tests/specs/nursery/noExcessiveComplexity/complexEventHandler.ts.snap @@ -113,7 +113,7 @@ complexEventHandler.ts:1:10 lint/nursery/noExcessiveComplexity ━━━━━ 2 │ const state = getState(); 3 │ const focusedCell = selectFocusedCellOrSurrogate(state); - i Please refactor this function to reduce its complexity score from 26 to the max allowed complexity 10. + i Please refactor this function to reduce its complexity score from 26 to the max allowed complexity 15. ``` diff --git a/crates/biome_service/src/configuration/linter/rules.rs b/crates/biome_service/src/configuration/linter/rules.rs index 41561c06e9e5..b2137b9a56c7 100644 --- a/crates/biome_service/src/configuration/linter/rules.rs +++ b/crates/biome_service/src/configuration/linter/rules.rs @@ -2175,7 +2175,7 @@ pub struct Nursery { )] #[serde(skip_serializing_if = "Option::is_none")] pub no_duplicate_json_keys: Option, - #[doc = "Disallow functions that exceed a given complexity score."] + #[doc = "Disallow functions that exceed a given Cognitive Complexity score."] #[bpaf( long("no-excessive-complexity"), argument("on|off|warn"), diff --git a/editors/vscode/configuration_schema.json b/editors/vscode/configuration_schema.json index 9ac33a121dbd..20edce3c8c2e 100644 --- a/editors/vscode/configuration_schema.json +++ b/editors/vscode/configuration_schema.json @@ -985,7 +985,7 @@ ] }, "noExcessiveComplexity": { - "description": "Disallow functions that exceed a given complexity score.", + "description": "Disallow functions that exceed a given Cognitive Complexity score.", "anyOf": [ { "$ref": "#/definitions/RuleConfiguration" }, { "type": "null" } diff --git a/packages/@biomejs/backend-jsonrpc/src/workspace.ts b/packages/@biomejs/backend-jsonrpc/src/workspace.ts index f6db13ea3a3b..b20c7f2b2dda 100644 --- a/packages/@biomejs/backend-jsonrpc/src/workspace.ts +++ b/packages/@biomejs/backend-jsonrpc/src/workspace.ts @@ -662,7 +662,7 @@ export interface Nursery { */ noDuplicateJsonKeys?: RuleConfiguration; /** - * Disallow functions that exceed a given complexity score. + * Disallow functions that exceed a given Cognitive Complexity score. */ noExcessiveComplexity?: RuleConfiguration; /** diff --git a/packages/@biomejs/biome/configuration_schema.json b/packages/@biomejs/biome/configuration_schema.json index 9ac33a121dbd..20edce3c8c2e 100644 --- a/packages/@biomejs/biome/configuration_schema.json +++ b/packages/@biomejs/biome/configuration_schema.json @@ -985,7 +985,7 @@ ] }, "noExcessiveComplexity": { - "description": "Disallow functions that exceed a given complexity score.", + "description": "Disallow functions that exceed a given Cognitive Complexity score.", "anyOf": [ { "$ref": "#/definitions/RuleConfiguration" }, { "type": "null" } diff --git a/website/src/content/docs/linter/rules/index.mdx b/website/src/content/docs/linter/rules/index.mdx index c6ee0c11a00b..cc1be0146d13 100644 --- a/website/src/content/docs/linter/rules/index.mdx +++ b/website/src/content/docs/linter/rules/index.mdx @@ -345,7 +345,7 @@ Disallow void type outside of generic or return types. ### [noDuplicateJsonKeys](/linter/rules/no-duplicate-json-keys) Disallow two keys with the same name inside a JSON object. ### [noExcessiveComplexity](/linter/rules/no-excessive-complexity) -Disallow functions that exceed a given complexity score. +Disallow functions that exceed a given Cognitive Complexity score. ### [noFallthroughSwitchClause](/linter/rules/no-fallthrough-switch-clause) Disallow fallthrough of switch clauses. ### [noGlobalIsFinite](/linter/rules/no-global-is-finite) diff --git a/website/src/content/docs/linter/rules/no-excessive-complexity.md b/website/src/content/docs/linter/rules/no-excessive-complexity.md index 8efeec301fff..d64981a01ecb 100644 --- a/website/src/content/docs/linter/rules/no-excessive-complexity.md +++ b/website/src/content/docs/linter/rules/no-excessive-complexity.md @@ -7,7 +7,7 @@ title: noExcessiveComplexity (since v1.0.0) This rule is part of the [nursery](/linter/rules/#nursery) group. ::: -Disallow functions that exceed a given complexity score. +Disallow functions that exceed a given Cognitive Complexity score. The more complexity a function contains, the harder it is to understand later on. @@ -17,7 +17,10 @@ it easier to understand as well as by reducing chances of accidental side-effects when making changes. This rule calculates a complexity score for every function and disallows -those that exceed a configured complexity threshold (default: 10). +those that exceed a configured complexity threshold (default: 15). + +The complexity score is calculated based on the Cognitive Complexity +algorithm: http://redirect.sonarsource.com/doc/cognitive-complexity.html Source: @@ -31,9 +34,11 @@ Source: function tooComplex() { for (let x = 0; x < 10; x++) { for (let y = 0; y < 10; y++) { - if (x % 2 === 0) { - if (y % 2 === 0) { - console.log(x > y ? `${x} > ${y}` : `${y} > ${x}`); + for (let z = 0; z < 10; z++) { + if (x % 2 === 0) { + if (y % 2 === 0) { + console.log(x > y ? `${x} > ${y}` : `${y} > ${x}`); + } } } } @@ -50,7 +55,7 @@ function tooComplex() { 2 │ for (let x = 0; x < 10; x++) { 3 │ for (let y = 0; y < 10; y++) { - Please refactor this function to reduce its complexity score from 15 to the max allowed complexity 10. + Please refactor this function to reduce its complexity score from 21 to the max allowed complexity 15. @@ -67,7 +72,7 @@ Allows to specify the maximum allowed complexity. } ``` -The allowed values range from 1 through 254. The default is 10. +The allowed values range from 1 through 254. The default is 15. ## Related links