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/src/cli.js
CHANGED
|
@@ -6,7 +6,6 @@ import { fileURLToPath } from "node:url";
|
|
|
6
6
|
import { parseArgs } from "node:util";
|
|
7
7
|
|
|
8
8
|
import { runCrapAudit } from "./crap-runner.js";
|
|
9
|
-
import { buildCrapScores } from "./crap-score.js";
|
|
10
9
|
|
|
11
10
|
const helpText = `crap-ts
|
|
12
11
|
|
|
@@ -14,24 +13,24 @@ Run a focused TypeScript quality audit.
|
|
|
14
13
|
|
|
15
14
|
Usage:
|
|
16
15
|
crap-ts score [--coverage-file coverage/unit/coverage-final.json] [--coverage-command "npm run test:coverage"]
|
|
17
|
-
crap-ts score --metrics complexity-metrics.json --coverage-file coverage/coverage-summary.json [--max-crap 30]
|
|
18
|
-
|
|
19
|
-
Legacy:
|
|
20
|
-
crap-ts --metrics complexity-metrics.json --coverage-file coverage/coverage-summary.json
|
|
21
16
|
|
|
22
17
|
Options:
|
|
23
18
|
--config <path> Config file path. Defaults to .crap-ts.json when present.
|
|
24
19
|
--coverage-file <path>
|
|
25
|
-
Istanbul coverage
|
|
20
|
+
Istanbul coverage-final.json path. Repeat to merge several runs.
|
|
26
21
|
--coverage-command <cmd>
|
|
27
22
|
Command to generate coverage before scoring.
|
|
28
23
|
--all Print every analyzed function instead of applying --limit.
|
|
29
|
-
--include <
|
|
30
|
-
--
|
|
31
|
-
--
|
|
24
|
+
--include <pattern> Only analyze matching source paths. Use dir/** to anchor.
|
|
25
|
+
--exclude <pattern> Skip matching source paths. Use dir/** to anchor.
|
|
26
|
+
--fail-on-ignored
|
|
27
|
+
Fail when an audited function has coverage evidence marked ignored.
|
|
28
|
+
--allow-empty-coverage
|
|
29
|
+
Accept listed coverage JSON files with no entries.
|
|
30
|
+
--json Print the full JSON report (schema version 1) instead of text.
|
|
31
|
+
--limit <n> Number of rows to print in the text report. Defaults to 20.
|
|
32
32
|
--min-lines <n> Ignore functions shorter than this. Defaults to 10.
|
|
33
|
-
--max <n> Fail
|
|
34
|
-
--max-crap <n> Fail CRAP scoring when any row is above this score.
|
|
33
|
+
--max <n> Fail when any function's CRAP score is above this. Defaults to 30.
|
|
35
34
|
--warn <n> Warn, without failing, when any row is above this score.
|
|
36
35
|
--report-only Print CRAP report without failing on threshold.
|
|
37
36
|
--skip-coverage Do not run the coverage command.
|
|
@@ -41,46 +40,46 @@ Options:
|
|
|
41
40
|
const defaultConfigPath = ".crap-ts.json";
|
|
42
41
|
|
|
43
42
|
export function parseCliArgs(argv, { cwd = process.cwd() } = {}) {
|
|
44
|
-
const command =
|
|
45
|
-
const
|
|
46
|
-
const { positionals, values } = parseArgs({
|
|
43
|
+
const [command, ...args] = argv;
|
|
44
|
+
const { values } = parseArgs({
|
|
47
45
|
args,
|
|
48
|
-
allowPositionals: true,
|
|
49
46
|
options: {
|
|
50
47
|
all: { type: "boolean" },
|
|
48
|
+
"allow-empty-coverage": { type: "boolean" },
|
|
51
49
|
config: { type: "string" },
|
|
52
|
-
"coverage-file": { type: "string" },
|
|
50
|
+
"coverage-file": { type: "string", multiple: true },
|
|
53
51
|
"coverage-command": { type: "string" },
|
|
52
|
+
exclude: { type: "string", multiple: true },
|
|
53
|
+
"fail-on-ignored": { type: "boolean" },
|
|
54
54
|
help: { type: "boolean", short: "h" },
|
|
55
55
|
include: { type: "string", multiple: true },
|
|
56
|
+
json: { type: "boolean" },
|
|
56
57
|
limit: { type: "string" },
|
|
57
58
|
max: { type: "string" },
|
|
58
|
-
"max-crap": { type: "string" },
|
|
59
|
-
metrics: { type: "string" },
|
|
60
59
|
"min-lines": { type: "string" },
|
|
61
60
|
"report-only": { type: "boolean" },
|
|
62
61
|
"skip-coverage": { type: "boolean" },
|
|
63
62
|
warn: { type: "string" },
|
|
64
63
|
},
|
|
65
64
|
});
|
|
66
|
-
const config =
|
|
67
|
-
command === "help" || values.help ? {} : readConfig(values.config, cwd);
|
|
65
|
+
const config = values.help ? {} : readConfig(values.config, cwd);
|
|
68
66
|
|
|
69
67
|
return applyDefaults(
|
|
70
68
|
mergeConfigWithCli(config, {
|
|
71
69
|
command,
|
|
72
70
|
all: values.all,
|
|
71
|
+
allowEmptyCoverage: values["allow-empty-coverage"],
|
|
73
72
|
configPath: values.config,
|
|
74
73
|
coverageCommand: values["coverage-command"],
|
|
75
|
-
|
|
74
|
+
coveragePaths: values["coverage-file"],
|
|
75
|
+
excludes: values.exclude,
|
|
76
|
+
failOnIgnored: values["fail-on-ignored"],
|
|
76
77
|
help: values.help,
|
|
77
78
|
includes: values.include,
|
|
79
|
+
json: values.json,
|
|
78
80
|
limit: parseOptionalNumber(values.limit, "--limit"),
|
|
79
|
-
maxCrap: parseOptionalNumber(values["max-crap"], "--max-crap"),
|
|
80
81
|
maxScore: parseOptionalNumber(values.max, "--max"),
|
|
81
|
-
metricsPath: values.metrics,
|
|
82
82
|
minLines: parseOptionalNumber(values["min-lines"], "--min-lines"),
|
|
83
|
-
paths: positionals,
|
|
84
83
|
reportOnly: values["report-only"],
|
|
85
84
|
skipCoverage: values["skip-coverage"],
|
|
86
85
|
warnScore: parseOptionalNumber(values.warn, "--warn"),
|
|
@@ -89,96 +88,40 @@ export function parseCliArgs(argv, { cwd = process.cwd() } = {}) {
|
|
|
89
88
|
}
|
|
90
89
|
|
|
91
90
|
export async function main(argv = process.argv.slice(2)) {
|
|
92
|
-
const
|
|
91
|
+
const [command] = argv;
|
|
93
92
|
|
|
94
|
-
if (
|
|
93
|
+
if (!command || ["help", "--help", "-h"].includes(command)) {
|
|
95
94
|
console.log(helpText);
|
|
96
95
|
return 0;
|
|
97
96
|
}
|
|
98
97
|
|
|
99
|
-
if (
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
) {
|
|
104
|
-
return runScore(options);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (options.command !== "legacy") {
|
|
108
|
-
console.error(`Unknown command: ${options.command}`);
|
|
98
|
+
if (command !== "score") {
|
|
99
|
+
console.error(
|
|
100
|
+
`Unknown command: ${command}. Run \`crap-ts score\`; see \`crap-ts --help\`.`,
|
|
101
|
+
);
|
|
109
102
|
return 2;
|
|
110
103
|
}
|
|
111
104
|
|
|
112
|
-
|
|
113
|
-
"Usage error: run `crap-ts score --help` for supported commands.",
|
|
114
|
-
);
|
|
115
|
-
return 2;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async function runScore(options) {
|
|
119
|
-
if (!options.metricsPath) {
|
|
120
|
-
return runCrapAudit(options);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (!options.coveragePath) {
|
|
124
|
-
console.error("CRAP scoring requires both --metrics and --coverage-file.");
|
|
125
|
-
return 2;
|
|
126
|
-
}
|
|
105
|
+
let options;
|
|
127
106
|
|
|
128
|
-
|
|
129
|
-
options
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
) {
|
|
133
|
-
console.error(
|
|
134
|
-
`CRAP warn (${options.warnScore}) must not exceed max-crap (${options.maxCrap}).`,
|
|
135
|
-
);
|
|
107
|
+
try {
|
|
108
|
+
options = parseCliArgs(argv);
|
|
109
|
+
} catch (error) {
|
|
110
|
+
console.error(`Usage error: ${error.message}`);
|
|
136
111
|
return 2;
|
|
137
112
|
}
|
|
138
113
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
);
|
|
143
|
-
const scores = buildCrapScores({ coverageSummary, metrics });
|
|
144
|
-
const failingScores = options.maxCrap
|
|
145
|
-
? scores.filter((score) => score.crap > options.maxCrap)
|
|
146
|
-
: [];
|
|
147
|
-
const warningScores =
|
|
148
|
-
options.warnScore === undefined
|
|
149
|
-
? []
|
|
150
|
-
: scores.filter(
|
|
151
|
-
(score) =>
|
|
152
|
-
score.crap > options.warnScore &&
|
|
153
|
-
!(options.maxCrap && score.crap > options.maxCrap),
|
|
154
|
-
);
|
|
155
|
-
|
|
156
|
-
console.log(JSON.stringify(scores, null, 2));
|
|
157
|
-
|
|
158
|
-
if (warningScores.length > 0) {
|
|
159
|
-
console.error(
|
|
160
|
-
`CRAP warning: ${warningScores.length} row(s) above ${options.warnScore}.`,
|
|
161
|
-
);
|
|
114
|
+
if (options.help) {
|
|
115
|
+
console.log(helpText);
|
|
116
|
+
return 0;
|
|
162
117
|
}
|
|
163
118
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
);
|
|
119
|
+
try {
|
|
120
|
+
return await runCrapAudit(options);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
console.error(`Error: ${error.message}`);
|
|
168
123
|
return 1;
|
|
169
124
|
}
|
|
170
|
-
|
|
171
|
-
return 0;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function getCommand(argv) {
|
|
175
|
-
const firstArg = argv[0];
|
|
176
|
-
|
|
177
|
-
if (firstArg === "score" || firstArg === "help") {
|
|
178
|
-
return firstArg;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return "legacy";
|
|
182
125
|
}
|
|
183
126
|
|
|
184
127
|
function parseOptionalNumber(value, label) {
|
|
@@ -221,19 +164,66 @@ function mergeConfigWithCli(config, cliOptions) {
|
|
|
221
164
|
};
|
|
222
165
|
}
|
|
223
166
|
|
|
167
|
+
const configKeys = new Set([
|
|
168
|
+
"all",
|
|
169
|
+
"allowEmptyCoverage",
|
|
170
|
+
"coverageCommand",
|
|
171
|
+
"coverageFile",
|
|
172
|
+
"exclude",
|
|
173
|
+
"failOnIgnored",
|
|
174
|
+
"include",
|
|
175
|
+
"json",
|
|
176
|
+
"limit",
|
|
177
|
+
"max",
|
|
178
|
+
"minLines",
|
|
179
|
+
"reportOnly",
|
|
180
|
+
"skipCoverage",
|
|
181
|
+
"warn",
|
|
182
|
+
]);
|
|
183
|
+
|
|
224
184
|
function normalizeConfig(config) {
|
|
185
|
+
if (!config || typeof config !== "object" || Array.isArray(config)) {
|
|
186
|
+
throw new TypeError(`${defaultConfigPath} must contain a JSON object.`);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const unknownKeys = Object.keys(config).filter((key) => !configKeys.has(key));
|
|
190
|
+
|
|
191
|
+
if (unknownKeys.length > 0) {
|
|
192
|
+
throw new TypeError(
|
|
193
|
+
`Unknown ${defaultConfigPath} key(s): ${unknownKeys.join(", ")}.`,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
225
197
|
return stripUndefined({
|
|
226
|
-
all: config.all,
|
|
227
|
-
|
|
228
|
-
|
|
198
|
+
all: parseConfigType(config.all, "all", "boolean"),
|
|
199
|
+
allowEmptyCoverage: parseConfigType(
|
|
200
|
+
config.allowEmptyCoverage,
|
|
201
|
+
"allowEmptyCoverage",
|
|
202
|
+
"boolean",
|
|
203
|
+
),
|
|
204
|
+
coverageCommand: parseConfigType(
|
|
205
|
+
config.coverageCommand,
|
|
206
|
+
"coverageCommand",
|
|
207
|
+
"string",
|
|
208
|
+
),
|
|
209
|
+
coveragePaths: normalizeConfigList(config.coverageFile, "coverageFile"),
|
|
210
|
+
excludes: normalizeConfigList(config.exclude, "exclude"),
|
|
211
|
+
failOnIgnored: parseConfigType(
|
|
212
|
+
config.failOnIgnored,
|
|
213
|
+
"failOnIgnored",
|
|
214
|
+
"boolean",
|
|
215
|
+
),
|
|
229
216
|
includes: normalizeConfigList(config.include, "include"),
|
|
217
|
+
json: parseConfigType(config.json, "json", "boolean"),
|
|
230
218
|
limit: parseConfigNumber(config.limit, "limit"),
|
|
231
|
-
maxCrap: parseConfigNumber(config.maxCrap, "maxCrap"),
|
|
232
219
|
maxScore: parseConfigNumber(config.max, "max"),
|
|
233
|
-
metricsPath: config.metrics,
|
|
234
220
|
minLines: parseConfigNumber(config.minLines, "minLines"),
|
|
235
|
-
reportOnly: config.reportOnly,
|
|
236
|
-
skipCoverage:
|
|
221
|
+
reportOnly: parseConfigType(config.reportOnly, "reportOnly", "boolean"),
|
|
222
|
+
skipCoverage: parseConfigType(
|
|
223
|
+
config.skipCoverage,
|
|
224
|
+
"skipCoverage",
|
|
225
|
+
"boolean",
|
|
226
|
+
),
|
|
237
227
|
warnScore: parseConfigNumber(config.warn, "warn"),
|
|
238
228
|
});
|
|
239
229
|
}
|
|
@@ -252,6 +242,14 @@ function parseConfigNumber(value, label) {
|
|
|
252
242
|
return value;
|
|
253
243
|
}
|
|
254
244
|
|
|
245
|
+
function parseConfigType(value, label, type) {
|
|
246
|
+
if (value !== undefined && typeof value !== type) {
|
|
247
|
+
throw new TypeError(`${label} must be a ${type} in ${defaultConfigPath}.`);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return value;
|
|
251
|
+
}
|
|
252
|
+
|
|
255
253
|
function normalizeConfigList(value, label) {
|
|
256
254
|
if (value === undefined) {
|
|
257
255
|
return undefined;
|
|
@@ -275,7 +273,9 @@ function applyDefaults(options) {
|
|
|
275
273
|
...options,
|
|
276
274
|
all: options.all ?? false,
|
|
277
275
|
help: options.help ?? false,
|
|
276
|
+
excludes: options.excludes ?? [],
|
|
278
277
|
includes: options.includes ?? [],
|
|
278
|
+
json: options.json ?? false,
|
|
279
279
|
reportOnly: options.reportOnly ?? false,
|
|
280
280
|
skipCoverage: options.skipCoverage ?? false,
|
|
281
281
|
};
|
package/src/crap-analysis.d.ts
CHANGED
|
@@ -1,33 +1,54 @@
|
|
|
1
|
+
interface SourcePosition {
|
|
2
|
+
line: number;
|
|
3
|
+
column: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
interface SourceRange {
|
|
7
|
+
start: SourcePosition;
|
|
8
|
+
end: { line: number; column: number | null };
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** One file's entry from an Istanbul `coverage-final.json`. Branch data is ignored. */
|
|
12
|
+
export interface FileCoverage {
|
|
13
|
+
statementMap: Record<string, SourceRange>;
|
|
14
|
+
s: Record<string, number>;
|
|
15
|
+
fnMap: Record<string, { loc: SourceRange }>;
|
|
16
|
+
f: Record<string, number>;
|
|
17
|
+
}
|
|
18
|
+
|
|
1
19
|
export interface FunctionRisk {
|
|
2
|
-
complexity: number;
|
|
3
|
-
coveragePercent: number;
|
|
4
|
-
crapScore: number;
|
|
5
|
-
endLine: number;
|
|
6
20
|
filePath: string;
|
|
7
|
-
lineCount: number;
|
|
8
21
|
name: string;
|
|
22
|
+
/** One-based line of the function's first token. */
|
|
9
23
|
startLine: number;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export interface CoverageFunction {
|
|
13
|
-
coveragePercent: number;
|
|
14
|
-
declarationLine: number;
|
|
24
|
+
/** One-based column of the function's first token. */
|
|
25
|
+
startColumn: number;
|
|
15
26
|
endLine: number;
|
|
16
|
-
|
|
27
|
+
lineCount: number;
|
|
28
|
+
complexity: number;
|
|
29
|
+
/** Percentage in [0, 100]. */
|
|
30
|
+
coveragePercent: number;
|
|
31
|
+
/** Statements whose start lies in this function and not in a nested one. */
|
|
32
|
+
statements: number;
|
|
33
|
+
coveredStatements: number;
|
|
34
|
+
/** `statements`: owned statement counters; `function`: the function's own counter (no owned statements); `ignored`: no counter, under a coverage ignore comment; `missing`: no counter and no ignore comment (no coverage entry, or added after the coverage run). */
|
|
35
|
+
evidence: "statements" | "function" | "ignored" | "missing";
|
|
36
|
+
crapScore: number;
|
|
17
37
|
}
|
|
18
38
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
)
|
|
22
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Score every function with a body in one source file.
|
|
41
|
+
* A function without counters (or without `coverage`) reports 0% coverage and
|
|
42
|
+
* `evidence: "missing"`, or `"ignored"` when a coverage ignore comment covers it.
|
|
43
|
+
*/
|
|
23
44
|
export function analyzeFileRisk(input: {
|
|
24
|
-
|
|
45
|
+
coverage?: FileCoverage;
|
|
25
46
|
filePath: string;
|
|
26
|
-
|
|
47
|
+
/** Drop functions shorter than this many lines. Defaults to 1. */
|
|
48
|
+
minLines?: number;
|
|
27
49
|
sourceFilePath?: string;
|
|
28
50
|
sourceText: string;
|
|
29
51
|
}): Promise<FunctionRisk[]>;
|
|
30
52
|
|
|
31
|
-
|
|
32
|
-
|
|
53
|
+
/** 1 plus one per decision point in `node`, excluding nested functions. */
|
|
33
54
|
export function calculateCyclomaticComplexity(node: unknown): number;
|