energy-state-analyzer 0.7.0 → 0.8.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/.esaignore +1 -1
- package/.vscodeignore +9 -3
- package/AGENTS.md +85 -69
- package/CHANGELOG.md +17 -1
- package/README.md +21 -5
- package/action.yml +3 -3
- package/dist/498894f52e4b9dcbc99d.wasm +0 -0
- package/dist/cli.js +1 -1
- package/dist/web-tree-sitter.wasm +0 -0
- package/docs/architecture.md +61 -0
- package/docs/cli.md +4 -1
- package/docs/detectors/magic-numbers.md +7 -1
- package/docs/detectors/magic-strings.md +1 -1
- package/docs/detectors/suppression.md +1 -1
- package/docs/fable-rewrite-plan.md +97 -0
- package/package.json +15 -21
- package/.prettierignore +0 -7
- package/dist/cli.js.map +0 -1
|
Binary file
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Architecture — Energy State Analyzer
|
|
2
|
+
|
|
3
|
+
Energy State Analyzer is implemented in F# and compiled with Fable 5 to JavaScript. The CLI and
|
|
4
|
+
VS Code extension share one host-independent detector pipeline, then webpack packages generated
|
|
5
|
+
ESM into the CommonJS artifacts exposed by `package.json`.
|
|
6
|
+
|
|
7
|
+
## Coherence rules
|
|
8
|
+
|
|
9
|
+
Each F# file stays at **≤12 functions** and **≤10 import sources**. The analyzer enforces the
|
|
10
|
+
same rule when `just analyze` dogfoods the project. Split a module by domain rather than growing
|
|
11
|
+
one past its responsibility.
|
|
12
|
+
|
|
13
|
+
## Module map
|
|
14
|
+
|
|
15
|
+
| Area | Owns | State |
|
|
16
|
+
| --- | --- | --- |
|
|
17
|
+
| `src/Core/` | Violation model, parser facade, detector pipeline, suppressions, scanning and reports | No host state |
|
|
18
|
+
| `src/Languages/` | Per-grammar `LanguageAdapter` records | Static registry |
|
|
19
|
+
| `src/Extension/Vscode*.fs` | Narrow Fable facade over VS Code | No |
|
|
20
|
+
| `src/Extension/Configuration*.fs` | VS Code settings and pure settings-to-threshold mapping | No |
|
|
21
|
+
| `src/Extension/Grammar.fs` | Parser initialization, loaded grammar cache, in-flight load cache | Cache supplied by root |
|
|
22
|
+
| `src/Extension/Analysis.fs` | Document analysis, `.esaignore`, `includeFixtures`, Python type-info logging | No |
|
|
23
|
+
| `src/Extension/DecorationModel.fs` / `Decorations.fs` | Pure decoration range/heatmap calculations and VS Code rendering | No |
|
|
24
|
+
| `src/Extension/DiagnosticModel.fs` / `Diagnostics.fs` | Pure Problems mapping and VS Code diagnostic rendering | No |
|
|
25
|
+
| `src/Extension/Extension.fs` | Activation, deactivation, lifecycle state, commands, events | Yes |
|
|
26
|
+
| `src/Cli*.fs`, `cli/Main.fs` | Argument parsing, CLI modes, Node boundary, Fable entry | Parser cache |
|
|
27
|
+
| `tests/` | F# Scriptorium integration and pure presentation/configuration tests | Test-only |
|
|
28
|
+
|
|
29
|
+
## Analysis pipeline
|
|
30
|
+
|
|
31
|
+
`src/Core/Analyze.fs` builds one ordered detector list over an immutable `AnalysisContext`.
|
|
32
|
+
Detectors remain synchronous once the source has been parsed; `Task` is confined to async edges
|
|
33
|
+
such as grammar loading. Suppressions run last over the combined result, so an `esa-ignore`
|
|
34
|
+
directive can name any detector category.
|
|
35
|
+
|
|
36
|
+
`EnergyViolation` uses F# discriminated unions for severity/type plus a record payload. The CLI
|
|
37
|
+
maps those values to the established JSON strings (`low`, `medium`, `high`, etc.), keeping its
|
|
38
|
+
public contract unchanged.
|
|
39
|
+
|
|
40
|
+
## Extension presentation
|
|
41
|
+
|
|
42
|
+
The composition root owns per-activation grammar caches, decoration types, diagnostic collection,
|
|
43
|
+
and subscriptions. It refreshes the active editor after editor/document/configuration events,
|
|
44
|
+
recreates decoration types when colors change, clears ignored/unsupported documents, and verifies
|
|
45
|
+
the active document again after an async grammar load.
|
|
46
|
+
|
|
47
|
+
`DecorationModel.fs` keeps category-specific range selection and per-violation normalized
|
|
48
|
+
complexity heatmaps testable outside VS Code. `DiagnosticModel.fs` groups same-line findings into
|
|
49
|
+
one Problems entry, preserving severity ordering, tags, codes, and combined messages.
|
|
50
|
+
|
|
51
|
+
## Build and packaging
|
|
52
|
+
|
|
53
|
+
`npm run fable` invokes Fable with `--lang javascript --noCache`, emitting ignored ESM into
|
|
54
|
+
`fable-out/`. `webpack.config.js` consumes:
|
|
55
|
+
|
|
56
|
+
- `fable-out/Extension/Extension.js` → `dist/extension.js`
|
|
57
|
+
- `fable-out/Main.js` → `dist/cli.js`
|
|
58
|
+
|
|
59
|
+
Webpack keeps `vscode` external, bundles `web-tree-sitter`, copies `web-tree-sitter.wasm`, and
|
|
60
|
+
adds the CLI shebang. Per-language grammar WASMs remain runtime files in `grammars/`. There is no
|
|
61
|
+
TypeScript compiler or loader in the product build.
|
package/docs/cli.md
CHANGED
|
@@ -133,4 +133,7 @@ With no path arguments, changed files are discovered via `git diff --name-only <
|
|
|
133
133
|
_2 files changed, 1 worsened, 1 improved, 1 new._
|
|
134
134
|
```
|
|
135
135
|
|
|
136
|
-
|
|
136
|
+
Single-file and scan modes exit `1` for any medium/high-severity violation (`0` otherwise).
|
|
137
|
+
Diff mode exits `1` only when a changed file worsens relative to its base revision, so pre-existing
|
|
138
|
+
debt and new files are reported without blocking the PR. `energy-state-cli <single-file>` with no
|
|
139
|
+
other flags keeps its original flat JSON violation-array contract.
|
|
@@ -14,6 +14,12 @@ Numbers get no free pass for "looking like prose" the way strings do, so this st
|
|
|
14
14
|
|
|
15
15
|
Negative literals are recognized by structural shape (a unary `-` immediately preceding the literal), so `-1` and `1` are both checked against the allowlist correctly.
|
|
16
16
|
|
|
17
|
+
### Default allowlist policy
|
|
18
|
+
|
|
19
|
+
The default allowlist is `[0, 1, -1, 2]`. These are common structural idioms—such as an empty count, a first item, a not-found sentinel, a boolean-like increment, or a two-way choice—where replacing the literal with a named constant usually adds noise rather than clarifying intent.
|
|
20
|
+
|
|
21
|
+
This is a deliberate usability policy, not a universal definition of a magic number: static-analysis tools and teams choose different exceptions. Values outside this small set remain visible unless they are in an exempt context or a workspace adds them to the allowlist. Configure the allowlist when a domain has additional well-understood literals.
|
|
22
|
+
|
|
17
23
|
## Example
|
|
18
24
|
|
|
19
25
|
```python
|
|
@@ -30,4 +36,4 @@ TAX_RATE_STANDARD = 1.05 # not flagged: module-level named constant
|
|
|
30
36
|
## Configuration
|
|
31
37
|
|
|
32
38
|
- `energyStateAnalyzer.magicNumber.enabled` (default `true`)
|
|
33
|
-
- `energyStateAnalyzer.magicNumber.allowlist` (default `[0, 1, -1, 2]`), values
|
|
39
|
+
- `energyStateAnalyzer.magicNumber.allowlist` (default `[0, 1, -1, 2]`), values exempt from findings regardless of context. Add domain-specific structural literals here when their meaning is already clear to the team.
|
|
@@ -32,6 +32,6 @@ def route(status):
|
|
|
32
32
|
|
|
33
33
|
## Known limitations
|
|
34
34
|
|
|
35
|
-
The decision-point scan (equality/membership/dict-key) and the formatted-string exemption are fully implemented for Python, and partially for TypeScript (no `.includes()` membership support yet) and F# (no dict/subscript node, no interpolated-string exemption). See the `LanguageAdapter` fields in `src/
|
|
35
|
+
The decision-point scan (equality/membership/dict-key) and the formatted-string exemption are fully implemented for Python, and partially for TypeScript (no `.includes()` membership support yet) and F# (no dict/subscript node, no interpolated-string exemption). See the `LanguageAdapter` fields in `src/Core/LanguageAdapter.fs` for exactly what's modeled per language.
|
|
36
36
|
|
|
37
37
|
It also doesn't (yet) special-case enum-like keyword/default arguments (e.g. `mode="fast"`) as a lower-confidence decision point, only equality, membership, and dict/index-key positions count.
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# Fable rewrite — completed architecture
|
|
2
|
+
|
|
3
|
+
Status: **complete.** The product implementation, CLI, and tests are F#. Fable 5 compiles only
|
|
4
|
+
to JavaScript; TypeScript is no longer a product build or test dependency.
|
|
5
|
+
|
|
6
|
+
## Target and source layout
|
|
7
|
+
|
|
8
|
+
F# source is the only implementation source:
|
|
9
|
+
|
|
10
|
+
```text
|
|
11
|
+
src/Core/ host-independent detector pipeline and reports
|
|
12
|
+
src/Languages/ Python, F#, TypeScript, and Kotlin grammar adapters
|
|
13
|
+
src/Extension/ VS Code facade, presentation, grammar lifecycle, and entry point
|
|
14
|
+
src/Cli*.fs CLI Node boundary, runtime, argument parser, and modes
|
|
15
|
+
cli/Main.fs Fable CLI entry point
|
|
16
|
+
tests/ Scriptorium test suite
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
`src/test/fixtures/**/*.ts` remain analysis inputs. They are not TypeScript product or test code.
|
|
20
|
+
Every F# file is explicitly ordered in its `.fsproj`, as required by the F# SDK.
|
|
21
|
+
|
|
22
|
+
## Final JavaScript build graph
|
|
23
|
+
|
|
24
|
+
```text
|
|
25
|
+
src/EnergyState.fsproj cli/EnergyState.Cli.fsproj
|
|
26
|
+
│ │
|
|
27
|
+
└──── dotnet fable --lang javascript --noCache ────┐
|
|
28
|
+
▼
|
|
29
|
+
fable-out/ (ESM)
|
|
30
|
+
┌───────────────────────┴──────────────────────┐
|
|
31
|
+
▼ ▼
|
|
32
|
+
fable-out/Extension/Extension.js fable-out/Main.js
|
|
33
|
+
│ │
|
|
34
|
+
└────────── webpack (target: node) ────────────┘
|
|
35
|
+
│
|
|
36
|
+
┌───────────────────────┴──────────────────────┐
|
|
37
|
+
▼ ▼
|
|
38
|
+
dist/extension.js dist/cli.js
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Webpack consumes Fable's ESM directly—there is no `tsc` or `ts-loader`. It keeps `vscode` as a
|
|
42
|
+
CommonJS external, bundles `web-tree-sitter`, copies `web-tree-sitter.wasm`, and applies the
|
|
43
|
+
shebang banner to `dist/cli.js`. The public `package.json` main/bin contracts remain unchanged.
|
|
44
|
+
|
|
45
|
+
`npm run fable` builds both entries to ignored `fable-out/`; the CLI project emits `Main.js` and
|
|
46
|
+
referenced source under `fable-out/src/`. `npm run fable-tests` emits Scriptorium tests to ignored
|
|
47
|
+
`fable-tests/` and writes its `{"type":"module"}` ESM shim before Node runs `Main.js`.
|
|
48
|
+
|
|
49
|
+
## Extension implementation
|
|
50
|
+
|
|
51
|
+
`Extension.fs` is the composition root. It initializes tree-sitter, owns per-activation loaded and
|
|
52
|
+
in-flight grammar caches, creates/disposes decorations and diagnostics, registers the command, and
|
|
53
|
+
wires active-editor, active-document, configuration, and close-document events.
|
|
54
|
+
|
|
55
|
+
- `Grammar.fs` lazily loads one parser per language and shares an in-flight `Task` for concurrent
|
|
56
|
+
edits.
|
|
57
|
+
- `Analysis.fs` honors workspace `.esaignore`, supports the editor-only `includeFixtures`
|
|
58
|
+
override, reads configured thresholds, clears failures instead of stale findings, and retains
|
|
59
|
+
Python type-info logging.
|
|
60
|
+
- `ConfigurationValues.fs`, `DecorationModel.fs`, and `DiagnosticModel.fs` are pure mappings.
|
|
61
|
+
- `Decorations.fs` preserves category-specific ranges, lightning icons, color fallback, and
|
|
62
|
+
normalized complexity heat bands.
|
|
63
|
+
- `Diagnostics.fs` preserves severity/tags, fixed-width ranges, same-line grouping, messages, and
|
|
64
|
+
source/code values.
|
|
65
|
+
|
|
66
|
+
The narrow `Vscode*.fs` facade is the only VS Code dynamic interop boundary. `Core/TreeSitter.fs`
|
|
67
|
+
is the only web-tree-sitter boundary.
|
|
68
|
+
|
|
69
|
+
## CLI implementation
|
|
70
|
+
|
|
71
|
+
`cli/Main.fs` invokes `Energy.Cli.runCli`. `Cli.fs` preserves value flags and threshold overrides;
|
|
72
|
+
`CliModes.fs` provides the legacy one-file JSON mode, aggregate scan reports (`json`, `md`,
|
|
73
|
+
`human`), and `--base-ref` diff reports. The parser cache is shared for the invocation. Legacy and
|
|
74
|
+
scan modes block on medium/high findings; diff mode blocks only worsened file scores.
|
|
75
|
+
|
|
76
|
+
## Tests and validation
|
|
77
|
+
|
|
78
|
+
Scriptorium is the test framework. The core integration suites continue to parse the multi-language
|
|
79
|
+
fixtures; `ExtensionPresentationTests.fs` validates configuration mapping, decoration ranges/color
|
|
80
|
+
fallback/heat normalization, and Problems mapping without requiring an interactive VS Code host.
|
|
81
|
+
The removed TypeScript host suite did not exercise activation or presentation behavior; it is
|
|
82
|
+
replaced by these direct F# tests.
|
|
83
|
+
|
|
84
|
+
Run the normal gates:
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
just format
|
|
88
|
+
just lint
|
|
89
|
+
just test
|
|
90
|
+
just build
|
|
91
|
+
just analyze
|
|
92
|
+
just pack
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
`fable-out/`, `fable-tests/`, `fable_modules/`, .NET build output, source maps, sources, and tests
|
|
96
|
+
are excluded from distribution artifacts. The shipped packages contain transpiled bundles, WASM
|
|
97
|
+
assets, grammars, and public metadata.
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "energy-state-analyzer",
|
|
3
3
|
"displayName": "Energy State Analyzer",
|
|
4
4
|
"description": "Visualize code energy states in real-time (Python, F#, TypeScript, Kotlin)",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.8.0",
|
|
6
6
|
"publisher": "cardamom-code",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"icon": "images/icon.png",
|
|
@@ -51,6 +51,11 @@
|
|
|
51
51
|
"configuration": {
|
|
52
52
|
"title": "Energy State Analyzer",
|
|
53
53
|
"properties": {
|
|
54
|
+
"energyStateAnalyzer.includeTestFiles": {
|
|
55
|
+
"type": "boolean",
|
|
56
|
+
"default": false,
|
|
57
|
+
"description": "Also flag magic numbers and magic strings in test files (test_*.py, *_test.py, *Test.py, *.test.ts, etc.). Off by default because test code is full of intentional literals."
|
|
58
|
+
},
|
|
54
59
|
"energyStateAnalyzer.nesting.mediumThreshold": {
|
|
55
60
|
"type": "number",
|
|
56
61
|
"default": 3,
|
|
@@ -200,33 +205,22 @@
|
|
|
200
205
|
"scripts": {
|
|
201
206
|
"vscode:prepublish": "npm run package",
|
|
202
207
|
"prepublishOnly": "npm run package",
|
|
203
|
-
"
|
|
204
|
-
"
|
|
205
|
-
"
|
|
206
|
-
"
|
|
207
|
-
"
|
|
208
|
-
"
|
|
209
|
-
"
|
|
210
|
-
"format": "
|
|
211
|
-
"
|
|
212
|
-
"test": "vscode-test",
|
|
208
|
+
"fable": "rm -rf fable-out && dotnet fable src/EnergyState.fsproj --lang javascript --outDir fable-out --noCache && dotnet fable cli/EnergyState.Cli.fsproj --lang javascript --outDir fable-out --noCache",
|
|
209
|
+
"fable-tests": "rm -rf fable-tests && dotnet fable tests/EnergyState.Tests.fsproj --lang javascript --outDir fable-tests --noCache && echo '{\"type\":\"module\"}' > fable-tests/package.json",
|
|
210
|
+
"compile": "npm run fable && webpack",
|
|
211
|
+
"watch": "npm run fable && (dotnet fable watch src/EnergyState.fsproj --lang javascript --outDir fable-out --noCache & dotnet fable watch cli/EnergyState.Cli.fsproj --lang javascript --outDir fable-out --noCache & webpack --watch)",
|
|
212
|
+
"package": "npm run fable && webpack --mode production --devtool hidden-source-map",
|
|
213
|
+
"lint": "dotnet fantomas src tests cli --check",
|
|
214
|
+
"format": "dotnet fantomas src tests cli",
|
|
215
|
+
"format-check": "dotnet fantomas src tests cli --check",
|
|
216
|
+
"test": "npm run fable-tests && node fable-tests/Main.js",
|
|
213
217
|
"analyze": "node dist/cli.js",
|
|
214
218
|
"license-check": "license-checker --production --onlyAllow \"MIT;Apache-2.0;BSD-2-Clause;BSD-3-Clause;ISC;0BSD;CC0-1.0;Unlicense\" --excludePackages \"$npm_package_name@$npm_package_version\""
|
|
215
219
|
},
|
|
216
220
|
"devDependencies": {
|
|
217
|
-
"@types/mocha": "^10.0.10",
|
|
218
|
-
"@types/node": "26.x",
|
|
219
|
-
"@types/vscode": "^1.74.0",
|
|
220
|
-
"@vscode/test-cli": "^0.0.15",
|
|
221
|
-
"@vscode/test-electron": "^3.1.0",
|
|
222
221
|
"@vscode/vsce": "^3.2.1",
|
|
223
222
|
"copy-webpack-plugin": "^14.0.0",
|
|
224
|
-
"eslint": "^10.9.0",
|
|
225
223
|
"license-checker": "^25.0.1",
|
|
226
|
-
"prettier": "^3.9.6",
|
|
227
|
-
"ts-loader": "^9.4.1",
|
|
228
|
-
"typescript": "^5.9.3",
|
|
229
|
-
"typescript-eslint": "^8.67.0",
|
|
230
224
|
"webpack": "^5.75.0",
|
|
231
225
|
"webpack-cli": "^7.2.2"
|
|
232
226
|
},
|