Skip to content

Commit 4392cce

Browse files
crisbetopkozlowski-opensource
authored andcommitted
fix(migrations): inject migration dropping code if everything except super is removed (#58959)
Fixes that in some cases the internal version of the migration was dropping code when all the statements after the `super` call are deleted. PR Close #58959
1 parent e17ff71 commit 4392cce

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ function migrateClass(
230230
const nextStatement = getNextPreservedStatement(superCall, removedStatements);
231231
tracker.insertText(
232232
sourceFile,
233-
nextStatement ? nextStatement.getFullStart() : superCall.getEnd() + 1,
234-
`\n${afterSuper.join('\n')}\n`,
233+
nextStatement ? nextStatement.getFullStart() : constructor.getEnd() - 1,
234+
`\n${afterSuper.join('\n')}\n` + (nextStatement ? '' : memberIndentation),
235235
);
236236
}
237237

packages/core/schematics/test/inject_migration_spec.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1714,8 +1714,8 @@ describe('inject migration', () => {
17141714
` const foo = inject(Foo, { optional: true }) ?? createFoo(bar);`,
17151715
``,
17161716
` super(foo);`,
1717+
` `,
17171718
` this.foo = foo;`,
1718-
``,
17191719
` }`,
17201720
`}`,
17211721
]);
@@ -2579,5 +2579,49 @@ describe('inject migration', () => {
25792579
`}`,
25802580
]);
25812581
});
2582+
2583+
it('should be able to insert statements after the `super` call when all subsequent statements have been deleted', async () => {
2584+
writeFile(
2585+
'/dir.ts',
2586+
[
2587+
`import { Directive } from '@angular/core';`,
2588+
`import { Foo } from 'deps';`,
2589+
`import { Parent } from './parent';`,
2590+
``,
2591+
`@Directive()`,
2592+
`class MyDir extends Parent {`,
2593+
` private value: number;`,
2594+
``,
2595+
` constructor(private foo: Foo) {`,
2596+
` super(foo, bar);`,
2597+
` this.value = this.foo.getValue();`,
2598+
` }`,
2599+
`}`,
2600+
].join('\n'),
2601+
);
2602+
2603+
await runInternalMigration();
2604+
2605+
expect(tree.readContent('/dir.ts').split('\n')).toEqual([
2606+
`import { Directive, inject } from '@angular/core';`,
2607+
`import { Foo } from 'deps';`,
2608+
`import { Parent } from './parent';`,
2609+
``,
2610+
`@Directive()`,
2611+
`class MyDir extends Parent {`,
2612+
` private foo: Foo;`,
2613+
``,
2614+
` private value: number = this.foo.getValue();`,
2615+
``,
2616+
` constructor() {`,
2617+
` const foo = inject(Foo);`,
2618+
``,
2619+
` super(foo, bar);`,
2620+
` `,
2621+
` this.foo = foo;`,
2622+
` }`,
2623+
`}`,
2624+
]);
2625+
});
25822626
});
25832627
});

0 commit comments

Comments
 (0)