1- use crate :: { services:: control_flow :: AnyJsControlFlowRoot , services :: semantic:: SemanticServices } ;
1+ use crate :: services:: semantic:: SemanticServices ;
22use biome_analyze:: { Rule , RuleDiagnostic , RuleSource , context:: RuleContext , declare_lint_rule} ;
33use biome_console:: markup;
44use biome_diagnostics:: Severity ;
55use biome_js_syntax:: {
6- AnyJsExportNamedSpecifier , AnyJsIdentifierUsage , JsFileSource , JsVariableDeclarationClause ,
7- TsDeclareStatement ,
6+ AnyJsExportNamedSpecifier , AnyJsFunction , AnyJsIdentifierUsage , JsClassDeclaration ,
7+ JsConstructorClassMember , JsFileSource , JsGetterClassMember , JsGetterObjectMember ,
8+ JsMethodClassMember , JsMethodObjectMember , JsModule , JsScript , JsSetterClassMember ,
9+ JsSetterObjectMember , JsStaticInitializationBlockClassMember , JsVariableDeclarationClause ,
10+ TsDeclareStatement , TsModuleDeclaration , TsPropertySignatureTypeMember ,
811 binding_ext:: { AnyJsBindingDeclaration , AnyJsIdentifierBinding } ,
912} ;
10- use biome_rowan:: { AstNode , SyntaxNodeOptionExt , TextRange } ;
13+ use biome_rowan:: { AstNode , SyntaxNodeOptionExt , TextRange , declare_node_union } ;
1114use biome_rule_options:: no_invalid_use_before_declaration:: NoInvalidUseBeforeDeclarationOptions ;
1215
1316declare_lint_rule ! {
@@ -126,11 +129,11 @@ impl Rule for NoInvalidUseBeforeDeclaration {
126129 } else {
127130 declaration. range ( ) . end ( )
128131 } ;
129- let declaration_control_flow_root = declaration
132+ let declaration_scope = declaration
130133 . syntax ( )
131134 . ancestors ( )
132135 . skip ( 1 )
133- . find ( |ancestor| AnyJsControlFlowRoot :: can_cast ( ancestor. kind ( ) ) ) ;
136+ . find ( |ancestor| AnyJsVariableScope :: can_cast ( ancestor. kind ( ) ) ) ;
134137 for reference in binding. all_references ( ) {
135138 if reference. range_start ( ) < declaration_end {
136139 let reference_syntax = reference. syntax ( ) ;
@@ -154,11 +157,10 @@ impl Rule for NoInvalidUseBeforeDeclaration {
154157 // function f() { X; }
155158 // const X = 0;
156159 // ```
157- && ( declaration_control_flow_root. is_none ( ) ||
158- declaration_control_flow_root == reference_syntax
160+ && declaration_scope == reference_syntax
159161 . ancestors ( )
160162 . skip ( 1 )
161- . find ( |ancestor| AnyJsControlFlowRoot :: can_cast ( ancestor. kind ( ) ) )
163+ . find ( |ancestor| AnyJsVariableScope :: can_cast ( ancestor. kind ( ) )
162164 )
163165 // ignore when used as a type.
164166 // For example:
@@ -295,3 +297,21 @@ impl TryFrom<&AnyJsBindingDeclaration> for DeclarationKind {
295297 }
296298 }
297299}
300+
301+ declare_node_union ! {
302+ AnyJsVariableScope =
303+ JsScript
304+ | JsModule
305+ | AnyJsFunction
306+ | JsClassDeclaration
307+ | JsConstructorClassMember
308+ | JsGetterClassMember
309+ | JsGetterObjectMember
310+ | JsMethodClassMember
311+ | JsMethodObjectMember
312+ | JsSetterClassMember
313+ | JsSetterObjectMember
314+ | JsStaticInitializationBlockClassMember
315+ | TsModuleDeclaration
316+ | TsPropertySignatureTypeMember
317+ }
0 commit comments