fix(linter): noUselessUndefinedInitialization false positive in svelte 4.#8776
Conversation
…rted variables
Skip flagging variables initialized to undefined when they are exported,
either directly (export let x = undefined) or through a named export
(let x = undefined; export { x }).
In frameworks like Svelte, exported variables with undefined initialization
are used to declare optional props. The false positive was occurring when
a variable was initialized with undefined and later exported with renaming
(e.g., export { className as class }).
Fixes biomejs#6003
🦋 Changeset detectedLatest commit: 9dd6a3a 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 changeset entry and updates the noUselessUndefinedInitialization lint to use semantic analysis (Semantic) and the project's semantic model to detect exported bindings. The rule now skips variables initialized to Suggested reviewers
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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 |
dyc3
left a comment
There was a problem hiding this comment.
Just a nitpick: "Svelte" -> "Svelte 4", because Svelte 5 declares props differently (even though the non-runes version is still supported)
crates/biome_js_analyze/src/lint/complexity/no_useless_undefined_initialization.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/tests/specs/complexity/noUselessUndefinedInitialization/valid.js.snap
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/lint/complexity/no_useless_undefined_initialization.rs
Outdated
Show resolved
Hide resolved
CodSpeed Performance ReportMerging this PR will not alter performanceComparing Summary
Footnotes
|
|
Lol, the output emitted in the PR description is bigger than the code itself |
|
@codiini You'll need to update the snapshots |
|
@dyc3 All done. |
ematipico
left a comment
There was a problem hiding this comment.
I think it's safe to land it as a patch, since we're adding a check to an edge case
I used Claude Code to think through the issue and validate my solution with tests.
Summary
Fixes #6003
The
noUselessUndefinedInitializationrule was producing false positives for exported variables. In Svelte 4, exported variables withundefinedinitialization are used to declare optional component props:
js let className = undefined; export { className as class }; The rule incorrectly flagged this pattern because it only looked at the variable declaration in isolation, without considering that it would be exported later.
Solution
Changed the rule to use Biome's semantic model to check if a variable is exported before flagging it. This handles all export patterns:
export let x = undefined;let x = undefined; export { x };let x = undefined; export { x as y };let x = undefined; export default x;Test Plan
noUselessUndefinedInitialization/valid.jscargo test -p biome_js_analyze --test spec_tests -- specs::complexity::no_useless_undefined_initialization- all tests passundefinedis incorrectly removedundefinedis preserved for exported variablesDocs
Documentation is included in the rule's rustdoc comments with examples showing that exported variables are not flagged.