-
-
Notifications
You must be signed in to change notification settings - Fork 794
fix(noInvalidUseBeforeDeclaration): handle class, enum, import-equals #8113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(noInvalidUseBeforeDeclaration): handle class, enum, import-equals #8113
Conversation
🦋 Changeset detectedLatest commit: b43edcc 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 |
WalkthroughThis PR extends the noInvalidUseBeforeDeclaration lint to add Class and Enum as distinct DeclarationKind variants, updates end-of-declaration and control-flow-root logic to special-case classes/enums, refines ambient/declare handling and import-equals recognition, maps diagnostics for the new kinds to "class" and "enum", derives Eq/PartialEq where needed, and updates tests to cover JavaScript and TypeScript class, enum and import‑equals usage-before-declaration scenarios. Possibly related PRs
Suggested reviewers
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
CodSpeed Performance ReportMerging #8113 will not alter performanceComparing Summary
Footnotes
|
622c837 to
1ecb148
Compare
1ecb148 to
ec559b9
Compare
There was a problem hiding this comment.
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 details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (3)
crates/biome_configuration/src/analyzer/linter/rules.rsis excluded by!**/rules.rsand included by**packages/@biomejs/backend-jsonrpc/src/workspace.tsis excluded by!**/backend-jsonrpc/src/workspace.tsand included by**packages/@biomejs/biome/configuration_schema.jsonis excluded by!**/configuration_schema.jsonand included by**
📒 Files selected for processing (1)
crates/biome_js_analyze/tests/specs/correctness/noInvalidUseBeforeDeclaration/valid.js(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (14)
- GitHub Check: Documentation
- GitHub Check: Lint project (depot-windows-2022)
- GitHub Check: Lint project (depot-ubuntu-24.04-arm-16)
- GitHub Check: End-to-end tests
- GitHub Check: Test (depot-ubuntu-24.04-arm-16)
- GitHub Check: Check Dependencies
- GitHub Check: Test (depot-windows-2022-16)
- GitHub Check: Test Node.js API
- GitHub Check: autofix
- GitHub Check: Check JS Files
- GitHub Check: Bench (biome_js_parser)
- GitHub Check: Bench (biome_js_analyze)
- GitHub Check: Bench (biome_js_formatter)
- GitHub Check: Bench (biome_configuration)
🔇 Additional comments (1)
crates/biome_js_analyze/tests/specs/correctness/noInvalidUseBeforeDeclaration/valid.js (1)
16-18: Good test case for self-referencing static fields.This correctly validates that static field initialisers can reference their own class, as they execute after the class is fully declared.
crates/biome_js_analyze/tests/specs/correctness/noInvalidUseBeforeDeclaration/valid.js
Outdated
Show resolved
Hide resolved
| let declaration_end = declaration.range().end(); | ||
| let declaration_control_flow_root = | ||
| if let AnyJsBindingDeclaration::JsVariableDeclarator(declarator) = declaration | ||
| .parent_binding_pattern_declaration() | ||
| .unwrap_or(declaration) | ||
| { | ||
| declarator | ||
| .syntax() | ||
| .ancestors() | ||
| .skip(1) | ||
| .find(|ancestor| AnyJsControlFlowRoot::can_cast(ancestor.kind())) | ||
| } else { | ||
| None | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know why we did this. No test seems to cover this. Previously we handled only variables and enum members. It seems that we wished to exclude enum members from same-scope checking.
f2779e5 to
b43edcc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
crates/biome_js_analyze/src/lint/correctness/no_invalid_use_before_declaration.rs (1)
119-128: Consider clarifying the comment about class and enum handling.The logic is correct—using
id.range().end()for classes and enums allows self-references within the declaration body whilst flagging earlier uses. The comment could be more explicit about this distinction.For example:
-// A class can be instantiated by its properties. -// Enum members can be qualified by the enum name. +// For classes and enums, use the identifier position as the declaration end. +// This allows self-references within the class/enum body (e.g., `new C()` in a method) +// whilst still flagging usage before the identifier (e.g., `new C(); class C {}`).
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (2)
crates/biome_js_analyze/tests/specs/correctness/noInvalidUseBeforeDeclaration/valid.js.snapis excluded by!**/*.snapand included by**crates/biome_js_analyze/tests/specs/correctness/noInvalidUseBeforeDeclaration/valid.ts.snapis excluded by!**/*.snapand included by**
📒 Files selected for processing (3)
crates/biome_js_analyze/src/lint/correctness/no_invalid_use_before_declaration.rs(8 hunks)crates/biome_js_analyze/tests/specs/correctness/noInvalidUseBeforeDeclaration/valid.js(1 hunks)crates/biome_js_analyze/tests/specs/correctness/noInvalidUseBeforeDeclaration/valid.ts(2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- crates/biome_js_analyze/tests/specs/correctness/noInvalidUseBeforeDeclaration/valid.ts
🧰 Additional context used
🧬 Code graph analysis (1)
crates/biome_js_analyze/tests/specs/correctness/noInvalidUseBeforeDeclaration/valid.js (1)
crates/biome_js_analyze/tests/specs/correctness/noInvalidUseBeforeDeclaration/invalid.js (1)
instance(20-20)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
- GitHub Check: Check Dependencies
- GitHub Check: Test (depot-ubuntu-24.04-arm-16)
- GitHub Check: Test (depot-windows-2022-16)
- GitHub Check: Lint project (depot-windows-2022)
- GitHub Check: Check JS Files
- GitHub Check: Test Node.js API
- GitHub Check: autofix
- GitHub Check: Bench (biome_js_formatter)
- GitHub Check: Bench (biome_js_parser)
- GitHub Check: Bench (biome_js_analyze)
- GitHub Check: Bench (biome_configuration)
🔇 Additional comments (6)
crates/biome_js_analyze/tests/specs/correctness/noInvalidUseBeforeDeclaration/valid.js (1)
13-18: LGTM! Past duplicate function issue resolved and test cases are spot-on.The function has been properly renamed from
f()touseClassInFunction(), resolving the previous duplicate declaration concern. Both test cases correctly validate the rule's intended behaviour:
- Using
Classinside an uncalled function before its declaration is valid (the class will exist when the function eventually executes)- Self-referencing
Classin its own static field is valid (static initialisers run after the constructor is defined)crates/biome_js_analyze/src/lint/correctness/no_invalid_use_before_declaration.rs (5)
14-73: Documentation clearly explains the extended rule coverage.The updated description and examples effectively communicate that the rule now handles classes and enums alongside variables and parameters.
192-193: Diagnostic text mapping is correct.The new variants are properly mapped to their user-facing names.
219-226: Enum extension and derives look good.The
EqandPartialEqderives are necessary for the pattern matching at line 119, and the newClassandEnumvariants properly extend the declaration kinds.
257-271: Ambient class and enum handling is correct.Properly excludes
declare classanddeclare enumfrom use-before-declaration checks, as these ambient declarations can be used before their declarations appear in the source.
240-240: The implementation is correct—no changes needed.TypeScript's
import x = require("mod")emits a runtimeconst x = require()assignment, not a hoisted ES module binding. It therefore behaves like anyconstdeclaration and should be subject to use-before-declaration checks. The existing test confirms this:x; import x = require("file");is correctly flagged as invalid. Both forms ofimport =(require and namespace) are appropriately classified asVariable.
…biomejs#8113) Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
…biomejs#8113) Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Summary
This PR adds the handling of classes, enums, and TS's import-equals to the
noInvalidUseBeforeDeclarationrule.For example, The following code is now reported as invalid:
Note that the following code is not reported because it is valid:
Test Plan
I added several tests to cover the new cases.
Docs
I added a changeset.
I also updated the rule's description.