crap-ts 0.1.0 → 0.1.1
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 +4 -0
- package/package.json +60 -49
- package/src/cli.js +175 -158
- package/src/crap-analysis.d.ts +20 -21
- package/src/crap-analysis.js +274 -142
- package/src/crap-runner.d.ts +11 -11
- package/src/crap-runner.js +104 -90
- package/src/crap-score.d.ts +22 -13
- package/src/crap-score.js +40 -37
- package/src/index.d.ts +11 -11
- package/src/index.js +9 -5
- package/src/path-utils.js +1 -1
package/README.md
CHANGED
|
@@ -15,6 +15,7 @@ Risky code plus weak tests, now with a name your manager will remember.
|
|
|
15
15
|
## Works Out Of The Box With
|
|
16
16
|
|
|
17
17
|
- TypeScript source files listed in an Istanbul-style `coverage-final.json`
|
|
18
|
+
- Consumer-provided TypeScript versions `>=5.9 <8`, including TypeScript 7
|
|
18
19
|
- Vitest V8 coverage, Jest coverage, or nyc/Istanbul coverage that includes
|
|
19
20
|
function maps
|
|
20
21
|
- npm, Bun, pnpm, or any runner that can produce coverage before `crap-ts` reads
|
|
@@ -29,6 +30,9 @@ glamorous work, obviously.
|
|
|
29
30
|
npm install --save-dev crap-ts
|
|
30
31
|
```
|
|
31
32
|
|
|
33
|
+
`crap-ts` uses your project's installed TypeScript peer dependency, so
|
|
34
|
+
TypeScript 7 projects do not get a nested TypeScript 5.x install.
|
|
35
|
+
|
|
32
36
|
## Usage
|
|
33
37
|
|
|
34
38
|
Run the built-in TypeScript AST audit against coverage-final data:
|
package/package.json
CHANGED
|
@@ -1,51 +1,62 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
2
|
+
"name": "crap-ts",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Small CRAP-score audit helper for TypeScript projects.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/cubanx/crap-ts.git"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"bin": {
|
|
11
|
+
"crap-ts": "src/cli.js"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./src/index.d.ts",
|
|
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
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"src",
|
|
33
|
+
"README.md",
|
|
34
|
+
"CONTRIBUTING.md",
|
|
35
|
+
"LICENSE"
|
|
36
|
+
],
|
|
37
|
+
"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 --metrics fixtures/complexity-metrics.json --coverage-file fixtures/coverage-summary.json --max-crap 30",
|
|
40
|
+
"test": "node --test tests/*.test.js",
|
|
41
|
+
"test:consumer-typescript": "node scripts/verify-consumer-typescript.mjs",
|
|
42
|
+
"validate": "npm run test && npm run score",
|
|
43
|
+
"lint": "biome ci .",
|
|
44
|
+
"format": "biome check --write ."
|
|
45
|
+
},
|
|
46
|
+
"keywords": [
|
|
47
|
+
"complexity",
|
|
48
|
+
"typescript",
|
|
49
|
+
"quality"
|
|
50
|
+
],
|
|
51
|
+
"license": "MIT",
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=20"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"typescript": ">=5.9 <8"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@biomejs/biome": "^2.5.3",
|
|
60
|
+
"typescript": "^5.9.3"
|
|
61
|
+
}
|
|
51
62
|
}
|
package/src/cli.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
4
4
|
import { resolve } from "node:path";
|
|
5
|
-
import { parseArgs } from "node:util";
|
|
6
5
|
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { parseArgs } from "node:util";
|
|
7
7
|
|
|
8
8
|
import { runCrapAudit } from "./crap-runner.js";
|
|
9
9
|
import { buildCrapScores } from "./crap-score.js";
|
|
@@ -40,215 +40,232 @@ Options:
|
|
|
40
40
|
const defaultConfigPath = ".crap-ts.json";
|
|
41
41
|
|
|
42
42
|
export function parseCliArgs(argv, { cwd = process.cwd() } = {}) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
43
|
+
const command = getCommand(argv);
|
|
44
|
+
const args = command === "legacy" ? argv : argv.slice(1);
|
|
45
|
+
const { positionals, values } = parseArgs({
|
|
46
|
+
args,
|
|
47
|
+
allowPositionals: true,
|
|
48
|
+
options: {
|
|
49
|
+
all: { type: "boolean" },
|
|
50
|
+
config: { type: "string" },
|
|
51
|
+
"coverage-file": { type: "string" },
|
|
52
|
+
"coverage-command": { type: "string" },
|
|
53
|
+
help: { type: "boolean", short: "h" },
|
|
54
|
+
include: { type: "string", multiple: true },
|
|
55
|
+
limit: { type: "string" },
|
|
56
|
+
max: { type: "string" },
|
|
57
|
+
"max-crap": { type: "string" },
|
|
58
|
+
metrics: { type: "string" },
|
|
59
|
+
"min-lines": { type: "string" },
|
|
60
|
+
"report-only": { type: "boolean" },
|
|
61
|
+
"skip-coverage": { type: "boolean" },
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
const config =
|
|
65
|
+
command === "help" || values.help ? {} : readConfig(values.config, cwd);
|
|
66
|
+
|
|
67
|
+
return applyDefaults(
|
|
68
|
+
mergeConfigWithCli(config, {
|
|
69
|
+
command,
|
|
70
|
+
all: values.all,
|
|
71
|
+
configPath: values.config,
|
|
72
|
+
coverageCommand: values["coverage-command"],
|
|
73
|
+
coveragePath: values["coverage-file"],
|
|
74
|
+
help: values.help,
|
|
75
|
+
includes: values.include,
|
|
76
|
+
limit: parseOptionalNumber(values.limit, "--limit"),
|
|
77
|
+
maxCrap: parseOptionalNumber(values["max-crap"], "--max-crap"),
|
|
78
|
+
maxScore: parseOptionalNumber(values.max, "--max"),
|
|
79
|
+
metricsPath: values.metrics,
|
|
80
|
+
minLines: parseOptionalNumber(values["min-lines"], "--min-lines"),
|
|
81
|
+
paths: positionals,
|
|
82
|
+
reportOnly: values["report-only"],
|
|
83
|
+
skipCoverage: values["skip-coverage"],
|
|
84
|
+
}),
|
|
85
|
+
);
|
|
83
86
|
}
|
|
84
87
|
|
|
85
|
-
export function main(argv = process.argv.slice(2)) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
88
|
+
export async function main(argv = process.argv.slice(2)) {
|
|
89
|
+
const options = parseCliArgs(argv);
|
|
90
|
+
|
|
91
|
+
if (options.help || options.command === "help") {
|
|
92
|
+
console.log(helpText);
|
|
93
|
+
return 0;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (
|
|
97
|
+
options.command === "score" ||
|
|
98
|
+
options.metricsPath ||
|
|
99
|
+
options.coveragePath
|
|
100
|
+
) {
|
|
101
|
+
return runScore(options);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (options.command !== "legacy") {
|
|
105
|
+
console.error(`Unknown command: ${options.command}`);
|
|
106
|
+
return 2;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
console.error(
|
|
110
|
+
"Usage error: run `crap-ts score --help` for supported commands.",
|
|
111
|
+
);
|
|
112
|
+
return 2;
|
|
104
113
|
}
|
|
105
114
|
|
|
106
|
-
function runScore(options) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
115
|
+
async function runScore(options) {
|
|
116
|
+
if (!options.metricsPath) {
|
|
117
|
+
return runCrapAudit(options);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (!options.coveragePath) {
|
|
121
|
+
console.error("CRAP scoring requires both --metrics and --coverage-file.");
|
|
122
|
+
return 2;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const metrics = JSON.parse(readFileSync(options.metricsPath, "utf8"));
|
|
126
|
+
const coverageSummary = JSON.parse(
|
|
127
|
+
readFileSync(options.coveragePath, "utf8"),
|
|
128
|
+
);
|
|
129
|
+
const scores = buildCrapScores({ coverageSummary, metrics });
|
|
130
|
+
const failingScores = options.maxCrap
|
|
131
|
+
? scores.filter((score) => score.crap > options.maxCrap)
|
|
132
|
+
: [];
|
|
133
|
+
|
|
134
|
+
console.log(JSON.stringify(scores, null, 2));
|
|
135
|
+
|
|
136
|
+
if (failingScores.length > 0) {
|
|
137
|
+
console.error(
|
|
138
|
+
`CRAP threshold failed: ${failingScores.length} row(s) exceeded ${options.maxCrap}.`,
|
|
139
|
+
);
|
|
140
|
+
return 1;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return 0;
|
|
133
144
|
}
|
|
134
145
|
|
|
135
146
|
function getCommand(argv) {
|
|
136
|
-
|
|
147
|
+
const firstArg = argv[0];
|
|
137
148
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
149
|
+
if (firstArg === "score" || firstArg === "help") {
|
|
150
|
+
return firstArg;
|
|
151
|
+
}
|
|
141
152
|
|
|
142
|
-
|
|
153
|
+
return "legacy";
|
|
143
154
|
}
|
|
144
155
|
|
|
145
156
|
function parseOptionalNumber(value, label) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
157
|
+
if (value === undefined) {
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
149
160
|
|
|
150
|
-
|
|
161
|
+
const parsed = Number(value);
|
|
151
162
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
163
|
+
if (!Number.isFinite(parsed)) {
|
|
164
|
+
throw new TypeError(`${label} must be a finite number.`);
|
|
165
|
+
}
|
|
155
166
|
|
|
156
|
-
|
|
167
|
+
return parsed;
|
|
157
168
|
}
|
|
158
169
|
|
|
159
170
|
function readConfig(configPath, cwd) {
|
|
160
|
-
|
|
171
|
+
const resolvedPath = resolveConfigPath(configPath, cwd);
|
|
161
172
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
173
|
+
if (!resolvedPath) {
|
|
174
|
+
return {};
|
|
175
|
+
}
|
|
165
176
|
|
|
166
|
-
|
|
177
|
+
return JSON.parse(readFileSync(resolvedPath, "utf8"));
|
|
167
178
|
}
|
|
168
179
|
|
|
169
180
|
function resolveConfigPath(configPath, cwd) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
181
|
+
if (configPath) {
|
|
182
|
+
return resolve(cwd, configPath);
|
|
183
|
+
}
|
|
173
184
|
|
|
174
|
-
|
|
175
|
-
|
|
185
|
+
const defaultPath = resolve(cwd, defaultConfigPath);
|
|
186
|
+
return existsSync(defaultPath) ? defaultPath : null;
|
|
176
187
|
}
|
|
177
188
|
|
|
178
189
|
function mergeConfigWithCli(config, cliOptions) {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
190
|
+
return {
|
|
191
|
+
...normalizeConfig(config),
|
|
192
|
+
...stripUndefined(cliOptions),
|
|
193
|
+
};
|
|
183
194
|
}
|
|
184
195
|
|
|
185
196
|
function normalizeConfig(config) {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
197
|
+
return stripUndefined({
|
|
198
|
+
all: config.all,
|
|
199
|
+
coverageCommand: config.coverageCommand,
|
|
200
|
+
coveragePath: config.coverageFile,
|
|
201
|
+
includes: normalizeConfigList(config.include, "include"),
|
|
202
|
+
limit: parseConfigNumber(config.limit, "limit"),
|
|
203
|
+
maxCrap: parseConfigNumber(config.maxCrap, "maxCrap"),
|
|
204
|
+
maxScore: parseConfigNumber(config.max, "max"),
|
|
205
|
+
metricsPath: config.metrics,
|
|
206
|
+
minLines: parseConfigNumber(config.minLines, "minLines"),
|
|
207
|
+
reportOnly: config.reportOnly,
|
|
208
|
+
skipCoverage: config.skipCoverage,
|
|
209
|
+
});
|
|
199
210
|
}
|
|
200
211
|
|
|
201
212
|
function parseConfigNumber(value, label) {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
213
|
+
if (value === undefined) {
|
|
214
|
+
return undefined;
|
|
215
|
+
}
|
|
205
216
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
217
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
218
|
+
throw new TypeError(
|
|
219
|
+
`${label} must be a finite number in ${defaultConfigPath}.`,
|
|
220
|
+
);
|
|
221
|
+
}
|
|
209
222
|
|
|
210
|
-
|
|
223
|
+
return value;
|
|
211
224
|
}
|
|
212
225
|
|
|
213
226
|
function normalizeConfigList(value, label) {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
227
|
+
if (value === undefined) {
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
217
230
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
231
|
+
if (typeof value === "string") {
|
|
232
|
+
return [value];
|
|
233
|
+
}
|
|
221
234
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
235
|
+
if (Array.isArray(value) && value.every((item) => typeof item === "string")) {
|
|
236
|
+
return value;
|
|
237
|
+
}
|
|
225
238
|
|
|
226
|
-
|
|
239
|
+
throw new TypeError(
|
|
240
|
+
`${label} must be a string or string array in ${defaultConfigPath}.`,
|
|
241
|
+
);
|
|
227
242
|
}
|
|
228
243
|
|
|
229
244
|
function applyDefaults(options) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
245
|
+
return {
|
|
246
|
+
...options,
|
|
247
|
+
all: options.all ?? false,
|
|
248
|
+
help: options.help ?? false,
|
|
249
|
+
includes: options.includes ?? [],
|
|
250
|
+
reportOnly: options.reportOnly ?? false,
|
|
251
|
+
skipCoverage: options.skipCoverage ?? false,
|
|
252
|
+
};
|
|
238
253
|
}
|
|
239
254
|
|
|
240
255
|
function stripUndefined(values) {
|
|
241
|
-
|
|
256
|
+
return Object.fromEntries(
|
|
257
|
+
Object.entries(values).filter(([, value]) => value !== undefined),
|
|
258
|
+
);
|
|
242
259
|
}
|
|
243
260
|
|
|
244
261
|
if (isDirectExecution()) {
|
|
245
|
-
|
|
262
|
+
process.exitCode = await main();
|
|
246
263
|
}
|
|
247
264
|
|
|
248
265
|
function isDirectExecution() {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
266
|
+
if (!process.argv[1]) {
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
252
269
|
|
|
253
|
-
|
|
270
|
+
return realpathSync(process.argv[1]) === fileURLToPath(import.meta.url);
|
|
254
271
|
}
|
package/src/crap-analysis.d.ts
CHANGED
|
@@ -1,34 +1,33 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
1
|
export interface FunctionRisk {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
2
|
+
complexity: number;
|
|
3
|
+
coveragePercent: number;
|
|
4
|
+
crapScore: number;
|
|
5
|
+
endLine: number;
|
|
6
|
+
filePath: string;
|
|
7
|
+
lineCount: number;
|
|
8
|
+
name: string;
|
|
9
|
+
startLine: number;
|
|
12
10
|
}
|
|
13
11
|
|
|
14
12
|
export interface CoverageFunction {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
coveragePercent: number;
|
|
14
|
+
declarationLine: number;
|
|
15
|
+
endLine: number;
|
|
16
|
+
startLine: number;
|
|
19
17
|
}
|
|
20
18
|
|
|
21
19
|
export function extractCoverageFunctions(
|
|
22
|
-
|
|
20
|
+
coverageJson: Record<string, unknown>,
|
|
23
21
|
): Map<string, CoverageFunction[]>;
|
|
24
22
|
|
|
25
23
|
export function analyzeFileRisk(input: {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
coverageFunctions: CoverageFunction[];
|
|
25
|
+
filePath: string;
|
|
26
|
+
minLines: number;
|
|
27
|
+
sourceFilePath?: string;
|
|
28
|
+
sourceText: string;
|
|
29
|
+
}): Promise<FunctionRisk[]>;
|
|
31
30
|
|
|
32
31
|
export function formatRiskLine(risk: FunctionRisk): string;
|
|
33
32
|
|
|
34
|
-
export function calculateCyclomaticComplexity(node:
|
|
33
|
+
export function calculateCyclomaticComplexity(node: unknown): number;
|