crap-ts 2.0.0 → 2.1.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 +51 -13
- package/package.json +1 -1
- package/src/cli.js +17 -1
- package/src/crap-runner.d.ts +2 -0
- package/src/crap-runner.js +53 -9
package/README.md
CHANGED
|
@@ -36,7 +36,7 @@ TypeScript 7 projects do not get a nested TypeScript 5.x install.
|
|
|
36
36
|
## Usage
|
|
37
37
|
|
|
38
38
|
```sh
|
|
39
|
-
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/**'
|
|
40
40
|
```
|
|
41
41
|
|
|
42
42
|
Add package scripts:
|
|
@@ -58,8 +58,7 @@ Put the long bits in `.crap-ts.json`:
|
|
|
58
58
|
{
|
|
59
59
|
"coverageCommand": "npm run test:coverage -- --coverage.reporter=json",
|
|
60
60
|
"coverageFile": "coverage/unit/coverage-final.json",
|
|
61
|
-
"include": ["src
|
|
62
|
-
"exclude": [".test.ts"],
|
|
61
|
+
"include": ["src/**"],
|
|
63
62
|
"max": 30,
|
|
64
63
|
"warn": 25
|
|
65
64
|
}
|
|
@@ -97,8 +96,18 @@ below a directory at that root. For example, `src/**` matches `src/a.ts`
|
|
|
97
96
|
and `src/lib/b.ts`. Plain patterns match substrings. For example, `src/`
|
|
98
97
|
also matches `tests/fixtures/src/c.ts`. An asterisk outside the trailing
|
|
99
98
|
`/**` is a usage error. Both forms accept `/` and `\` as
|
|
100
|
-
separators. The same filters
|
|
101
|
-
|
|
99
|
+
separators. The same filters, including the test-file default below, apply
|
|
100
|
+
to files named in the coverage JSON. Coverage entries for `.js`, `.jsx`,
|
|
101
|
+
`.mjs` and `.cjs` files are scored too. Coverage entries for other files, such
|
|
102
|
+
as CSS or JSON modules that Vitest reports for side-effect imports, are not
|
|
103
|
+
scored.
|
|
104
|
+
|
|
105
|
+
If the filters select no source files, `crap-ts` exits `1` before scoring,
|
|
106
|
+
also with `--report-only`. If only the test-file default removed the matching
|
|
107
|
+
files, the message names `--include-tests`.
|
|
108
|
+
|
|
109
|
+
If the selected files hold no function with at least `--min-lines` lines,
|
|
110
|
+
the gate fails: a gate that checks nothing never passes. With `--report-only`, `crap-ts` prints a warning and exits `0`.
|
|
102
111
|
|
|
103
112
|
An inventoried file with no coverage entry **fails the gate** if it contains a
|
|
104
113
|
function body or an executable module-level statement. Declaration-only files
|
|
@@ -109,8 +118,15 @@ import (`import "./setup"`) counts as executable. The functions in such a file
|
|
|
109
118
|
are still listed, with `evidence: "missing"` unless an ignore comment covers
|
|
110
119
|
them.
|
|
111
120
|
|
|
112
|
-
|
|
113
|
-
|
|
121
|
+
Test files are skipped by default: file names that end in `.test.<ext>`,
|
|
122
|
+
`.spec.<ext>`, `.test-d.<ext>` or `.spec-d.<ext>`, and any file below a
|
|
123
|
+
`__tests__/` directory. These are the files Vitest and Jest treat as tests by
|
|
124
|
+
default, so their coverage runs do not instrument them. `include` and
|
|
125
|
+
`exclude` apply on top of this default. Pass `--include-tests` (or set
|
|
126
|
+
`includeTests: true`) to analyze test files too.
|
|
127
|
+
|
|
128
|
+
Use `exclude` for other files that your coverage run does not instrument,
|
|
129
|
+
such as fixtures, test helpers and tooling.
|
|
114
130
|
|
|
115
131
|
### Coverage
|
|
116
132
|
|
|
@@ -244,6 +260,8 @@ error.
|
|
|
244
260
|
- `--fail-on-ignored`: fail on ignored functions and require coverage entries
|
|
245
261
|
for executable files with an `ignore file` comment.
|
|
246
262
|
- `--allow-empty-coverage`: accept listed coverage files containing `{}`.
|
|
263
|
+
- `--include-tests`: also analyze test files, which are skipped by default.
|
|
264
|
+
See [Source inventory](#source-inventory).
|
|
247
265
|
- `--json`: print the JSON report instead of the text report.
|
|
248
266
|
- `--all`: print every analyzed function instead of applying `--limit`.
|
|
249
267
|
- `--limit <n>`: number of text report rows to print. Defaults to `20`.
|
|
@@ -254,9 +272,9 @@ error.
|
|
|
254
272
|
- `--report-only`: print results without failing on the gate.
|
|
255
273
|
- `--skip-coverage`: do not run the coverage command.
|
|
256
274
|
|
|
257
|
-
Exit codes: `0` passed (or `--report-only`), `1` gate failed,
|
|
258
|
-
command failed, or coverage input is
|
|
259
|
-
for being empty, `2` usage or config error (unknown command or flag, invalid
|
|
275
|
+
Exit codes: `0` passed (or `--report-only`), `1` gate failed, no source
|
|
276
|
+
files matched the filters, coverage command failed, or coverage input is
|
|
277
|
+
missing, malformed, stale or rejected for being empty, `2` usage or config error (unknown command or flag, invalid
|
|
260
278
|
value, unreadable or invalid config file).
|
|
261
279
|
|
|
262
280
|
When the coverage command fails, `crap-ts` exits `1` before scoring and
|
|
@@ -280,6 +298,7 @@ word, because you'd be mad at me if it didn't, and I'd also be mad at me.
|
|
|
280
298
|
| `exclude` | string or string[] | `--exclude` | nothing |
|
|
281
299
|
| `failOnIgnored` | boolean | `--fail-on-ignored` | `false` |
|
|
282
300
|
| `allowEmptyCoverage` | boolean | `--allow-empty-coverage` | `false` |
|
|
301
|
+
| `includeTests` | boolean | `--include-tests` | `false` |
|
|
283
302
|
| `json` | boolean | `--json` | `false` |
|
|
284
303
|
| `all` | boolean | `--all` | `false` |
|
|
285
304
|
| `limit` | number | `--limit` | `20` |
|
|
@@ -367,7 +386,7 @@ The package root is the only entry point. These exports, and the types in
|
|
|
367
386
|
when coverage or source input is malformed or stale, or when a required
|
|
368
387
|
coverage file is empty and `allowEmptyCoverage` is false. Options include
|
|
369
388
|
`coverageCommand`, `coveragePaths`, `cwd`, `includes`, `excludes`,
|
|
370
|
-
`failOnIgnored`, `allowEmptyCoverage`,
|
|
389
|
+
`failOnIgnored`, `allowEmptyCoverage`, `includeTests`,
|
|
371
390
|
`json`, `all`, `limit`, `maxScore`, `warnScore`, `minLines`, `reportOnly`
|
|
372
391
|
and `skipCoverage`.
|
|
373
392
|
- `analyzeFileRisk({ filePath, sourceText, coverage?, minLines?, sourceFilePath? }): Promise<FunctionRisk[]>`
|
|
@@ -382,6 +401,26 @@ The package root is the only entry point. These exports, and the types in
|
|
|
382
401
|
|
|
383
402
|
Everything else under `src/` is internal and may change in any release.
|
|
384
403
|
|
|
404
|
+
## Upgrading From 2.0
|
|
405
|
+
|
|
406
|
+
Scores and the JSON schema do not change. The gate can fail where 2.0 passed
|
|
407
|
+
or crashed:
|
|
408
|
+
|
|
409
|
+
- Test files (`*.test.*`, `*.spec.*`, `*.test-d.*`, `*.spec-d.*` and files
|
|
410
|
+
below `__tests__/`) are skipped by default. You can remove `exclude` entries
|
|
411
|
+
such as `.test.ts`. To keep scoring test files, pass `--include-tests` or
|
|
412
|
+
set `includeTests: true`.
|
|
413
|
+
- If `include` and `exclude` select no source files, `crap-ts` exits `1`,
|
|
414
|
+
also with `--report-only`.
|
|
415
|
+
- If no function is checked, the gate fails. With `--report-only`, `crap-ts`
|
|
416
|
+
warns and exits `0`.
|
|
417
|
+
- Coverage entries for files that are not TypeScript or JavaScript sources,
|
|
418
|
+
such as CSS and JSON modules from side-effect imports, are no longer
|
|
419
|
+
scored. With
|
|
420
|
+
TypeScript 7, these entries crashed 2.0.
|
|
421
|
+
- A positional argument such as `crap-ts score src` is still a usage error.
|
|
422
|
+
The message now suggests the matching `--include` pattern.
|
|
423
|
+
|
|
385
424
|
## Upgrading From 1.x
|
|
386
425
|
|
|
387
426
|
2.0 has two breaking changes. Scores and the JSON schema do not change.
|
|
@@ -445,8 +484,7 @@ With `.crap-ts.json`:
|
|
|
445
484
|
{
|
|
446
485
|
"coverageCommand": "bun x vitest run --coverage --coverage.reporter=json",
|
|
447
486
|
"coverageFile": "coverage/unit/coverage-final.json",
|
|
448
|
-
"include": ["app
|
|
449
|
-
"exclude": [".test.ts"]
|
|
487
|
+
"include": ["app/**", "scripts/**"]
|
|
450
488
|
}
|
|
451
489
|
```
|
|
452
490
|
|
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -27,6 +27,8 @@ Options:
|
|
|
27
27
|
Fail when an audited function has coverage evidence marked ignored.
|
|
28
28
|
--allow-empty-coverage
|
|
29
29
|
Accept listed coverage JSON files with no entries.
|
|
30
|
+
--include-tests Also analyze test files (*.test.*, *.spec.*, __tests__/),
|
|
31
|
+
which are skipped by default.
|
|
30
32
|
--json Print the full JSON report (schema version 1) instead of text.
|
|
31
33
|
--limit <n> Number of rows to print in the text report. Defaults to 20.
|
|
32
34
|
--min-lines <n> Ignore functions shorter than this. Defaults to 10.
|
|
@@ -53,6 +55,7 @@ export function parseCliArgs(argv, { cwd = process.cwd() } = {}) {
|
|
|
53
55
|
"fail-on-ignored": { type: "boolean" },
|
|
54
56
|
help: { type: "boolean", short: "h" },
|
|
55
57
|
include: { type: "string", multiple: true },
|
|
58
|
+
"include-tests": { type: "boolean" },
|
|
56
59
|
json: { type: "boolean" },
|
|
57
60
|
limit: { type: "string" },
|
|
58
61
|
max: { type: "string" },
|
|
@@ -76,6 +79,7 @@ export function parseCliArgs(argv, { cwd = process.cwd() } = {}) {
|
|
|
76
79
|
failOnIgnored: values["fail-on-ignored"],
|
|
77
80
|
help: values.help,
|
|
78
81
|
includes: values.include,
|
|
82
|
+
includeTests: values["include-tests"],
|
|
79
83
|
json: values.json,
|
|
80
84
|
limit: parseOptionalNumber(values.limit, "--limit"),
|
|
81
85
|
maxScore: parseOptionalNumber(values.max, "--max"),
|
|
@@ -107,7 +111,12 @@ export async function main(argv = process.argv.slice(2)) {
|
|
|
107
111
|
try {
|
|
108
112
|
options = parseCliArgs(argv);
|
|
109
113
|
} catch (error) {
|
|
110
|
-
|
|
114
|
+
const positional = /^Unexpected argument '(.*)'/.exec(error.message)?.[1];
|
|
115
|
+
const hint =
|
|
116
|
+
error.code === "ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL" && positional
|
|
117
|
+
? ` Pass source directories as --include patterns, for example --include '${positional.replace(/\/+$/, "")}/**'.`
|
|
118
|
+
: "";
|
|
119
|
+
console.error(`Usage error: ${error.message}${hint}`);
|
|
111
120
|
return 2;
|
|
112
121
|
}
|
|
113
122
|
|
|
@@ -172,6 +181,7 @@ const configKeys = new Set([
|
|
|
172
181
|
"exclude",
|
|
173
182
|
"failOnIgnored",
|
|
174
183
|
"include",
|
|
184
|
+
"includeTests",
|
|
175
185
|
"json",
|
|
176
186
|
"limit",
|
|
177
187
|
"max",
|
|
@@ -214,6 +224,11 @@ function normalizeConfig(config) {
|
|
|
214
224
|
"boolean",
|
|
215
225
|
),
|
|
216
226
|
includes: normalizeConfigList(config.include, "include"),
|
|
227
|
+
includeTests: parseConfigType(
|
|
228
|
+
config.includeTests,
|
|
229
|
+
"includeTests",
|
|
230
|
+
"boolean",
|
|
231
|
+
),
|
|
217
232
|
json: parseConfigType(config.json, "json", "boolean"),
|
|
218
233
|
limit: parseConfigNumber(config.limit, "limit"),
|
|
219
234
|
maxScore: parseConfigNumber(config.max, "max"),
|
|
@@ -275,6 +290,7 @@ function applyDefaults(options) {
|
|
|
275
290
|
help: options.help ?? false,
|
|
276
291
|
excludes: options.excludes ?? [],
|
|
277
292
|
includes: options.includes ?? [],
|
|
293
|
+
includeTests: options.includeTests ?? false,
|
|
278
294
|
json: options.json ?? false,
|
|
279
295
|
reportOnly: options.reportOnly ?? false,
|
|
280
296
|
skipCoverage: options.skipCoverage ?? false,
|
package/src/crap-runner.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export interface CrapAuditOptions {
|
|
|
12
12
|
failOnIgnored?: boolean;
|
|
13
13
|
/** Analyze files matching these path substrings or root-anchored `dir/**` patterns. */
|
|
14
14
|
includes?: string[];
|
|
15
|
+
/** Also analyze test files (`*.test.*`, `*.spec.*`, `*.test-d.*`, `__tests__/`). Defaults to false. */
|
|
16
|
+
includeTests?: boolean;
|
|
15
17
|
/** Print the JSON report (schema version 1) to stdout instead of the text report. */
|
|
16
18
|
json?: boolean;
|
|
17
19
|
limit?: number;
|
package/src/crap-runner.js
CHANGED
|
@@ -8,7 +8,14 @@ import { normalizePath } from "./path-utils.js";
|
|
|
8
8
|
const reportSchemaVersion = 1;
|
|
9
9
|
|
|
10
10
|
const sourceFilePattern = /\.[cm]?tsx?$/;
|
|
11
|
+
// The inventory walk finds TypeScript only, but JavaScript sources named in
|
|
12
|
+
// coverage are scored too, as in 2.0.
|
|
13
|
+
const javaScriptFilePattern = /\.[cm]?jsx?$/;
|
|
11
14
|
const declarationFilePattern = /\.d\.[cm]?ts$/;
|
|
15
|
+
// The test files Vitest and Jest find by default: `*.test.*`, `*.spec.*`
|
|
16
|
+
// (and Vitest's `*.test-d.*` type tests), and anything under `__tests__/`.
|
|
17
|
+
const testFilePattern =
|
|
18
|
+
/(?:^|\/)__tests__\/|\.(?:test|spec)(?:-d)?\.[cm]?[jt]sx?$/;
|
|
12
19
|
|
|
13
20
|
export async function runCrapAudit({
|
|
14
21
|
all = false,
|
|
@@ -19,6 +26,7 @@ export async function runCrapAudit({
|
|
|
19
26
|
excludes = [],
|
|
20
27
|
failOnIgnored = false,
|
|
21
28
|
includes = [],
|
|
29
|
+
includeTests = false,
|
|
22
30
|
json = false,
|
|
23
31
|
limit = 20,
|
|
24
32
|
maxScore = 30,
|
|
@@ -97,21 +105,44 @@ export async function runCrapAudit({
|
|
|
97
105
|
root,
|
|
98
106
|
allowEmptyCoverage,
|
|
99
107
|
);
|
|
100
|
-
const
|
|
108
|
+
const matchesFilters = (path) => {
|
|
101
109
|
const relativePath = relative(root, path);
|
|
102
110
|
return (
|
|
103
111
|
matchesAny(relativePath, includes, true) &&
|
|
104
112
|
!matchesAny(relativePath, excludes, false)
|
|
105
113
|
);
|
|
106
114
|
};
|
|
107
|
-
const
|
|
115
|
+
const isTestFile = (path) =>
|
|
116
|
+
testFilePattern.test(normalizePath(relative(root, path)));
|
|
117
|
+
const candidatePaths = new Set(listSourceFiles(root).filter(matchesFilters));
|
|
108
118
|
|
|
119
|
+
// Coverage tools also list non-source modules (CSS, JSON) and even
|
|
120
|
+
// directories; only TypeScript and JavaScript sources can be scored.
|
|
109
121
|
for (const path of coverageByFile.keys()) {
|
|
110
|
-
if (
|
|
111
|
-
|
|
122
|
+
if (
|
|
123
|
+
isCoverageSourceFile(path) &&
|
|
124
|
+
existsSync(path) &&
|
|
125
|
+
matchesFilters(path)
|
|
126
|
+
) {
|
|
127
|
+
candidatePaths.add(path);
|
|
112
128
|
}
|
|
113
129
|
}
|
|
114
130
|
|
|
131
|
+
const sourcePaths = new Set(
|
|
132
|
+
[...candidatePaths].filter((path) => includeTests || !isTestFile(path)),
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
if (sourcePaths.size === 0) {
|
|
136
|
+
const testHint =
|
|
137
|
+
candidatePaths.size > 0
|
|
138
|
+
? ` The ${candidatePaths.size} matching file(s) were skipped as test files; pass --include-tests to analyze them.`
|
|
139
|
+
: "";
|
|
140
|
+
console.error(
|
|
141
|
+
`No source files matched under ${root}. Check --include and --exclude; patterns are relative to the working directory, for example --include 'src/**'.${testHint}`,
|
|
142
|
+
);
|
|
143
|
+
return 1;
|
|
144
|
+
}
|
|
145
|
+
|
|
115
146
|
const functions = [];
|
|
116
147
|
const missingFiles = [];
|
|
117
148
|
|
|
@@ -160,6 +191,13 @@ export async function runCrapAudit({
|
|
|
160
191
|
);
|
|
161
192
|
}
|
|
162
193
|
|
|
194
|
+
if (summary.functions === 0) {
|
|
195
|
+
console.error("");
|
|
196
|
+
console.error(
|
|
197
|
+
`No functions checked in ${summary.files} source file(s). Check --include, --exclude and --min-lines (${minLines}).`,
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
163
201
|
if (missingFiles.length > 0) {
|
|
164
202
|
console.error("");
|
|
165
203
|
console.error(
|
|
@@ -233,7 +271,9 @@ function buildReport({
|
|
|
233
271
|
thresholds: { max: maxScore, warn: warnScore ?? null, minLines },
|
|
234
272
|
summary: {
|
|
235
273
|
...summary,
|
|
274
|
+
// A gate that checks nothing must not pass.
|
|
236
275
|
passed:
|
|
276
|
+
summary.functions > 0 &&
|
|
237
277
|
summary.aboveMax === 0 &&
|
|
238
278
|
summary.missingEvidence === 0 &&
|
|
239
279
|
summary.missingFiles === 0 &&
|
|
@@ -463,14 +503,18 @@ function listSourceFiles(directory) {
|
|
|
463
503
|
: listSourceFiles(path);
|
|
464
504
|
}
|
|
465
505
|
|
|
466
|
-
return entry.isFile() &&
|
|
467
|
-
sourceFilePattern.test(entry.name) &&
|
|
468
|
-
!declarationFilePattern.test(entry.name)
|
|
469
|
-
? [path]
|
|
470
|
-
: [];
|
|
506
|
+
return entry.isFile() && isSourceFile(entry.name) ? [path] : [];
|
|
471
507
|
});
|
|
472
508
|
}
|
|
473
509
|
|
|
510
|
+
function isSourceFile(path) {
|
|
511
|
+
return sourceFilePattern.test(path) && !declarationFilePattern.test(path);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function isCoverageSourceFile(path) {
|
|
515
|
+
return isSourceFile(path) || javaScriptFilePattern.test(path);
|
|
516
|
+
}
|
|
517
|
+
|
|
474
518
|
function matchesAny(path, patterns, emptyResult) {
|
|
475
519
|
if (patterns.length === 0) {
|
|
476
520
|
return emptyResult;
|