Skip to content

Commit 8468df1

Browse files
JeanMechepkozlowski-opensource
authored andcommitted
fix(migrations): Prevent a component from importing itself. (#50554)
This commit fixes the migrations for recursive components. fixes #50525 PR Close #50554
1 parent 5dc7a99 commit 8468df1

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

packages/compiler-cli/src/ngtsc/typecheck/src/checker.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,11 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker {
719719
}
720720
const emitted = emittedRef.expression;
721721
if (emitted instanceof WrappedNodeExpr) {
722+
if (refTo.node === inContext) {
723+
// Suppress self-imports since components do not have to import themselves.
724+
return null;
725+
}
726+
722727
let isForwardReference = false;
723728
if (emitted.node.getStart() > inContext.getStart()) {
724729
const declaration = this.programDriver.getProgram()

packages/core/schematics/test/standalone_migration_spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,39 @@ describe('standalone migration', () => {
15501550
`));
15511551
});
15521552

1553+
it('should not generate a forwardRef for a self import', async () => {
1554+
writeFile('decls.ts', `
1555+
import {Component} from '@angular/core';
1556+
1557+
@Component({
1558+
selector: 'comp',
1559+
template: '<comp/>'
1560+
})
1561+
export class MyComp {}
1562+
`);
1563+
1564+
writeFile('module.ts', `
1565+
import {NgModule} from '@angular/core';
1566+
import {MyComp} from './decls';
1567+
1568+
@NgModule({declarations: [MyComp]})
1569+
export class Mod {}
1570+
`);
1571+
1572+
await runMigration('convert-to-standalone');
1573+
1574+
expect(stripWhitespace(tree.readContent('decls.ts'))).toEqual(stripWhitespace(`
1575+
import {Component} from '@angular/core';
1576+
1577+
@Component({
1578+
selector: 'comp',
1579+
template: '<comp/>',
1580+
standalone: true
1581+
})
1582+
export class MyComp {}
1583+
`));
1584+
});
1585+
15531586
it('should not generate a forwardRef when adding an imported module dependency', async () => {
15541587
writeFile('./comp.ts', `
15551588
import {Component, NgModule} from '@angular/core';

0 commit comments

Comments
 (0)