@@ -3,7 +3,8 @@ use biome_analyze::{Rule, RuleDiagnostic, RuleSource, context::RuleContext, decl
33use biome_console:: markup;
44use biome_diagnostics:: Severity ;
55use biome_js_syntax:: {
6- AnyJsExportNamedSpecifier , AnyJsIdentifierUsage ,
6+ AnyJsExportNamedSpecifier , AnyJsIdentifierUsage , JsFileSource , JsVariableDeclarationClause ,
7+ TsDeclareStatement ,
78 binding_ext:: { AnyJsBindingDeclaration , AnyJsIdentifierBinding } ,
89} ;
910use biome_rowan:: { AstNode , SyntaxNodeOptionExt , TextRange } ;
@@ -81,6 +82,13 @@ impl Rule for NoInvalidUseBeforeDeclaration {
8182 fn run ( ctx : & RuleContext < Self > ) -> Self :: Signals {
8283 let model = ctx. model ( ) ;
8384 let mut result = vec ! [ ] ;
85+ let is_declaration_file = ctx
86+ . source_type :: < JsFileSource > ( )
87+ . language ( )
88+ . is_definition_file ( ) ;
89+ if is_declaration_file {
90+ return Box :: default ( ) ;
91+ }
8492 for binding in model. all_bindings ( ) {
8593 let id = binding. tree ( ) ;
8694 if matches ! (
@@ -212,8 +220,21 @@ impl TryFrom<&AnyJsBindingDeclaration> for DeclarationKind {
212220 | AnyJsBindingDeclaration :: JsArrayBindingPatternRestElement ( _)
213221 | AnyJsBindingDeclaration :: JsObjectBindingPatternProperty ( _)
214222 | AnyJsBindingDeclaration :: JsObjectBindingPatternRest ( _)
215- | AnyJsBindingDeclaration :: JsObjectBindingPatternShorthandProperty ( _)
216- | AnyJsBindingDeclaration :: JsVariableDeclarator ( _) => Ok ( Self :: Variable ) ,
223+ | AnyJsBindingDeclaration :: JsObjectBindingPatternShorthandProperty ( _) => {
224+ Ok ( Self :: Variable )
225+ }
226+ AnyJsBindingDeclaration :: JsVariableDeclarator ( declarator) => {
227+ if let Some ( var_decl) = declarator. declaration ( )
228+ && let Some ( var_decl_clause) = var_decl. parent :: < JsVariableDeclarationClause > ( )
229+ && var_decl_clause. parent :: < TsDeclareStatement > ( ) . is_some ( )
230+ {
231+ // Ambient variables, such as `declare const c;`,
232+ // can be used before their declarations.
233+ Err ( ( ) )
234+ } else {
235+ Ok ( Self :: Variable )
236+ }
237+ }
217238 // Parameters
218239 AnyJsBindingDeclaration :: JsFormalParameter ( _)
219240 | AnyJsBindingDeclaration :: JsRestParameter ( _)
0 commit comments