Skip to content

Commit 90c7ec3

Browse files
crisbetoalxhub
authored andcommitted
fix(migrations): inject migration always inserting generated variables before super call (#58393)
Fixes that if a class has a `super` call, the `inject` migration would always insert the generated variable before it, even if there's other code before the `super` call. PR Close #58393
1 parent eafe904 commit 90c7ec3

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ function migrateClass(
166166
? getSuperParameters(constructor, superCall, localTypeChecker)
167167
: null;
168168
const removedStatementCount = removedStatements.size;
169-
const innerReference =
170-
superCall ||
171-
constructor.body?.statements.find((statement) => !removedStatements.has(statement)) ||
172-
constructor;
169+
const firstConstructorStatement = constructor.body?.statements.find(
170+
(statement) => !removedStatements.has(statement),
171+
);
172+
const innerReference = superCall || firstConstructorStatement || constructor;
173173
const innerIndentation = getLeadingLineWhitespaceOfNode(innerReference);
174174
const prependToConstructor: string[] = [];
175175
const afterSuper: string[] = [];
@@ -217,7 +217,7 @@ function migrateClass(
217217
if (prependToConstructor.length > 0) {
218218
tracker.insertText(
219219
sourceFile,
220-
innerReference.getFullStart(),
220+
(firstConstructorStatement || innerReference).getFullStart(),
221221
`\n${prependToConstructor.join('\n')}\n`,
222222
);
223223
}

packages/core/schematics/test/inject_migration_spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,43 @@ describe('inject migration', () => {
14641464
]);
14651465
});
14661466

1467+
it('should insert generated variables on top of statements that appear before the `super` call', async () => {
1468+
writeFile(
1469+
'/dir.ts',
1470+
[
1471+
`import { Directive } from '@angular/core';`,
1472+
`import { Parent } from './parent';`,
1473+
`import { SomeService } from './service';`,
1474+
``,
1475+
`@Directive()`,
1476+
`class MyDir extends Parent {`,
1477+
` constructor(service: SomeService) {`,
1478+
` console.log(service.getId());`,
1479+
` super(service);`,
1480+
` }`,
1481+
`}`,
1482+
].join('\n'),
1483+
);
1484+
1485+
await runMigration();
1486+
1487+
expect(tree.readContent('/dir.ts').split('\n')).toEqual([
1488+
`import { Directive, inject } from '@angular/core';`,
1489+
`import { Parent } from './parent';`,
1490+
`import { SomeService } from './service';`,
1491+
``,
1492+
`@Directive()`,
1493+
`class MyDir extends Parent {`,
1494+
` constructor() {`,
1495+
` const service = inject(SomeService);`,
1496+
``,
1497+
` console.log(service.getId());`,
1498+
` super(service);`,
1499+
` }`,
1500+
`}`,
1501+
]);
1502+
});
1503+
14671504
describe('internal-only behavior', () => {
14681505
function runInternalMigration() {
14691506
return runMigration({_internalCombineMemberInitializers: true});

0 commit comments

Comments
 (0)