Skip to content

Commit fe5fa80

Browse files
authored
feat(lint): enforce no-chained-type-assertions (#124119)
1 parent 4a6f99c commit fe5fa80

7 files changed

Lines changed: 206 additions & 3 deletions

File tree

‎config/oxlint/boundary-guards.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"rules": {
77
"openclaw-boundaries/no-raw-window-open-call": "error",
88
"openclaw-boundaries/no-register-http-handler-call": "error",
9-
"openclaw-boundaries/no-widen-then-assert": "error"
9+
"openclaw-boundaries/no-widen-then-assert": "error",
10+
"openclaw-boundaries/no-chained-type-assertions": "error"
1011
}
1112
}

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,7 @@
16571657
"lint:plugins:no-monolithic-plugin-sdk-entry-imports": "node --import tsx scripts/check-no-monolithic-plugin-sdk-entry-imports.ts",
16581658
"lint:plugins:no-register-http-handler": "node scripts/run-oxlint.mjs --openclaw-focused-config --config config/oxlint/boundary-guards.json src extensions",
16591659
"lint:plugins:plugin-sdk-subpaths-exported": "node --import tsx scripts/check-plugin-sdk-subpath-exports.mts",
1660+
"lint:no-chained-type-assertions": "node scripts/run-oxlint.mjs --openclaw-focused-config --config config/oxlint/boundary-guards.json src extensions packages ui/src",
16601661
"lint:no-widen-then-assert": "node scripts/run-oxlint.mjs --openclaw-focused-config --config config/oxlint/boundary-guards.json src extensions packages ui/src",
16611662
"lint:scripts": "pnpm lint:docker-e2e && pnpm lint:tmp:no-raw-http2-imports && node scripts/run-oxlint.mjs --tsconfig config/tsconfig/oxlint.scripts.json scripts",
16621663
"lint:swift": "./scripts/lint-swift.sh",

‎scripts/oxlint-boundary-guards.mjs‎

Lines changed: 181 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,49 @@
11
const EXPRESSION_WRAPPER_RE =
22
/^(?:ChainExpression|ParenthesizedExpression|TSAsExpression|TSNonNullExpression|TSTypeAssertion)$/;
3-
const TEST_FILE_SUFFIXES = [".test.ts", ".test-utils.ts", ".test-harness.ts", ".e2e-harness.ts"];
3+
const BOUNDARY_GUARD_FIXTURE_ROOT = "test/fixtures/oxlint-boundary-guards";
4+
// Shared test-path policy for guards that intentionally exclude fixture, mock, and harness code.
5+
const TEST_FILE_SUFFIXES = [
6+
".test.ts",
7+
".test.tsx",
8+
".spec.ts",
9+
".spec.tsx",
10+
".test-utils.ts",
11+
".test-utils.tsx",
12+
".test-harness.ts",
13+
".test-harness.tsx",
14+
".e2e-harness.ts",
15+
".e2e-harness.tsx",
16+
];
17+
const TEST_PATH_MARKERS = [
18+
"/test/",
19+
"/tests/",
20+
"__tests__",
21+
"/e2e/",
22+
"test-helpers",
23+
"test-support",
24+
"test-fixtures",
25+
"test-mocks",
26+
"test-utils",
27+
"mock-http",
28+
"-harness.",
29+
".test-utils.",
30+
"/mocks/",
31+
];
32+
33+
function pathMatchesRoot(repoPath, root) {
34+
return repoPath === root || repoPath.startsWith(`${root}/`);
35+
}
36+
37+
function isSkippedTestPath(repoPath) {
38+
if (pathMatchesRoot(repoPath, BOUNDARY_GUARD_FIXTURE_ROOT)) {
39+
return false;
40+
}
41+
const slashPrefixedPath = `/${repoPath}`;
42+
return (
43+
TEST_FILE_SUFFIXES.some((suffix) => repoPath.endsWith(suffix)) ||
44+
TEST_PATH_MARKERS.some((marker) => slashPrefixedPath.includes(marker))
45+
);
46+
}
447

