Repository navigation
Commit dff1be1
[leancode_lint] Add cognitive_complexity (#581)
* [leancode_lint] Add cognitive_complexity
Reports functions, methods, constructors and closures whose cognitive
complexity is over LeanCodeLintConfig.cognitiveComplexity.maximum (15 by
default), scored like Biome's noExcessiveCognitiveComplexity.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X1sawdgkJ3Snt4Ug7QGZEr
* [leancode_lint] Make cognitive_complexity opt-in
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X1sawdgkJ3Snt4Ug7QGZEr
* [leancode_lint] Nest only the first level of functions in cognitive_complexity
As SonarJS does, so a test closure inside group inside main is not penalized
for the test framework's structure.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X1sawdgkJ3Snt4Ug7QGZEr
* [leancode_lint] Let a collection if cost nothing in cognitive_complexity
A conditional child in a widget list reads like JSX's short-circuit, which
SonarJS ignores. Also pin that null-aware operators cost nothing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X1sawdgkJ3Snt4Ug7QGZEr
* [leancode_lint] Tighten cognitive_complexity's messages, docs and tests
The message names what was detected and the correction says what to do, the
README entry is shorter, and the tests use range markers like the others.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X1sawdgkJ3Snt4Ug7QGZEr
* [leancode_lint] Document how cognitive_complexity scores
The rule's doc comment holds the full scoring table and why each deviation
from SonarSource's paper exists; the README links to it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X1sawdgkJ3Snt4Ug7QGZEr
* [leancode_lint] Unit-test cognitive_complexity's scoring
Scoring is split from reporting so it can be tested without a lint rule, and
a constructor's initializer list is no longer added to its body's score. Also
fix the scoring table row the alignment broke.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X1sawdgkJ3Snt4Ug7QGZEr
* [leancode_lint] Report a closure's cognitive complexity at its parameters
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X1sawdgkJ3Snt4Ug7QGZEr
* [leancode_lint] Keep cognitive_complexity on analyzer 13
ConstructorDeclaration.errorRange is newer than analyzer 13.0.0, the lowest
version allowed, so pana's downgrade analysis failed; the range is now built
from the constructor's name. Also make ScoredFunction a class and realign the
scoring table.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X1sawdgkJ3Snt4Ug7QGZEr
* [leancode_lint] Score primary constructors in cognitive_complexity
A primary constructor's body is a PrimaryConstructorBody, not a
ConstructorDeclaration, so it was never scored. It is now reported at its
`this`, without its initializers, like other constructors.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X1sawdgkJ3Snt4Ug7QGZEr
* [leancode_lint] Report a constructor without its type name at its keyword
`new named`, `new` and `factory make` were reported at the name alone, or at
the parameters when there was no name.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X1sawdgkJ3Snt4Ug7QGZEr
* [leancode_lint] Use errorRange for constructors in cognitive_complexity
ConstructorDeclaration.errorRange covers a constructor without its type name
too, and exists since analyzer 13.1.0, so require that instead of rebuilding
the range by hand for 13.0.0.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X1sawdgkJ3Snt4Ug7QGZEr
* [leancode_lint] Use shorthand range markers in single-diagnostic tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X1sawdgkJ3Snt4Ug7QGZEr
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 5403466 commit dff1be1
8 files changed
Lines changed: 680 additions & 1 deletion
File tree
- packages/leancode_lint
- lib
- src/lints
- test/test_cases
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
595 | 595 | | |
596 | 596 | | |
597 | 597 | | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
598 | 645 | | |
599 | 646 | | |
600 | 647 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
20 | 23 | | |
21 | 24 | | |
22 | 25 | | |
| |||
34 | 37 | | |
35 | 38 | | |
36 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
37 | 46 | | |
38 | 47 | | |
39 | 48 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
65 | 69 | | |
66 | 70 | | |
67 | 71 | | |
| |||
0 commit comments