1+ use oxc_ast:: { AstKind , ast:: BindingIdentifier } ;
12use oxc_diagnostics:: OxcDiagnostic ;
23use oxc_macros:: declare_oxc_lint;
3- use oxc_semantic:: SymbolId ;
4+ use oxc_semantic:: AstNode ;
45use oxc_span:: Span ;
56
67use crate :: { context:: LintContext , rule:: Rule } ;
@@ -82,17 +83,27 @@ declare_oxc_lint!(
8283) ;
8384
8485impl Rule for NoClassAssign {
85- fn run_on_symbol ( & self , symbol_id : SymbolId , ctx : & LintContext < ' _ > ) {
86+ fn run < ' a > ( & self , node : & AstNode < ' a > , ctx : & LintContext < ' a > ) {
87+ let AstKind :: Class ( class) = node. kind ( ) else {
88+ return ;
89+ } ;
90+
91+ let Some ( symbol_id) = class. id . as_ref ( ) . map ( BindingIdentifier :: symbol_id) else {
92+ return ;
93+ } ;
94+
8695 let symbol_table = ctx. scoping ( ) ;
87- if symbol_table. symbol_flags ( symbol_id) . is_class ( ) {
88- for reference in symbol_table. get_resolved_references ( symbol_id) {
89- if reference. is_write ( ) {
90- ctx. diagnostic ( no_class_assign_diagnostic (
91- symbol_table. symbol_name ( symbol_id) ,
92- symbol_table. symbol_span ( symbol_id) ,
93- ctx. semantic ( ) . reference_span ( reference) ,
94- ) ) ;
95- }
96+ // This should always be considered a class (since we got it from a class declaration),
97+ // but we check in debug mode just to be sure.
98+ debug_assert ! ( symbol_table. symbol_flags( symbol_id) . is_class( ) ) ;
99+
100+ for reference in symbol_table. get_resolved_references ( symbol_id) {
101+ if reference. is_write ( ) {
102+ ctx. diagnostic ( no_class_assign_diagnostic (
103+ symbol_table. symbol_name ( symbol_id) ,
104+ symbol_table. symbol_span ( symbol_id) ,
105+ ctx. semantic ( ) . reference_span ( reference) ,
106+ ) ) ;
96107 }
97108 }
98109 }
@@ -119,6 +130,9 @@ fn test() {
119130 ( "if (foo) { class A {} } else { class A {} } A = 1;" , None ) ,
120131 // Sequence expression
121132 ( "(class A {}, A = 1)" , None ) ,
133+ // Class expressions
134+ ( "let A = class { }; A = 1;" , None ) ,
135+ ( "let A = class B { }; A = 1;" , None ) ,
122136 ] ;
123137
124138 let fail = vec ! [
0 commit comments