1
1
use oxc_ast:: { AstKind , ast:: VariableDeclarationKind } ;
2
2
use oxc_diagnostics:: OxcDiagnostic ;
3
3
use oxc_macros:: declare_oxc_lint;
4
- use oxc_span:: Span ;
4
+ use oxc_span:: { GetSpan , Span } ;
5
5
6
6
use crate :: { AstNode , context:: LintContext , rule:: Rule } ;
7
7
8
- fn block_scoped_var_diagnostic ( span : Span , name : & str ) -> OxcDiagnostic {
8
+ fn redeclaration_diagnostic ( decl_span : Span , redecl_span : Span , name : & str ) -> OxcDiagnostic {
9
9
OxcDiagnostic :: warn ( format ! ( "'{name}' is used outside of binding context." ) )
10
10
. with_help ( format ! ( "Variable '{name}' is used outside its declaration block. Declare it outside the block or use 'let'/'const'." ) )
11
- . with_label ( span)
11
+ . with_labels ( [
12
+ redecl_span. label ( "it is redeclared here" ) ,
13
+ decl_span. label ( format ! ( "'{name}' is first declared here" ) ) ,
14
+ ] )
15
+ }
16
+ fn use_outside_scope_diagnostic ( decl_span : Span , used_span : Span , name : & str ) -> OxcDiagnostic {
17
+ OxcDiagnostic :: warn ( format ! ( "'{name}' is used outside of binding context." ) )
18
+ . with_help ( format ! ( "Variable '{name}' is used outside its declaration block. Declare it outside the block or use 'let'/'const'." ) )
19
+ . with_labels ( [
20
+ used_span. label ( format ! ( "'{name}' is used here" ) ) ,
21
+ decl_span. label ( "It is declared in a different scope here" ) ,
22
+ ] )
12
23
}
13
24
14
25
#[ derive( Debug , Default , Clone ) ]
@@ -84,7 +95,11 @@ impl Rule for BlockScopedVar {
84
95
for redeclaration in ctx. scoping ( ) . symbol_redeclarations ( symbol_id) {
85
96
let re_scope_id = ctx. nodes ( ) . get_node ( redeclaration. declaration ) . scope_id ( ) ;
86
97
if !scope_arr. contains ( & re_scope_id) && re_scope_id != cur_node_scope_id {
87
- ctx. diagnostic ( block_scoped_var_diagnostic ( redeclaration. span , name) ) ;
98
+ ctx. diagnostic ( redeclaration_diagnostic (
99
+ item. id . span ( ) ,
100
+ redeclaration. span ,
101
+ name,
102
+ ) ) ;
88
103
}
89
104
}
90
105
// e.g. "var a = 4; console.log(a);"
@@ -93,7 +108,8 @@ impl Rule for BlockScopedVar {
93
108
if !scope_arr. contains ( & reference_scope_id)
94
109
&& reference_scope_id != cur_node_scope_id
95
110
{
96
- ctx. diagnostic ( block_scoped_var_diagnostic (
111
+ ctx. diagnostic ( use_outside_scope_diagnostic (
112
+ item. id . span ( ) ,
97
113
ctx. reference_span ( reference) ,
98
114
name,
99
115
) ) ;
0 commit comments