Skip to content

Add code quality lint for type assertions - #19462

Merged
AmethystLiang merged 2 commits into
mainfrom
add-lint-for-casting
Sep 13, 2026
Merged

AmethystLiang merged 2 commits into
mainfrom
add-lint-for-casting

Conversation

@AmethystLiang

@AmethystLiang AmethystLiang commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​153 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​153
Prod 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​86 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​86

Problem

The codebase needs automated enforcement of safe type assertion patterns. Without it, developers can accidentally use unsafe casts that hide type issues.

Solution

Added a dedicated oxlint scan that enforces the typescript/consistent-type-assertions rule set to never style. This prevents unsafe casts while allowing documented exceptions with SAFETY: comments and as const patterns. Includes:

  • New oxlint config file (config/oxlint-code-quality-casting.json) targeting just the casting rule
  • Documentation in AGENTS.md explaining the pattern and exception syntax
  • Test coverage verifying the lint catches problematic casts and respects disable comments

The scan integrates with the existing changed-code-quality check and only flags new or modified lines.

Testing

  • Added test in config/scripts/casting-code-quality.test.mjs that verifies:
    • Unsafe casts (as string | null, as unknown as string, <string>) are caught
    • Safe patterns (as const, type annotations, satisfies) pass
    • Documented exceptions with SAFETY: comments are respected
    • Full lint still passes with no false positives

Checklist

  • This PR is small and focused
  • I explained what changed and why (including ELI5)
  • N/A for screenshots (code quality tooling, no UI changes)
  • Self-reviewed for correctness and security
  • Cross-platform and SSH impact considered (applies to all lint runs universally)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (CI will cover)

Enforce type assertion style by adding a new oxlint scan with `typescript/consistent-type-assertions` rule. Requires using `as const`, type annotations, or `satisfies` instead of raw type casts, with documented `SAFETY:` exceptions for unavoidable cases.
@coderabbitai

coderabbitai Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

📝 Walkthrough

Walkthrough

The change adds guidance to avoid TypeScript type assertions except as const. It adds an Oxlint configuration that reports non-const assertions as errors. It registers the configuration as a changed-code quality scan. It adds checks for unused casting suppressions and missing SAFETY: rationales on added lines. Tests cover suppression comments, changed-line filtering, and trailing directives.

Priority: ⬇️ Low

Merge Risk: 🟡 Moderate · up to 6c11d

The new casting-policy gate can accept undocumented exceptions and can impose casting requirements on unrelated suppressions. Fix these parsing gaps before relying on the check to enforce the documented policy.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the problem, solution, testing, and scope, but it omits several required template sections. It does not include ELI5, What Changed, Why, a Linked Issue, Visual Proof with the … Add the missing required sections and provide their information. Include a linked issue after "Fixes #". Use the required Visual Proof text: "N/A" with a brief reason. Complete the remaining checklist items or state that CI will cover them.
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the primary change: adding code-quality linting for type assertions.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description explains the problem, solution, testing, and scope, but it omits several required template sections. It does not include ELI5, What Changed, Why, a Linked Issue, Visual Proof with the required N/A wording, Review, Agent skill upstream boundary, or Notes.

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: cd291006-f0e5-422e-bfcc-677085150287

📥 Commits

Reviewing files that changed from the base of the PR and between a278d84 and a00365d.

📒 Files selected for processing (4)
  • AGENTS.md
  • config/oxlint-code-quality-casting.json
  • config/scripts/casting-code-quality.test.mjs
  • config/scripts/check-changed-code-quality.mjs

Included review availability: Your plan provides up to 10 included reviews per hour; 6 remain after this review.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

The documented SAFETY: escape hatch for unavoidable casts itself fails the changed-code-quality gate, so any PR that legitimately needs a cast would be blocked from merging. Details inline.

