energy-state-analyzer 0.22.0 → 0.23.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/CHANGELOG.md +10 -1
- package/README.md +12 -6
- package/dist/cli.js +1 -1
- package/docs/architecture.md +4 -0
- package/docs/cli.md +2 -1
- package/docs/detectors/README.md +6 -0
- package/docs/detectors/cognitive-complexity.md +2 -2
- package/docs/detectors/cyclomatic-complexity.md +1 -2
- package/docs/detectors/file-coherence.md +3 -0
- package/docs/detectors/inversion-opportunities.md +1 -1
- package/docs/detectors/match-opportunities.md +1 -1
- package/docs/detectors/parameter-explosion.md +4 -3
- package/docs/detectors/parse-dont-validate.md +48 -2
- package/docs/detectors/primitive-obsession.md +2 -2
- package/grammars/tree-sitter-rust.LICENSE +21 -0
- package/grammars/tree-sitter-rust.NOTICE.md +7 -0
- package/grammars/tree-sitter-rust.wasm +0 -0
- package/package.json +11 -3
package/docs/architecture.md
CHANGED
|
@@ -64,3 +64,7 @@ The registry is keyed by VS Code language ID. CLI filenames are matched against
|
|
|
64
64
|
case-insensitive suffix table, which is required for compound C++ suffixes such as `.hpp.in` and
|
|
65
65
|
keeps C, CUDA, and Objective-C++ outside the C++ adapter. Bundled third-party grammar artifacts keep
|
|
66
66
|
their provenance, checksum, and license notice beside the WASM.
|
|
67
|
+
|
|
68
|
+
Rust `.rs` files use the same registry and parser pipeline. The Rust adapter maps syntax from
|
|
69
|
+
`tree-sitter-rust` to the shared detector contracts, including function and closure views,
|
|
70
|
+
typed parameters, `use` imports, and multi-arm `match` decisions.
|
package/docs/cli.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
The same detectors also run headlessly, without VS Code, useful for CI or for an AI coding agent that wants to fix findings in code it just generated and verify the result. Published to npm, so no clone or install step is required:
|
|
4
4
|
|
|
5
5
|
```bash
|
|
6
|
-
npx energy-state-analyzer path/to/file.py # or .fs / .fsx / .ts / .kt / .cpp / .cs
|
|
6
|
+
npx energy-state-analyzer path/to/file.py # or .fs / .fsx / .ts / .kt / .cpp / .cs / .erl / .rs
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
Or install it as a project/global dependency and call it directly:
|
|
@@ -32,6 +32,7 @@ All threshold flags are optional: `--medium-nesting`, `--high-nesting`, `--mediu
|
|
|
32
32
|
| TypeScript | `.ts` |
|
|
33
33
|
| C# | `.cs`, `.csx` |
|
|
34
34
|
| Erlang | `.erl`, `.hrl` |
|
|
35
|
+
| Rust | `.rs` |
|
|
35
36
|
| Kotlin | `.kt`, `.kts` |
|
|
36
37
|
| C++ | `.cpp`, `.cppm`, `.cc`, `.ccm`, `.cxx`, `.cxxm`, `.c++`, `.c++m`, `.hpp`, `.hh`, `.hxx`, `.h++`, `.h`, `.ii`, `.ino`, `.inl`, `.ipp`, `.ixx`, `.mpp`, `.mxx`, `.tpp`, `.txx`, `.hpp.in`, `.h.in` |
|
|
37
38
|
|
package/docs/detectors/README.md
CHANGED
|
@@ -8,6 +8,12 @@ subscripts, `if`/loops/`try`/`catch`, ternaries, and branch-aware `switch` state
|
|
|
8
8
|
syntax-only: preprocessing, include resolution, template instantiation, overload resolution, and
|
|
9
9
|
type checking remain the compiler's job.
|
|
10
10
|
|
|
11
|
+
Rust uses the same detector pipeline for functions, closures, typed parameters, literals, `use`
|
|
12
|
+
imports, `if`/loop/match complexity, nesting, and control-flow opportunities. Rust parsing is
|
|
13
|
+
syntax-only, so macro expansion and type resolution are not modeled. Parse-don't-validate
|
|
14
|
+
recognizes direct Rust `Result`/`Option` guard and match validators; error-boundary
|
|
15
|
+
rules do not yet classify Rust `Result`/`Option` propagation.
|
|
16
|
+
|
|
11
17
|
## Complexity and structure
|
|
12
18
|
|
|
13
19
|
- [ESA002: Cyclomatic complexity](cyclomatic-complexity.md), too many independent execution paths.
|
|
@@ -55,8 +55,8 @@ This implementation does not claim full SonarSource compatibility:
|
|
|
55
55
|
- Match/when constructs use one structural increment, including pattern-based dispatch.
|
|
56
56
|
- Preprocessor conditionals and macro-expanded control flow are not scored.
|
|
57
57
|
- Anonymous callables are recognized directly from syntax: Python lambdas, F# function
|
|
58
|
-
expressions, TypeScript arrows and function expressions, Kotlin and C++ lambdas,
|
|
59
|
-
and anonymous methods. Aliases and callables created by macro expansion are not resolved.
|
|
58
|
+
expressions, TypeScript arrows and function expressions, Kotlin and C++ lambdas, C# lambdas
|
|
59
|
+
and anonymous methods, and Rust closures. Aliases and callables created by macro expansion are not resolved.
|
|
60
60
|
|
|
61
61
|
Python and F# have no `do`-`while` syntax. C++ local callables are covered through lambdas,
|
|
62
62
|
not nested named function declarations.
|
|
@@ -16,8 +16,7 @@ Starting from a base of **1**, every decision point adds **+1**, no matter how d
|
|
|
16
16
|
|
|
17
17
|
A nested named or anonymous callable is scored separately, as its own violation, and is never
|
|
18
18
|
folded into the enclosing callable's count. This includes Python lambdas, F# function expressions,
|
|
19
|
-
TypeScript arrows and function expressions, Kotlin and C++ lambdas,
|
|
20
|
-
methods.
|
|
19
|
+
TypeScript arrows and function expressions, Kotlin and C++ lambdas, C# lambdas and anonymous methods, and Rust closures.
|
|
21
20
|
|
|
22
21
|
Two functions with the same number of `if`s score the same whether those `if`s are sequential or nested five deep. This metric measures *independent control-flow paths*, rather than every possible execution.
|
|
23
22
|
[Cognitive complexity](cognitive-complexity.md) estimates reading effort through breaks in
|
|
@@ -4,6 +4,9 @@ Flags files that have lost a single responsibility, the "utils/helpers sprawl" p
|
|
|
4
4
|
|
|
5
5
|
## What it flags
|
|
6
6
|
|
|
7
|
+
Rust `impl` blocks group their methods for this check. Separate `impl` blocks are assessed as
|
|
8
|
+
separate scopes; trait relationships and methods produced by macros are not resolved.
|
|
9
|
+
|
|
7
10
|
**Class methods are grouped by their enclosing class, not counted as free-standing functions.** Every signal below that operates on "functions" (function-count sprawl and its type-cohesion check) only ever sees module-level, free-standing functions — a class's methods are excluded and evaluated instead by the class-relatedness check further down. This matters for OOP-style files (Python/TypeScript/Kotlin/C++/C#; F# has no class construct this detector models): without it, a file of two small, tightly related classes with 15 methods total between them reads exactly like 15 unrelated free-standing functions, and the type-cohesion signal — which only sees each method's own explicitly-annotated parameter/return types, not the fact that the method belongs to one of only two classes — misreads the handful of incidental types (mostly the classes' own names, showing up in constructors and factory-style methods) as "unrelated types" sprawl. A class's *own* method count and responsibility are judged separately by the god-class check further down — see [God class](god-class.md), which flags one type whose methods span too many unrelated domains rather than counting classes or free-standing functions.
|
|
8
11
|
|
|
9
12
|
**Directly bound anonymous callables follow the same responsibility boundaries.** An anonymous callable assigned directly to a module-level name counts as a free-standing function. One assigned directly to a class member counts as a method in languages whose classes this detector models. Function-local callable variables, returned closures, and inline callbacks do not add file or class responsibilities; their enclosing callable already represents that implementation boundary. Binding names feed the naming-cohesion heuristic, and explicit anonymous-callable parameter types feed type cohesion under the same coverage requirement as named functions. Recognition is intentionally syntax-only: aliases and assignments through intermediate expressions are not resolved. C# has no modeled module-level anonymous-callable binding form, and F# class members remain outside class-relatedness because the detector does not model an F# class construct.
|
|
@@ -50,4 +50,4 @@ This receives no guard recommendation: an early return could skip `record_attemp
|
|
|
50
50
|
|
|
51
51
|
## Known limitations
|
|
52
52
|
|
|
53
|
-
Runs for Python, TypeScript, Kotlin, C++, and
|
|
53
|
+
Runs for Python, TypeScript, Kotlin, C++, C#, and Rust. F#'s grammar has no block-boundary node to anchor this heuristic on. Guard-chain recognition follows explicit block bodies, so unbraced chains may be missed. Dominant-block recognition requires an explicit block; general depth detection also handles unbraced conditionals. In C++, only explicit syntax is considered; macro-expanded guard chains are not visible.
|
|
@@ -6,7 +6,7 @@ Flags an `if`/`elif`/`elif` chain (or a nested `else if` in TypeScript, Kotlin,
|
|
|
6
6
|
|
|
7
7
|
A chain of 3 or more branches (configurable), all discriminating via equality or membership checks against the *same single variable*, is flagged. An unconditional catch-all `else` at the end still qualifies, since it contributes no discriminant and isn't itself a "branch" for this check, but a chain mixing unrelated conditions across branches does not qualify, since a `match`/`switch` can't express that kind of dispatch anyway.
|
|
8
8
|
|
|
9
|
-
Runs on Python, F#, TypeScript, Kotlin, and
|
|
9
|
+
Runs on Python, F#, TypeScript, Kotlin, C++, and Rust.
|
|
10
10
|
|
|
11
11
|
For C++, only character and integral literals qualify because `switch` does not accept string or
|
|
12
12
|
floating-point case values.
|
|
@@ -180,11 +180,12 @@ In the editor this is `energyStateAnalyzer.parameterCount.mediumThreshold` / `.h
|
|
|
180
180
|
## Known limitations
|
|
181
181
|
|
|
182
182
|
The detector analyzes named definitions and anonymous callables: Python lambdas, F# function
|
|
183
|
-
expressions, TypeScript arrows and function expressions, Kotlin and C++ lambdas,
|
|
184
|
-
anonymous methods. TypeScript and C# single-parameter shorthand is counted without requiring
|
|
183
|
+
expressions, TypeScript arrows and function expressions, Kotlin and C++ lambdas, C# lambdas and
|
|
184
|
+
anonymous methods, and Rust closures. TypeScript and C# single-parameter shorthand is counted without requiring
|
|
185
185
|
parentheses.
|
|
186
186
|
|
|
187
187
|
Only parameters explicitly present in the syntax tree count. Kotlin's implicit `it` therefore
|
|
188
|
-
counts as zero declared parameters, and C++ capture lists do not count as parameters.
|
|
188
|
+
counts as zero declared parameters, and C++ capture lists do not count as parameters. Rust's
|
|
189
|
+
`self` receiver is excluded because callers do not pass it as a positional argument. C++ parameter
|
|
189
190
|
packs count when the grammar exposes them as parameter declarations, but macro-generated parameters
|
|
190
191
|
remain invisible without preprocessing.
|
|
@@ -32,7 +32,9 @@ property. Using exceptions versus `Result` is a separate choice.
|
|
|
32
32
|
|
|
33
33
|
## Detection
|
|
34
34
|
|
|
35
|
-
**Severity: Low.** Enabled by default in Python, F#, TypeScript, Kotlin, C++,
|
|
35
|
+
**Severity: Low.** Enabled by default in Python, F#, TypeScript, Kotlin, C++, C#,
|
|
36
|
+
Erlang, and Rust. Rust recognizes a narrow set of `Result`/`Option` validators;
|
|
37
|
+
panic-based validation remains outside the rule.
|
|
36
38
|
|
|
37
39
|
The rule recognizes a function consisting of:
|
|
38
40
|
|
|
@@ -62,6 +64,41 @@ supported. Comments and Python docstrings do not count as executable items.
|
|
|
62
64
|
Numeric bounds, non-empty collection checks, and boolean validators are covered by
|
|
63
65
|
the cross-language fixture matrix.
|
|
64
66
|
|
|
67
|
+
In Erlang, the rule recognizes a single `case` or `if` expression with two clauses:
|
|
68
|
+
the first rejects through `error/1,2`, `throw/1`, or `exit/1` (including explicit
|
|
69
|
+
`erlang:` calls), and the second returns the checked parameter or a bare boolean.
|
|
70
|
+
A `case` rejection needs a selective literal or collection pattern; an `if` success
|
|
71
|
+
clause needs `true` as its fallback guard. Erlang has no
|
|
72
|
+
statement-form `return`, so its identity cases use this explicit rejecting clause
|
|
73
|
+
instead of a parameter annotation as evidence of validation. An ordinary predicate
|
|
74
|
+
query remains clean when neither clause calls a terminating function.
|
|
75
|
+
|
|
76
|
+
```erlang
|
|
77
|
+
checked_items(Items) ->
|
|
78
|
+
case Items of
|
|
79
|
+
[] -> error(empty);
|
|
80
|
+
_ -> Items % flagged: the successful result is still a plain list
|
|
81
|
+
end.
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
In Rust, a direct `if` guard that returns `Err(...)` or `None`, followed by
|
|
85
|
+
`Ok(input)` or `Some(input)`, is reported when the returned input is the checked
|
|
86
|
+
parameter. The same applies to a two-arm `match` with a selective rejecting
|
|
87
|
+
pattern and an unguarded `_` success arm. The error carrier alone does not encode
|
|
88
|
+
the checked property in the successful value:
|
|
89
|
+
|
|
90
|
+
```rust
|
|
91
|
+
fn positive(value: i32) -> Result<i32, &'static str> {
|
|
92
|
+
if value <= 0 { return Err("must be positive"); }
|
|
93
|
+
Ok(value) // flagged: the successful value is still an unconstrained i32
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Returning a constructed domain type, such as `Result<Positive, E>`, stays clean.
|
|
98
|
+
The Rust extractor requires an unqualified `Result` or `Option` return
|
|
99
|
+
type and direct `Err`/`Ok` or `None`/`Some` variants; it does not infer aliases or
|
|
100
|
+
validate arbitrary calls. It cannot resolve names that shadow those variants.
|
|
101
|
+
|
|
65
102
|
### Boolean validators
|
|
66
103
|
|
|
67
104
|
A boolean result carries the verdict, never the value: `bool` is two states with no
|
|
@@ -95,7 +132,8 @@ construct; there a null-checking boolean validator remains a finding.
|
|
|
95
132
|
This is a syntax heuristic, not a proof about domain invariants. Review whether the
|
|
96
133
|
checked property belongs in the public contract before introducing a domain type.
|
|
97
134
|
|
|
98
|
-
- Untyped identity returns, multiple guards, `Ok input
|
|
135
|
+
- Untyped identity returns, multiple guards, `Ok input` in languages without a
|
|
136
|
+
dedicated wrapper extractor, and assertion statements
|
|
99
137
|
are not yet recognized. `if/else` result expressions are recognized only when the
|
|
100
138
|
rejection branch is a bare falsy literal; other rejection branches (for example
|
|
101
139
|
F# `if … then failwith() else …`) are not. Indirect validation calls are
|
|
@@ -120,6 +158,14 @@ checked property belongs in the public contract before introducing a domain type
|
|
|
120
158
|
remain candidates because no narrowed value is returned.
|
|
121
159
|
- The analyzer does not resolve aliases, infer types, follow callers, prove
|
|
122
160
|
predicate purity, or resolve shadowed F# failure-function names.
|
|
161
|
+
- Erlang validation spread across function clauses, function-head guards, guarded
|
|
162
|
+
`case` clauses, reversed clause order, and clause bodies with additional work are
|
|
163
|
+
outside this rule. It does not read Erlang `-spec` declarations or resolve local
|
|
164
|
+
functions that shadow unqualified `error`, `throw`, or `exit` calls.
|
|
165
|
+
- Rust validation with `panic!`, `?`, `unwrap`, guarded match arms, multiple arms,
|
|
166
|
+
qualified or aliased variants, or intervening work is outside the narrow
|
|
167
|
+
`Result`/`Option` extractor. Its syntax check cannot prove that an `Ok` or `Some`
|
|
168
|
+
payload's type actually enforces a domain invariant.
|
|
123
169
|
|
|
124
170
|
## Configuration and suppression
|
|
125
171
|
|
|
@@ -4,11 +4,11 @@ Flags strings and numbers standing in for what should be a distinct, validated t
|
|
|
4
4
|
|
|
5
5
|
## What it flags
|
|
6
6
|
|
|
7
|
-
**Parameter-swap risk.** Two adjacent parameters sharing the same unqualified primitive type (e.g. `lat: float, lon: float`) are indistinguishable at the call site: nothing stops a caller from passing them in the wrong order. Runs on Python, F#, TypeScript, Kotlin,
|
|
7
|
+
**Parameter-swap risk.** Two adjacent parameters sharing the same unqualified primitive type (e.g. `lat: float, lon: float`) are indistinguishable at the call site: nothing stops a caller from passing them in the wrong order. Runs on Python, F#, TypeScript, Kotlin, C++, C#, and Rust. C++ pointer, reference, array, and function-declarator shapes remain part of the extracted type identity, so `int`, `int*`, and `int&` do not collide.
|
|
8
8
|
|
|
9
9
|
In Python, a pair is suppressed only when *both* parameters are keyword-only (after a bare `*` or `*args` in the signature), since the signature itself then makes a positional call impossible. Named-parameter naming is still a weaker mitigation than a distinct type (`NewType`, a dataclass, etc.), since nothing stops a future `**kwargs`-splat call from transposing the values by hand, but that gap isn't worth detecting. This suppression doesn't apply to TypeScript, Kotlin, or C++, which have no enforcing keyword-only boundary, or F#, whose named arguments are limited to member and constructor calls and do not prevent a positional call.
|
|
10
10
|
|
|
11
|
-
**Boolean blindness.** Two or more boolean parameters (`bool` in Python, F#, and
|
|
11
|
+
**Boolean blindness.** Two or more boolean parameters (`bool` in Python, F#, C++, and Rust; `boolean` in TypeScript; `Boolean` in Kotlin) anywhere in a signature, not just adjacent ones, are flagged separately from the swap-risk check above. `exportData(compress: bool, notify: bool, overwrite: bool)` reads the same at a positional call site regardless of which flag controls what, and so does `exportData(mode: bool, name: str, notify: bool)`: a non-boolean parameter between two booleans breaks swap-*adjacency*, but it does nothing to fix the blindness, since the domain still permits fewer combinations than the type system allows. A boolean is excluded from the count only when the language's keyword-only mechanism forces every call site to name it (e.g. Python's `*`/`*args` boundary). The suggested fix is a single named type covering the valid combinations (an enum, a discriminated union), not a distinct type per flag, since wrapping each boolean separately keeps the combinatorial explosion, only the swap risk goes away.
|
|
12
12
|
|
|
13
13
|
**Stringly-typed control flow.** A variable compared against 3 or more distinct string literals within one function is a de facto enum encoded as strings, with no exhaustiveness checking and no typo protection at the type level. Runs on Python, F#, TypeScript, Kotlin, and C++; Python additionally flags a variable checked against a literal tuple/list/set in one `in` expression, and F# additionally flags the idiomatic `match` on string-literal cases dispatching on one variable, since the two are the same de facto enum expressed in each language's dominant form.
|
|
14
14
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Maxim Sokolov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# tree-sitter-rust grammar
|
|
2
|
+
|
|
3
|
+
`tree-sitter-rust.wasm` is the official WebAssembly release artifact from
|
|
4
|
+
[`tree-sitter-rust` v0.24.2](https://github.com/tree-sitter/tree-sitter-rust/releases/tag/v0.24.2).
|
|
5
|
+
|
|
6
|
+
- SHA-256: `24c89bd9252255e4aebbcbd7d2d308bd92c86dd95a130fdc80efa49577b8d738`
|
|
7
|
+
- License: MIT; see `tree-sitter-rust.LICENSE` in this directory.
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "energy-state-analyzer",
|
|
3
3
|
"displayName": "Energy State Analyzer",
|
|
4
|
-
"description": "Visualize code energy states in real-time (Python, F#, TypeScript, Kotlin, C++, C#, Erlang)",
|
|
5
|
-
"version": "0.
|
|
4
|
+
"description": "Visualize code energy states in real-time (Python, F#, TypeScript, Kotlin, C++, C#, Erlang, Rust)",
|
|
5
|
+
"version": "0.23.0",
|
|
6
6
|
"publisher": "cardamom-code",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"icon": "images/icon.png",
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"c++",
|
|
27
27
|
"csharp",
|
|
28
28
|
"erlang",
|
|
29
|
+
"rust",
|
|
29
30
|
"code quality",
|
|
30
31
|
"cyclomatic complexity",
|
|
31
32
|
"cognitive complexity",
|
|
@@ -40,7 +41,8 @@
|
|
|
40
41
|
"onLanguage:kotlin",
|
|
41
42
|
"onLanguage:cpp",
|
|
42
43
|
"onLanguage:csharp",
|
|
43
|
-
"onLanguage:erlang"
|
|
44
|
+
"onLanguage:erlang",
|
|
45
|
+
"onLanguage:rust"
|
|
44
46
|
],
|
|
45
47
|
"main": "./dist/extension.js",
|
|
46
48
|
"bin": {
|
|
@@ -58,6 +60,12 @@
|
|
|
58
60
|
".erl",
|
|
59
61
|
".hrl"
|
|
60
62
|
]
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"id": "rust",
|
|
66
|
+
"extensions": [
|
|
67
|
+
".rs"
|
|
68
|
+
]
|
|
61
69
|
}
|
|
62
70
|
],
|
|
63
71
|
"commands": [
|