Skip to content

Commit

Permalink
refcator(lint/noInvalidUseBeforeDeclaration): allow use before as a t…
Browse files Browse the repository at this point in the history
…ype (#1461)
  • Loading branch information
Conaclos authored Jan 7, 2024
1 parent 8e3d3e2 commit 8ce8f4e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use biome_analyze::{context::RuleContext, declare_rule, Rule, RuleDiagnostic};
use biome_console::markup;
use biome_js_syntax::{
binding_ext::{AnyJsBindingDeclaration, AnyJsIdentifierBinding},
AnyJsExportNamedSpecifier,
AnyJsExportNamedSpecifier, AnyJsIdentifierUsage,
};
use biome_rowan::{AstNode, SyntaxNodeOptionExt, TextRange};

Expand Down Expand Up @@ -75,6 +75,7 @@ impl Rule for NoInvalidUseBeforeDeclaration {
let mut result = vec![];
for binding in model.all_bindings() {
let AnyJsIdentifierBinding::JsIdentifierBinding(id) = binding.tree() else {
// Ignore type declarations (interfaces, type-aliases, ...)
continue;
};
let Some(declaration) = id.declaration() else {
Expand Down Expand Up @@ -121,8 +122,21 @@ impl Rule for NoInvalidUseBeforeDeclaration {
// const X = 0;
// ```
&& (declaration_control_flow_root.is_none() ||
declaration_control_flow_root == reference.syntax().ancestors().skip(1).find(|ancestor| AnyJsControlFlowRoot::can_cast(ancestor.kind()))
declaration_control_flow_root == reference
.syntax()
.ancestors()
.skip(1)
.find(|ancestor| AnyJsControlFlowRoot::can_cast(ancestor.kind()))
)
// ignore when used as a type.
// For example:
//
// ```js
// type Y = typeof X;
// const X = 0;
// ```
&& !AnyJsIdentifierUsage::cast_ref(reference.syntax())
.is_some_and(|usage| usage.is_only_type())
{
result.push(InvalidUseBeforeDeclaration {
declaration_kind,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export { X }; const X = 1;

let a; console.log(a);

function h() { X; }; const X = 0;
function h() { Y; }; const Y = 0;
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export { X }; const X = 1;

let a; console.log(a);

function h() { X; }; const X = 0;
function h() { Y; }; const Y = 0;

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ let n = N.X;
namespace N {
export const X = 0;
}

type X = typeof X; const X = 0;
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace N {
export const X = 0;
}

type X = typeof X; const X = 0;

```


0 comments on commit 8ce8f4e

Please sign in to comment.