crap-ts 0.1.1 → 0.2.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 CHANGED
@@ -62,11 +62,16 @@ Put the long bits in `.crap-ts.json`:
62
62
  "coverageFile": "coverage/unit/coverage-final.json",
63
63
  "include": ["src"],
64
64
  "max": 30,
65
+ "warn": 25,
65
66
  "minLines": 10,
66
67
  "limit": 20
67
68
  }
68
69
  ```
69
70
 
71
+ `warn` gives you an early heads-up: functions above 25 are flagged `WARN` and
72
+ reported on stderr, but only scores above `max` fail the build. Leave it out to
73
+ skip warnings.
74
+
70
75
  You can also compute CRAP scores from an external metrics file and coverage
71
76
  summary:
72
77
 
@@ -120,6 +125,8 @@ Options:
120
125
  - `--include <text>`: only analyze coverage files whose path contains this text.
121
126
  - `--limit <n>`: number of report rows to print. Defaults to `20`.
122
127
  - `--max <n>`: strict CRAP threshold for direct audit. Defaults to `30`.
128
+ - `--warn <n>`: warn, without failing, when a score is above this. Must not
129
+ exceed `--max` (or `--max-crap` with `--metrics`). Off by default.
123
130
  - `--min-lines <n>`: ignore functions shorter than this. Defaults to `10`.
124
131
  - `--metrics <path>`: read complexity metrics from JSON.
125
132
  - `--report-only`: print CRAP results without failing on threshold.
@@ -147,6 +154,7 @@ Config files use camelCase keys:
147
154
  "all": false,
148
155
  "limit": 20,
149
156
  "max": 30,
157
+ "warn": 25,
150
158
  "minLines": 10,
151
159
  "reportOnly": false,
152
160
  "skipCoverage": false
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "crap-ts",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Small CRAP-score audit helper for TypeScript projects.",
5
5
  "repository": {
6
6
  "type": "git",
package/src/cli.js CHANGED
@@ -32,6 +32,7 @@ Options:
32
32
  --min-lines <n> Ignore functions shorter than this. Defaults to 10.
33
33
  --max <n> Fail direct CRAP audit when any row is above this score. Defaults to 30.
34
34
  --max-crap <n> Fail CRAP scoring when any row is above this score.
35
+ --warn <n> Warn, without failing, when any row is above this score.
35
36
  --report-only Print CRAP report without failing on threshold.
36
37
  --skip-coverage Do not run the coverage command.
37
38
  --help Show this help.
@@ -59,6 +60,7 @@ export function parseCliArgs(argv, { cwd = process.cwd() } = {}) {
59
60
  "min-lines": { type: "string" },
60
61
  "report-only": { type: "boolean" },
61
62
  "skip-coverage": { type: "boolean" },
63
+ warn: { type: "string" },
62
64
  },
63
65
  });
64
66
  const config =
@@ -81,6 +83,7 @@ export function parseCliArgs(argv, { cwd = process.cwd() } = {}) {
81
83
  paths: positionals,
82
84
  reportOnly: values["report-only"],
83
85
  skipCoverage: values["skip-coverage"],
86
+ warnScore: parseOptionalNumber(values.warn, "--warn"),
84
87
  }),
85
88
  );
86
89
  }
@@ -122,6 +125,17 @@ async function runScore(options) {
122
125
  return 2;
123
126
  }
124
127
 
128
+ if (
129
+ options.maxCrap &&
130
+ options.warnScore !== undefined &&
131
+ options.warnScore > options.maxCrap
132
+ ) {
133
+ console.error(
134
+ `CRAP warn (${options.warnScore}) must not exceed max-crap (${options.maxCrap}).`,
135
+ );
136
+ return 2;
137
+ }
138
+
125
139
  const metrics = JSON.parse(readFileSync(options.metricsPath, "utf8"));
126
140
  const coverageSummary = JSON.parse(
127
141
  readFileSync(options.coveragePath, "utf8"),
@@ -130,9 +144,23 @@ async function runScore(options) {
130
144
  const failingScores = options.maxCrap
131
145
  ? scores.filter((score) => score.crap > options.maxCrap)
132
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
+ );
133
155
 
134
156
  console.log(JSON.stringify(scores, null, 2));
135
157
 
158
+ if (warningScores.length > 0) {
159
+ console.error(
160
+ `CRAP warning: ${warningScores.length} row(s) above ${options.warnScore}.`,
161
+ );
162
+ }
163
+
136
164
  if (failingScores.length > 0) {
137
165
  console.error(
138
166
  `CRAP threshold failed: ${failingScores.length} row(s) exceeded ${options.maxCrap}.`,
@@ -206,6 +234,7 @@ function normalizeConfig(config) {
206
234
  minLines: parseConfigNumber(config.minLines, "minLines"),
207
235
  reportOnly: config.reportOnly,
208
236
  skipCoverage: config.skipCoverage,
237
+ warnScore: parseConfigNumber(config.warn, "warn"),
209
238
  });
210
239
  }
211
240
 
@@ -9,6 +9,8 @@ export interface CrapAuditOptions {
9
9
  minLines?: number;
10
10
  reportOnly?: boolean;
11
11
  skipCoverage?: boolean;
12
+ /** Report, without failing, functions whose CRAP score is above this and at or below `maxScore`. */
13
+ warnScore?: number;
12
14
  }
13
15
 
14
16
  export function runCrapAudit(options?: CrapAuditOptions): Promise<number>;
@@ -20,7 +20,15 @@ export async function runCrapAudit({
20
20
  minLines = 10,
21
21
  reportOnly = false,
22
22
  skipCoverage = false,
23
+ warnScore,
23
24
  } = {}) {
25
+ if (warnScore !== undefined && warnScore > maxScore) {
26
+ console.error(
27
+ `CRAP warn (${warnScore}) must not exceed max (${maxScore}).`,
28
+ );
29
+ return 2;
30
+ }
31
+
24
32
  if (!skipCoverage && coverageCommand) {
25
33
  const result = spawnSync(coverageCommand, {
26
34
  cwd,
@@ -81,16 +89,35 @@ export async function runCrapAudit({
81
89
  (left, right) => right.crapScore - left.crapScore,
82
90
  );
83
91
  const failingRisks = sortedRisks.filter((risk) => risk.crapScore > maxScore);
92
+ const warningRisks =
93
+ warnScore === undefined
94
+ ? []
95
+ : sortedRisks.filter(
96
+ (risk) => risk.crapScore > warnScore && risk.crapScore <= maxScore,
97
+ );
98
+ const warningSummary =
99
+ warnScore === undefined
100
+ ? ""
101
+ : `, ${warningRisks.length} above ${warnScore} (warn)`;
84
102
 
85
103
  console.log();
86
104
  console.log(
87
- `CRAP report: ${sortedRisks.length} functions checked, ${failingRisks.length} above ${maxScore}.`,
105
+ `CRAP report: ${sortedRisks.length} functions checked, ${failingRisks.length} above ${maxScore}${warningSummary}.`,
88
106
  );
89
107
 
90
108
  const printedRisks = all ? sortedRisks : sortedRisks.slice(0, limit);
91
109
 
92
110
  for (const risk of printedRisks) {
93
- console.log(formatRiskLine(risk));
111
+ console.log(
112
+ `${getRiskLevel(risk, { maxScore, warnScore })} ${formatRiskLine(risk)}`,
113
+ );
114
+ }
115
+
116
+ if (warningRisks.length > 0) {
117
+ console.error("");
118
+ console.error(
119
+ `CRAP warning: ${warningRisks.length} function(s) above ${warnScore}. Add coverage or simplify them before they cross ${maxScore}.`,
120
+ );
94
121
  }
95
122
 
96
123
  if (failingRisks.length > 0 && !reportOnly) {
@@ -104,6 +131,18 @@ export async function runCrapAudit({
104
131
  return 0;
105
132
  }
106
133
 
134
+ function getRiskLevel(risk, { maxScore, warnScore }) {
135
+ if (risk.crapScore > maxScore) {
136
+ return "FAIL";
137
+ }
138
+
139
+ if (warnScore !== undefined && risk.crapScore > warnScore) {
140
+ return "WARN";
141
+ }
142
+
143
+ return " ok";
144
+ }
145
+
107
146
  function shouldIncludePath(filePath, includes) {
108
147
  if (includes.length === 0) {
109
148
  return true;