Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Conaclos
Copy link
Member

Summary

This PR adds the handling of classes, enums, and TS's import-equals to the noInvalidUseBeforeDeclaration rule.

For example, The following code is now reported as invalid:

new C();
class C {}

Note that the following code is not reported because it is valid:

function f() {
  new C();
}
let c: C;
class C {}

new C2();
declare class C2 {}

Test Plan

I added several tests to cover the new cases.

Docs

I added a changeset.
I also updated the rule's description.

@changeset-bot
Copy link

changeset-bot bot commented Nov 15, 2025

🦋 Changeset detected

Latest commit: b43edcc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
@biomejs/biome Patch
@biomejs/cli-win32-x64 Patch
@biomejs/cli-win32-arm64 Patch
@biomejs/cli-darwin-x64 Patch
@biomejs/cli-darwin-arm64 Patch
@biomejs/cli-linux-x64 Patch
@biomejs/cli-linux-arm64 Patch
@biomejs/cli-linux-x64-musl Patch
@biomejs/cli-linux-arm64-musl Patch
@biomejs/wasm-web Patch
@biomejs/wasm-bundler Patch
@biomejs/wasm-nodejs Patch
@biomejs/backend-jsonrpc Patch

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

@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages A-Project Area: project labels Nov 15, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 15, 2025

Walkthrough

This 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

  • dyc3
  • ematipico

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and concisely captures the main change: extending noInvalidUseBeforeDeclaration to handle classes, enums, and import-equals.
Description check ✅ Passed The description is well-related to the changeset, providing clear examples and explaining the new functionality, test coverage, and documentation updates.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch conaclos/noInvalidUseBeforeDeclaration-class-enum

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

coderabbitai[bot]

This comment was marked as resolved.

@codspeed-hq
Copy link

codspeed-hq bot commented Nov 15, 2025

CodSpeed Performance Report

Merging #8113 will not alter performance

Comparing conaclos/noInvalidUseBeforeDeclaration-class-enum (b43edcc) with main (041196b)

Summary

✅ 58 untouched
⏩ 95 skipped1

Footnotes

  1. 95 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@Conaclos Conaclos force-pushed the conaclos/noInvalidUseBeforeDeclaration-class-enum branch from 622c837 to 1ecb148 Compare November 15, 2025 12:32
coderabbitai[bot]

This comment was marked as resolved.

@Conaclos Conaclos force-pushed the conaclos/noInvalidUseBeforeDeclaration-class-enum branch from 1ecb148 to ec559b9 Compare November 15, 2025 12:40
Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between 1ecb148 and ec559b9.

⛔ Files ignored due to path filters (3)
  • crates/biome_configuration/src/analyzer/linter/rules.rs is excluded by !**/rules.rs and included by **
  • packages/@biomejs/backend-jsonrpc/src/workspace.ts is excluded by !**/backend-jsonrpc/src/workspace.ts and included by **
  • packages/@biomejs/biome/configuration_schema.json is excluded by !**/configuration_schema.json and 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.

Comment on lines -108 to -121
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
};
Copy link
Member Author

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.

@Conaclos Conaclos force-pushed the conaclos/noInvalidUseBeforeDeclaration-class-enum branch 3 times, most recently from f2779e5 to b43edcc Compare November 15, 2025 12:55
@Conaclos Conaclos requested review from a team November 15, 2025 12:56
Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between bc4d926 and b43edcc.

⛔ Files ignored due to path filters (2)
  • crates/biome_js_analyze/tests/specs/correctness/noInvalidUseBeforeDeclaration/valid.js.snap is excluded by !**/*.snap and included by **
  • crates/biome_js_analyze/tests/specs/correctness/noInvalidUseBeforeDeclaration/valid.ts.snap is excluded by !**/*.snap and 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() to useClassInFunction(), resolving the previous duplicate declaration concern. Both test cases correctly validate the rule's intended behaviour:

  • Using Class inside an uncalled function before its declaration is valid (the class will exist when the function eventually executes)
  • Self-referencing Class in 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 Eq and PartialEq derives are necessary for the pattern matching at line 119, and the new Class and Enum variants properly extend the declaration kinds.


257-271: Ambient class and enum handling is correct.

Properly excludes declare class and declare enum from 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 runtime const x = require() assignment, not a hoisted ES module binding. It therefore behaves like any const declaration 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 of import = (require and namespace) are appropriately classified as Variable.

@Conaclos Conaclos merged commit fb8e3e7 into main Nov 15, 2025
20 checks passed
@Conaclos Conaclos deleted the conaclos/noInvalidUseBeforeDeclaration-class-enum branch November 15, 2025 15:51
@github-actions github-actions bot mentioned this pull request Nov 15, 2025
ematipico pushed a commit to hamirmahal/biome that referenced this pull request Nov 19, 2025
…biomejs#8113)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
l0ngvh pushed a commit to l0ngvh/biome that referenced this pull request Dec 21, 2025
…biomejs#8113)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Linter Area: linter A-Project Area: project L-JavaScript Language: JavaScript and super languages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants