@konvert7/klint 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -733,6 +733,7 @@ doc-comments alongside `/** */`.
733
733
  | `no-string-match` | `String#match()` usage better expressed with clearer APIs |
734
734
  | `blank-line-before-control-flow` | Blank line above a control-flow statement that follows another statement; autofix |
735
735
  | `blank-line-after-block` | Blank line below a block statement when another statement follows; autofix |
736
+ | `cognitive-complexity` | Function whose cognitive complexity (Biome / SonarSource model) is above `max`, in all five languages; opt-in, no autofix |
736
737
 
737
738
  Rules that needed TypeScript's type checker were removed in 1.0; see [Upgrading from klint 0.x](#upgrading-from-klint-0x).
738
739
 
@@ -782,6 +783,62 @@ rules:
782
783
 
783
784
  **Not checked.** A file with a syntax error gets no report and no fix. A braceless body (`if (a) return 1;`) is not a block for the after rule. `do…while` and `repeat…while` are never reported by the after rule. A statement that shares its line with the previous statement is not reported, because the fix would have to split that line. Expression forms are not statements: `let x = if …` and `let r = match …` in Rust, `let x = if …` in Swift, a C# switch expression, and control flow inside a call are never reported. A Python docstring is documentation, not a statement, so the statement after it counts as first in its block. A C# preprocessor line (`#region`, `#if`, `#pragma`, …) or a Swift `#if`/`#elseif`/`#else`/`#endif` line separates statements like a blank line, so neither rule inserts a line next to one.
784
785
 
786
+ ### Cognitive complexity
787
+
788
+ `cognitive-complexity` reports every function whose cognitive complexity is above `max`, in TypeScript/JavaScript, Python, Swift, Rust and C#. It is opt-in and has no fix. It uses Biome 2.6.5's `noExcessiveCognitiveComplexity` scoring (the SonarSource Cognitive Complexity model, G. Ann Campbell 2017) for every language, so a TypeScript function gets the same score as in Biome.
789
+
790
+ ```yaml
791
+ rules:
792
+ cognitive-complexity: error
793
+ ```
794
+
795
+ ```yaml
796
+ rules:
797
+ cognitive-complexity:
798
+ severity: warn
799
+ include: ["src/**", "!src/generated/**"]
800
+ max: 14
801
+ ```
802
+
803
+ - `max` is an integer of at least 1, default 15. A function is reported when its score is **greater than** `max`. Any other value (0, a negative number, a fraction, a string) gives one `klint/config` warning naming the rule and the value, and the rule uses 15.
804
+ - The message has the form ``Function `big` has cognitive complexity 16 (max 15) — split it into smaller functions.``. The name is the declared name. For an anonymous function assigned to a variable or property it is that name. Otherwise it is `<anonymous>`. The line is the function's first line.
805
+ - Each function starts at 0. Top-level code is not scored.
806
+
807
+ **Scoring.** "Nesting" is how deep a construct sits: each enclosing `if`, loop, `switch`, `catch`, `guard`, `let … else` body and each enclosing function adds one.
808
+
809
+ | Increment | TypeScript / JavaScript | Python | Swift | Rust | C# |
810
+ |---|---|---|---|---|---|
811
+ | +1 plus nesting: `if`, and `else if` (which also gets the nesting) | `if`, `else if` | `if`, `elif` | `if`, `else if` | `if`, `if let`, `else if` | `if`, `else if` |
812
+ | +1 plus nesting: ternary | `?:` | `x if c else y` | `?:` | — | `?:` |
813
+ | +1 plus nesting: switch / match | `switch` | `match` | `switch` | `match` | `switch` statement, `switch` expression |
814
+ | +1 plus nesting: loops | `for`, `for…in`, `for…of`, `while`, `do…while` | `for`, `while` | `for`, `while`, `repeat…while` | `for`, `while`, `while let`, `loop` | `for`, `foreach`, `while`, `do` |
815
+ | +1 plus nesting: `catch` | `catch` | `except` | `catch` | — | `catch` |
816
+ | +1 plus nesting: `guard`, `let … else` (its `else` body is nested) | — | — | `guard` | `let … else` | — |
817
+ | +1, no nesting: `finally` (its body is not nested) | `finally` | `finally` | — | — | `finally` |
818
+ | nesting only: nested function (scored on its own, see below) | arrow, function expression, function declaration, method | `lambda`, `def` | closure | closure | lambda, local function, anonymous method |
819
+ | +1, no nesting: plain `else` (not `else if`/`elif`) | `else` | `else` of `if`, `try`, `for`, `while` | `else` | `else` | `else` |
820
+ | +1: labeled `break` / `continue`, `goto` | `break label`, `continue label` | — | `break label`, `continue label` | `break 'label`, `continue 'label` | `goto` |
821
+ | +1 per logical operator, unless its direct parent is the same operator | `&&`, `\|\|`, `??` | `and`, `or` | `&&`, `\|\|`, `??` | `&&`, `\|\|` | `&&`, `\|\|`, `??`, pattern `and`, `or` |
822
+ | +1: comprehension `if` | — | comprehension `if` | — | — | — |
823
+ | +1: guard on a case or arm | — | — | `case … where` | match-arm guard | `when` clause |
824
+
825
+ Each logical operator scores +1, unless its direct parent is a logical operator of the same kind. Parentheses break that link, so a parenthesised group scores on its own: `a && b || c` is 2, `a && (b || c) && a` is 2, `a && (b && c)` is 2, and `a ?? b ?? c` is 1. Negation (`!`, `not`) adds nothing.
826
+
827
+ It adds nothing for `try` itself, `return`, `throw`, recursion, Rust `?`, Python `with`, Swift `do` without `catch`, an `if let` or `while let` beyond the `if` or `while`, optional chaining `?.`, assignment operators `||=`, `&&=`, `??=`, and a Rust `let` without `else`.
828
+
829
+ **Differences from the Sonar paper.** This rule follows Biome, which differs from the paper in these places:
830
+
831
+ - `else if` gets the nesting increment.
832
+ - `finally` adds one and does not nest its body.
833
+ - `??` counts as a boolean operator.
834
+ - A logical operator is scored unless its direct parent is the same operator, so `a && (b && c)` is 2, not 1.
835
+ - Recursion adds nothing.
836
+ - A nested function (closure, lambda, arrow, local function) is scored and reported on its own. It starts at its enclosing nesting level plus one and does not add to the outer function.
837
+
838
+ **What counts as a function.** TypeScript/JavaScript: `function` declarations and expressions, arrow functions, methods (including getters and setters), generator functions. Python: `def` and `lambda`. Swift: `func`, `init`, `deinit`, closures, computed getters and setters. Rust: `fn` items and closures. C#: methods, constructors, destructors, local functions, lambdas, anonymous methods, property and indexer accessors, operators and conversion operators.
839
+
840
+ **Not checked.** Rust macro bodies are opaque token trees, so code inside a macro call is not scored. Code inside a Python decorator does not count toward the decorated function; a lambda there is scored on its own. A file with a syntax error gets no diagnostic. A Swift computed property without an explicit `get` is not a function. Bodyless declarations are never reported: TypeScript method signatures, Swift protocol requirements and Rust trait signatures are not functions at all, and C# interface and abstract members have nothing to score, so they score 0, which can never exceed `max`.
841
+
785
842
  ## Plugins
