Skip to content

Commit

Permalink
Change default complexity (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr authored Sep 19, 2023
1 parent 9bc8a92 commit d7a799e
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
///
Expand All @@ -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}`);
/// }
/// }
/// }
/// }
Expand All @@ -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",
Expand Down Expand Up @@ -379,7 +384,7 @@ pub struct ComplexityOptions {
impl Default for ComplexityOptions {
fn default() -> Self {
Self {
max_allowed_complexity: 10,
max_allowed_complexity: 15,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ complexEventHandler.ts:1:10 lint/nursery/noExcessiveComplexity ━━━━━
2const state = getState();
3const 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.


```
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_service/src/configuration/linter/rules.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion editors/vscode/configuration_schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/@biomejs/backend-jsonrpc/src/workspace.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/@biomejs/biome/configuration_schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion website/src/content/docs/linter/rules/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ Disallow <code>void</code> 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 <code>switch</code> clauses.
### [noGlobalIsFinite](/linter/rules/no-global-is-finite)
Expand Down
19 changes: 12 additions & 7 deletions website/src/content/docs/linter/rules/no-excessive-complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:

Expand All @@ -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}`);
}
}
}
}
Expand All @@ -50,7 +55,7 @@ function tooComplex() {
<strong>2 │ </strong> for (let x = 0; x &lt; 10; x++) {
<strong>3 │ </strong> for (let y = 0; y &lt; 10; y++) {

<strong><span style="color: rgb(38, 148, 255);"> </span></strong><strong><span style="color: rgb(38, 148, 255);">ℹ</span></strong> <span style="color: rgb(38, 148, 255);">Please refactor this function to reduce its complexity score from 15 to the max allowed complexity 10.</span>
<strong><span style="color: rgb(38, 148, 255);"> </span></strong><strong><span style="color: rgb(38, 148, 255);">ℹ</span></strong> <span style="color: rgb(38, 148, 255);">Please refactor this function to reduce its complexity score from 21 to the max allowed complexity 15.</span>

</code></pre>

Expand All @@ -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

Expand Down

0 comments on commit d7a799e

Please sign in to comment.