Repository navigation
Expand file tree
/
Copy pathcheck-error-policy.mjs
More file actions
199 lines (157 loc) · 4.63 KB
/
Copy pathcheck-error-policy.mjs
File metadata and controls
199 lines (157 loc) · 4.63 KB
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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import fs from "node:fs";
import path from "node:path";
import ts from "typescript";
const failures = [];
for (const file of sourceFiles("src")) checkFile(file);
if (failures.length) {
console.error("Exception boundary policy violations found.");
console.error(
"Forward caught values unchanged and exactly once; do not combine $exceptionHandler with another terminal failure path.",
);
for (const failure of failures) console.error(`- ${failure}`);
process.exitCode = 1;
} else {
console.log(
"Error policy check passed; exception boundaries have one unmodified owner.",
);
}
function* sourceFiles(directory) {
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
const file = path.join(directory, entry.name);
if (entry.isDirectory()) {
yield* sourceFiles(file);
continue;
}
if (
entry.isFile() &&
/\.(?:cts|mts|tsx?)$/.test(entry.name) &&
!entry.name.endsWith(".d.ts") &&
!entry.name.endsWith(".spec.ts") &&
!entry.name.endsWith(".test.ts")
) {
yield file;
}
}
}
function checkFile(file) {
const sourceText = fs.readFileSync(file, "utf8");
const sourceFile = ts.createSourceFile(
file,
sourceText,
ts.ScriptTarget.Latest,
true,
);
const catchHandlerCalls = new Map();
visit(sourceFile);
for (const [catchClause, calls] of catchHandlerCalls) {
if (calls.length > 1) {
report(
catchClause,
"a catch block forwards the same failure boundary more than once",
);
}
if (containsThrowOutsideNestedFunction(catchClause.block)) {
report(
catchClause,
"a catch block both invokes the exception handler and throws",
);
}
if (containsConsoleReportOutsideNestedFunction(catchClause.block)) {
report(
catchClause,
"a catch block both invokes the exception handler and reports to console",
);
}
}
function visit(node) {
if (ts.isCallExpression(node) && isExceptionHandlerCall(node.expression)) {
if (node.arguments.length !== 1) {
report(node, "$exceptionHandler must receive exactly one value");
}
const catchClause = findCatchClause(node);
if (catchClause) {
const calls = catchHandlerCalls.get(catchClause) ?? [];
calls.push(node);
catchHandlerCalls.set(catchClause, calls);
const catchVariable = catchClause.variableDeclaration?.name;
const argument = node.arguments[0];
if (
catchVariable &&
ts.isIdentifier(catchVariable) &&
argument &&
(!ts.isIdentifier(argument) || argument.text !== catchVariable.text)
) {
report(
node,
`forward the caught value '${catchVariable.text}' unchanged`,
);
}
}
}
ts.forEachChild(node, visit);
}
function report(node, message) {
const position = sourceFile.getLineAndCharacterOfPosition(
node.getStart(sourceFile),
);
failures.push(
`${file}:${String(position.line + 1)}:${String(position.character + 1)} ${message}`,
);
}
}
function isExceptionHandlerCall(expression) {
if (ts.isIdentifier(expression)) {
return isExceptionHandlerName(expression.text);
}
if (
ts.isPropertyAccessExpression(expression) ||
ts.isPropertyAccessChain(expression)
) {
return isExceptionHandlerName(expression.name.text);
}
return false;
}
function isExceptionHandlerName(name) {
return /^(?:\$|_)?exceptionHandler$/.test(name);
}
function findCatchClause(node) {
let current = node.parent;
while (current) {
if (ts.isCatchClause(current)) return current;
if (ts.isFunctionLike(current)) return undefined;
current = current.parent;
}
return undefined;
}
function containsThrowOutsideNestedFunction(node) {
let found = false;
visit(node);
return found;
function visit(current) {
if (current !== node && ts.isFunctionLike(current)) return;
if (ts.isThrowStatement(current)) {
found = true;
return;
}
ts.forEachChild(current, visit);
}
}
function containsConsoleReportOutsideNestedFunction(node) {
let found = false;
visit(node);
return found;
function visit(current) {
if (current !== node && ts.isFunctionLike(current)) return;
if (
ts.isCallExpression(current) &&
ts.isPropertyAccessExpression(current.expression) &&
ts.isIdentifier(current.expression.expression) &&
current.expression.expression.text === "console" &&
["error", "log", "warn"].includes(current.expression.name.text)
) {
found = true;
return;
}
ts.forEachChild(current, visit);
}
}