548
function unwrapExpression(node) {
649
let current = node;
@@ -18,7 +61,7 @@ function restrictedCallRule({ allowedFiles = [], message, objects, property, roo
1861
const repoPath = filename.startsWith(`${cwd}/`) ? filename.slice(cwd.length + 1) : filename;
1962
if (
2063
!filename.endsWith(".ts") ||
21-
!roots.some((root) => repoPath === root || repoPath.startsWith(`${root}/`)) ||
64+
!roots.some((root) => pathMatchesRoot(repoPath, root)) ||
2265
TEST_FILE_SUFFIXES.some((suffix) => filename.endsWith(suffix)) ||
2366
allowedFiles.includes(repoPath)
2467
) {
@@ -46,6 +89,86 @@ function restrictedCallRule({ allowedFiles = [], message, objects, property, roo
4689
};
4790
}
4891

92+
// Adapted from dmmulroy/anti-slop@446268e5d15baa968eaec669ff65358d36ae6259, MIT.
93+
function isTypeAssertionExpression(node) {
94+
return node.type === "TSAsExpression" || node.type === "TSTypeAssertion";
95+
}
96+
97+
function isConstAssertion(node) {
98+
const { typeAnnotation } = node;
99+
return (
100+
typeAnnotation.type === "TSTypeReference" &&
101+
typeAnnotation.typeName.type === "Identifier" &&
102+
typeAnnotation.typeName.name === "const"
103+
);
104+
}
105+
106+
function isOutermostAssertionInChain(node) {
107+
let current = node;
108+
let parent = node.parent;
109+
110+
while (parent.type === "ParenthesizedExpression" && parent.expression === current) {
111+
current = parent;
112+
parent = parent.parent;
113+
}
114+
115+
return !isTypeAssertionExpression(parent) || parent.expression !== current;
116+
}
117+
118+
function isForbiddenAssertionChain(node) {
119+
let assertionCount = 0;
120+
let hasNonConstAssertion = false;
121+
let current = node;
122+
123+
while (isTypeAssertionExpression(current)) {
124+
assertionCount += 1;
125+
hasNonConstAssertion ||= !isConstAssertion(current);
126+
current = unwrapExpressionParentheses(current.expression);
127+
}
128+
129+
return assertionCount > 1 && hasNonConstAssertion;
130+
}
131+
132+
function noChainedTypeAssertionsRule({ excludedRoots = [], roots }) {
133+
return {
134+
meta: {
135+
type: "problem",
136+
docs: {
137+
description:
138+
"Disallow chained TypeScript as and angle-bracket assertions, including parenthesized chains.",
139+
},
140+
messages: {
141+
chained:
142+
"This assertion chain discards type evidence. Keep the original precise type, or parse untrusted input at its boundary before narrowing it.",
143+
},
144+
},
145+
create(context) {
146+
const filename = context.physicalFilename.replaceAll("\\", "/");
147+
const cwd = context.cwd.replaceAll("\\", "/");
148+
const repoPath = filename.startsWith(`${cwd}/`) ? filename.slice(cwd.length + 1) : filename;
149+
if (
150+
!roots.some((root) => pathMatchesRoot(repoPath, root)) ||
151+
excludedRoots.some((root) => pathMatchesRoot(repoPath, root)) ||
152+
isSkippedTestPath(repoPath)
153+
) {
154+
return {};
155+
}
156+
157+
const checkTypeAssertion = (node) => {
158+
if (!isOutermostAssertionInChain(node) || !isForbiddenAssertionChain(node)) {
159+
return;
160+
}
161+
context.report({ node, messageId: "chained" });
162+
};
163+
164+
return {
165+
TSAsExpression: checkTypeAssertion,
166+
TSTypeAssertion: checkTypeAssertion,
167+
};
168+
},
169+
};
170+
}
171+
49172
// Adapted from dmmulroy/anti-slop, MIT.
50173
const FUNCTION_BOUNDARY_TYPES = new Set([
51174
"ArrowFunctionExpression",
@@ -504,5 +627,61 @@ export default {
504627
"no-widen-then-assert": noWidenThenAssertRule({
505628
roots: ["src", "extensions", "packages", "ui/src", "test/fixtures/oxlint-boundary-guards"],
506629
}),
630+
"no-chained-type-assertions": noChainedTypeAssertionsRule({
631+
roots: ["src", "extensions", "packages", "ui/src", BOUNDARY_GUARD_FIXTURE_ROOT],
632+
// Burn-down ledger — shrink only; see PR #124060/#124073/#124079/#124082.
633+
excludedRoots: [
634+
"extensions/amazon-bedrock-mantle",
635+
"extensions/anthropic-vertex",
636+
"extensions/browser",
637+
"extensions/codex",
638+
"extensions/copilot",
639+
"extensions/deepinfra",
640+
"extensions/diagnostics-otel",
641+
"extensions/diagnostics-prometheus",
642+
"extensions/discord",
643+
"extensions/github-copilot",
644+
"extensions/google",
645+
"extensions/googlechat",
646+
"extensions/imessage",
647+
"extensions/line",
648+
"extensions/llm-task",
649+
"extensions/longcat",
650+
"extensions/matrix",
651+
"extensions/microsoft-foundry",
652+
"extensions/msteams",
653+
"extensions/qa-lab",
654+
"extensions/reef",
655+
"extensions/signal",
656+
"extensions/slack",
657+
"extensions/sms",
658+
"extensions/synology-chat",
659+
"extensions/telegram",
660+
"extensions/tlon",
661+
"extensions/voice-call",
662+
"extensions/whatsapp",
663+
"extensions/workboard",
664+
"extensions/zalo",
665+
"extensions/zalouser",
666+
"packages/ai",
667+
"src/runtime.ts",
668+
"src/acp",
669+
"src/agents",
670+
"src/channels",
671+
"src/commands",
672+
"src/config",
673+
"src/gateway",
674+
"src/infra",
675+
"src/media",
676+
"src/meeting-bot",
677+
"src/plugin-sdk",
678+
"src/plugins",
679+
"src/process",
680+
"src/proxy-capture",
681+
"src/shared",
682+
"src/trajectory",
683+
"ui/src",
684+
],
685+
}),
507686
},
508687
};

‎scripts/run-additional-boundary-checks.mts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const BOUNDARY_CHECKS = (
6262
["lint:tmp:no-raw-channel-fetch", "pnpm", ["run", "lint:tmp:no-raw-channel-fetch"]],
6363
["lint:tmp:no-raw-http2-imports", "pnpm", ["run", "lint:tmp:no-raw-http2-imports"]],
6464
["lint:agent:ingress-owner", "pnpm", ["run", "lint:agent:ingress-owner"]],
65+
["lint:no-chained-type-assertions", "pnpm", ["run", "lint:no-chained-type-assertions"]],
6566
["lint:no-widen-then-assert", "pnpm", ["run", "lint:no-widen-then-assert"]],
6667
[
6768
"lint:plugins:no-register-http-handler",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
declare const input: unknown;
2+
3+
input as unknown as { readonly id: string };
4+
input as object as Record<string, unknown> as { readonly id: string };
5+
<{ readonly id: string }>(input as object);
6+
7+
input as { readonly id: string };
8+
({ id: "fixture" }) as const as const;

‎test/scripts/oxlint-boundary-guards.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ const cases = [
1818
violation: `${FIXTURES}/widen-then-assert-violation.test.ts`,
1919
violations: 3,
2020
},
21+
{
22+
rule: "openclaw-boundaries/no-chained-type-assertions",
23+
violation: `${FIXTURES}/chained-type-assertions-violation.ts`,
24+
violations: 3,
25+
},
2126
];
2227

2328
function runGuard(target: string) {

‎test/scripts/run-additional-boundary-checks.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,14 @@ describe("run-additional-boundary-checks", () => {
254254
});
255255
});
256256

257+
it("keeps chained-type-assertions lint in CI boundary checks", () => {
258+
expect(BOUNDARY_CHECKS).toContainEqual({
259+
label: "lint:no-chained-type-assertions",
260+
command: "pnpm",
261+
args: ["run", "lint:no-chained-type-assertions"],
262+
});
263+
});
264+
257265
it("keeps the Telegram grammY type import guard in source boundary checks", () => {
258266
expect(BOUNDARY_CHECKS).toContainEqual({
259267
label: "lint:extensions:telegram-grammy-types",

0 commit comments

Comments
 (0)