Skip to content

Commit 8520e54

Browse files
committed
fix(lint): close assertion ratchet bypasses
1 parent fe099bd commit 8520e54

4 files changed

Lines changed: 40 additions & 5 deletions

File tree

‎scripts/check-assertion-safety-ratchet.mts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ function collectSafetyCommentLines(sourceFile: ts.SourceFile, source: string) {
5252
sourceFile.languageVariant,
5353
source,
5454
);
55-
const lines = new Set<number>();
55+
const sameLine = new Set<number>();
56+
const standalone = new Set<number>();
5657
for (let token = scanner.scan(); token !== ts.SyntaxKind.EndOfFileToken; token = scanner.scan()) {
5758
if (token !== ts.SyntaxKind.SingleLineCommentTrivia) {
5859
continue;
@@ -61,9 +62,15 @@ function collectSafetyCommentLines(sourceFile: ts.SourceFile, source: string) {
6162
if (!/^\/\/\s*SAFETY:\s*\S/u.test(comment)) {
6263
continue;
6364
}
64-
lines.add(sourceFile.getLineAndCharacterOfPosition(scanner.getTokenPos()).line);
65+
const position = scanner.getTokenPos();
66+
const line = sourceFile.getLineAndCharacterOfPosition(position).line;
67+
sameLine.add(line);
68+
const lineStart = sourceFile.getPositionOfLineAndCharacter(line, 0);
69+
if (source.slice(lineStart, position).trim() === "") {
70+
standalone.add(line);
71+
}
6572
}
66-
return lines;
73+
return { sameLine, standalone };
6774
}
6875

6976
function assertionOperatorPosition(sourceFile: ts.SourceFile, node: AssertionNode) {
@@ -118,7 +125,10 @@ export function countUnsafeAssertions(source: string, filePath = "src/source.ts"
118125
const operatorLine = sourceFile.getLineAndCharacterOfPosition(
119126
assertionOperatorPosition(sourceFile, node),
120127
).line;
121-
if (!safetyCommentLines.has(operatorLine) && !safetyCommentLines.has(operatorLine - 1)) {
128+
if (
129+
!safetyCommentLines.sameLine.has(operatorLine) &&
130+
!safetyCommentLines.standalone.has(operatorLine - 1)
131+
) {
122132
count += 1;
123133
}
124134
}

‎scripts/check-changed.mts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,20 @@ export function createChangedCheckPlan(
603603
options.staged ? "HEAD" : (options.base ?? "origin/main"),
604604
]);
605605
}
606+
if (
607+
result.paths.some((filePath) =>
608+
/^(?:src\/|ui\/src\/|packages\/|extensions\/|config\/assertion-safety-baseline\.txt$|scripts\/check-assertion-safety-ratchet\.mts$|scripts\/lib\/type-assertion-guard-scope\.mjs$|scripts\/oxlint-boundary-guards\.mjs$)/u.test(
609+
filePath,
610+
),
611+
)
612+
) {
613+
add("assertion SAFETY comment ratchet", [
614+
"check:assertion-safety",
615+
...(options.staged ? ["--staged"] : []),
616+
"--base",
617+
options.staged ? "HEAD" : (options.base ?? "origin/main"),
618+
]);
619+
}
606620
add("changelog attributions", ["check:changelog-attributions"]);
607621
add("doctor deprecation registry", ["check:doctor-deprecation-registry"]);
608622
add("guarded extension wildcard re-exports", ["lint:extensions:no-guarded-wildcard-reexports"]);

‎test/scripts/changed-lanes.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,7 @@ describe("scripts/changed-lanes", () => {
14531453
"conflict markers",
14541454
"environment variable count ratchet",
14551455
"max-lines suppression ratchet",
1456+
"assertion SAFETY comment ratchet",
14561457
"changelog attributions",
14571458
"doctor deprecation registry",
14581459
"guarded extension wildcard re-exports",
@@ -2317,6 +2318,15 @@ describe("scripts/changed-lanes", () => {
23172318
staged: ["check:env-var-count", "--staged", "--base", "HEAD"],
23182319
},
23192320
},
2321+
{
2322+
name: "adds the assertion SAFETY comment ratchet for production source",
2323+
commandName: "assertion SAFETY comment ratchet",
2324+
worktreeOptions: { base: "main" },
2325+
expected: {
2326+
worktree: ["check:assertion-safety", "--base", "main"],
2327+
staged: ["check:assertion-safety", "--staged", "--base", "HEAD"],
2328+
},
2329+
},
23202330
])("$name", ({ commandName, worktreeOptions, expected }) => {
23212331
const result = detectChangedLanes(["src/runtime.ts"]);
23222332
const worktreePlan = createChangedCheckPlan(result, worktreeOptions);

‎test/scripts/check-assertion-safety-ratchet.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ describe("check-assertion-safety-ratchet", () => {
5555
"// SAFETY: the schema parser established Shape.",
5656
"const safe = value as Shape;",
5757
"const inlineSafe = value as Shape; // SAFETY: the schema parser established Shape.",
58+
"const afterInline = value as Shape;",
5859
"const present = value!;",
5960
'const note = "// SAFETY: string content is not a comment.";',
6061
"const unsafe = value as Shape;",
6162
"const angle = <Shape>value;",
6263
].join("\n");
6364

64-
expect(countUnsafeAssertions(source, "src/example.ts")).toBe(2);
65+
expect(countUnsafeAssertions(source, "src/example.ts")).toBe(3);
6566
expect(
6667
countUnsafeAssertions("value as unknown as Shape;", "src/agents/agent-model-discovery.ts"),
6768
).toBe(1);

0 commit comments

Comments
 (0)