786
843
 
787
844
  Enable plugin rules with `plugins: [sonar]`, then override individual severities in `rules` when needed.
package/klint.schema.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "$id": "https://raw.githubusercontent.com/konvert7/klint/refs/tags/v1.1.0/klint.schema.json",
2
+ "$id": "https://raw.githubusercontent.com/konvert7/klint/refs/tags/v1.2.0/klint.schema.json",
3
3
  "$schema": "http://json-schema.org/draft-07/schema#",
4
4
  "additionalProperties": false,
5
5
  "description": "klint configuration. Lives at klint.yaml (or klint.config.json) next to biome.json and knip.json.",
@@ -725,6 +725,55 @@
725
725
  "blank-line-before-control-flow:\n severity: error\n include: [\"src/**\", \"!src/generated/**\"]\n statements: [if, for, while, switch, try, return, guard, loop, with]\n allowSetup: true"
726
726
  ]
727
727
  },
728
+ "cognitive-complexity": {
729
+ "anyOf": [
730
+ {
731
+ "description": "Rule severity. \"error\" exits with code 2; \"warn\" reports but exits 0; \"off\" silences.",
732
+ "enum": [
733
+ "error",
734
+ "warn",
735
+ "off"
736
+ ],
737
+ "type": "string"
738
+ },
739
+ {
740
+ "additionalProperties": false,
741
+ "description": "Rule options object. Omit severity to default to \"error\". Add include to scope the rule to specific files.",
742
+ "properties": {
743
+ "include": {
744
+ "description": "Glob patterns scoping this rule to a subset of files. Prefix with ! to exclude. Example: [\"src/hooks/**\", \"!src/hooks/scripts/**\"]",
745
+ "items": {
746
+ "type": "string"
747
+ },
748
+ "type": "array"
749
+ },
750
+ "max": {
751
+ "default": 15,
752
+ "description": "Highest cognitive complexity a function may have; a function scoring above it is reported. An integer of at least 1. Defaults to 15.",
753
+ "format": "uint32",
754
+ "minimum": 1,
755
+ "type": "integer"
756
+ },
757
+ "severity": {
758
+ "description": "Rule severity. \"error\" exits with code 2; \"warn\" reports but exits 0; \"off\" silences.",
759
+ "enum": [
760
+ "error",
761
+ "warn",
762
+ "off",
763
+ null
764
+ ],
765
+ "type": "string"
766
+ }
767
+ },
768
+ "type": "object"
769
+ }
770
+ ],
771
+ "description": "Reports every function whose cognitive complexity is above `max` (default 15), scored like Biome 2.6.5's `noExcessiveCognitiveComplexity` (the SonarSource Cognitive Complexity model). Works on TypeScript/JavaScript, Python, Swift, Rust and C#. Opt-in; no autofix.",
772
+ "examples": [
773
+ "cognitive-complexity: error",
774
+ "cognitive-complexity:\n severity: warn\n include: [\"src/**\", \"!src/generated/**\"]\n max: 14"
775
+ ]
776
+ },
728
777
  "no-async-predicate": {
729
778
  "anyOf": [
730
779
  {
package/klint.schema.yaml CHANGED
@@ -1,4 +1,4 @@
1
- $id: https://raw.githubusercontent.com/konvert7/klint/refs/tags/v1.1.0/klint.schema.json
1
+ $id: https://raw.githubusercontent.com/konvert7/klint/refs/tags/v1.2.0/klint.schema.json
2
2
  $schema: http://json-schema.org/draft-07/schema#
3
3
  additionalProperties: false
4
4
  description: klint configuration. Lives at klint.yaml (or klint.config.json) next to biome.json and knip.json.
@@ -552,6 +552,45 @@ properties:
552
552
  include: ["src/**", "!src/generated/**"]
553
553
  statements: [if, for, while, switch, try, return, guard, loop, with]
554
554
  allowSetup: true
555
+ cognitive-complexity:
556
+ anyOf:
557
+ - description: Rule severity. "error" exits with code 2; "warn" reports but exits 0; "off" silences.
558
+ enum:
559
+ - error
560
+ - warn
561
+ - off
562
+ type: string
563
+ - additionalProperties: false
564
+ description: Rule options object. Omit severity to default to "error". Add include to scope the rule to specific files.
565
+ properties:
566
+ include:
567
+ description: 'Glob patterns scoping this rule to a subset of files. Prefix with ! to exclude. Example: ["src/hooks/**", "!src/hooks/scripts/**"]'
568
+ items:
569
+ type: string
570
+ type: array
571
+ max:
572
+ default: 15
573
+ description: Highest cognitive complexity a function may have; a function scoring above it is reported. An integer of at least 1. Defaults to 15.
574
+ format: uint32
575
+ minimum: 1
576
+ type: integer
577
+ severity:
578
+ description: Rule severity. "error" exits with code 2; "warn" reports but exits 0; "off" silences.
579
+ enum:
580
+ - error
581
+ - warn
582
+ - off
583
+ - null
584
+ type: string
585
+ type: object
586
+ description: Reports every function whose cognitive complexity is above `max` (default 15), scored like Biome 2.6.5's `noExcessiveCognitiveComplexity` (the SonarSource Cognitive Complexity model). Works on TypeScript/JavaScript, Python, Swift, Rust and C#. Opt-in; no autofix.
587
+ examples:
588
+ - 'cognitive-complexity: error'
589
+ - |-
590
+ cognitive-complexity:
591
+ severity: warn
592
+ include: ["src/**", "!src/generated/**"]
593
+ max: 14
555
594
  no-async-predicate:
556
595
  anyOf:
557
596
  - description: Rule severity. "error" exits with code 2; "warn" reports but exits 0; "off" silences.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konvert7/klint",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Architecture-as-Code linter for TypeScript. Enforce layer boundaries, import rules, and singleton patterns in YAML.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -56,12 +56,12 @@
56
56
  ]
57
57
  },
58
58
  "optionalDependencies": {
59
- "@konvert7/klint-darwin-arm64": "1.1.0",
60
- "@konvert7/klint-darwin-x64": "1.1.0",
61
- "@konvert7/klint-linux-arm64": "1.1.0",
62
- "@konvert7/klint-linux-x64": "1.1.0",
63
- "@konvert7/klint-linux-x64-musl": "1.1.0",
64
- "@konvert7/klint-win32-x64": "1.1.0"
59
+ "@konvert7/klint-darwin-arm64": "1.2.0",
60
+ "@konvert7/klint-darwin-x64": "1.2.0",
61
+ "@konvert7/klint-linux-arm64": "1.2.0",
62
+ "@konvert7/klint-linux-x64": "1.2.0",
63
+ "@konvert7/klint-linux-x64-musl": "1.2.0",
64
+ "@konvert7/klint-win32-x64": "1.2.0"
65
65
  },
66
66
  "devDependencies": {
67
67
  "@biomejs/biome": "2.6.5",