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

Skip to content

Conversation

@Conaclos
Copy link
Member

@Conaclos Conaclos commented Nov 14, 2025

Summary

This is a small fix of an issue I met on a personal project.
Basically noInvalidUseBeforeDeclaration reported the following code as invalid:

c;
declare const c: number

While it is valid because c is an ambient variable that is hoisted.

Test Plan

I added a test.

Docs

I added a changeset.

@changeset-bot
Copy link

changeset-bot bot commented Nov 14, 2025

🦋 Changeset detected

Latest commit: cbd209d

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 labels Nov 14, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 14, 2025

Walkthrough

The PR updates the noInvalidUseBeforeDeclaration lint: it skips TypeScript declaration files by checking JsFileSource::is_definition_file and treats ambient declare variable declarations as non-violations by adjusting DeclarationKind::try_from (including special-case handling for object shorthand and ambient TS_DECLARE_STATEMENT). A test was added that uses c; before declare const c: number;. No public API signatures changed.

Suggested reviewers

  • ematipico
  • dyc3

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 clearly and specifically describes the main fix: preventing the noInvalidUseBeforeDeclaration rule from reporting uses before ambient variable declarations.
Description check ✅ Passed The description is directly related to the changeset, explaining the issue with ambient variables, providing a concrete example, and noting test coverage and documentation additions.
✨ 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-ambient-variables

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

coderabbitai[bot]

This comment was marked as off-topic.

@Conaclos Conaclos force-pushed the conaclos/noInvalidUseBeforeDeclaration-ambient-variables branch from 6f3c4c2 to fe1f34b Compare November 14, 2025 19:25
coderabbitai[bot]

This comment was marked as resolved.

@codspeed-hq
Copy link

codspeed-hq bot commented Nov 14, 2025

CodSpeed Performance Report

Merging #8104 will not alter performance

Comparing conaclos/noInvalidUseBeforeDeclaration-ambient-variables (cbd209d) with main (82b9a8e)

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.

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)

223-225: Optional: Simplify by adding to the pattern list above.

The block wrapper is unnecessary—you could just append this variant to the | pattern chain at lines 219–222.

Apply this diff to simplify:

 AnyJsBindingDeclaration::JsArrayBindingPatternElement(_)
 | AnyJsBindingDeclaration::JsArrayBindingPatternRestElement(_)
 | AnyJsBindingDeclaration::JsObjectBindingPatternProperty(_)
 | AnyJsBindingDeclaration::JsObjectBindingPatternRest(_)
-| AnyJsBindingDeclaration::JsObjectBindingPatternShorthandProperty(_) => {
-    Ok(Self::Variable)
-}
+| AnyJsBindingDeclaration::JsObjectBindingPatternShorthandProperty(_)
-AnyJsBindingDeclaration::JsVariableDeclarator(declarator) => {
+| AnyJsBindingDeclaration::JsVariableDeclarator(declarator) => {
     if let Some(var_decl) = declarator.declaration()
📜 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 fe1f34b and cbd209d.

📒 Files selected for processing (1)
  • crates/biome_js_analyze/src/lint/correctness/no_invalid_use_before_declaration.rs (3 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). (13)
  • GitHub Check: Bench (biome_js_analyze)
  • GitHub Check: Bench (biome_js_formatter)
  • GitHub Check: Bench (biome_js_parser)
  • GitHub Check: Check Dependencies
  • GitHub Check: Lint project (depot-windows-2022)
  • GitHub Check: Lint project (depot-ubuntu-24.04-arm-16)
  • GitHub Check: Test (depot-windows-2022-16)
  • GitHub Check: Documentation
  • GitHub Check: Test (depot-ubuntu-24.04-arm-16)
  • GitHub Check: End-to-end tests
  • GitHub Check: Validate rules documentation
  • GitHub Check: autofix
  • GitHub Check: Test Node.js API
🔇 Additional comments (3)
crates/biome_js_analyze/src/lint/correctness/no_invalid_use_before_declaration.rs (3)

6-7: LGTM! Required imports for the fix.

The new imports support the declaration file check and ambient declaration detection.


85-91: Good optimisation! Skipping declaration files entirely.

Declaration files (.d.ts) only contain ambient declarations, so this early exit is both correct and efficient.


226-237: Excellent fix! Ambient declarations are now correctly excluded.

The parent-chain check properly identifies declare variables, which are hoisted and can be used before their declaration point. Returning Err(()) ensures they skip validation at line 105.

@ematipico ematipico merged commit 041196b into main Nov 15, 2025
18 checks passed
@ematipico ematipico deleted the conaclos/noInvalidUseBeforeDeclaration-ambient-variables branch November 15, 2025 08:42
@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
l0ngvh pushed a commit to l0ngvh/biome that referenced this pull request Dec 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants