@@ -69,18 +69,17 @@ declare_oxc_lint!(
6969 /// ```
7070 NoExtraneousClass ,
7171 typescript,
72- suspicious
72+ suspicious,
73+ dangerous_suggestion
7374) ;
7475
7576fn empty_class_diagnostic ( span : Span , has_decorators : bool ) -> OxcDiagnostic {
76- let diagnostic = OxcDiagnostic :: warn ( "Unexpected empty class." ) . with_label ( span) ;
77- if has_decorators {
78- diagnostic. with_help (
79- r#"Set "allowWithDecorator": true in your config to allow empty decorated classes"# ,
80- )
77+ let help = if has_decorators {
78+ r#"Set "allowWithDecorator": true in your config to allow empty decorated classes"#
8179 } else {
82- diagnostic
83- }
80+ "Delete this class"
81+ } ;
82+ OxcDiagnostic :: warn ( "Unexpected empty class." ) . with_label ( span) . with_help ( help)
8483}
8584
8685fn only_static_no_extraneous_class_diagnostic ( span : Span ) -> OxcDiagnostic {
@@ -147,7 +146,22 @@ impl Rule for NoExtraneousClass {
147146 unsafe { std:: hint:: assert_unchecked ( start <= u32:: MAX as usize ) } ;
148147 span = span. shrink_left ( start as u32 ) ;
149148 }
150- ctx. diagnostic ( empty_class_diagnostic ( span, !class. decorators . is_empty ( ) ) ) ;
149+ let has_decorators = !class. decorators . is_empty ( ) ;
150+ ctx. diagnostic_with_suggestion (
151+ empty_class_diagnostic ( span, has_decorators) ,
152+ |fixer| {
153+ if has_decorators {
154+ return fixer. noop ( ) ;
155+ }
156+ if let Some ( AstKind :: ExportNamedDeclaration ( decl) ) =
157+ ctx. nodes ( ) . parent_kind ( node. id ( ) )
158+ {
159+ fixer. delete ( decl)
160+ } else {
161+ fixer. delete ( class)
162+ }
163+ } ,
164+ ) ;
151165 }
152166 }
153167 [ ClassElement :: MethodDefinition ( constructor) ] if constructor. kind . is_constructor ( ) => {
@@ -308,5 +322,18 @@ fn test() {
308322 ( "abstract class Foo { constructor() {} }" , None ) ,
309323 ] ;
310324
311- Tester :: new ( NoExtraneousClass :: NAME , NoExtraneousClass :: PLUGIN , pass, fail) . test_and_snapshot ( ) ;
325+ let fix = vec ! [
326+ ( "class Foo {}" , "" , None , FixKind :: DangerousSuggestion ) ,
327+ ( "export class Foo {}" , "" , None , FixKind :: DangerousSuggestion ) ,
328+ (
329+ "@foo class Foo {}" ,
330+ "@foo class Foo {}" ,
331+ Some ( json!( [ { "allowWithDecorator" : false } ] ) ) ,
332+ FixKind :: DangerousSuggestion ,
333+ ) ,
334+ ] ;
335+
336+ Tester :: new ( NoExtraneousClass :: NAME , NoExtraneousClass :: PLUGIN , pass, fail)
337+ . expect_fix ( fix)
338+ . test_and_snapshot ( ) ;
312339}
0 commit comments