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 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
- "name": "crap-ts",
3
- "version": "0.1.0",
4
- "description": "Small CRAP-score audit helper for TypeScript projects.",
5
- "type": "module",
6
- "bin": {
7
- "crap-ts": "src/cli.js"
8
- },
9
- "exports": {
10
- ".": {
11
- "types": "./src/index.d.ts",
12
- "default": "./src/index.js"
13
- },
14
- "./crap-analysis": {
15
- "types": "./src/crap-analysis.d.ts",
16
- "default": "./src/crap-analysis.js"
17
- },
18
- "./crap-runner": {
19
- "types": "./src/crap-runner.d.ts",
20
- "default": "./src/crap-runner.js"
21
- },
22
- "./crap-score": {
23
- "types": "./src/crap-score.d.ts",
24
- "default": "./src/crap-score.js"
25
- }
26
- },
27
- "files": [
28
- "src",
29
- "README.md",
30
- "CONTRIBUTING.md",
31
- "LICENSE"
32
- ],
33
- "scripts": {
34
- "check": "node --check src/cli.js && node --check src/crap-analysis.js && node --check src/crap-runner.js && node --check src/crap-score.js",
35
- "score": "node src/cli.js score --metrics fixtures/complexity-metrics.json --coverage-file fixtures/coverage-summary.json --max-crap 30",
36
- "test": "node --test tests/*.test.js",
37
- "validate": "npm run test && npm run score"
38
- },
39
- "keywords": [
40
- "complexity",
41
- "typescript",
42
- "quality"
43
- ],
44
- "license": "MIT",
45
- "dependencies": {
46
- "typescript": "^5.9.3"
47
- },
48
- "engines": {
49
- "node": ">=20"
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
- 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 = command === "help" || values.help ? {} : readConfig(values.config, cwd);
65
-
66
- return applyDefaults(mergeConfigWithCli(config, {
67
- command,
68
- all: values.all,
69
- configPath: values.config,
70
- coverageCommand: values["coverage-command"],
71
- coveragePath: values["coverage-file"],
72
- help: values.help,
73
- includes: values.include,
74
- limit: parseOptionalNumber(values.limit, "--limit"),
75
- maxCrap: parseOptionalNumber(values["max-crap"], "--max-crap"),
76
- maxScore: parseOptionalNumber(values.max, "--max"),
77
- metricsPath: values.metrics,
78
- minLines: parseOptionalNumber(values["min-lines"], "--min-lines"),
79
- paths: positionals,
80
- reportOnly: values["report-only"],
81
- skipCoverage: values["skip-coverage"],
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
- const options = parseCliArgs(argv);
87
-
88
- if (options.help || options.command === "help") {
89
- console.log(helpText);
90
- return 0;
91
- }
92
-
93
- if (options.command === "score" || options.metricsPath || options.coveragePath) {
94
- return runScore(options);
95
- }
96
-
97
- if (options.command !== "legacy") {
98
- console.error(`Unknown command: ${options.command}`);
99
- return 2;
100
- }
101
-
102
- console.error("Usage error: run `crap-ts score --help` for supported commands.");
103
- return 2;
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
- if (!options.metricsPath) {
108
- return runCrapAudit(options);
109
- }
110
-
111
- if (!options.coveragePath) {
112
- console.error("CRAP scoring requires both --metrics and --coverage-file.");
113
- return 2;
114
- }
115
-
116
- const metrics = JSON.parse(readFileSync(options.metricsPath, "utf8"));
117
- const coverageSummary = JSON.parse(readFileSync(options.coveragePath, "utf8"));
118
- const scores = buildCrapScores({ coverageSummary, metrics });
119
- const failingScores = options.maxCrap
120
- ? scores.filter((score) => score.crap > options.maxCrap)
121
- : [];
122
-
123
- console.log(JSON.stringify(scores, null, 2));
124
-
125
- if (failingScores.length > 0) {
126
- console.error(
127
- `CRAP threshold failed: ${failingScores.length} row(s) exceeded ${options.maxCrap}.`,
128
- );
129
- return 1;
130
- }
131
-
132
- return 0;
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
- const firstArg = argv[0];
147
+ const firstArg = argv[0];
137
148
 
138
- if (firstArg === "score" || firstArg === "help") {
139
- return firstArg;
140
- }
149
+ if (firstArg === "score" || firstArg === "help") {
150
+ return firstArg;
151
+ }
141
152
 
142
- return "legacy";
153
+ return "legacy";
143
154
  }
144
155
 
145
156
  function parseOptionalNumber(value, label) {
146
- if (value === undefined) {
147
- return undefined;
148
- }
157
+ if (value === undefined) {
158
+ return undefined;
159
+ }
149
160
 
150
- const parsed = Number(value);
161
+ const parsed = Number(value);
151
162
 
152
- if (!Number.isFinite(parsed)) {
153
- throw new TypeError(`${label} must be a finite number.`);
154
- }
163
+ if (!Number.isFinite(parsed)) {
164
+ throw new TypeError(`${label} must be a finite number.`);
165
+ }
155
166
 
156
- return parsed;
167
+ return parsed;
157
168
  }
158
169
 
159
170
  function readConfig(configPath, cwd) {
160
- const resolvedPath = resolveConfigPath(configPath, cwd);
171
+ const resolvedPath = resolveConfigPath(configPath, cwd);
161
172
 
162
- if (!resolvedPath) {
163
- return {};
164
- }
173
+ if (!resolvedPath) {
174
+ return {};
175
+ }
165
176
 
166
- return JSON.parse(readFileSync(resolvedPath, "utf8"));
177
+ return JSON.parse(readFileSync(resolvedPath, "utf8"));
167
178
  }
168
179
 
169
180
  function resolveConfigPath(configPath, cwd) {
170
- if (configPath) {
171
- return resolve(cwd, configPath);
172
- }
181
+ if (configPath) {
182
+ return resolve(cwd, configPath);
183
+ }
173
184
 
174
- const defaultPath = resolve(cwd, defaultConfigPath);
175
- return existsSync(defaultPath) ? defaultPath : null;
185
+ const defaultPath = resolve(cwd, defaultConfigPath);
186
+ return existsSync(defaultPath) ? defaultPath : null;
176
187
  }
177
188
 
178
189
  function mergeConfigWithCli(config, cliOptions) {
179
- return {
180
- ...normalizeConfig(config),
181
- ...stripUndefined(cliOptions),
182
- };
190
+ return {
191
+ ...normalizeConfig(config),
192
+ ...stripUndefined(cliOptions),
193
+ };
183
194
  }
184
195
 
185
196
  function normalizeConfig(config) {
186
- return stripUndefined({
187
- all: config.all,
188
- coverageCommand: config.coverageCommand,
189
- coveragePath: config.coverageFile,
190
- includes: normalizeConfigList(config.include, "include"),
191
- limit: parseConfigNumber(config.limit, "limit"),
192
- maxCrap: parseConfigNumber(config.maxCrap, "maxCrap"),
193
- maxScore: parseConfigNumber(config.max, "max"),
194
- metricsPath: config.metrics,
195
- minLines: parseConfigNumber(config.minLines, "minLines"),
196
- reportOnly: config.reportOnly,
197
- skipCoverage: config.skipCoverage,
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
- if (value === undefined) {
203
- return undefined;
204
- }
213
+ if (value === undefined) {
214
+ return undefined;
215
+ }
205
216
 
206
- if (typeof value !== "number" || !Number.isFinite(value)) {
207
- throw new TypeError(`${label} must be a finite number in ${defaultConfigPath}.`);
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
- return value;
223
+ return value;
211
224
  }
212
225
 
213
226
  function normalizeConfigList(value, label) {
214
- if (value === undefined) {
215
- return undefined;
216
- }
227
+ if (value === undefined) {
228
+ return undefined;
229
+ }
217
230
 
218
- if (typeof value === "string") {
219
- return [value];
220
- }
231
+ if (typeof value === "string") {
232
+ return [value];
233
+ }
221
234
 
222
- if (Array.isArray(value) && value.every((item) => typeof item === "string")) {
223
- return value;
224
- }
235
+ if (Array.isArray(value) && value.every((item) => typeof item === "string")) {
236
+ return value;
237
+ }
225
238
 
226
- throw new TypeError(`${label} must be a string or string array in ${defaultConfigPath}.`);
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
- return {
231
- ...options,
232
- all: options.all ?? false,
233
- help: options.help ?? false,
234
- includes: options.includes ?? [],
235
- reportOnly: options.reportOnly ?? false,
236
- skipCoverage: options.skipCoverage ?? false,
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
- return Object.fromEntries(Object.entries(values).filter(([, value]) => value !== undefined));
256
+ return Object.fromEntries(
257
+ Object.entries(values).filter(([, value]) => value !== undefined),
258
+ );
242
259
  }
243
260
 
244
261
  if (isDirectExecution()) {
245
- process.exitCode = main();
262
+ process.exitCode = await main();
246
263
  }
247
264
 
248
265
  function isDirectExecution() {
249
- if (!process.argv[1]) {
250
- return false;
251
- }
266
+ if (!process.argv[1]) {
267
+ return false;
268
+ }
252
269
 
253
- return realpathSync(process.argv[1]) === fileURLToPath(import.meta.url);
270
+ return realpathSync(process.argv[1]) === fileURLToPath(import.meta.url);
254
271
  }
@@ -1,34 +1,33 @@
1
- import ts from "typescript";
2
-
3
1
  export interface FunctionRisk {
4
- complexity: number;
5
- coveragePercent: number;
6
- crapScore: number;
7
- endLine: number;
8
- filePath: string;
9
- lineCount: number;
10
- name: string;
11
- startLine: number;
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
- coveragePercent: number;
16
- declarationLine: number;
17
- endLine: number;
18
- startLine: number;
13
+ coveragePercent: number;
14
+ declarationLine: number;
15
+ endLine: number;
16
+ startLine: number;
19
17
  }
20
18
 
21
19
  export function extractCoverageFunctions(
22
- coverageJson: Record<string, unknown>,
20
+ coverageJson: Record<string, unknown>,
23
21
  ): Map<string, CoverageFunction[]>;
24
22
 
25
23
  export function analyzeFileRisk(input: {
26
- coverageFunctions: CoverageFunction[];
27
- filePath: string;
28
- minLines: number;
29
- sourceText: string;
30
- }): FunctionRisk[];
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: ts.Node): number;
33
+ export function calculateCyclomaticComplexity(node: unknown): number;