Reviewed changes

  • New casting code quality oxlint scan — adds config/oxlint-code-quality-casting.json (typescript/consistent-type-assertions with assertionStyle: "never") and registers it as a fourth scan in check-changed-code-quality.mjs, gated to added lines like the existing scans.
  • AGENTS.md guidance — documents "avoid type assertions except as const" and the // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: ... exception syntax.
  • Test coverage — casting-code-quality.test.mjs verifies the scan flags unsafe casts (as string | null, as unknown as string, <string>), allows as const / satisfies / annotations, and respects the SAFETY: disable directive.

Note: 1 inline comment(s) dropped because they did not anchor to lines inside the PR diff:

  • config/scripts/casting-code-quality.test.mjs:92 (RIGHT) — line 92 (RIGHT) is not inside a diff hunk

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: c220cc78-7465-4405-9b8f-afa81e1e2f66

📥 Commits

Reviewing files that changed from the base of the PR and between a00365d and 6c11da5.

📒 Files selected for processing (2)
  • config/scripts/casting-code-quality.test.mjs
  • config/scripts/check-changed-code-quality.mjs

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — one minor suggestion inline.

Reviewed changes

Re-reviewed the fix commit 6c11da5 against the prior pullfrog review. The prior blocker (the documented SAFETY: escape hatch failing the gate) is resolved and I verified it end-to-end; one minor observation remains.

  • Exempted casting directives from unused-directive warnings — isCastingDirectiveUnusedWarning filters the code quality scan's unused-directive warning for directives naming typescript/consistent-type-assertions, so a SAFETY: suppression no longer fails the gate.
  • Enforced the SAFETY: rationale — findCastingDirectivesMissingSafety flags added/changed lines carrying a casting suppression without a SAFETY: explanation, reported and counted by main().
  • Added tests — cover the unused-directive exemption, missing-rationale rejection, gating to added lines, and a trailing oxlint-disable-line abutting a string literal.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@AmethystLiang
AmethystLiang merged commit 182cd4c into main Sep 13, 2026
28 checks passed
nwparker added a commit that referenced this pull request Sep 13, 2026
The fabricated-Dirent readdir mock could not satisfy both gates at once:
vi.mocked(readdirSync) resolves to Node's Dirent<NonSharedBuffer> overload, so
the mock needed a type assertion, and #19462's casting gate rejects new ones on
changed lines. Removing the cast then failed tsc.

Extract directoryEntryNeedsStat and test it directly with a structural probe.
No mock, no cast, no top-level await, and the DT_UNKNOWN case is pinned:
removing the fallback fails 'stats an entry whose type readdir could not report'.
nwparker added a commit that referenced this pull request Sep 13, 2026
* perf(tooling): reuse directory entry types in source scans

* fix(source-scan): stat DT_UNKNOWN dirents so untyped directories are still walked

`readdirSync(..., { withFileTypes: true })` can hand back a Dirent whose
type the filesystem did not report. For that entry every predicate is
false, so the readdir-type fast path treated a real directory as a file
and silently dropped its subtree from every ratchet guard. Fall back to
`statSync` whenever the entry is neither conclusively a file nor a
directory, keeping the no-stat fast path for ordinary entries.

Also make the two readdir-order assertions in the walk test
order-independent; `scanSourceTree` returns raw readdir order, which
differs on tmpfs.

* test(source-scan): unit-test the stat fallback via an extracted helper

The fabricated-Dirent readdir mock could not satisfy both gates at once:
vi.mocked(readdirSync) resolves to Node's Dirent<NonSharedBuffer> overload, so
the mock needed a type assertion, and #19462's casting gate rejects new ones on
changed lines. Removing the cast then failed tsc.

Extract directoryEntryNeedsStat and test it directly with a structural probe.
No mock, no cast, no top-level await, and the DT_UNKNOWN case is pinned:
removing the fallback fails 'stats an entry whose type readdir could not report'.

---------

Co-authored-by: m4air <m4air@Mac.localdomain>
Co-authored-by: Neil <neil@stably.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant