11use oxc_ast:: AstKind ;
22use oxc_diagnostics:: { LabeledSpan , OxcDiagnostic } ;
33use oxc_macros:: declare_oxc_lint;
4+ use oxc_semantic:: AstNode ;
45use oxc_span:: GetSpan ;
56
67use crate :: { context:: LintContext , rule:: Rule } ;
@@ -72,20 +73,27 @@ declare_oxc_lint!(
7273) ;
7374
7475impl Rule for NoDuplicateHead {
75- fn run_on_symbol ( & self , symbol_id : oxc_semantic:: SymbolId , ctx : & LintContext < ' _ > ) {
76- let symbols = ctx. scoping ( ) ;
77- let name = symbols. symbol_name ( symbol_id) ;
78- if name != "Head" {
76+ fn run < ' a > ( & self , node : & AstNode < ' a > , ctx : & LintContext < ' a > ) {
77+ let AstKind :: ImportDeclaration ( import_decl) = node. kind ( ) else {
7978 return ;
80- }
79+ } ;
80+ let Some ( specifiers) = & import_decl. specifiers else {
81+ return ;
82+ } ;
83+ let Some ( head_specifier) = specifiers. iter ( ) . find ( |s| s. name ( ) == "Head" ) else {
84+ return ;
85+ } ;
86+
87+ let scoping = ctx. scoping ( ) ;
88+ let symbol_id = head_specifier. local ( ) . symbol_id ( ) ;
8189
82- let flags = symbols . symbol_flags ( symbol_id) ;
90+ let flags = scoping . symbol_flags ( symbol_id) ;
8391 if !flags. is_import ( ) {
8492 return ;
8593 }
8694
87- let scope_id = symbols . symbol_scope_id ( symbol_id) ;
88- if scope_id != ctx . scoping ( ) . root_scope_id ( ) {
95+ let scope_id = scoping . symbol_scope_id ( symbol_id) ;
96+ if scope_id != scoping. root_scope_id ( ) {
8997 return ;
9098 }
9199
@@ -100,7 +108,7 @@ impl Rule for NoDuplicateHead {
100108 LabeledSpan :: underline ( span)
101109 } ;
102110
103- for reference in symbols . get_resolved_references ( symbol_id) {
111+ for reference in scoping . get_resolved_references ( symbol_id) {
104112 if !reference. is_read ( ) {
105113 continue ;
106114 }
@@ -125,11 +133,9 @@ impl Rule for NoDuplicateHead {
125133 }
126134
127135 // `labels` is empty if 0 or 1 `<Head>` found
128- if labels. is_empty ( ) {
129- return ;
136+ if ! labels. is_empty ( ) {
137+ ctx . diagnostic ( no_duplicate_head ( labels ) ) ;
130138 }
131-
132- ctx. diagnostic ( no_duplicate_head ( labels) ) ;
133139 }
134140}
135141
0 commit comments