|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import {initMockFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing'; |
10 | | -import {spawn} from 'child_process'; |
11 | 10 | import ts from 'typescript'; |
12 | 11 |
|
13 | 12 | import {FixIdForCodeFixesAll} from '../src/codefixes/utils'; |
@@ -357,7 +356,7 @@ describe('code fixes', () => { |
357 | 356 | const actionChanges = allChangesForCodeActions(fixFile.contents, codeActions); |
358 | 357 | actionChangesMatch(actionChanges, `Import BarComponent from './bar' on FooModule`, [ |
359 | 358 | [``, `import { BarComponent } from "./bar";`], |
360 | | - [`imp`, `imports: [BarComponent]`], |
| 359 | + [`imports: []`, `imports: [BarComponent]`], |
361 | 360 | ]); |
362 | 361 | }); |
363 | 362 |
|
@@ -527,7 +526,7 @@ describe('code fixes', () => { |
527 | 526 | ]); |
528 | 527 | const actionChanges = allChangesForCodeActions(fixFile.contents, codeActions); |
529 | 528 | actionChangesMatch(actionChanges, `Import BarComponent from './bar' on FooComponent`, [ |
530 | | - [`{te`, `BarComponent, { test }`], |
| 529 | + [`{test}`, `BarComponent, { test }`], |
531 | 530 | [``, `, imports: [BarComponent]`], |
532 | 531 | ]); |
533 | 532 | }); |
@@ -573,7 +572,77 @@ describe('code fixes', () => { |
573 | 572 | }); |
574 | 573 |
|
575 | 574 | describe('unused standalone imports', () => { |
576 | | - it('should fix imports array where some imports are not used', () => { |
| 575 | + it('should fix single diagnostic about individual imports that are not used', () => { |
| 576 | + const files = { |
| 577 | + 'app.ts': ` |
| 578 | + import {Component, Directive} 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 | + @Component({ |
| 587 | + template: '<span used></span>', |
| 588 | + standalone: true, |
| 589 | + imports: [UnusedDirective, UsedDirective], |
| 590 | + }) |
| 591 | + export class AppComponent {} |
| 592 | + `, |
| 593 | + }; |
| 594 | + |
| 595 | + const project = createModuleAndProjectWithDeclarations(env, 'test', files); |
| 596 | + const fixFile = project.openFile('app.ts'); |
| 597 | + fixFile.moveCursorToText('Unused¦Directive,'); |
| 598 | + |
| 599 | + const diags = project.getDiagnosticsForFile('app.ts'); |
| 600 | + const codeActions = project.getCodeFixesAtPosition('app.ts', fixFile.cursor, fixFile.cursor, [ |
| 601 | + diags[0].code, |
| 602 | + ]); |
| 603 | + const actionChanges = allChangesForCodeActions(fixFile.contents, codeActions); |
| 604 | + |
| 605 | + actionChangesMatch(actionChanges, 'Remove unused import UnusedDirective', [ |
| 606 | + ['[UnusedDirective, UsedDirective]', '[UsedDirective]'], |
| 607 | + ]); |
| 608 | + }); |
| 609 | + |
| 610 | + it('should fix single diagnostic about all imports that are not used', () => { |
| 611 | + const files = { |
| 612 | + 'app.ts': ` |
| 613 | + import {Component, Directive, Pipe} from '@angular/core'; |
| 614 | +
|
| 615 | + @Directive({selector: '[unused]', standalone: true}) |
| 616 | + export class UnusedDirective {} |
| 617 | +
|
| 618 | + @Pipe({name: 'unused', standalone: true}) |
| 619 | + export class UnusedPipe {} |
| 620 | +
|
| 621 | + @Component({ |
| 622 | + template: '', |
| 623 | + standalone: true, |
| 624 | + imports: [UnusedDirective, UnusedPipe], |
| 625 | + }) |
| 626 | + export class AppComponent {} |
| 627 | + `, |
| 628 | + }; |
| 629 | + |
| 630 | + const project = createModuleAndProjectWithDeclarations(env, 'test', files); |
| 631 | + const fixFile = project.openFile('app.ts'); |
| 632 | + fixFile.moveCursorToText('impo¦rts:'); |
| 633 | + |
| 634 | + const diags = project.getDiagnosticsForFile('app.ts'); |
| 635 | + const codeActions = project.getCodeFixesAtPosition('app.ts', fixFile.cursor, fixFile.cursor, [ |
| 636 | + diags[0].code, |
| 637 | + ]); |
| 638 | + const actionChanges = allChangesForCodeActions(fixFile.contents, codeActions); |
| 639 | + |
| 640 | + actionChangesMatch(actionChanges, 'Remove all unused imports', [ |
| 641 | + ['[UnusedDirective, UnusedPipe]', '[]'], |
| 642 | + ]); |
| 643 | + }); |
| 644 | + |
| 645 | + it('should fix all imports arrays where some imports are not used', () => { |
577 | 646 | const files = { |
578 | 647 | 'app.ts': ` |
579 | 648 | import {Component, Directive, Pipe} from '@angular/core'; |
@@ -627,7 +696,7 @@ describe('code fixes', () => { |
627 | 696 | }); |
628 | 697 | }); |
629 | 698 |
|
630 | | - it('should fix imports array where all imports are not used', () => { |
| 699 | + it('should fix all imports arrays where all imports are not used', () => { |
631 | 700 | const files = { |
632 | 701 | 'app.ts': ` |
633 | 702 | import {Component, Directive, Pipe} from '@angular/core'; |
@@ -697,7 +766,7 @@ function allChangesForCodeActions( |
697 | 766 | for (const action of codeActions) { |
698 | 767 | const actionChanges = action.changes.flatMap((change) => { |
699 | 768 | return change.textChanges.map((tc) => { |
700 | | - const oldText = collapse(fileContents.slice(tc.span.start, tc.span.start + spawn.length)); |
| 769 | + const oldText = collapse(fileContents.slice(tc.span.start, tc.span.start + tc.span.length)); |
701 | 770 | const newText = collapse(tc.newText); |
702 | 771 | return [oldText, newText] as const; |
703 | 772 | }); |
|
0 commit comments