Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/bright-tires-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@biomejs/biome": patch
---

Fixed [#8148](https://github.com/biomejs/biome/issues/8148). [`noInvalidUseBeforeDeclaration`](https://biomejs.dev/linter/rules/no-invalid-use-before-declaration/) no longer reports some valid use before declarations.

The following code is no longer reported as invalid:

```ts
class classA {
C = C;
}
const C = 0;
```
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use crate::{services::control_flow::AnyJsControlFlowRoot, services::semantic::SemanticServices};
use crate::services::semantic::SemanticServices;
use biome_analyze::{Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule};
use biome_console::markup;
use biome_diagnostics::Severity;
use biome_js_syntax::{
AnyJsExportNamedSpecifier, AnyJsIdentifierUsage, JsFileSource, JsVariableDeclarationClause,
TsDeclareStatement,
AnyJsExportNamedSpecifier, AnyJsFunction, AnyJsIdentifierUsage, JsClassDeclaration,
JsConstructorClassMember, JsFileSource, JsGetterClassMember, JsGetterObjectMember,
JsMethodClassMember, JsMethodObjectMember, JsModule, JsScript, JsSetterClassMember,
JsSetterObjectMember, JsStaticInitializationBlockClassMember, JsVariableDeclarationClause,
TsDeclareStatement, TsModuleDeclaration, TsPropertySignatureTypeMember,
binding_ext::{AnyJsBindingDeclaration, AnyJsIdentifierBinding},
};
use biome_rowan::{AstNode, SyntaxNodeOptionExt, TextRange};
use biome_rowan::{AstNode, SyntaxNodeOptionExt, TextRange, declare_node_union};
use biome_rule_options::no_invalid_use_before_declaration::NoInvalidUseBeforeDeclarationOptions;

declare_lint_rule! {
Expand Down Expand Up @@ -126,11 +129,11 @@ impl Rule for NoInvalidUseBeforeDeclaration {
} else {
declaration.range().end()
};
let declaration_control_flow_root = declaration
let declaration_scope = declaration
.syntax()
.ancestors()
.skip(1)
.find(|ancestor| AnyJsControlFlowRoot::can_cast(ancestor.kind()));
.find(|ancestor| AnyJsVariableScope::can_cast(ancestor.kind()));
for reference in binding.all_references() {
if reference.range_start() < declaration_end {
let reference_syntax = reference.syntax();
Expand All @@ -154,11 +157,10 @@ impl Rule for NoInvalidUseBeforeDeclaration {
// function f() { X; }
// const X = 0;
// ```
&& (declaration_control_flow_root.is_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 removed the declaration_scope.is_none() condition because it is completely useless.

declaration_control_flow_root == reference_syntax
&& declaration_scope == reference_syntax
.ancestors()
.skip(1)
.find(|ancestor| AnyJsControlFlowRoot::can_cast(ancestor.kind()))
.find(|ancestor| AnyJsVariableScope::can_cast(ancestor.kind())
)
// ignore when used as a type.
// For example:
Expand Down Expand Up @@ -295,3 +297,21 @@ impl TryFrom<&AnyJsBindingDeclaration> for DeclarationKind {
}
}
}

declare_node_union! {
AnyJsVariableScope =
JsScript
| JsModule
| AnyJsFunction
| JsClassDeclaration
| JsConstructorClassMember
| JsGetterClassMember
| JsGetterObjectMember
| JsMethodClassMember
| JsMethodObjectMember
| JsSetterClassMember
| JsSetterObjectMember
| JsStaticInitializationBlockClassMember
| TsModuleDeclaration
| TsPropertySignatureTypeMember
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ function h() { Y; }; const Y = 0;
function useClassInFunction() {
const instance = new Class();
}

class Class {
static SINGLETON = new Class();
}

class classA {
C = C;
prop = new classB();
}
class classB {}
const C = 0;
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ function h() { Y; }; const Y = 0;
function useClassInFunction() {
const instance = new Class();
}

class Class {
static SINGLETON = new Class();
}

class classA {
C = C;
prop = new classB();
}
class classB {}
const C = 0;

```