optiprune 2.2.21 → 2.2.22

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.
Files changed (2) hide show
  1. package/README.md +168 -89
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,131 +1,210 @@
1
- ![Optiprune Logo](./logo.svg)
2
- ![NPM Version](https://img.shields.io/npm/v/@optiprune/cli)
3
- ![GitHub License](https://img.shields.io/github/license/optiprune/cli)
1
+ ![OptiPrune analyzer animation](https://raw.githubusercontent.com/optiprune/core/main/animation.svg)
4
2
 
5
- # 🚀 OptiPrune
6
- ---
3
+ [![CLI npm version](https://img.shields.io/npm/v/%40optiprune%2Fcli?label=%40optiprune%2Fcli)](https://www.npmjs.com/package/@optiprune/cli)[![Core npm version](https://img.shields.io/npm/v/%40optiprune%2Fcore?label=%40optiprune%2Fcore)](https://www.npmjs.com/package/@optiprune/core)[![CLI package](https://img.shields.io/github/package-json/v/optiprune/cli?label=CLI%20package)](https://github.com/optiprune/cli)[![Core tests](https://img.shields.io/github/actions/workflow/status/optiprune/core/tests.yml?branch=main&label=core%20tests)](https://github.com/optiprune/core/actions/workflows/tests.yml)[![License](https://img.shields.io/github/license/optiprune/cli)](./LICENSE)[![Node.js](https://img.shields.io/badge/Node.js-%3E%3D21-339933?logo=node.js&logoColor=white)](https://nodejs.org/)
7
4
 
8
- ## 💎 The Vision
9
- Most dead-code analyzers just guess. They look at import graphs and hope they don't miss anything critical. This leads to **false positives**, broken builds, and developer frustration.
5
+ # OptiPrune
10
6
 
11
- **OptiPrune is different.** We use formal logic, isolated execution, and a high-performance engine to not just find dead code, but to mathematically **prove** it.
7
+ OptiPrune is a static dead-code analyzer for TypeScript and JavaScript projects. It combines parser-backed module graphs, export and member reachability, dependency and workspace inspection, dynamic-import analysis, semantic contracts, optional symbolic/concolic checks, and source-aware plugins.
12
8
 
13
- > **"Stop Guessing, Start Proving."**
9
+ The CLI package is `@optiprune/cli`. The analysis engine is available separately as the headless package `@optiprune/core`.
14
10
 
15
- ---
11
+ ## Features
16
12
 
17
- ## The 3 Pillars of Superiority
13
+ | Area | What is included |
14
+ | --- | --- |
15
+ | Project analysis | Entry discovery, module graphs, export/member reachability, dependency edges, strongly connected components, and cycle reporting. |
16
+ | TypeScript and JavaScript | `.ts`, `.tsx`, `.js`, `.jsx`, and `.vue` extensions by default, with custom extension lists available from the CLI or config. |
17
+ | Dynamic paths | Literal and pattern-based dynamic imports, unresolved-path findings, recovery information, and isolated execution checks. |
18
+ | Logic analysis | Constant conditions, contradictory guards, unreachable statements, and schema-impossible guards. |
19
+ | Dependencies | `package.json`, scripts, dependency/devDependency usage, package exports, bins, workspace packages, and lockfile-aware context. |
20
+ | Contracts and entries | Public API contracts, schema-aware protection, conventional entries, entry-file exports, test-file handling, and framework/plugin entry points. |
21
+ | Fixes | Opt-in fixes for unreachable files, unused exports and members, dependencies, development dependencies, and verified conditions. Every fix is confidence-gated and supports dry runs. |
22
+ | Output | Human-readable terminal output, JSON reports, and SARIF output for CI/code-scanning workflows. |
23
+ | Headless usage | `analyze`, `shouldFail`, cache helpers, fix helpers, reporters, and public TypeScript types from `@optiprune/core`. |
24
+ | Plugins | Source-aware adapters for frameworks, build tools, test tools, runtimes, package managers, and workspace conventions. |
18
25
 
19
- ### 1. Raw Power: The Yuku Engine (Zig)
20
- While other tools are throttled by the N-API bottleneck, OptiPrune utilizes the **Yuku Engine** written in **Zig**. It minimizes the overhead between native performance and the JavaScript runtime.
21
- * **The Result:** Up to 3x faster parsing than traditional tools, even in massive monorepos.
26
+ ## Installation
22
27
 
23
- ### 2. Deep Intelligence: SMT & Z3 Solver
24
- OptiPrune is the first analyzer to use a real **SMT Solver (Z3)**. We don't just analyze if a function is exported; we analyze if the code *inside* the function is logically reachable.
25
- * **The Result:** Detects dead logic paths (e.g., impossible `if` conditions) that are completely invisible to Knip.
28
+ Install the CLI as a development dependency:
26
29
 
27
- ### 3. Absolute Precision: WASM Sandbox Execution
28
- Dynamic imports are the final boss of static analysis. OptiPrune solves this through a **WASM-based QuickJS sandbox**. We securely execute critical code snippets to resolve paths at runtime.
29
- * **The Result:** Zero false alarms for dynamic paths. If OptiPrune says it's dead, it's dead.
30
+ ```bash
31
+ npm install --save-dev @optiprune/cli
32
+ # or
33
+ pnpm add -D @optiprune/cli
34
+ # or
35
+ yarn add -D @optiprune/cli
36
+ ```
30
37
 
31
- ---
38
+ The Core package currently requires Node.js 21 or newer.
32
39
 
33
- ## 🥊 OptiPrune vs. Knip: The Head-to-Head
40
+ ## Quick start
34
41
 
35
- | Feature | Knip | OptiPrune |
36
- | :--- | :--- | :--- |
37
- | **Engine** | Babel / OXC (Standard) | **Yuku / Zig (Hyper-Speed)** |
38
- | **Logic Analysis** | Heuristics (Guessing) | **Z3 SMT Solver (Proving)** |
39
- | **Dynamic Paths** | Pattern Matching | **WASM Sandbox Execution** |
40
- | **Interface Audit** | Ignores Members | **Deep Member-Level Analysis** |
41
- | **Framework Support** | Plugins (Core-Level) | **7-Layer Semantic Context** |
42
- | **False Positives** | High (in complex setups) | **Near-Zero (Context Aware)** |
42
+ Run an analysis from the project root:
43
43
 
44
- ---
44
+ ```bash
45
+ npx @optiprune/cli analyze
46
+ ```
45
47
 
46
- ## 🏗️ The 7-Layer Architecture
47
- OptiPrune operates in seven specialized layers to guarantee maximum accuracy:
48
+ The default command is `analyze`, so this is equivalent:
48
49
 
49
- 1. **Layer 1: Discovery** – Ultra-fast file scanning.
50
- 2. **Layer 2: Basic CFG** – Detects standard dead code after terminal statements.
51
- 3. **Layer 3: SMT Logic** – Mathematical path proofs with Z3.
52
- 4. **Layer 4: WASM Sandbox** – Dynamic path resolution via execution.
53
- 5. **Layer 6: Schema Shield** – Protection for Zod, Decorators & Contracts.
54
- 6. **Layer 6: Dependency Audit** – Scans lockfiles & package.json scripts.
55
- 7. **Layer 7: Topology Engine** – Understands NestJS DI & Event Buses.
50
+ ```bash
51
+ npx @optiprune/cli
52
+ ```
56
53
 
57
- ---
54
+ Select a machine-readable output format when integrating with tooling:
58
55
 
59
- ## 📈 Benchmark Numbers (Real-World Test)
60
- *Tested on a NestJS project with 1000+ files.*
56
+ ```bash
57
+ npx @optiprune/cli analyze --json
58
+ npx @optiprune/cli analyze --sarif > optiprune.sarif
59
+ ```
61
60
 
62
- * **Knip Speed:** 1.26s (with crash risks on complex types)
63
- * **OptiPrune Speed:** **0.87s** (Stable & Precise)
64
- * **Accuracy:** OptiPrune found **15% more** real dead code (unused interface properties & logical errors) that Knip completely missed.
61
+ ## Commands
65
62
 
66
- ---
63
+ | Command | Purpose |
64
+ | --- | --- |
65
+ | `analyze [options]` | Analyze the project. This is the default command. |
66
+ | `export-cache <targetPath>` | Export the current analysis cache to a JSON file. |
67
+ | `import-cache <sourcePath>` | Import an external cache JSON file into the local project cache. |
68
+ | `optiprune --help` | Print command and option help. |
69
+ | `optiprune --version` | Print the CLI and detected Core versions. |
67
70
 
68
- ## Installation
71
+ ## Analyze flags
69
72
 
70
- Install Optiprune as a dev dependency via pnpm, npm, or yarn:
73
+ | Flag | Description | Default |
74
+ | --- | --- | --- |
75
+ | `-r, --rootDir <path>` | Root directory of the project. | Current working directory |
76
+ | `-e, --entry <patterns...>` | Entry-point patterns, globs, or file paths. | `[]` |
77
+ | `-x, --extensions <exts...>` | File extensions to analyze. | `.ts .tsx .js .jsx .vue` |
78
+ | `-i, --ignore <patterns...>` | Glob patterns to ignore. | `[]` |
79
+ | `--no-report-unused-exports` | Disable unused-export reporting. | Enabled |
80
+ | `--no-conventional-entries` | Exclude conventional entries such as `src/index.ts`. | Included |
81
+ | `--include-entry-exports` | Report unused exports declared directly in entry files. | Disabled |
82
+ | `--cycles` | Print detected dependency cycles. | Disabled |
83
+ | `--ignore-tests` | Ignore test files such as `test.ts`, `*.test.ts`, and `__tests__`. | Disabled |
84
+ | `--fail-on <confidence>` | Exit non-zero when findings meet the selected confidence level: `high`, `medium`, `low`, or `none`. | `high` |
85
+ | `--json` | Print the structured analysis report as JSON. | Disabled |
86
+ | `--sarif` | Print SARIF output. | Disabled |
87
+ | `--skip-3` | Skip the SMT constraint-analysis layer. | Disabled |
88
+ | `--skip-4` | Skip the concolic execution-proof layer. | Disabled |
89
+ | `-v, --verbose` | Print verbose output and internal graph state. | Disabled |
90
+ | `--fix <rules...>` | Select fix targets: `files`, `exports`, `dependencies`, `devDependencies`, or `conditions`. | None |
91
+ | `--confidence <level>` | Minimum fix confidence: `high`, `medium+`, or `low+`. | `high` |
92
+ | `--force` | Allow a selected fix when the source edit is otherwise considered unsafe. | Disabled |
93
+ | `--dry-run` | Log planned fixes without changing files. | Disabled |
94
+ | `--cache-from <path>` | Import a JSON cache before analysis. | None |
95
+ | `--cache-to <path>` | Export the resulting cache after analysis. | None |
96
+
97
+ `--confidence`, `--force`, and `--dry-run` require `--fix`. Unknown fix targets are rejected before analysis begins.
98
+
99
+ ## Fixes
100
+
101
+ Fixes are explicit rather than implicit. Start with a dry run, inspect the output, then omit `--dry-run` when the proposed changes are acceptable.
71
102
 
72
103
  ```bash
73
- pnpm add -D @optiprune/core
74
- # or
75
- npm install --save-dev @optiprune/core
76
- # or
77
- yarn add -D @optiprune/core
104
+ npx @optiprune/cli analyze \
105
+ --fix files exports dependencies devDependencies conditions \
106
+ --confidence medium+ \
107
+ --dry-run
108
+ ```
109
+
110
+ | Target | Applies to |
111
+ | --- | --- |
112
+ | `files` | Verified unreachable files. |
113
+ | `exports` | Verified unused exports and members. |
114
+ | `dependencies` | Unused runtime dependencies. |
115
+ | `devDependencies` | Unused development dependencies. |
116
+ | `conditions` | Verified constant conditions. |
78
117
 
79
- ---
118
+ `--force` changes the safety decision for the selected fix operation; it does not make an unverified finding correct. Use it only when the source edit has been reviewed.
80
119
 
81
- ## Usage
120
+ ## Cache
82
121
 
83
- Run Optiprune from your project root:
122
+ Use cache files to reuse analysis state in local workflows or CI:
84
123
 
85
124
  ```bash
86
- npx @optiprune/cli
125
+ npx @optiprune/cli analyze \
126
+ --cache-from .optiprune/cache.json \
127
+ --cache-to .optiprune/cache.json
128
+
129
+ npx @optiprune/cli export-cache .optiprune/cache.json
130
+ npx @optiprune/cli import-cache .optiprune/cache.json
87
131
  ```
88
132
 
89
- ### CLI Options (@optiprune/cli)
133
+ `export-cache` and `import-cache` accept `-r, --rootDir <path>` when the cache belongs to a directory other than the current working directory.
90
134
 
91
- | Flag | Description | Default |
92
- | :--- | :--- | :--- |
93
- | `-r, --rootDir` | Project root directory | `process.cwd()` |
94
- | `-e, --entry` | Entry point patterns (glob) | `[]` |
95
- | `-i, --ignore` | Patterns to ignore | `[]` |
96
- | `--no-report-unused-exports` | Disable unused export reporting | `false` |
97
- | `--fail-on` | Fail on confidence (high/medium/low/none) | `high` |
98
- | `--json` | Output as JSON | `false` |
99
- | `--sarif` | Output as SARIF | `false` |
100
- | `--skip-3` | Skip Layer 3 (SMT Constraint Solver) | `false` |
101
- | `--skip-4` | Skip Layer 4 (Concolic Execution Proofs) | `false` |
102
- | `--fix <targets...>` | Required fix targets: `files`, `exports`, `dependencies`, `devDependencies`, or `conditions` | none |
103
- | `--confidence <level>` | Minimum fix confidence: `high`, `medium+`, `low+`, or `all` | `high` |
104
- | `--force` | Override the configured confidence safety boundary | `false` |
105
- | `--dry-run` | Report planned fixes without modifying files | `false` |
106
-
107
- For example, to fix files, exports, dependencies, and development dependencies with low-confidence findings included, run:
135
+ ## Configuration
136
+
137
+ OptiPrune reads configuration through the Core loader. Supported sources include:
138
+
139
+ | Source | Notes |
140
+ | --- | --- |
141
+ | `optiprune.json` | Standard JSON configuration. |
142
+ | `optiprune.jsonc` | JSON with comments and trailing commas. |
143
+ | `optiprune.config.ts` | TypeScript configuration with a default export. |
144
+ | `optiprune.config.js` | JavaScript ESM configuration with a default export. |
145
+ | `optiprune.config.mjs` | JavaScript ESM configuration with a default export. |
146
+ | `package.json#optiprune` | Package field configuration. |
147
+
148
+ See [`config.md`](./config.md) for the configuration reference and [`schema.json`](https://github.com/optiprune/core/blob/main/schema.json) for the authoritative schema.
149
+
150
+ ## Headless Core API
151
+
152
+ Use `@optiprune/core` directly when the CLI is not the right integration boundary:
108
153
 
109
154
  ```bash
110
- npx @optiprune/cli analyze --fix files exports dependencies devDependencies --confidence low+
155
+ npm install @optiprune/core
111
156
  ```
112
157
 
113
- Use `--force` only when you explicitly accept fixes below the configured safety boundary:
158
+ ```
159
+ import { analyze, shouldFail } from "@optiprune/core";
114
160
 
115
- ```bash
116
- npx @optiprune/cli analyze --fix exports --confidence high --force
161
+ const report = await analyze({
162
+ rootDir: process.cwd(),
163
+ entry: ["src/index.ts"],
164
+ output: "json",
165
+ });
166
+
167
+ console.log(report.summary);
168
+
169
+ if (shouldFail(report, "high")) {
170
+ process.exitCode = 1;
171
+ }
172
+ ```
173
+
174
+ The Core package also exposes cache helpers, `applyFixes`, reporters, and public types:
175
+
176
+ ```
177
+ import { applyFixes, exportCache, importCache } from "@optiprune/core";
178
+ import { formatSarif, formatTerminal } from "@optiprune/core/reporters";
179
+ import type { AnalysisReport, AnalyzerOptions, Finding } from "@optiprune/core/types";
117
180
  ```
118
181
 
119
- `--confidence`, `--force`, and `--dry-run` require at least one `--fix` target. Unsupported or unknown targets are rejected before analysis begins.
182
+ An `AnalysisReport` contains summary counts, findings, entry points, module records, exports, dependency edges, and strongly connected components.
183
+
184
+ ## Plugin model
185
+
186
+ Plugins provide source-aware context for frameworks, build tools, test runners, runtimes, package managers, and workspace conventions. They can contribute entry patterns, mark files or packages as used, interpret project metadata, and participate in analysis lifecycle hooks.
120
187
 
121
- ---
188
+ Browse the [Core plugin directory](https://github.com/optiprune/core/tree/main/src/plugins) to inspect the current source-backed set and the `AnalyzerPlugin`/`PluginAdapter` contracts.
189
+
190
+ ## Development
191
+
192
+ Build the package from this repository:
193
+
194
+ ```bash
195
+ npm run build
196
+ npm test
197
+ ```
122
198
 
123
- ## 🤝 Join the Revolution
124
- OptiPrune isn't just a tool. It's a technical statement. Help us save the world from dirty code.
199
+ The Core repository uses Vitest for its test suite. The workflow badges above reflect the status reported by GitHub Actions rather than a hard-coded claim in this README.
125
200
 
126
- **GitHub:** [DreamLongYT/optiprune](https://github.com/optiprune/core)
127
- **Web:** [opti.drml.int.yt](https://opti.drml.int.yt)
201
+ ## Links
128
202
 
129
- ---
130
- # Config
131
- To setup OptiPrune, see [config.md](config.md) for more
203
+ | Resource | Link |
204
+ | --- | --- |
205
+ | CLI repository | [github.com/optiprune/cli](https://github.com/optiprune/cli) |
206
+ | Core repository | [github.com/optiprune/core](https://github.com/optiprune/core) |
207
+ | CLI package | [npmjs.com/package/@optiprune/cli](https://www.npmjs.com/package/@optiprune/cli) |
208
+ | Core package | [npmjs.com/package/@optiprune/core](https://www.npmjs.com/package/@optiprune/core) |
209
+ | Documentation site | [opti.drml.int.yt](https://opti.drml.int.yt/) |
210
+ | License | [MIT](./LICENSE) |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "optiprune",
3
- "version": "2.2.21",
3
+ "version": "2.2.22",
4
4
  "description": "CLI for resilient static dead-code analyzer for TypeScript and JavaScript workspaces.",
5
5
  "type": "module",
6
6
  "bin": {