Skip to content

Commit

Permalink
fix(migrations): Prevent a component from importing itself. (angular#…
Browse files Browse the repository at this point in the history
…50554)

This commit fixes the migrations for recursive components.

fixes angular#50525

PR Close angular#50554
  • Loading branch information
JeanMeche authored and ChellappanRajan committed Jan 23, 2024
1 parent 2a4529c commit 33c9d8e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/compiler-cli/src/ngtsc/typecheck/src/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,11 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker {
}
const emitted = emittedRef.expression;
if (emitted instanceof WrappedNodeExpr) {
if (refTo.node === inContext) {
// Suppress self-imports since components do not have to import themselves.
return null;
}

let isForwardReference = false;
if (emitted.node.getStart() > inContext.getStart()) {
const declaration = this.programDriver.getProgram()
Expand Down
33 changes: 33 additions & 0 deletions packages/core/schematics/test/standalone_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,39 @@ describe('standalone migration', () => {
`));
});

it('should not generate a forwardRef for a self import', async () => {
writeFile('decls.ts', `
import {Component} from '@angular/core';
@Component({
selector: 'comp',
template: '<comp/>'
})
export class MyComp {}
`);

writeFile('module.ts', `
import {NgModule} from '@angular/core';
import {MyComp} from './decls';
@NgModule({declarations: [MyComp]})
export class Mod {}
`);

await runMigration('convert-to-standalone');

expect(stripWhitespace(tree.readContent('decls.ts'))).toEqual(stripWhitespace(`
import {Component} from '@angular/core';
@Component({
selector: 'comp',
template: '<comp/>',
standalone: true
})
export class MyComp {}
`));
});

it('should not generate a forwardRef when adding an imported module dependency', async () => {
writeFile('./comp.ts', `
import {Component, NgModule} from '@angular/core';
Expand Down

0 comments on commit 33c9d8e

Please sign in to comment.