crap-ts 0.2.0 → 2.0.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/CONTRIBUTING.md +1 -0
- package/README.md +329 -67
- package/package.json +3 -15
- package/src/cli.js +105 -105
- package/src/crap-analysis.d.ts +40 -19
- package/src/crap-analysis.js +485 -200
- package/src/crap-runner.d.ts +15 -1
- package/src/crap-runner.js +415 -76
- package/src/crap-score.d.ts +4 -29
- package/src/crap-score.js +0 -51
- package/src/index.d.ts +2 -10
- package/src/index.js +1 -7
package/CONTRIBUTING.md
CHANGED
package/README.md
CHANGED
|
@@ -14,10 +14,10 @@ Risky code plus weak tests, now with a name your manager will remember.
|
|
|
14
14
|
|
|
15
15
|
## Works Out Of The Box With
|
|
16
16
|
|
|
17
|
-
- TypeScript source files
|
|
17
|
+
- TypeScript source files (`.ts`, `.tsx`, `.mts`, `.cts`)
|
|
18
|
+
- Istanbul-format `coverage-final.json` with statement and function maps:
|
|
19
|
+
Vitest (V8 or Istanbul provider), Jest, nyc/Istanbul
|
|
18
20
|
- Consumer-provided TypeScript versions `>=5.9 <8`, including TypeScript 7
|
|
19
|
-
- Vitest V8 coverage, Jest coverage, or nyc/Istanbul coverage that includes
|
|
20
|
-
function maps
|
|
21
21
|
- npm, Bun, pnpm, or any runner that can produce coverage before `crap-ts` reads
|
|
22
22
|
it
|
|
23
23
|
|
|
@@ -35,10 +35,8 @@ TypeScript 7 projects do not get a nested TypeScript 5.x install.
|
|
|
35
35
|
|
|
36
36
|
## Usage
|
|
37
37
|
|
|
38
|
-
Run the built-in TypeScript AST audit against coverage-final data:
|
|
39
|
-
|
|
40
38
|
```sh
|
|
41
|
-
npx crap-ts score --coverage-command "npm run test:coverage -- --coverage.reporter=json" --coverage-file coverage/unit/coverage-final.json --include src
|
|
39
|
+
npx crap-ts score --coverage-command "npm run test:coverage -- --coverage.reporter=json" --coverage-file coverage/unit/coverage-final.json --include src/ --exclude .test.ts
|
|
42
40
|
```
|
|
43
41
|
|
|
44
42
|
Add package scripts:
|
|
@@ -58,13 +56,12 @@ Put the long bits in `.crap-ts.json`:
|
|
|
58
56
|
|
|
59
57
|
```json
|
|
60
58
|
{
|
|
61
|
-
"coverageCommand": "npm run test:coverage -- --coverage.reporter=json
|
|
59
|
+
"coverageCommand": "npm run test:coverage -- --coverage.reporter=json",
|
|
62
60
|
"coverageFile": "coverage/unit/coverage-final.json",
|
|
63
|
-
"include": ["src"],
|
|
61
|
+
"include": ["src/"],
|
|
62
|
+
"exclude": [".test.ts"],
|
|
64
63
|
"max": 30,
|
|
65
|
-
"warn": 25
|
|
66
|
-
"minLines": 10,
|
|
67
|
-
"limit": 20
|
|
64
|
+
"warn": 25
|
|
68
65
|
}
|
|
69
66
|
```
|
|
70
67
|
|
|
@@ -72,33 +69,151 @@ Put the long bits in `.crap-ts.json`:
|
|
|
72
69
|
reported on stderr, but only scores above `max` fail the build. Leave it out to
|
|
73
70
|
skip warnings.
|
|
74
71
|
|
|
75
|
-
|
|
76
|
-
summary:
|
|
72
|
+
In CI, keep the machine-readable report even when the gate fails:
|
|
77
73
|
|
|
78
74
|
```sh
|
|
79
|
-
npx crap-ts --
|
|
75
|
+
npx crap-ts score --json > crap-report.json
|
|
80
76
|
```
|
|
81
77
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
The direct CRAP audit:
|
|
85
|
-
|
|
86
|
-
- reads function coverage from Istanbul/V8 `coverage-final.json`
|
|
87
|
-
- calculates function cyclomatic complexity from the TypeScript AST
|
|
88
|
-
- prints the highest-risk functions
|
|
89
|
-
- fails strict mode when any score is above the configured threshold
|
|
78
|
+
The report is printed in full before `crap-ts` exits non-zero, and the coverage
|
|
79
|
+
command's own output is sent to stderr so stdout stays valid JSON.
|
|
90
80
|
|
|
91
|
-
|
|
81
|
+
## How Scoring Works
|
|
92
82
|
|
|
93
83
|
```text
|
|
94
84
|
crap = complexity^2 * (1 - coverage)^3 + complexity
|
|
95
85
|
```
|
|
96
86
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
87
|
+
Scores are compared unrounded: with `max` 30, a score of exactly 30 passes and
|
|
88
|
+
30.01 fails.
|
|
89
|
+
|
|
90
|
+
### Source inventory
|
|
91
|
+
|
|
92
|
+
`crap-ts score` walks the working directory for `.ts`, `.tsx`, `.mts` and
|
|
93
|
+
`.cts` files, skipping `node_modules`, directories whose name starts with `.`,
|
|
94
|
+
symbolic links and `.d.ts` declarations. `include` and `exclude` match paths
|
|
95
|
+
relative to the working directory. A pattern ending in `/**` matches files
|
|
96
|
+
below a directory at that root. For example, `src/**` matches `src/a.ts`
|
|
97
|
+
and `src/lib/b.ts`. Plain patterns match substrings. For example, `src/`
|
|
98
|
+
also matches `tests/fixtures/src/c.ts`. An asterisk outside the trailing
|
|
99
|
+
`/**` is a usage error. Both forms accept `/` and `\` as
|
|
100
|
+
separators. The same filters apply to files named in the coverage JSON,
|
|
101
|
+
which can have other extensions.
|
|
102
|
+
|
|
103
|
+
An inventoried file with no coverage entry **fails the gate** if it contains a
|
|
104
|
+
function body or an executable module-level statement. Declaration-only files
|
|
105
|
+
are exempt: type aliases, interfaces, `declare` statements, re-exports
|
|
106
|
+
(`export … from`), binding imports (`import { x } from`), type-only
|
|
107
|
+
`import type X = require()` and namespaces that hold only those. A side-effect
|
|
108
|
+
import (`import "./setup"`) counts as executable. The functions in such a file
|
|
109
|
+
are still listed, with `evidence: "missing"` unless an ignore comment covers
|
|
110
|
+
them.
|
|
111
|
+
|
|
112
|
+
Use `exclude` for tests, fixtures and tooling that your coverage run does not
|
|
113
|
+
instrument.
|
|
114
|
+
|
|
115
|
+
### Coverage
|
|
116
|
+
|
|
117
|
+
Coverage is per function, from Istanbul statement counters:
|
|
118
|
+
|
|
119
|
+
- A statement belongs to the innermost function whose source range contains
|
|
120
|
+
the statement's start position. Nested functions and callbacks get their own
|
|
121
|
+
rows and never count toward their parent. A statement that starts exactly at
|
|
122
|
+
a function's first token, such as Istanbul's initializer counter for
|
|
123
|
+
`const f = () => …`, belongs to the enclosing scope: it runs when the
|
|
124
|
+
enclosing code does, not when the function is called.
|
|
125
|
+
- `coverage` = executed owned statements / owned statements. Only positive
|
|
126
|
+
counters count as executed.
|
|
127
|
+
- A function with no owned statements (for example `() => {}`) uses its own
|
|
128
|
+
Istanbul function counter instead (`evidence: "function"`): 100% if it ran,
|
|
129
|
+
0% if not. A counter inside the function is preferred; one placed exactly
|
|
130
|
+
at the function's first token (as per-line V8 converters do) is used when
|
|
131
|
+
there is none inside.
|
|
132
|
+
- A function with no counter at all is reported at 0% with
|
|
133
|
+
`evidence: "missing"` and **fails the gate**: its file has no coverage
|
|
134
|
+
entry, or the function was added after coverage ran.
|
|
135
|
+
- The exception is code under a coverage ignore comment, which coverage tools
|
|
136
|
+
leave without counters on purpose. Such a function is listed with
|
|
137
|
+
`evidence: "ignored"` and sorts after every other row. By default, its
|
|
138
|
+
level is `ignored` and it does not affect the gate. Its `coverage` (0) and
|
|
139
|
+
`crap` are shown for reference only. crap-ts recognises `istanbul`, `v8` and `c8` comments
|
|
140
|
+
(block or line):
|
|
141
|
+
- `ignore next` covers the next statement or declaration, including any
|
|
142
|
+
functions inside it.
|
|
143
|
+
- `ignore next N` covers the next N lines.
|
|
144
|
+
- `ignore if` and `ignore else`, before an `if` statement, cover its `if`
|
|
145
|
+
branch or its `else` branch.
|
|
146
|
+
- `ignore start` … `ignore stop` covers the region between them.
|
|
147
|
+
- `ignore file` covers the whole file. By default, the file needs no
|
|
148
|
+
coverage entry.
|
|
149
|
+
|
|
150
|
+
Comment-like text inside strings, template literals, regular expressions
|
|
151
|
+
and JSX text is not treated as a comment.
|
|
152
|
+
|
|
153
|
+
An ignore comment only exempts a function that has no counters. If the
|
|
154
|
+
coverage tool still measured it, its counters are used.
|
|
155
|
+
|
|
156
|
+
Set `failOnIgnored: true` or pass `--fail-on-ignored` to fail on audited
|
|
157
|
+
functions with `evidence: "ignored"`. The text report names these functions
|
|
158
|
+
even when they fall outside `--limit`. An executable file with an
|
|
159
|
+
`ignore file` comment also needs a coverage entry when this setting is on.
|
|
160
|
+
- Module-level statements belong to no function.
|
|
161
|
+
|
|
162
|
+
Pass `--coverage-file` more than once (or a `coverageFile` array) to merge
|
|
163
|
+
runs, such as Node and browser suites. Entries for the same file are merged by
|
|
164
|
+
each counter's original source position, never by counter id or by averaging
|
|
165
|
+
percentages.
|
|
166
|
+
|
|
167
|
+
Each listed coverage file must contain at least one source entry. The CLI
|
|
168
|
+
rejects `{}` before applying path filters, exits with code 1, and names the
|
|
169
|
+
empty file. This check also runs with `--report-only`. To accept empty files
|
|
170
|
+
as in 1.0, set `allowEmptyCoverage: true` or pass `--allow-empty-coverage`.
|
|
171
|
+
|
|
172
|
+
`crap-ts` refuses coverage whose statements start outside the current source
|
|
173
|
+
(a line that does not exist, or a column past the end of the line): re-run
|
|
174
|
+
coverage after changing the source. This is a cheap staleness guard, not a
|
|
175
|
+
content hash; edits that keep every statement position valid are not detected.
|
|
176
|
+
Malformed coverage (a top-level value that is not an object, missing maps,
|
|
177
|
+
negative or non-integer counters, invalid or reversed positions) is an error,
|
|
178
|
+
not a silent zero.
|
|
179
|
+
|
|
180
|
+
V8-based coverage (for example Vitest's default provider) reports one
|
|
181
|
+
statement per line, so a function's coverage is line-granular there. Use the
|
|
182
|
+
Istanbul provider when you need statement precision.
|
|
183
|
+
|
|
184
|
+
### Complexity
|
|
185
|
+
|
|
186
|
+
Each function starts at 1 and adds 1 for each:
|
|
187
|
+
|
|
188
|
+
- `if` (an `else if` is another `if`)
|
|
189
|
+
- ternary `?:`
|
|
190
|
+
- `for`, `for…in`, `for…of`, `while`, `do…while`
|
|
191
|
+
- `catch`
|
|
192
|
+
- `case` in a `switch` (`default` does not count)
|
|
193
|
+
- `&&`, `||`, `??`
|
|
194
|
+
- logical assignment `&&=`, `||=`, `??=`
|
|
195
|
+
- default values: parameters (`a = 1`), destructuring in declarations and
|
|
196
|
+
parameters (`const { a = 1 } = x`, `[a = 1]`), and shorthand defaults in
|
|
197
|
+
assignment patterns (`({ a = 1 } = x)`)
|
|
198
|
+
|
|
199
|
+
These add nothing: `else`, `finally`, `default:`, optional chaining (`?.`),
|
|
200
|
+
early `return`/`break`/`continue`, JSX, and anything at the type level (type
|
|
201
|
+
annotations, conditional types). Decisions inside a nested function count only
|
|
202
|
+
toward that nested function.
|
|
203
|
+
|
|
204
|
+
Functions are declarations, function expressions, arrow functions, methods,
|
|
205
|
+
constructors, getters and setters that have a body. Overload signatures and
|
|
206
|
+
abstract methods have no body and no row. Unnamed functions are reported as
|
|
207
|
+
`<anonymous>`; their file, line and column identify them.
|
|
208
|
+
|
|
209
|
+
### Rows and thresholds
|
|
210
|
+
|
|
211
|
+
`minLines` (default 10) hides functions shorter than that many lines from both
|
|
212
|
+
reports and the gate. A function fails when its score is above `max` or its
|
|
213
|
+
evidence is `missing`. With `failOnIgnored` enabled, it also fails when its
|
|
214
|
+
evidence is `ignored`.
|
|
215
|
+
|
|
216
|
+
When the CRAP check flags a hotspot:
|
|
102
217
|
|
|
103
218
|
1. Name the affected function or file in the review.
|
|
104
219
|
2. Confirm there is direct test coverage for that path.
|
|
@@ -111,58 +226,204 @@ branching visible before it becomes a maintenance tax.
|
|
|
111
226
|
## CLI
|
|
112
227
|
|
|
113
228
|
```sh
|
|
114
|
-
crap-ts score [
|
|
115
|
-
crap-ts
|
|
229
|
+
crap-ts score [options]
|
|
230
|
+
crap-ts help
|
|
116
231
|
```
|
|
117
232
|
|
|
118
|
-
|
|
233
|
+
`score` is required: running `crap-ts` with options but no command is a usage
|
|
234
|
+
error.
|
|
119
235
|
|
|
120
236
|
- `--config <path>`: read config from a JSON file. Defaults to `.crap-ts.json`
|
|
121
237
|
when present.
|
|
122
|
-
- `--coverage-file <path>`:
|
|
238
|
+
- `--coverage-file <path>`: Istanbul `coverage-final.json`. Repeat to merge
|
|
239
|
+
several runs. Defaults to `coverage/unit/coverage-final.json`.
|
|
123
240
|
- `--coverage-command <cmd>`: run a coverage command before scoring.
|
|
241
|
+
- `--include <pattern>`: only analyze matching paths. Repeatable. Quote
|
|
242
|
+
patterns like `'src/**'` to pass them directly to crap-ts.
|
|
243
|
+
- `--exclude <pattern>`: skip matching paths. Repeatable.
|
|
244
|
+
- `--fail-on-ignored`: fail on ignored functions and require coverage entries
|
|
245
|
+
for executable files with an `ignore file` comment.
|
|
246
|
+
- `--allow-empty-coverage`: accept listed coverage files containing `{}`.
|
|
247
|
+
- `--json`: print the JSON report instead of the text report.
|
|
124
248
|
- `--all`: print every analyzed function instead of applying `--limit`.
|
|
125
|
-
- `--
|
|
126
|
-
- `--
|
|
127
|
-
- `--max <n>`: strict CRAP threshold for direct audit. Defaults to `30`.
|
|
249
|
+
- `--limit <n>`: number of text report rows to print. Defaults to `20`.
|
|
250
|
+
- `--max <n>`: fail when any score is above this. Defaults to `30`.
|
|
128
251
|
- `--warn <n>`: warn, without failing, when a score is above this. Must not
|
|
129
|
-
exceed `--max
|
|
252
|
+
exceed `--max`. Off by default.
|
|
130
253
|
- `--min-lines <n>`: ignore functions shorter than this. Defaults to `10`.
|
|
131
|
-
- `--
|
|
132
|
-
- `--
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
254
|
+
- `--report-only`: print results without failing on the gate.
|
|
255
|
+
- `--skip-coverage`: do not run the coverage command.
|
|
256
|
+
|
|
257
|
+
Exit codes: `0` passed (or `--report-only`), `1` gate failed, coverage
|
|
258
|
+
command failed, or coverage input is missing, malformed, stale or rejected
|
|
259
|
+
for being empty, `2` usage or config error (unknown command or flag, invalid
|
|
260
|
+
value, unreadable or invalid config file).
|
|
261
|
+
|
|
262
|
+
When the coverage command fails, `crap-ts` exits `1` before scoring and
|
|
263
|
+
prints no report, not even with `--json`.
|
|
264
|
+
|
|
265
|
+
The text report is for people and may change in any release. Parse `--json`
|
|
266
|
+
instead.
|
|
267
|
+
|
|
268
|
+
## Configuration
|
|
269
|
+
|
|
270
|
+
`.crap-ts.json` (or the file passed to `--config`) is a JSON object with these
|
|
271
|
+
keys. Unknown keys are an error, so a typo or a removed option never fails
|
|
272
|
+
silently. CLI flags override config. Naturally. The command line gets the last
|
|
273
|
+
word, because you'd be mad at me if it didn't, and I'd also be mad at me.
|
|
274
|
+
|
|
275
|
+
| Key | Type | CLI flag | Default |
|
|
276
|
+
| --- | --- | --- | --- |
|
|
277
|
+
| `coverageCommand` | string | `--coverage-command` | none |
|
|
278
|
+
| `coverageFile` | string or string[] | `--coverage-file` | `coverage/unit/coverage-final.json` |
|
|
279
|
+
| `include` | string or string[] | `--include` | everything |
|
|
280
|
+
| `exclude` | string or string[] | `--exclude` | nothing |
|
|
281
|
+
| `failOnIgnored` | boolean | `--fail-on-ignored` | `false` |
|
|
282
|
+
| `allowEmptyCoverage` | boolean | `--allow-empty-coverage` | `false` |
|
|
283
|
+
| `json` | boolean | `--json` | `false` |
|
|
284
|
+
| `all` | boolean | `--all` | `false` |
|
|
285
|
+
| `limit` | number | `--limit` | `20` |
|
|
286
|
+
| `max` | number | `--max` | `30` |
|
|
287
|
+
| `warn` | number | `--warn` | none |
|
|
288
|
+
| `minLines` | number | `--min-lines` | `10` |
|
|
289
|
+
| `reportOnly` | boolean | `--report-only` | `false` |
|
|
290
|
+
| `skipCoverage` | boolean | `--skip-coverage` | `false` |
|
|
291
|
+
|
|
292
|
+
## JSON Report
|
|
293
|
+
|
|
294
|
+
`crap-ts score --json` prints one JSON document to stdout. Schema version `1`
|
|
295
|
+
is stable for crap-ts 1.x and 2.x: fields are only added, never renamed, removed or
|
|
296
|
+
changed in meaning. A breaking change bumps `schemaVersion`.
|
|
148
297
|
|
|
149
298
|
```json
|
|
150
299
|
{
|
|
151
|
-
"
|
|
152
|
-
"
|
|
153
|
-
"
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
300
|
+
"schemaVersion": 1,
|
|
301
|
+
"thresholds": { "max": 30, "warn": 25, "minLines": 10 },
|
|
302
|
+
"summary": {
|
|
303
|
+
"files": 28,
|
|
304
|
+
"functions": 126,
|
|
305
|
+
"aboveMax": 0,
|
|
306
|
+
"warning": 2,
|
|
307
|
+
"missingEvidence": 0,
|
|
308
|
+
"ignored": 0,
|
|
309
|
+
"missingFiles": 0,
|
|
310
|
+
"passed": true
|
|
311
|
+
},
|
|
312
|
+
"functions": [
|
|
313
|
+
{
|
|
314
|
+
"file": "src/station/docking.ts",
|
|
315
|
+
"name": "assignDockingPort",
|
|
316
|
+
"line": 12,
|
|
317
|
+
"column": 8,
|
|
318
|
+
"endLine": 41,
|
|
319
|
+
"lines": 30,
|
|
320
|
+
"complexity": 6,
|
|
321
|
+
"coverage": 87.5,
|
|
322
|
+
"statements": 16,
|
|
323
|
+
"coveredStatements": 14,
|
|
324
|
+
"evidence": "statements",
|
|
325
|
+
"crap": 6.0703125,
|
|
326
|
+
"level": "ok"
|
|
327
|
+
}
|
|
328
|
+
],
|
|
329
|
+
"missingFiles": []
|
|
161
330
|
}
|
|
162
331
|
```
|
|
163
332
|
|
|
164
|
-
|
|
165
|
-
|
|
333
|
+
- `thresholds.warn` is `null` when no warn threshold is set.
|
|
334
|
+
- `summary.files`: source files analyzed. `functions`: rows listed.
|
|
335
|
+
`aboveMax`: rows with `crap > max`, excluding rows with evidence `ignored`.
|
|
336
|
+
`warning`: rows with level `warn`.
|
|
337
|
+
`missingEvidence`: rows with evidence `missing`. `ignored`: rows with
|
|
338
|
+
evidence `ignored`. `missingFiles`: length of `missingFiles`.
|
|
339
|
+
- `summary.passed` is true when `aboveMax`, `missingEvidence` and
|
|
340
|
+
`missingFiles` are all zero. With `failOnIgnored` enabled, `ignored` must
|
|
341
|
+
also be zero. The verdict is independent of `--report-only`.
|
|
342
|
+
- `functions` lists every function at or above `minLines`, never truncated by
|
|
343
|
+
`--limit`, sorted by `crap` descending, then `file`, `line`, `column`, with
|
|
344
|
+
`ignored` rows after all others.
|
|
345
|
+
- `file` is relative to the working directory with `/` separators. `line` and
|
|
346
|
+
`column` are one-based and point at the function's first token.
|
|
347
|
+
- `coverage` is a percentage from 0 to 100. `crap` and `coverage` are
|
|
348
|
+
unrounded.
|
|
349
|
+
- `evidence` is `statements`, `function`, `ignored` or `missing` (see
|
|
350
|
+
[Coverage](#coverage)).
|
|
351
|
+
- `level` is `fail` (above `max`, missing evidence, or ignored evidence with
|
|
352
|
+
`failOnIgnored` enabled), `warn` (above `warn`), `ignored` or `ok`. With
|
|
353
|
+
ignored evidence, `coverage` is 0 and `crap` is the score at 0% coverage,
|
|
354
|
+
for reference only.
|
|
355
|
+
- `missingFiles` lists inventoried files with executable code and no coverage
|
|
356
|
+
entry, sorted.
|
|
357
|
+
|
|
358
|
+
## Programmatic API
|
|
359
|
+
|
|
360
|
+
The package root is the only entry point. These exports, and the types in
|
|
361
|
+
`src/index.d.ts`, are the 2.x API:
|
|
362
|
+
|
|
363
|
+
- `runCrapAudit(options?: CrapAuditOptions): Promise<number>` runs the same
|
|
364
|
+
audit as the CLI and resolves to its exit code (`2` for invalid options,
|
|
365
|
+
including non-finite numbers; `1` for a failed gate, a failed coverage
|
|
366
|
+
command or a missing coverage file). It rejects with an `Error`
|
|
367
|
+
when coverage or source input is malformed or stale, or when a required
|
|
368
|
+
coverage file is empty and `allowEmptyCoverage` is false. Options include
|
|
369
|
+
`coverageCommand`, `coveragePaths`, `cwd`, `includes`, `excludes`,
|
|
370
|
+
`failOnIgnored`, `allowEmptyCoverage`,
|
|
371
|
+
`json`, `all`, `limit`, `maxScore`, `warnScore`, `minLines`, `reportOnly`
|
|
372
|
+
and `skipCoverage`.
|
|
373
|
+
- `analyzeFileRisk({ filePath, sourceText, coverage?, minLines?, sourceFilePath? }): Promise<FunctionRisk[]>`
|
|
374
|
+
scores the functions in one source text. `coverage` is that file's entry
|
|
375
|
+
from `coverage-final.json`. A function without counters has
|
|
376
|
+
`evidence: "missing"`, or `"ignored"` when an ignore comment covers it.
|
|
377
|
+
`minLines` defaults to 1.
|
|
378
|
+
- `calculateCyclomaticComplexity(node): number` applies the complexity rules
|
|
379
|
+
above to a TypeScript AST node from the consumer's `typescript` package.
|
|
380
|
+
- `calculateCrapScore({ complexity, coverage }): number` is the formula, with
|
|
381
|
+
`coverage` as a percentage from 0 to 100.
|
|
382
|
+
|
|
383
|
+
Everything else under `src/` is internal and may change in any release.
|
|
384
|
+
|
|
385
|
+
## Upgrading From 1.x
|
|
386
|
+
|
|
387
|
+
2.0 has two breaking changes. Scores and the JSON schema do not change.
|
|
388
|
+
|
|
389
|
+
- A listed coverage file that contains `{}` now fails with exit code 1, also
|
|
390
|
+
with `--report-only`. Fix the coverage run so that it writes entries. To
|
|
391
|
+
accept empty files as in 1.0, pass `--allow-empty-coverage` or set
|
|
392
|
+
`allowEmptyCoverage: true`.
|
|
393
|
+
- An `include` or `exclude` pattern with `*` anywhere other than a trailing
|
|
394
|
+
`/**` now exits with code 2. Replace `src/*` or `src/**/*.ts` with `src/**`,
|
|
395
|
+
or use a plain substring pattern such as `src/`.
|
|
396
|
+
|
|
397
|
+
New options:
|
|
398
|
+
|
|
399
|
+
- `--fail-on-ignored` (`failOnIgnored`) fails the gate on functions that
|
|
400
|
+
coverage ignore comments exempt.
|
|
401
|
+
- A pattern ending in `/**` matches only below that directory at the working
|
|
402
|
+
directory root. Plain patterns still match substrings.
|
|
403
|
+
|
|
404
|
+
## Upgrading From 0.x
|
|
405
|
+
|
|
406
|
+
1.0 changes scores and removes an unused mode. Expect different numbers.
|
|
407
|
+
|
|
408
|
+
- Coverage is now owned-statement coverage per function, not the 0/100
|
|
409
|
+
function-called flag. Partially tested functions score lower; a function
|
|
410
|
+
whose callback never runs no longer looks covered.
|
|
411
|
+
- Nested functions no longer add their decisions to the parent. Parameter and
|
|
412
|
+
destructuring defaults and logical assignment now add decisions.
|
|
413
|
+
Constructors, getters and setters now get rows.
|
|
414
|
+
- Source files without coverage entries, and functions without any coverage
|
|
415
|
+
counter, now fail the gate, unless a coverage ignore comment covers them. Add `exclude` entries for tests and tooling
|
|
416
|
+
(for example `"exclude": [".test.ts", "vitest.config"]`) if your `include`
|
|
417
|
+
matches them.
|
|
418
|
+
- `--metrics`, `--max-crap`, the `metrics` and `maxCrap` config keys, and
|
|
419
|
+
running without the `score` command are gone, along with the
|
|
420
|
+
`buildCrapScores` and `getCoverageForFile` exports.
|
|
421
|
+
- The `crap-ts/crap-analysis`, `crap-ts/crap-runner` and `crap-ts/crap-score`
|
|
422
|
+
subpath exports are gone; import from `crap-ts`. `extractCoverageFunctions`
|
|
423
|
+
and `formatRiskLine` are no longer exported, and `analyzeFileRisk` takes an
|
|
424
|
+
Istanbul `coverage` entry instead of `coverageFunctions`.
|
|
425
|
+
- `CrapAuditOptions.coveragePath` is now `coveragePaths` (an array).
|
|
426
|
+
- Unknown `.crap-ts.json` keys are an error.
|
|
166
427
|
|
|
167
428
|
## Replacing Local CRAP Scripts
|
|
168
429
|
|
|
@@ -182,9 +443,10 @@ With `.crap-ts.json`:
|
|
|
182
443
|
|
|
183
444
|
```json
|
|
184
445
|
{
|
|
185
|
-
"coverageCommand": "bun x vitest run --coverage --coverage.reporter=json
|
|
446
|
+
"coverageCommand": "bun x vitest run --coverage --coverage.reporter=json",
|
|
186
447
|
"coverageFile": "coverage/unit/coverage-final.json",
|
|
187
|
-
"include": ["
|
|
448
|
+
"include": ["app/", "scripts/"],
|
|
449
|
+
"exclude": [".test.ts"]
|
|
188
450
|
}
|
|
189
451
|
```
|
|
190
452
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "crap-ts",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Small CRAP-score audit helper for TypeScript projects.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,18 +14,6 @@
|
|
|
14
14
|
".": {
|
|
15
15
|
"types": "./src/index.d.ts",
|
|
16
16
|
"default": "./src/index.js"
|
|
17
|
-
},
|
|
18
|
-
"./crap-analysis": {
|
|
19
|
-
"types": "./src/crap-analysis.d.ts",
|
|
20
|
-
"default": "./src/crap-analysis.js"
|
|
21
|
-
},
|
|
22
|
-
"./crap-runner": {
|
|
23
|
-
"types": "./src/crap-runner.d.ts",
|
|
24
|
-
"default": "./src/crap-runner.js"
|
|
25
|
-
},
|
|
26
|
-
"./crap-score": {
|
|
27
|
-
"types": "./src/crap-score.d.ts",
|
|
28
|
-
"default": "./src/crap-score.js"
|
|
29
17
|
}
|
|
30
18
|
},
|
|
31
19
|
"files": [
|
|
@@ -35,8 +23,8 @@
|
|
|
35
23
|
"LICENSE"
|
|
36
24
|
],
|
|
37
25
|
"scripts": {
|
|
38
|
-
"check": "node --check src/cli.js && node --check src/crap-analysis.js && node --check src/crap-runner.js && node --check src/crap-score.js",
|
|
39
|
-
"score": "node src/cli.js score --
|
|
26
|
+
"check": "node --check src/cli.js && node --check src/crap-analysis.js && node --check src/crap-runner.js && node --check src/crap-score.js && node --check src/index.js",
|
|
27
|
+
"score": "node src/cli.js score --coverage-file fixtures/coverage-final.json --coverage-file fixtures/risky-coverage-final.json --include fixtures/ --min-lines 1 --report-only",
|
|
40
28
|
"test": "node --test tests/*.test.js",
|
|
41
29
|
"test:consumer-typescript": "node scripts/verify-consumer-typescript.mjs",
|
|
42
30
|
"validate": "npm run test && npm run score",
|