feat(lint): add nursery rule noUnsafeAssignment#9046
feat(lint): add nursery rule noUnsafeAssignment#9046sargunv wants to merge 3 commits intobiomejs:mainfrom
noUnsafeAssignment#9046Conversation
Co-authored-by: Sargun Vohra <sargunv@users.noreply.github.com> https://claude.ai/code/session_01RLJN4W4J3HJ67Acq9eQKCh
🦋 Changeset detectedLatest commit: 37979dc The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
WalkthroughAdds a new TypeScript nursery lint rule Suggested reviewers
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In @.changeset/add-no-unsafe-assignment-rule.md:
- Around line 1-3: The changeset currently declares the release type as "minor"
for the "@biomejs/biome" entry; change that declaration to "patch" so the
nursery rule targets the main branch instead of being treated as a feature for
next. Locate the changeset header containing the string '"@biomejs/biome":
minor' and update it to '"@biomejs/biome": patch', keeping the rest of the
changeset content intact.
🧹 Nitpick comments (2)
crates/biome_js_analyze/src/lint/nursery/no_unsafe_assignment.rs (1)
105-122: Consider narrowing the diagnostic range to just the expression.Currently
initializer.range()covers= getPayload()including the=token. Usingexpression.range()instead would point directly at theany-typed value. This is a minor UX polish — entirely optional.Proposed diff
fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { let declarator = ctx.query(); let initializer = declarator.initializer()?; + let expression = initializer.expression().ok()?; Some( RuleDiagnostic::new( rule_category!(), - initializer.range(), + expression.range(), markup! { "The assigned expression has the type "<Emphasis>"any"</Emphasis>", which undermines type safety." },crates/biome_js_analyze/tests/specs/nursery/noUnsafeAssignment/valid.ts (1)
23-24: Nitpick:readFileSyncdoesn't exist onnode:fs/promises.The test still works (the type is unresolved either way), but using
fs.readFile(...)or importing from"node:fs"would be more accurate for anyone reading this as an example.
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
- Use expression.range() instead of initializer.range() so the diagnostic points at the any-typed value, not the `=` token. - Change `node:fs/promises` to `node:fs` in valid.ts since readFileSync is a sync API from the `fs` module. https://claude.ai/code/session_01RLJN4W4J3HJ67Acq9eQKCh
ematipico
left a comment
There was a problem hiding this comment.
Please direct this rule against the next branch. We're about to ship a dedicated domain for type-aware lint rules. This would avoid unpleasant conflicts.
The name of the domain is Types
AI disclosure: I used Opus 4.6 with Claude Code to produce this PR. I've made the Claude session public, if you'd like to inspect it. The PR description was written by me. Any discussion on the PR will be replied to by me, not AI.
Summary
This PR adds a new nursery rule
noUnsafeAssignmentthat detects when a value typed asanyis assigned to a variable, preventinganyfrom silently spreading through the codebase.The rule is inspired by typescript-eslint's
no-unsafe-assignmentrule. It flags cases where:anyis assigned to an unannotated variableanyis assigned to a variable with a non-any/unknownannotationThe rule allows assignments when the variable is explicitly annotated as
anyorunknown, since the developer has intentionally opted into those types.Relevant rule request: #7587
If this is merged, I'd be happy to work on porting the other
no-unsafe-*too.Test Plan
The implementation includes test cases:
any-typed values to unannotated variables and variables with non-anyannotationsany, explicitany/unknownannotations, variables without initializers, and unresolved types