Skip to content

Commit 32cf4e5

Browse files
crisbetoatscott
authored andcommitted
fix(migrations): avoid internal modules when generating imports (#48958)
Adds some logic to prefer non-Angular-internal modules when generating imports. This allows us to generate better code for some cases like the `ɵInternalFormsSharedModule` in Forms. Also adds some logic to prefer symbols that are already in the same file. Fixes #48942. PR Close #48958
1 parent 49a7c9f commit 32cf4e5

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

packages/core/schematics/ng-generate/standalone-migration/to-standalone.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ function findImportLocation(
310310
target: Reference<NamedClassDeclaration>, inComponent: Reference<ts.ClassDeclaration>,
311311
importMode: PotentialImportMode, typeChecker: TemplateTypeChecker): PotentialImport|null {
312312
const importLocations = typeChecker.getPotentialImportsFor(target, inComponent.node, importMode);
313+
let firstSameFileImport: PotentialImport|null = null;
313314
let firstModuleImport: PotentialImport|null = null;
314315

315316
for (const location of importLocations) {
@@ -318,12 +319,17 @@ function findImportLocation(
318319
if (location.kind === PotentialImportKind.Standalone) {
319320
return location;
320321
}
321-
if (location.kind === PotentialImportKind.NgModule && !firstModuleImport) {
322+
if (!location.moduleSpecifier && !firstSameFileImport) {
323+
firstSameFileImport = location;
324+
}
325+
if (location.kind === PotentialImportKind.NgModule && !firstModuleImport &&
326+
// ɵ is used for some internal Angular modules that we want to skip over.
327+
!location.symbolName.startsWith('ɵ')) {
322328
firstModuleImport = location;
323329
}
324330
}
325331

326-
return firstModuleImport;
332+
return firstSameFileImport || firstModuleImport || importLocations[0] || null;
327333
}
328334

329335
/**

packages/core/schematics/test/standalone_migration_spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,52 @@ describe('standalone migration', () => {
970970
expect(myCompContent).toContain('imports: [ButtonModule]');
971971
});
972972

973+
it('should not reference internal modules', async () => {
974+
writeFile('./should-migrate/module.ts', `
975+
import {NgModule} from '@angular/core';
976+
import {MyComp} from './comp';
977+
import {ɵButtonModule} from '../do-not-migrate/button.module';
978+
979+
@NgModule({imports: [ɵButtonModule], declarations: [MyComp]})
980+
export class Mod {}
981+
`);
982+
983+
writeFile('./should-migrate/comp.ts', `
984+
import {Component} from '@angular/core';
985+
986+
@Component({selector: 'my-comp', template: '<my-button>Hello</my-button>'})
987+
export class MyComp {}
988+
`);
989+
990+
writeFile('./do-not-migrate/button.module.ts', `
991+
import {NgModule, forwardRef} from '@angular/core';
992+
import {MyButton} from './button';
993+
994+
@NgModule({
995+
imports: [forwardRef(() => ɵButtonModule)],
996+
exports: [forwardRef(() => ɵButtonModule)]
997+
})
998+
export class ExporterModule {}
999+
1000+
@NgModule({declarations: [MyButton], exports: [MyButton]})
1001+
export class ɵButtonModule {}
1002+
`);
1003+
1004+
writeFile('./do-not-migrate/button.ts', `
1005+
import {Component} from '@angular/core';
1006+
1007+
@Component({selector: 'my-button', template: '<ng-content></ng-content>'})
1008+
export class MyButton {}
1009+
`);
1010+
1011+
await runMigration('convert-to-standalone', './should-migrate');
1012+
1013+
const myCompContent = tree.readContent('./should-migrate/comp.ts');
1014+
expect(myCompContent)
1015+
.toContain(`import { ExporterModule } from '../do-not-migrate/button.module';`);
1016+
expect(myCompContent).toContain('imports: [ExporterModule]');
1017+
});
1018+
9731019
it('should migrate tests with a component declared through TestBed', async () => {
9741020
writeFile('app.spec.ts', `
9751021
import {NgModule, Component} from '@angular/core';

0 commit comments

Comments
 (0)