Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions packages/core/schematics/ng-generate/inject-migration/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,7 @@ function migrateClass(
}
}

if (
!options.backwardsCompatibleConstructors &&
(!constructor.body || constructor.body.statements.length - removedStatementCount === 0)
) {
if (canRemoveConstructor(options, constructor, removedStatementCount, superCall)) {
// Drop the constructor if it was empty.
removedMembers.add(constructor);
tracker.replaceText(sourceFile, constructor.getFullStart(), constructor.getFullWidth(), '');
Expand Down Expand Up @@ -661,3 +658,30 @@ function cloneName(node: ts.PropertyName): ts.PropertyName {
return node;
}
}

/**
* Determines whether it's safe to delete a class constructor.
* @param options Options used to configure the migration.
* @param constructor Node representing the constructor.
* @param removedStatementCount Number of statements that were removed by the migration.
* @param superCall Node representing the `super()` call within the constructor.
*/
function canRemoveConstructor(
options: MigrationOptions,
constructor: ts.ConstructorDeclaration,
removedStatementCount: number,
superCall: ts.CallExpression | null,
): boolean {
if (options.backwardsCompatibleConstructors) {
return false;
}

const statementCount = constructor.body
? constructor.body.statements.length - removedStatementCount
: 0;

return (
statementCount === 0 ||
(statementCount === 1 && superCall !== null && superCall.arguments.length === 0)
);
}
69 changes: 69 additions & 0 deletions packages/core/schematics/test/inject_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,37 @@ describe('inject migration', () => {
]);
});

it('should remove the constructor if it only has a super() call after the migration', async () => {
writeFile(
'/dir.ts',
[
`import { Directive } from '@angular/core';`,
`import { Parent } from './parent';`,
`import { SomeService } from './service';`,
``,
`@Directive()`,
`class MyDir extends Parent {`,
` constructor(private service: SomeService) {`,
` super();`,
` }`,
`}`,
].join('\n'),
);

await runMigration();

expect(tree.readContent('/dir.ts').split('\n')).toEqual([
`import { Directive, inject } from '@angular/core';`,
`import { Parent } from './parent';`,
`import { SomeService } from './service';`,
``,
`@Directive()`,
`class MyDir extends Parent {`,
` private service = inject(SomeService);`,
`}`,
]);
});

it('should be able to opt into generating backwards-compatible constructors for a class with existing members', async () => {
writeFile(
'/dir.ts',
Expand Down Expand Up @@ -987,6 +1018,44 @@ describe('inject migration', () => {
]);
});

it('should not remove the constructor, even if it only has a super call, if backwards compatible constructors are enabled', async () => {
writeFile(
'/dir.ts',
[
`import { Directive } from '@angular/core';`,
`import { Parent } from './parent';`,
`import { SomeService } from './service';`,
``,
`@Directive()`,
`class MyDir extends Parent {`,
` constructor(private service: SomeService) {`,
` super();`,
` }`,
`}`,
].join('\n'),
);

await runMigration({backwardsCompatibleConstructors: true});

expect(tree.readContent('/dir.ts').split('\n')).toEqual([
`import { Directive, inject } from '@angular/core';`,
`import { Parent } from './parent';`,
`import { SomeService } from './service';`,
``,
`@Directive()`,
`class MyDir extends Parent {`,
` private service = inject(SomeService);`,
``,
` /** Inserted by Angular inject() migration for backwards compatibility */`,
` constructor(...args: unknown[]);`,
``,
` constructor() {`,
` super();`,
` }`,
`}`,
]);
});

it('should not migrate abstract classes by default', async () => {
const initialContent = [
`import { Directive } from '@angular/core';`,
Expand Down