From 9322acd134dca48c6a9fe8ce2d01d3514148c76d Mon Sep 17 00:00:00 2001 From: Chris Grieser <73286100+chrisgrieser@users.noreply.github.com> Date: Sat, 21 Sep 2024 17:06:22 +0200 Subject: [PATCH 1/5] feat(lsp): `noExcessiveCognitiveComplexity` diagnostic shows levels --- .../src/lint/complexity/no_excessive_cognitive_complexity.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs b/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs index e7f0390a094a..9db7c6f936f0 100644 --- a/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs +++ b/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs @@ -117,7 +117,10 @@ impl Rule for NoExcessiveCognitiveComplexity { RuleDiagnostic::new( rule_category!(), range, - markup!("Excessive complexity detected."), + markup!({ + format!("Excessive complexity of {calculated_score} detected \ + (max: {max_allowed_complexity}).") + }), ) .note(if calculated_score == &MAX_SCORE { "Please refactor this function to reduce its complexity. \ From b1b0142aa25e6de6041bdbb42c6d2625c9a9df0b Mon Sep 17 00:00:00 2001 From: Chris Grieser <73286100+chrisgrieser@users.noreply.github.com> Date: Sat, 21 Sep 2024 17:24:24 +0200 Subject: [PATCH 2/5] style: fix formatting --- .../lint/complexity/no_excessive_cognitive_complexity.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs b/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs index 9db7c6f936f0..3afac6f48b1d 100644 --- a/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs +++ b/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs @@ -118,8 +118,10 @@ impl Rule for NoExcessiveCognitiveComplexity { rule_category!(), range, markup!({ - format!("Excessive complexity of {calculated_score} detected \ - (max: {max_allowed_complexity}).") + format!( + "Excessive complexity of {calculated_score} detected \ + (max: {max_allowed_complexity})." + ) }), ) .note(if calculated_score == &MAX_SCORE { From 4f3d11bea88db1161d2737f7bd025a990712d507 Mon Sep 17 00:00:00 2001 From: Chris Grieser <73286100+chrisgrieser@users.noreply.github.com> Date: Sat, 21 Sep 2024 17:48:42 +0200 Subject: [PATCH 3/5] test: update tests for `noExcessiveCognitiveComplexity` --- .../booleanOperators.js.snap | 2 +- .../booleanOperators2.js.snap | 2 +- .../complexEventHandler.ts.snap | 2 +- .../excessiveNesting.js.snap | 2 +- .../functionalChain.js.snap | 2 +- .../noExcessiveCognitiveComplexity/lambdas.js.snap | 8 ++++---- .../nestedControlFlowStructures.js.snap | 2 +- .../noExcessiveCognitiveComplexity/simpleBranches.js.snap | 2 +- .../simpleBranches2.js.snap | 2 +- .../noExcessiveCognitiveComplexity/sumOfPrimes.js.snap | 2 +- 10 files changed, 13 insertions(+), 13 deletions(-) diff --git a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/booleanOperators.js.snap b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/booleanOperators.js.snap index ae9cea1bd226..1d74ab1cf69b 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/booleanOperators.js.snap +++ b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/booleanOperators.js.snap @@ -19,7 +19,7 @@ function booleanOperators() { ``` booleanOperators.js:1:10 lint/complexity/noExcessiveCognitiveComplexity ━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Excessive complexity detected. + ! Excessive complexity of 4 detected (max: 3). > 1 │ function booleanOperators() { │ ^^^^^^^^^^^^^^^^ diff --git a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/booleanOperators2.js.snap b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/booleanOperators2.js.snap index 380d37c5b02c..71ea9028bb8c 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/booleanOperators2.js.snap +++ b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/booleanOperators2.js.snap @@ -18,7 +18,7 @@ function booleanOperators2() { ``` booleanOperators2.js:1:10 lint/complexity/noExcessiveCognitiveComplexity ━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Excessive complexity detected. + ! Excessive complexity of 3 detected (max: 2). > 1 │ function booleanOperators2() { │ ^^^^^^^^^^^^^^^^^ diff --git a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/complexEventHandler.ts.snap b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/complexEventHandler.ts.snap index 4829ee577752..92d327a1b8f7 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/complexEventHandler.ts.snap +++ b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/complexEventHandler.ts.snap @@ -106,7 +106,7 @@ function handleArrowDown(event: React.KeyboardEvent) { ``` complexEventHandler.ts:1:10 lint/complexity/noExcessiveCognitiveComplexity ━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Excessive complexity detected. + ! Excessive complexity of 26 detected (max: 15). > 1 │ function handleArrowDown(event: React.KeyboardEvent) { │ ^^^^^^^^^^^^^^^ diff --git a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/excessiveNesting.js.snap b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/excessiveNesting.js.snap index 1eb3e977552d..f0701dc5020c 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/excessiveNesting.js.snap +++ b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/excessiveNesting.js.snap @@ -38,7 +38,7 @@ function excessiveNesting() { ``` excessiveNesting.js:10:46 lint/complexity/noExcessiveCognitiveComplexity ━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Excessive complexity detected. + ! Excessive complexity of 255 detected (max: 15). 8 │ function nested7() { 9 │ function nested8() { diff --git a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/functionalChain.js.snap b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/functionalChain.js.snap index c8119dad3e59..013e5ab68271 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/functionalChain.js.snap +++ b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/functionalChain.js.snap @@ -25,7 +25,7 @@ function functionalChain(array) { ``` functionalChain.js:4:24 lint/complexity/noExcessiveCognitiveComplexity ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Excessive complexity detected. + ! Excessive complexity of 3 detected (max: 2). 2 │ return array 3 │ .filter(Boolean) diff --git a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/lambdas.js.snap b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/lambdas.js.snap index cf41bf10e1d4..4d46d0e9f22a 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/lambdas.js.snap +++ b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/lambdas.js.snap @@ -40,7 +40,7 @@ function lambdas(array) { ``` lambdas.js:4:26 lint/complexity/noExcessiveCognitiveComplexity ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Excessive complexity detected. + ! Excessive complexity of 4 detected (max: 3). 2 │ for (item of array) { // +1, nesting = 1 3 │ if (item) { // +2, nesting = 2 @@ -57,7 +57,7 @@ lambdas.js:4:26 lint/complexity/noExcessiveCognitiveComplexity ━━━━━ ``` lambdas.js:10:22 lint/complexity/noExcessiveCognitiveComplexity ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Excessive complexity detected. + ! Excessive complexity of 4 detected (max: 3). 8 │ }; 9 │ @@ -74,7 +74,7 @@ lambdas.js:10:22 lint/complexity/noExcessiveCognitiveComplexity ━━━━━ ``` lambdas.js:16:23 lint/complexity/noExcessiveCognitiveComplexity ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Excessive complexity detected. + ! Excessive complexity of 4 detected (max: 3). 14 │ } 15 │ @@ -91,7 +91,7 @@ lambdas.js:16:23 lint/complexity/noExcessiveCognitiveComplexity ━━━━━ ``` lambdas.js:22:32 lint/complexity/noExcessiveCognitiveComplexity ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Excessive complexity detected. + ! Excessive complexity of 4 detected (max: 3). 20 │ }; 21 │ diff --git a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/nestedControlFlowStructures.js.snap b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/nestedControlFlowStructures.js.snap index 96396fa7bd6b..ec35a51b2984 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/nestedControlFlowStructures.js.snap +++ b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/nestedControlFlowStructures.js.snap @@ -22,7 +22,7 @@ function nestedControlFlowStructures(num) { ``` nestedControlFlowStructures.js:1:10 lint/complexity/noExcessiveCognitiveComplexity ━━━━━━━━━━━━━━━━━ - ! Excessive complexity detected. + ! Excessive complexity of 9 detected (max: 8). > 1 │ function nestedControlFlowStructures(num) { │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/simpleBranches.js.snap b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/simpleBranches.js.snap index 3e8030df654f..98d39d751625 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/simpleBranches.js.snap +++ b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/simpleBranches.js.snap @@ -20,7 +20,7 @@ function simpleBranches() { ``` simpleBranches.js:1:10 lint/complexity/noExcessiveCognitiveComplexity ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Excessive complexity detected. + ! Excessive complexity of 3 detected (max: 2). > 1 │ function simpleBranches() { │ ^^^^^^^^^^^^^^ diff --git a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/simpleBranches2.js.snap b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/simpleBranches2.js.snap index 40141b1e4665..d7313ee6d656 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/simpleBranches2.js.snap +++ b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/simpleBranches2.js.snap @@ -18,7 +18,7 @@ function simpleBranches2() { ``` simpleBranches2.js:1:10 lint/complexity/noExcessiveCognitiveComplexity ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Excessive complexity detected. + ! Excessive complexity of 2 detected (max: 1). > 1 │ function simpleBranches2() { │ ^^^^^^^^^^^^^^^ diff --git a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/sumOfPrimes.js.snap b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/sumOfPrimes.js.snap index 649f6bd16bf7..524926bb007d 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/sumOfPrimes.js.snap +++ b/crates/biome_js_analyze/tests/specs/complexity/noExcessiveCognitiveComplexity/sumOfPrimes.js.snap @@ -23,7 +23,7 @@ function sumOfPrimes(max) { ``` sumOfPrimes.js:1:10 lint/complexity/noExcessiveCognitiveComplexity ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Excessive complexity detected. + ! Excessive complexity of 7 detected (max: 6). > 1 │ function sumOfPrimes(max) { │ ^^^^^^^^^^^ From 14ca7848f52e01abb35346a0ab5582ff4dfa04a5 Mon Sep 17 00:00:00 2001 From: Chris Grieser <73286100+chrisgrieser@users.noreply.github.com> Date: Sat, 21 Sep 2024 17:59:25 +0200 Subject: [PATCH 4/5] style: remove trailing space --- .../src/lint/complexity/no_excessive_cognitive_complexity.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs b/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs index 3afac6f48b1d..bdddcc8abdd5 100644 --- a/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs +++ b/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs @@ -121,7 +121,7 @@ impl Rule for NoExcessiveCognitiveComplexity { format!( "Excessive complexity of {calculated_score} detected \ (max: {max_allowed_complexity})." - ) + ) }), ) .note(if calculated_score == &MAX_SCORE { From b57824a2544979d8e61b6457967d297d1494bbce Mon Sep 17 00:00:00 2001 From: Chris Grieser <73286100+chrisgrieser@users.noreply.github.com> Date: Sat, 21 Sep 2024 18:16:26 +0200 Subject: [PATCH 5/5] style: remove another trailing space --- .../src/lint/complexity/no_excessive_cognitive_complexity.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs b/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs index bdddcc8abdd5..0d8041ab152e 100644 --- a/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs +++ b/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs @@ -117,7 +117,7 @@ impl Rule for NoExcessiveCognitiveComplexity { RuleDiagnostic::new( rule_category!(), range, - markup!({ + markup!({ format!( "Excessive complexity of {calculated_score} detected \ (max: {max_allowed_complexity})."