@@ -570,6 +570,98 @@ describe('code fixes', () => {
570570 ] ) ;
571571 } ) ;
572572 } ) ;
573+
574+ describe ( 'unused standalone imports' , ( ) => {
575+ it ( 'should fix imports array where some imports are not used' , ( ) => {
576+ const files = {
577+ 'app.ts' : `
578+ import {Component, Directive, Pipe} from '@angular/core';
579+
580+ @Directive({selector: '[used]', standalone: true})
581+ export class UsedDirective {}
582+
583+ @Directive({selector: '[unused]', standalone: true})
584+ export class UnusedDirective {}
585+
586+ @Pipe({name: 'unused', standalone: true})
587+ export class UnusedPipe {}
588+
589+ @Component({
590+ selector: 'used-cmp',
591+ standalone: true,
592+ template: '',
593+ })
594+ export class UsedComponent {}
595+
596+ @Component({
597+ template: \`
598+ <section>
599+ <div></div>
600+ <span used></span>
601+ <div>
602+ <used-cmp/>
603+ </div>
604+ </section>
605+ \`,
606+ standalone: true,
607+ imports: [UnusedDirective, UsedDirective, UnusedPipe, UsedComponent],
608+ })
609+ export class AppComponent {}
610+ ` ,
611+ } ;
612+
613+ const project = createModuleAndProjectWithDeclarations ( env , 'test' , files ) ;
614+ const appFile = project . openFile ( 'app.ts' ) ;
615+
616+ const fixesAllActions = project . getCombinedCodeFix (
617+ 'app.ts' ,
618+ FixIdForCodeFixesAll . FIX_UNUSED_STANDALONE_IMPORTS ,
619+ ) ;
620+ expectIncludeReplacementTextForFileTextChange ( {
621+ fileTextChanges : fixesAllActions . changes ,
622+ content : appFile . contents ,
623+ text : '[UnusedDirective, UsedDirective, UnusedPipe, UsedComponent]' ,
624+ newText : '[UsedDirective, UsedComponent]' ,
625+ fileName : 'app.ts' ,
626+ } ) ;
627+ } ) ;
628+
629+ it ( 'should fix imports array where all imports are not used' , ( ) => {
630+ const files = {
631+ 'app.ts' : `
632+ import {Component, Directive, Pipe} from '@angular/core';
633+
634+ @Directive({selector: '[unused]', standalone: true})
635+ export class UnusedDirective {}
636+
637+ @Pipe({name: 'unused', standalone: true})
638+ export class UnusedPipe {}
639+
640+ @Component({
641+ template: '',
642+ standalone: true,
643+ imports: [UnusedDirective, UnusedPipe],
644+ })
645+ export class AppComponent {}
646+ ` ,
647+ } ;
648+
649+ const project = createModuleAndProjectWithDeclarations ( env , 'test' , files ) ;
650+ const appFile = project . openFile ( 'app.ts' ) ;
651+
652+ const fixesAllActions = project . getCombinedCodeFix (
653+ 'app.ts' ,
654+ FixIdForCodeFixesAll . FIX_UNUSED_STANDALONE_IMPORTS ,
655+ ) ;
656+ expectIncludeReplacementTextForFileTextChange ( {
657+ fileTextChanges : fixesAllActions . changes ,
658+ content : appFile . contents ,
659+ text : '[UnusedDirective, UnusedPipe]' ,
660+ newText : '[]' ,
661+ fileName : 'app.ts' ,
662+ } ) ;
663+ } ) ;
664+ } ) ;
573665} ) ;
574666
575667type ActionChanges = {
0 commit comments