Skip to content

Commit 3fc14ec

Browse files
Ryan RussellthePunderWoman
authored andcommitted
fix(migrations): Mark hoisted properties as removed in inject migration (angular#58804)
Before, in a situation like shown in the test, all of the initializers would be dropped, as the code would try to insert it at the hoisted property which is going to be deleted. PR Close angular#58804
1 parent b46ac88 commit 3fc14ec

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

packages/core/schematics/ng-generate/inject-migration/migration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ function applyInternalOnlyChanges(
814814
memberIndentation + printer.printNode(ts.EmitHint.Unspecified, decl, decl.getSourceFile()),
815815
);
816816
tracker.removeNode(decl, true);
817+
removedMembers.add(decl);
817818
});
818819

819820
// If we added any hoisted properties, separate them visually with a new line.

packages/core/schematics/test/inject_migration_spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,6 +2122,45 @@ describe('inject migration', () => {
21222122
]);
21232123
});
21242124

2125+
it('should handle re-ordering when all fields are removed or hoisted', async () => {
2126+
writeFile(
2127+
'/dir.ts',
2128+
[
2129+
`import { Injectable } from '@angular/core';`,
2130+
`import { ActivatedRoute } from '@angular/router';`,
2131+
``,
2132+
`@Injectable()`,
2133+
`export class MyClass {`,
2134+
` uninitialized!: string;`,
2135+
``,
2136+
` readonly b;`,
2137+
` readonly a;`,
2138+
``,
2139+
` constructor(private readonly route: ActivatedRoute) {`,
2140+
` this.a = this.route.get();`,
2141+
` this.b = this.a.get();`,
2142+
` }`,
2143+
`}`,
2144+
].join('\n'),
2145+
);
2146+
2147+
await runInternalMigration();
2148+
2149+
expect(tree.readContent('/dir.ts').split('\n')).toEqual([
2150+
`import { Injectable, inject } from '@angular/core';`,
2151+
`import { ActivatedRoute } from '@angular/router';`,
2152+
``,
2153+
`@Injectable()`,
2154+
`export class MyClass {`,
2155+
` uninitialized!: string;`,
2156+
``,
2157+
` private readonly route = inject(ActivatedRoute);`,
2158+
` readonly a = this.route.get();`,
2159+
` readonly b = this.a.get();`,
2160+
`}`,
2161+
]);
2162+
});
2163+
21252164
it('should be able to insert statements after the `super` call when running in internal migration mode', async () => {
21262165
writeFile(
21272166
'/dir.ts',

0 commit comments

Comments
 (0)