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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { checkSource, unusedExemptions, type Exemption } from "../../scripts/check-boundaries";
const noExemptions: ReadonlyArray<Exemption> = [];
describe("boundary check", () => {
test("flags as any", () => {
const v = checkSource([{ path: "a.ts", text: "const x = y as any;" }], noExemptions);
assert.equal(v.length, 1);
assert.equal(v[0]?.rule, "as-any");
});
test("flags as any[]", () => {
const v = checkSource([{ path: "a.ts", text: "const x = y as any[];" }], noExemptions);
assert.equal(v.length, 1);
assert.equal(v[0]?.rule, "as-any");
});
test("flags a ts-ignore", () => {
const v = checkSource([{ path: "a.ts", text: "// @ts-ignore\nconst x = 1;" }], noExemptions);
assert.equal(v.length, 1);
assert.equal(v[0]?.rule, "ts-ignore");
});
test("flags ts-expect-error and ts-nocheck", () => {
const expectError = checkSource([{ path: "a.ts", text: "// @ts-expect-error" }], noExemptions);
const noCheck = checkSource([{ path: "a.ts", text: "// @ts-nocheck" }], noExemptions);
assert.equal(expectError.length, 1);
assert.equal(noCheck.length, 1);
});
test("flags a non-null assertion before a property access", () => {
const v = checkSource([{ path: "a.ts", text: "const w = data.model!.width;" }], noExemptions);
assert.equal(v.length, 1);
assert.equal(v[0]?.rule, "non-null-assertion");
});
test("flags a non-null assertion with no property access after it", () => {
const forms = ["const a = b!;", "f(g!);", "const c = arr[0]!;", "const d = [x!, y];", "const e = obj\n .a!\n .b;"];
for (const text of forms) {
const v = checkSource([{ path: "a.ts", text }], noExemptions);
assert.equal(v.length, 1, text);
assert.equal(v[0]?.rule, "non-null-assertion", text);
}
});
test("does not flag strict inequality, spaced or unspaced", () => {
for (const text of ["if (a !== b.c) return;", "if (a!==b.c) return;", "if (a != b.c) return;", "if (a!=b.c) return;"]) {
assert.equal(checkSource([{ path: "a.ts", text }], noExemptions).length, 0, text);
}
});
test("does not flag boolean negation", () => {
for (const text of ["if (!ok.value) return;", "const n = a && !b.c;", "return !this.done;", "const m = [!x.y];"]) {
assert.equal(checkSource([{ path: "a.ts", text }], noExemptions).length, 0, text);
}
});
test("does not flag an exclamation mark in prose", () => {
const text = 'description: "Done! Use browser_snapshot as the default. Options: any of these."';
assert.equal(checkSource([{ path: "a.ts", text }], noExemptions).length, 0);
});
test("flags an inline object cast", () => {
const v = checkSource([{ path: "a.ts", text: "const d = r.data as { sessionId: string };" }], noExemptions);
assert.equal(v.length, 1);
assert.equal(v[0]?.rule, "inline-object-cast");
});
test("flags a double cast", () => {
const v = checkSource([{ path: "a.ts", text: "const d = r as unknown as Session;" }], noExemptions);
assert.equal(v.some((x) => x.rule === "double-cast"), true);
});
test("allows as const", () => {
assert.equal(checkSource([{ path: "a.ts", text: "const x = [1] as const;" }], noExemptions).length, 0);
});
test("flags a cast to a named type", () => {
const v = checkSource([{ path: "a.ts", text: "return ok(raw as ResultOf<M>);" }], noExemptions);
assert.equal(v.some((x) => x.rule === "named-cast"), true);
});
test("flags a cast to a function type", () => {
const v = checkSource([{ path: "a.ts", text: "(fn as (a: unknown) => void)(x);" }], noExemptions);
assert.equal(v.some((x) => x.rule === "function-cast"), true);
});
test("flags a cast whose target type sits on the next line", () => {
const v = checkSource([{ path: "a.ts", text: "const f = base.constructor as\n new () => void;" }], noExemptions);
assert.equal(v.some((x) => x.rule === "trailing-cast"), true);
});
test("does not flag the word as inside prose", () => {
const text = 'const d = "Capture the page as a JPEG. DO NOT use as a default tool.";';
assert.equal(checkSource([{ path: "a.ts", text }], noExemptions).length, 0);
});
test("flags a tool reaching the session directly", () => {
const forms = [
"const r = await client.session().call(\"DOM.resolveNode\", { backendNodeId: id });",
"const r = await client.session().callBrowser(\"Target.getTargets\");",
"const r = await client.session().callOnTarget(\"Page.enable\", {}, sid);",
"const r = await session.call(\"Accessibility.getFullAXTree\", {});",
"const r = await session.callBrowser(\"Browser.getWindowForTarget\", { targetId });",
];
for (const text of forms) {
const v = checkSource([{ path: "src/domains/thing.ts", text }], noExemptions);
assert.equal(v.some((x) => x.rule === "raw-cdp-call"), true, text);
}
});
test("flags a tool calling evaluateJs directly", () => {
const v = checkSource(
[{ path: "src/domains/thing.ts", text: "const r = await client.evaluateJs(expr);" }],
noExemptions,
);
assert.equal(v.some((x) => x.rule === "raw-evaluate"), true);
});
test("does not flag the session layer, which owns the session", () => {
const text = 'const r = await session.callBrowser("Target.getTargets");\nawait client.evaluateJs(expr);';
for (const path of ["src/client.ts", "src/cdp/session.ts", "src/cdp/target-factory.ts"]) {
assert.equal(checkSource([{ path, text }], noExemptions).length, 0, path);
}
});
test("does not mistake Function.prototype.call in an evaluated snippet for a CDP call", () => {
const text = "const expr = `${prelude} ${opts.fnBody} }).call(el, __arg); })()`;";
assert.equal(checkSource([{ path: "src/domains/form.ts", text }], noExemptions).length, 0);
});
test("reports the 1-indexed line number and the trimmed text", () => {
const v = checkSource([{ path: "a.ts", text: "const a = 1;\n const b = c as any;" }], noExemptions);
assert.equal(v[0]?.line, 2);
assert.equal(v[0]?.text, "const b = c as any;");
assert.equal(v[0]?.path, "a.ts");
});
test("scans every file it is given", () => {
const v = checkSource(
[
{ path: "a.ts", text: "const x = y as any;" },
{ path: "b.ts", text: "const z = w as any;" },
],
noExemptions,
);
assert.deepEqual(
v.map((x) => x.path),
["a.ts", "b.ts"],
);
});
});
describe("boundary check exemptions", () => {
const exemptions: ReadonlyArray<Exemption> = [
{ path: "a.ts", rule: "inline-object-cast", count: 1, reason: "narrows an untyped dynamic import" },
];
test("permits up to count violations for the exempted path and rule", () => {
const v = checkSource([{ path: "a.ts", text: "const m = mod as { default?: unknown };" }], exemptions);
assert.equal(v.length, 0);
});
test("reports the count+1-th violation so new casts cannot hide behind an exemption", () => {
const text = "const m = mod as { default?: unknown };\nconst n = other as { x?: unknown };";
const v = checkSource([{ path: "a.ts", text }], exemptions);
assert.equal(v.length, 1);
assert.equal(v[0]?.line, 2);
assert.equal(v[0]?.rule, "inline-object-cast");
});
test("does not exempt a different rule in the same file", () => {
const v = checkSource([{ path: "a.ts", text: "const x = y as any;" }], exemptions);
assert.equal(v.length, 1);
assert.equal(v[0]?.rule, "as-any");
});
test("does not exempt the same rule in a different file", () => {
const v = checkSource([{ path: "b.ts", text: "const m = mod as { default?: unknown };" }], exemptions);
assert.equal(v.length, 1);
assert.equal(v[0]?.path, "b.ts");
});
test("counts exempted violations per file, not across all files", () => {
const wide: ReadonlyArray<Exemption> = [
{ path: "a.ts", rule: "as-any", count: 1, reason: "one" },
{ path: "b.ts", rule: "as-any", count: 1, reason: "two" },
];
const v = checkSource(
[
{ path: "a.ts", text: "const x = y as any;" },
{ path: "b.ts", text: "const z = w as any;" },
],
wide,
);
assert.equal(v.length, 0);
});
test("normalises windows path separators before matching", () => {
const v = checkSource([{ path: "src\\util\\a.ts", text: "const x = y as any;" }], [
{ path: "src/util/a.ts", rule: "as-any", count: 1, reason: "platform independent" },
]);
assert.equal(v.length, 0);
});
test("unusedExemptions names exemptions no longer earning their keep", () => {
const stale = unusedExemptions([{ path: "a.ts", text: "const x = 1;" }], exemptions);
assert.deepEqual(
stale.map((e) => e.rule),
["inline-object-cast"],
);
});
test("unusedExemptions is empty when every exemption is consumed", () => {
const used = unusedExemptions([{ path: "a.ts", text: "const m = mod as { default?: unknown };" }], exemptions);
assert.deepEqual(used, []);
});
});
|