Skip to content

Commit 36b9ff7

Browse files
crisbetoalxhub
authored andcommitted
fix(migrations): return correct alias when conflicting import exists (#49139)
Fixes that the `ImportManager` was returning the `propertyName` instead of the `name` when there's an import with a conflicting identifier. PR Close #49139
1 parent 9136050 commit 36b9ff7

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

packages/core/schematics/test/standalone_migration_spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,51 @@ describe('standalone migration', () => {
15261526
`));
15271527
});
15281528

1529+
it('should use the generated alias if a conflicting symbol already exists', async () => {
1530+
writeFile('module.ts', `
1531+
import {NgModule} from '@angular/core';
1532+
import {MyComp} from './comp';
1533+
import {MyButton} from './button';
1534+
1535+
@NgModule({declarations: [MyComp, MyButton], exports: [MyComp]})
1536+
export class Mod {}
1537+
`);
1538+
1539+
writeFile('comp.ts', `
1540+
import {Component} from '@angular/core';
1541+
import {MyButton} from '@external/button';
1542+
1543+
MyButton.sayHello();
1544+
1545+
@Component({selector: 'my-comp', template: '<my-button>Hello</my-button>'})
1546+
export class MyComp {}
1547+
`);
1548+
1549+
writeFile('button.ts', `
1550+
import {Component} from '@angular/core';
1551+
1552+
@Component({selector: 'my-button', template: '<ng-content></ng-content>'})
1553+
export class MyButton {}
1554+
`);
1555+
1556+
await runMigration('convert-to-standalone');
1557+
1558+
expect(stripWhitespace(tree.readContent('comp.ts'))).toBe(stripWhitespace(`
1559+
import {Component} from '@angular/core';
1560+
import {MyButton} from '@external/button';
1561+
import {MyButton as MyButton_1} from './button';
1562+
1563+
MyButton.sayHello();
1564+
1565+
@Component({
1566+
selector: 'my-comp', template: '<my-button>Hello</my-button>',
1567+
standalone: true,
1568+
imports: [MyButton_1]
1569+
})
1570+
export class MyComp {}
1571+
`));
1572+
});
1573+
15291574
it('should remove a module that only has imports and exports', async () => {
15301575
writeFile('app.module.ts', `
15311576
import {NgModule} from '@angular/core';

packages/core/schematics/utils/import_manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export class ImportManager {
170170
if (symbolName) {
171171
const {propertyName, name} = this._getImportParts(sourceFile, symbolName, alias);
172172
const importMap = this.newImports.get(sourceFile)!.namedImports;
173-
identifier = propertyName || name;
173+
identifier = name;
174174

175175
if (!importMap.has(moduleName)) {
176176
importMap.set(moduleName, []);

0 commit comments

Comments
 (0)