Skip to content

Commit d504452

Browse files
pkozlowski-opensourcealxhub
authored andcommitted
fix(migrations): properly replace imports across files (#58414)
This change fixes a bug where the output migration was interacting with the InputManager utility in the way that was resulting in incorrect import replacements. The fix consists of making sure that a new ImportManager instance is created for each and every file containing @output declarations. PR Close #58414
1 parent ddef41e commit d504452

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

packages/core/schematics/migrations/output-migration/output-migration.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,43 @@ describe('outputs', () => {
579579
expect(stats.counters['successRate']).toBe(1);
580580
});
581581
});
582+
583+
describe('non-regression', () => {
584+
it('should properly process import replacements across multiple files', async () => {
585+
const {fs} = await runTsurgeMigration(new OutputMigration(), [
586+
{
587+
name: absoluteFrom('/app.component.ts'),
588+
isProgramRootFile: true,
589+
contents: `
590+
import {Component, Output, EventEmitter} from '@angular/core';
591+
592+
@Component({selector: 'app-component'})
593+
export class AppComponent {
594+
@Output() appOut = new EventEmitter();
595+
}
596+
`,
597+
},
598+
{
599+
name: absoluteFrom('/other.component.ts'),
600+
isProgramRootFile: true,
601+
contents: `
602+
import {Component, Output, EventEmitter} from '@angular/core';
603+
604+
@Component({selector: 'other-component'})
605+
export class OtherComponent {
606+
@Output() otherOut = new EventEmitter();
607+
}
608+
`,
609+
},
610+
]);
611+
612+
for (const file of ['/app.component.ts', '/other.component.ts']) {
613+
const content = fs.readFile(absoluteFrom(file)).trim();
614+
const firstLine = content.split('\n')[0];
615+
expect(firstLine).toBe(`import {Component, output} from '@angular/core';`);
616+
}
617+
});
618+
});
582619
});
583620

584621
async function verifyDeclaration(testCase: {before: string; after: string}) {

packages/core/schematics/migrations/output-migration/output-replacements.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ export function calculateImportReplacements(info: ProgramInfo, sourceFiles: Set<
6868
{add: Replacement[]; addAndRemove: Replacement[]}
6969
> = {};
7070

71-
const importManager = new ImportManager();
72-
7371
for (const sf of sourceFiles) {
72+
const importManager = new ImportManager();
73+
7474
const addOnly: Replacement[] = [];
7575
const addRemove: Replacement[] = [];
7676
const file = projectFile(sf, info);

0 commit comments

Comments
 (0)