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/crap-analysis.js
CHANGED
|
@@ -4,81 +4,179 @@ import { basename, join } from "node:path";
|
|
|
4
4
|
|
|
5
5
|
import ts from "typescript";
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
export function extractCoverageFunctions(coverageJson) {
|
|
10
|
-
const coverageByFile = new Map();
|
|
11
|
-
|
|
12
|
-
for (const [filePath, coverage] of Object.entries(coverageJson)) {
|
|
13
|
-
const functions = Object.entries(coverage.fnMap ?? {}).flatMap(
|
|
14
|
-
([id, metadata]) => {
|
|
15
|
-
const startLine = metadata.loc?.start?.line;
|
|
16
|
-
const endLine = metadata.loc?.end?.line;
|
|
17
|
-
|
|
18
|
-
if (!startLine || !endLine) {
|
|
19
|
-
return [];
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return [
|
|
23
|
-
{
|
|
24
|
-
coveragePercent: (coverage.f?.[id] ?? 0) > 0 ? 100 : 0,
|
|
25
|
-
declarationLine: metadata.line ?? startLine,
|
|
26
|
-
endLine,
|
|
27
|
-
startLine,
|
|
28
|
-
},
|
|
29
|
-
];
|
|
30
|
-
},
|
|
31
|
-
);
|
|
7
|
+
import { calculateCrapScore } from "./crap-score.js";
|
|
32
8
|
|
|
33
|
-
|
|
34
|
-
|
|
9
|
+
// TypeScript 7 has no `createSourceFile`; its AST lives behind the unstable API.
|
|
10
|
+
const ts7Ast = ts.createSourceFile
|
|
11
|
+
? null
|
|
12
|
+
: await import("typescript/unstable/ast");
|
|
13
|
+
const defaultAdapter = ts7Ast
|
|
14
|
+
? { SyntaxKind: ts7Ast.SyntaxKind, forEachChild: forEachTs7Child }
|
|
15
|
+
: { SyntaxKind: ts.SyntaxKind, forEachChild: ts.forEachChild };
|
|
16
|
+
|
|
17
|
+
export async function analyzeFileRisk({
|
|
18
|
+
coverage,
|
|
19
|
+
filePath,
|
|
20
|
+
minLines = 1,
|
|
21
|
+
sourceFilePath,
|
|
22
|
+
sourceText,
|
|
23
|
+
}) {
|
|
24
|
+
const { functions } = await analyzeSource({
|
|
25
|
+
coverage,
|
|
26
|
+
filePath,
|
|
27
|
+
sourceFilePath,
|
|
28
|
+
sourceText,
|
|
29
|
+
});
|
|
35
30
|
|
|
36
|
-
return
|
|
31
|
+
return functions.filter((risk) => risk.lineCount >= minLines);
|
|
37
32
|
}
|
|
38
33
|
|
|
39
|
-
export async function
|
|
40
|
-
|
|
34
|
+
export async function analyzeSource({
|
|
35
|
+
coverage,
|
|
36
|
+
failOnIgnored = false,
|
|
41
37
|
filePath,
|
|
42
|
-
minLines,
|
|
43
38
|
sourceFilePath,
|
|
44
39
|
sourceText,
|
|
45
40
|
}) {
|
|
46
|
-
const
|
|
41
|
+
const sourceFile = await parseSourceFile({
|
|
47
42
|
filePath,
|
|
48
43
|
sourceFilePath,
|
|
49
44
|
sourceText,
|
|
50
45
|
});
|
|
51
|
-
const { sourceFile }
|
|
52
|
-
const
|
|
46
|
+
const adapter = { ...defaultAdapter, sourceFile };
|
|
47
|
+
const ignore = findIgnoreComments(sourceText, collectLiteralRanges(adapter));
|
|
48
|
+
const functions = collectFunctions(adapter, filePath, ignore);
|
|
49
|
+
|
|
50
|
+
if (coverage) {
|
|
51
|
+
assertStatementsInSource(coverage, sourceText, filePath);
|
|
52
|
+
assignStatementCoverage(functions, coverage);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
executable:
|
|
57
|
+
(!ignore.file || failOnIgnored) &&
|
|
58
|
+
(functions.length > 0 || hasExecutableModuleStatements(adapter)),
|
|
59
|
+
functions: functions.map(({ ignored, risk }) => {
|
|
60
|
+
// No counters is only acceptable where an ignore comment removed them.
|
|
61
|
+
if (ignored && risk.evidence === "missing") {
|
|
62
|
+
risk.evidence = "ignored";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
risk.crapScore = calculateCrapScore({
|
|
66
|
+
complexity: risk.complexity,
|
|
67
|
+
coverage: risk.coveragePercent,
|
|
68
|
+
});
|
|
69
|
+
return risk;
|
|
70
|
+
}),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function calculateCyclomaticComplexity(node, adapter = defaultAdapter) {
|
|
75
|
+
let complexity = 1;
|
|
76
|
+
|
|
77
|
+
function visit(child) {
|
|
78
|
+
// Nested functions get their own rows; never charge their decisions here.
|
|
79
|
+
if (isFunctionLikeNode(adapter, child)) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (addsDecisionPath(adapter, child)) {
|
|
84
|
+
complexity += 1;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
adapter.forEachChild(child, visit);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (isFunctionLikeNode(adapter, node)) {
|
|
91
|
+
adapter.forEachChild(node, visit);
|
|
92
|
+
} else {
|
|
93
|
+
visit(node);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return complexity;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function addsDecisionPath({ SyntaxKind }, node) {
|
|
100
|
+
switch (node.kind) {
|
|
101
|
+
case SyntaxKind.IfStatement:
|
|
102
|
+
case SyntaxKind.ConditionalExpression:
|
|
103
|
+
case SyntaxKind.ForStatement:
|
|
104
|
+
case SyntaxKind.ForInStatement:
|
|
105
|
+
case SyntaxKind.ForOfStatement:
|
|
106
|
+
case SyntaxKind.WhileStatement:
|
|
107
|
+
case SyntaxKind.DoStatement:
|
|
108
|
+
case SyntaxKind.CaseClause:
|
|
109
|
+
case SyntaxKind.CatchClause:
|
|
110
|
+
return true;
|
|
111
|
+
case SyntaxKind.BinaryExpression:
|
|
112
|
+
return [
|
|
113
|
+
SyntaxKind.AmpersandAmpersandToken,
|
|
114
|
+
SyntaxKind.BarBarToken,
|
|
115
|
+
SyntaxKind.QuestionQuestionToken,
|
|
116
|
+
SyntaxKind.AmpersandAmpersandEqualsToken,
|
|
117
|
+
SyntaxKind.BarBarEqualsToken,
|
|
118
|
+
SyntaxKind.QuestionQuestionEqualsToken,
|
|
119
|
+
].includes(node.operatorToken.kind);
|
|
120
|
+
case SyntaxKind.Parameter:
|
|
121
|
+
case SyntaxKind.BindingElement:
|
|
122
|
+
return Boolean(node.initializer);
|
|
123
|
+
case SyntaxKind.ShorthandPropertyAssignment:
|
|
124
|
+
return Boolean(node.objectAssignmentInitializer);
|
|
125
|
+
default:
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function collectFunctions(adapter, filePath, ignore) {
|
|
131
|
+
const { SyntaxKind, sourceFile } = adapter;
|
|
132
|
+
const functions = [];
|
|
53
133
|
|
|
54
134
|
function visit(node) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
135
|
+
const start = toPosition(sourceFile, node.getStart(sourceFile));
|
|
136
|
+
|
|
137
|
+
const targetKey = positionKey(start);
|
|
138
|
+
const directive = ignore.nextTargets.get(targetKey);
|
|
139
|
+
|
|
140
|
+
// An `ignore next|if|else` comment applies to the outermost node after it.
|
|
141
|
+
if (directive && node.kind !== SyntaxKind.SourceFile) {
|
|
142
|
+
ignore.nextTargets.delete(targetKey);
|
|
143
|
+
const target =
|
|
144
|
+
directive === "next"
|
|
145
|
+
? node
|
|
146
|
+
: node.kind === SyntaxKind.IfStatement
|
|
147
|
+
? node[directive === "if" ? "thenStatement" : "elseStatement"]
|
|
148
|
+
: undefined;
|
|
149
|
+
|
|
150
|
+
if (target) {
|
|
151
|
+
ignore.ranges.push({
|
|
152
|
+
end: toPosition(sourceFile, target.getEnd()),
|
|
153
|
+
start: toPosition(sourceFile, target.getStart(sourceFile)),
|
|
69
154
|
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
70
157
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
158
|
+
if (isFunctionLikeNode(adapter, node) && node.body) {
|
|
159
|
+
const end = toPosition(sourceFile, node.getEnd());
|
|
160
|
+
|
|
161
|
+
functions.push({
|
|
162
|
+
bodyStart: toPosition(sourceFile, node.body.getStart(sourceFile)),
|
|
163
|
+
ignored: isIgnored(ignore, start),
|
|
164
|
+
range: { end, start },
|
|
165
|
+
risk: {
|
|
166
|
+
complexity: calculateCyclomaticComplexity(node, adapter),
|
|
167
|
+
coveragePercent: 0,
|
|
168
|
+
coveredStatements: 0,
|
|
169
|
+
crapScore: 0,
|
|
170
|
+
endLine: end.line,
|
|
171
|
+
evidence: "missing",
|
|
76
172
|
filePath,
|
|
77
|
-
lineCount,
|
|
78
|
-
name: getFunctionName(adapter, node
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
173
|
+
lineCount: end.line - start.line + 1,
|
|
174
|
+
name: getFunctionName(adapter, node),
|
|
175
|
+
startColumn: start.column + 1,
|
|
176
|
+
startLine: start.line,
|
|
177
|
+
statements: 0,
|
|
178
|
+
},
|
|
179
|
+
});
|
|
82
180
|
}
|
|
83
181
|
|
|
84
182
|
adapter.forEachChild(node, visit);
|
|
@@ -86,191 +184,384 @@ export async function analyzeFileRisk({
|
|
|
86
184
|
|
|
87
185
|
visit(sourceFile);
|
|
88
186
|
|
|
89
|
-
return
|
|
187
|
+
return functions;
|
|
90
188
|
}
|
|
91
189
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
risk.name,
|
|
96
|
-
`CRAP ${risk.crapScore.toFixed(1)}`,
|
|
97
|
-
`complexity ${risk.complexity}`,
|
|
98
|
-
`coverage ${risk.coveragePercent.toFixed(0)}%`,
|
|
99
|
-
`${risk.lineCount} lines`,
|
|
100
|
-
].join(" | ");
|
|
101
|
-
}
|
|
190
|
+
function assignStatementCoverage(functions, coverage) {
|
|
191
|
+
for (const [id, location] of Object.entries(coverage.statementMap)) {
|
|
192
|
+
const owner = findOwner(functions, location.start);
|
|
102
193
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
let complexity = 1;
|
|
194
|
+
// Module-level statements belong to no function row.
|
|
195
|
+
if (!owner) {
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
108
198
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
199
|
+
owner.risk.statements += 1;
|
|
200
|
+
owner.risk.coveredStatements += coverage.s[id] > 0 ? 1 : 0;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const v8Layout = hasFirstTokenCounters(functions, coverage.fnMap);
|
|
204
|
+
const countersByOwner = groupCountersBy(coverage.fnMap, (start) =>
|
|
205
|
+
findCounterOwner(functions, start, v8Layout),
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
for (const fn of functions) {
|
|
209
|
+
const { risk } = fn;
|
|
210
|
+
|
|
211
|
+
if (risk.statements > 0) {
|
|
212
|
+
risk.coveragePercent = (risk.coveredStatements / risk.statements) * 100;
|
|
213
|
+
risk.evidence = "statements";
|
|
214
|
+
continue;
|
|
112
215
|
}
|
|
113
216
|
|
|
114
|
-
|
|
217
|
+
const counters = countersByOwner.get(fn) ?? [];
|
|
218
|
+
|
|
219
|
+
if (counters.length === 1) {
|
|
220
|
+
risk.coveragePercent = coverage.f[counters[0]] > 0 ? 100 : 0;
|
|
221
|
+
risk.evidence = "function";
|
|
222
|
+
}
|
|
115
223
|
}
|
|
224
|
+
}
|
|
116
225
|
|
|
117
|
-
|
|
226
|
+
// Istanbul places a function counter at the function's body; per-line V8
|
|
227
|
+
// converters place it at the function's first token. Only V8 puts a counter at
|
|
228
|
+
// the first token of a function that is not another arrow's expression body.
|
|
229
|
+
function hasFirstTokenCounters(functions, fnMap) {
|
|
230
|
+
const counterStarts = new Set(
|
|
231
|
+
Object.values(fnMap).map(({ loc }) => positionKey(loc.start)),
|
|
232
|
+
);
|
|
233
|
+
const expressionBodies = new Set(
|
|
234
|
+
functions.map((fn) => positionKey(fn.bodyStart)),
|
|
235
|
+
);
|
|
118
236
|
|
|
119
|
-
return
|
|
237
|
+
return functions.some((fn) => {
|
|
238
|
+
const start = positionKey(fn.range.start);
|
|
239
|
+
return !expressionBodies.has(start) && counterStarts.has(start);
|
|
240
|
+
});
|
|
120
241
|
}
|
|
121
242
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
) {
|
|
134
|
-
return true;
|
|
243
|
+
// The two layouts meet only where an arrow is another arrow's expression body
|
|
244
|
+
// (`a => b => 1`): Istanbul's counter there is the outer arrow's, V8's is the
|
|
245
|
+
// inner arrow's.
|
|
246
|
+
function findCounterOwner(functions, start, v8Layout) {
|
|
247
|
+
const startingHere = functions.findLast(
|
|
248
|
+
(fn) => comparePositions(fn.range.start, start) === 0,
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
if (!startingHere) {
|
|
252
|
+
// A nested function's counter must never stand in for its parent's.
|
|
253
|
+
return findOwner(functions, start);
|
|
135
254
|
}
|
|
136
255
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
(node.operatorToken.kind === adapter.SyntaxKind.AmpersandAmpersandToken ||
|
|
140
|
-
node.operatorToken.kind === adapter.SyntaxKind.BarBarToken ||
|
|
141
|
-
node.operatorToken.kind === adapter.SyntaxKind.QuestionQuestionToken)
|
|
256
|
+
const parent = functions.find(
|
|
257
|
+
(fn) => comparePositions(fn.bodyStart, start) === 0,
|
|
142
258
|
);
|
|
259
|
+
|
|
260
|
+
return parent && !v8Layout ? parent : startingHere;
|
|
143
261
|
}
|
|
144
262
|
|
|
145
|
-
function
|
|
146
|
-
const
|
|
147
|
-
(coverageFunction) =>
|
|
148
|
-
coverageFunction.declarationLine === startLine ||
|
|
149
|
-
(coverageFunction.startLine === startLine &&
|
|
150
|
-
coverageFunction.endLine === endLine),
|
|
151
|
-
);
|
|
263
|
+
function groupCountersBy(fnMap, getFunction) {
|
|
264
|
+
const groups = new Map();
|
|
152
265
|
|
|
153
|
-
|
|
154
|
-
|
|
266
|
+
for (const [id, metadata] of Object.entries(fnMap)) {
|
|
267
|
+
const fn = getFunction(metadata.loc.start);
|
|
268
|
+
|
|
269
|
+
if (fn) {
|
|
270
|
+
groups.set(fn, [...(groups.get(fn) ?? []), id]);
|
|
271
|
+
}
|
|
155
272
|
}
|
|
156
273
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
274
|
+
return groups;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Functions are collected in preorder, so the last match is the innermost.
|
|
278
|
+
function findOwner(functions, position) {
|
|
279
|
+
return functions.findLast((fn) => containsPosition(fn.range, position));
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// The start is exclusive: Istanbul places initializer counters and an outer
|
|
283
|
+
// arrow's expression body at a function's first token, and those belong to
|
|
284
|
+
// the enclosing scope.
|
|
285
|
+
function containsPosition(range, position) {
|
|
286
|
+
return (
|
|
287
|
+
comparePositions(range.start, position) < 0 &&
|
|
288
|
+
comparePositions(position, range.end) < 0
|
|
163
289
|
);
|
|
290
|
+
}
|
|
164
291
|
|
|
165
|
-
|
|
292
|
+
function comparePositions(left, right) {
|
|
293
|
+
return left.line - right.line || left.column - right.column;
|
|
166
294
|
}
|
|
167
295
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
296
|
+
// The line terminators TypeScript and Babel recognise.
|
|
297
|
+
const lineTerminatorPattern = /\r\n|[\n\r\u2028\u2029]/g;
|
|
298
|
+
const commentPattern = /\/\*([\s\S]*?)\*\/|\/\/([^\n\r\u2028\u2029]*)/g;
|
|
299
|
+
const ignoreDirectivePattern =
|
|
300
|
+
/^\s*(?:istanbul|v8|c8)\s+ignore\s+(next|if|else|start|stop|file)(?:\s+(\d+))?(?=\W|$)/;
|
|
301
|
+
const triviaPattern = /(?:\s|\/\*[\s\S]*?\*\/|\/\/[^\n\r\u2028\u2029]*)*/y;
|
|
302
|
+
|
|
303
|
+
// Coverage tools drop counters for code under these comments:
|
|
304
|
+
// `istanbul|v8|c8 ignore next [lines]`, `ignore if|else`, `ignore start` … `ignore stop`,
|
|
305
|
+
// `ignore file`.
|
|
306
|
+
function findIgnoreComments(sourceText, literalRanges) {
|
|
307
|
+
const lineStarts = [0];
|
|
308
|
+
|
|
309
|
+
for (const match of sourceText.matchAll(lineTerminatorPattern)) {
|
|
310
|
+
lineStarts.push(match.index + match[0].length);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const toSourcePosition = (offset) => {
|
|
314
|
+
const line = lineStarts.findLastIndex((lineStart) => lineStart <= offset);
|
|
315
|
+
return { column: offset - lineStarts[line], line: line + 1 };
|
|
316
|
+
};
|
|
317
|
+
const toOffset = ({ column, line }) => lineStarts[line - 1] + column;
|
|
318
|
+
const literals = literalRanges.map(({ end, start }) => ({
|
|
319
|
+
end: toOffset(end),
|
|
320
|
+
start: toOffset(start),
|
|
321
|
+
}));
|
|
322
|
+
const ignore = {
|
|
323
|
+
file: false,
|
|
324
|
+
lineRanges: [],
|
|
325
|
+
nextTargets: new Map(),
|
|
326
|
+
ranges: [],
|
|
327
|
+
};
|
|
328
|
+
let regionStart;
|
|
329
|
+
|
|
330
|
+
commentPattern.lastIndex = 0;
|
|
331
|
+
|
|
332
|
+
for (
|
|
333
|
+
let match = commentPattern.exec(sourceText);
|
|
334
|
+
match;
|
|
335
|
+
match = commentPattern.exec(sourceText)
|
|
173
336
|
) {
|
|
174
|
-
|
|
337
|
+
const literal = literals.find(
|
|
338
|
+
({ end, start }) => start <= match.index && match.index < end,
|
|
339
|
+
);
|
|
340
|
+
|
|
341
|
+
// Comment-like text inside a literal is not a comment; resume after it.
|
|
342
|
+
if (literal) {
|
|
343
|
+
commentPattern.lastIndex = literal.end;
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const directive = ignoreDirectivePattern.exec(match[1] ?? match[2]);
|
|
348
|
+
|
|
349
|
+
if (!directive) {
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const [, kind, lineCount] = directive;
|
|
354
|
+
const commentEnd = match.index + match[0].length;
|
|
355
|
+
|
|
356
|
+
if (kind === "file") {
|
|
357
|
+
ignore.file = true;
|
|
358
|
+
} else if (kind === "start") {
|
|
359
|
+
regionStart ??= toSourcePosition(commentEnd);
|
|
360
|
+
} else if (kind === "stop" && regionStart) {
|
|
361
|
+
ignore.ranges.push({
|
|
362
|
+
end: toSourcePosition(match.index),
|
|
363
|
+
start: regionStart,
|
|
364
|
+
});
|
|
365
|
+
regionStart = undefined;
|
|
366
|
+
} else if (kind === "next" && lineCount) {
|
|
367
|
+
// c8 style: the next N lines.
|
|
368
|
+
const { line } = toSourcePosition(commentEnd);
|
|
369
|
+
ignore.lineRanges.push([line + 1, line + Number(lineCount)]);
|
|
370
|
+
} else if (kind === "next" || kind === "if" || kind === "else") {
|
|
371
|
+
triviaPattern.lastIndex = commentEnd;
|
|
372
|
+
triviaPattern.exec(sourceText);
|
|
373
|
+
ignore.nextTargets.set(
|
|
374
|
+
positionKey(toSourcePosition(triviaPattern.lastIndex)),
|
|
375
|
+
kind,
|
|
376
|
+
);
|
|
377
|
+
}
|
|
175
378
|
}
|
|
176
379
|
|
|
177
|
-
if (
|
|
178
|
-
|
|
380
|
+
if (regionStart) {
|
|
381
|
+
ignore.ranges.push({
|
|
382
|
+
end: { column: 0, line: Number.POSITIVE_INFINITY },
|
|
383
|
+
start: regionStart,
|
|
384
|
+
});
|
|
179
385
|
}
|
|
180
386
|
|
|
181
|
-
|
|
387
|
+
return ignore;
|
|
388
|
+
}
|
|
182
389
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
390
|
+
// Strings, templates, regex literals and JSX text can contain `/*` or `//`.
|
|
391
|
+
function collectLiteralRanges(adapter) {
|
|
392
|
+
const { SyntaxKind, sourceFile } = adapter;
|
|
393
|
+
const literalKinds = new Set([
|
|
394
|
+
SyntaxKind.StringLiteral,
|
|
395
|
+
SyntaxKind.NoSubstitutionTemplateLiteral,
|
|
396
|
+
SyntaxKind.TemplateHead,
|
|
397
|
+
SyntaxKind.TemplateMiddle,
|
|
398
|
+
SyntaxKind.TemplateTail,
|
|
399
|
+
SyntaxKind.RegularExpressionLiteral,
|
|
400
|
+
SyntaxKind.JsxText,
|
|
401
|
+
]);
|
|
402
|
+
const ranges = [];
|
|
186
403
|
|
|
187
|
-
|
|
188
|
-
|
|
404
|
+
function visit(node) {
|
|
405
|
+
if (literalKinds.has(node.kind)) {
|
|
406
|
+
ranges.push({
|
|
407
|
+
end: toPosition(sourceFile, node.getEnd()),
|
|
408
|
+
// JSX text has no leading trivia; its full start is its text.
|
|
409
|
+
start: toPosition(
|
|
410
|
+
sourceFile,
|
|
411
|
+
node.kind === SyntaxKind.JsxText
|
|
412
|
+
? node.pos
|
|
413
|
+
: node.getStart(sourceFile),
|
|
414
|
+
),
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
adapter.forEachChild(node, visit);
|
|
189
419
|
}
|
|
190
420
|
|
|
191
|
-
|
|
421
|
+
visit(sourceFile);
|
|
422
|
+
|
|
423
|
+
return ranges;
|
|
192
424
|
}
|
|
193
425
|
|
|
194
|
-
function
|
|
426
|
+
function isIgnored(ignore, position) {
|
|
195
427
|
return (
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
428
|
+
ignore.file ||
|
|
429
|
+
ignore.ranges.some(
|
|
430
|
+
(range) =>
|
|
431
|
+
comparePositions(range.start, position) <= 0 &&
|
|
432
|
+
comparePositions(position, range.end) < 0,
|
|
433
|
+
) ||
|
|
434
|
+
ignore.lineRanges.some(
|
|
435
|
+
([first, last]) => position.line >= first && position.line <= last,
|
|
436
|
+
)
|
|
200
437
|
);
|
|
201
438
|
}
|
|
202
439
|
|
|
203
|
-
function
|
|
204
|
-
return {
|
|
205
|
-
...ts,
|
|
206
|
-
forEachChild: ts.forEachChild,
|
|
207
|
-
};
|
|
440
|
+
function positionKey({ column, line }) {
|
|
441
|
+
return `${line}:${column}`;
|
|
208
442
|
}
|
|
209
443
|
|
|
210
|
-
function
|
|
211
|
-
|
|
212
|
-
|
|
444
|
+
function assertStatementsInSource(coverage, sourceText, filePath) {
|
|
445
|
+
const lines = sourceText.split(lineTerminatorPattern);
|
|
446
|
+
|
|
447
|
+
for (const { start } of Object.values(coverage.statementMap)) {
|
|
448
|
+
const line = lines[start.line - 1];
|
|
449
|
+
|
|
450
|
+
if (line === undefined || start.column > line.length) {
|
|
451
|
+
throw new Error(
|
|
452
|
+
`Coverage for ${filePath} does not match its current source (statement at ${start.line}:${start.column + 1}). Re-run coverage.`,
|
|
453
|
+
);
|
|
454
|
+
}
|
|
213
455
|
}
|
|
456
|
+
}
|
|
214
457
|
|
|
215
|
-
|
|
458
|
+
function hasExecutableModuleStatements({ SyntaxKind, sourceFile }) {
|
|
459
|
+
return hasExecutableStatements(SyntaxKind, sourceFile.statements);
|
|
216
460
|
}
|
|
217
461
|
|
|
218
|
-
function
|
|
219
|
-
return {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
462
|
+
function hasExecutableStatements(SyntaxKind, statements) {
|
|
463
|
+
return statements.some((statement) => {
|
|
464
|
+
if (
|
|
465
|
+
statement.modifiers?.some(
|
|
466
|
+
(modifier) => modifier.kind === SyntaxKind.DeclareKeyword,
|
|
467
|
+
)
|
|
468
|
+
) {
|
|
469
|
+
return false;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
switch (statement.kind) {
|
|
473
|
+
case SyntaxKind.ImportEqualsDeclaration:
|
|
474
|
+
return !statement.isTypeOnly;
|
|
475
|
+
case SyntaxKind.ImportDeclaration:
|
|
476
|
+
// Side-effect imports (`import "./x"`) run code; binding imports do not.
|
|
477
|
+
return !statement.importClause;
|
|
478
|
+
case SyntaxKind.ModuleDeclaration:
|
|
479
|
+
// TypeScript emits a namespace only when its body has runtime code.
|
|
480
|
+
return isExecutableNamespace(SyntaxKind, statement);
|
|
481
|
+
case SyntaxKind.ExportDeclaration:
|
|
482
|
+
case SyntaxKind.InterfaceDeclaration:
|
|
483
|
+
case SyntaxKind.TypeAliasDeclaration:
|
|
484
|
+
case SyntaxKind.FunctionDeclaration:
|
|
485
|
+
case SyntaxKind.EmptyStatement:
|
|
486
|
+
return false;
|
|
487
|
+
default:
|
|
488
|
+
return true;
|
|
489
|
+
}
|
|
490
|
+
});
|
|
235
491
|
}
|
|
236
492
|
|
|
237
|
-
function
|
|
238
|
-
|
|
493
|
+
function isExecutableNamespace(SyntaxKind, declaration) {
|
|
494
|
+
const { body } = declaration;
|
|
495
|
+
|
|
496
|
+
if (!body) {
|
|
497
|
+
return false;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// `namespace A.B {}` nests one ModuleDeclaration per name.
|
|
501
|
+
return body.kind === SyntaxKind.ModuleDeclaration
|
|
502
|
+
? isExecutableNamespace(SyntaxKind, body)
|
|
503
|
+
: hasExecutableStatements(SyntaxKind, body.statements);
|
|
239
504
|
}
|
|
240
505
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
ForOfStatement: 251,
|
|
251
|
-
ForStatement: 249,
|
|
252
|
-
IfStatement: 246,
|
|
253
|
-
QuestionQuestionToken: 60,
|
|
254
|
-
WhileStatement: 248,
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
async function getTypescriptAdapter({ filePath, sourceFilePath, sourceText }) {
|
|
258
|
-
if (ts.createSourceFile) {
|
|
259
|
-
return {
|
|
260
|
-
...getTs5Adapter(),
|
|
261
|
-
sourceFile: ts.createSourceFile(
|
|
262
|
-
filePath,
|
|
263
|
-
sourceText,
|
|
264
|
-
ts.ScriptTarget.Latest,
|
|
265
|
-
true,
|
|
266
|
-
),
|
|
267
|
-
};
|
|
506
|
+
function toPosition(sourceFile, offset) {
|
|
507
|
+
const { character, line } = sourceFile.getLineAndCharacterOfPosition(offset);
|
|
508
|
+
|
|
509
|
+
return { column: character, line: line + 1 };
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function getFunctionName({ SyntaxKind, sourceFile }, node) {
|
|
513
|
+
if (node.kind === SyntaxKind.Constructor) {
|
|
514
|
+
return "constructor";
|
|
268
515
|
}
|
|
269
516
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
517
|
+
if (node.name) {
|
|
518
|
+
return node.name.getText(sourceFile);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
const parent = node.parent;
|
|
522
|
+
|
|
523
|
+
if (
|
|
524
|
+
(parent?.kind === SyntaxKind.VariableDeclaration ||
|
|
525
|
+
parent?.kind === SyntaxKind.PropertyAssignment ||
|
|
526
|
+
parent?.kind === SyntaxKind.PropertyDeclaration) &&
|
|
527
|
+
parent.name
|
|
528
|
+
) {
|
|
529
|
+
return parent.name.getText(sourceFile);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
return "<anonymous>";
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function isFunctionLikeNode({ SyntaxKind }, node) {
|
|
536
|
+
switch (node.kind) {
|
|
537
|
+
case SyntaxKind.FunctionDeclaration:
|
|
538
|
+
case SyntaxKind.FunctionExpression:
|
|
539
|
+
case SyntaxKind.ArrowFunction:
|
|
540
|
+
case SyntaxKind.MethodDeclaration:
|
|
541
|
+
case SyntaxKind.Constructor:
|
|
542
|
+
case SyntaxKind.GetAccessor:
|
|
543
|
+
case SyntaxKind.SetAccessor:
|
|
544
|
+
return true;
|
|
545
|
+
default:
|
|
546
|
+
return false;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function forEachTs7Child(node, visitor) {
|
|
551
|
+
ts7Ast.visitEachChild(node, visitor, null);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
async function parseSourceFile({ filePath, sourceFilePath, sourceText }) {
|
|
555
|
+
if (!ts7Ast) {
|
|
556
|
+
return ts.createSourceFile(
|
|
557
|
+
filePath,
|
|
558
|
+
sourceText,
|
|
559
|
+
ts.ScriptTarget.Latest,
|
|
560
|
+
true,
|
|
561
|
+
);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
const { API } = await import("typescript/unstable/sync");
|
|
274
565
|
const temporaryDirectory = mkdtempSync(join(tmpdir(), "crap-ts-"));
|
|
275
566
|
const resolvedPath = join(
|
|
276
567
|
temporaryDirectory,
|
|
@@ -290,13 +581,7 @@ async function getTypescriptAdapter({ filePath, sourceFilePath, sourceText }) {
|
|
|
290
581
|
throw new Error(`Unable to parse TypeScript source file: ${filePath}`);
|
|
291
582
|
}
|
|
292
583
|
|
|
293
|
-
return
|
|
294
|
-
...ast,
|
|
295
|
-
forEachChild(node, visitor) {
|
|
296
|
-
ast.visitEachChild(node, visitor, null);
|
|
297
|
-
},
|
|
298
|
-
sourceFile,
|
|
299
|
-
};
|
|
584
|
+
return sourceFile;
|
|
300
585
|
} finally {
|
|
301
586
|
api.close();
|
|
302
587
|
|