From 792edaba489df47ba9a498ba9a5de3cecafbf750 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Sun, 21 Jun 2026 20:38:16 +0200 Subject: [PATCH] refactor(migrations): account for inheritance in service migration Updates the `@Service` migration to account for inheritance when determining if a class can be migrated. --- .../service-migration/migration.ts | 37 +++-- .../schematics/test/service_migration_spec.ts | 132 +++++++++++++++--- 2 files changed, 143 insertions(+), 26 deletions(-) diff --git a/packages/core/schematics/ng-generate/service-migration/migration.ts b/packages/core/schematics/ng-generate/service-migration/migration.ts index 7364b9bd74a..b1f0b467594 100644 --- a/packages/core/schematics/ng-generate/service-migration/migration.ts +++ b/packages/core/schematics/ng-generate/service-migration/migration.ts @@ -9,14 +9,13 @@ import ts from 'typescript'; import {ChangeTracker, PendingChange} from '../../utils/change_tracker'; import {getAngularDecorators} from '../../utils/ng_decorators'; +import {findBaseClassDeclarations} from '../../utils/typescript/find_base_classes'; export function migrateFile( sourceFile: ts.SourceFile, typeChecker: ts.TypeChecker, ): PendingChange[] { - const printer = ts.createPrinter(); - const tracker = new ChangeTracker(printer); - + const tracker = new ChangeTracker(ts.createPrinter()); let totalInjectables = 0; let migratedInjectables = 0; @@ -27,7 +26,7 @@ export function migrateFile( ); if (decorator) { - const analysis = analyzeClass(node, decorator.node.expression.arguments); + const analysis = analyzeClass(node, decorator.node.expression.arguments, typeChecker); totalInjectables++; if (analysis.canMigrate) { @@ -52,18 +51,18 @@ export function migrateFile( return tracker.recordChanges().get(sourceFile) || []; } -function analyzeClass(node: ts.ClassDeclaration, decoratorArgs: readonly ts.Expression[]) { +function analyzeClass( + node: ts.ClassDeclaration, + decoratorArgs: readonly ts.Expression[], + typeChecker: ts.TypeChecker, +) { const analysis = { canMigrate: false, providedInRoot: false, }; - const constructorNode = node.members.find((member): member is ts.ConstructorDeclaration => { - return ts.isConstructorDeclaration(member) && !!member.body; - }); - // We can't migrate if the class is using constructor DI. - if (constructorNode && constructorNode.parameters.length > 0) { + if (usesConstructorDI(node, typeChecker)) { return analysis; } else if (decoratorArgs.length === 0) { analysis.canMigrate = true; @@ -97,3 +96,21 @@ function analyzeClass(node: ts.ClassDeclaration, decoratorArgs: readonly ts.Expr analysis.canMigrate = true; return analysis; } + +/** Determines whether a class uses constructor-based dependency injection. */ +function usesConstructorDI(node: ts.ClassDeclaration, typeChecker: ts.TypeChecker): boolean { + const baseClasses = [node, ...findBaseClassDeclarations(node, typeChecker).map((c) => c.node)]; + + for (const current of baseClasses) { + const constructorNode = current.members.find( + (member): member is ts.ConstructorDeclaration => + ts.isConstructorDeclaration(member) && !!member.body, + ); + + if (constructorNode) { + return constructorNode.parameters.length > 0; + } + } + + return false; +} diff --git a/packages/core/schematics/test/service_migration_spec.ts b/packages/core/schematics/test/service_migration_spec.ts index c5c762498e2..3435a5c90e0 100644 --- a/packages/core/schematics/test/service_migration_spec.ts +++ b/packages/core/schematics/test/service_migration_spec.ts @@ -55,7 +55,7 @@ describe('service migration schematic', () => { it('should convert root injectable to @Service', async () => { writeFile( - 'comp.ts', + 'service.ts', ` import {Injectable} from '@angular/core'; @@ -65,7 +65,7 @@ describe('service migration schematic', () => { ); await runMigration(); - const contents = tree.readContent('comp.ts'); + const contents = tree.readContent('service.ts'); expect(contents).toContain('@Service()'); expect(contents).not.toContain('@Injectable'); @@ -75,7 +75,7 @@ describe('service migration schematic', () => { it('should convert non-root injectable to @Service with autoProvided: false', async () => { writeFile( - 'comp.ts', + 'service.ts', ` import {Injectable} from '@angular/core'; @@ -85,7 +85,7 @@ describe('service migration schematic', () => { ); await runMigration(); - const contents = tree.readContent('comp.ts'); + const contents = tree.readContent('service.ts'); expect(contents).toContain('@Service({ autoProvided: false })'); expect(contents).not.toContain('@Injectable'); @@ -100,10 +100,10 @@ describe('service migration schematic', () => { @Injectable({providedIn: 'platform'}) export class MyService {} `; - writeFile('comp.ts', initialContent); + writeFile('service.ts', initialContent); await runMigration(); - const contents = tree.readContent('comp.ts'); + const contents = tree.readContent('service.ts'); expect(contents).toContain(`providedIn: 'platform'`); expect(contents).toContain('@Injectable'); @@ -119,10 +119,10 @@ describe('service migration schematic', () => { @Injectable({providedIn: 'root', useClass: OtherService}) export class MyService {} `; - writeFile('comp.ts', initialContent); + writeFile('service.ts', initialContent); await runMigration(); - const contents = tree.readContent('comp.ts'); + const contents = tree.readContent('service.ts'); expect(contents).toContain(`useClass:`); expect(contents).toContain('@Injectable'); @@ -130,27 +130,127 @@ describe('service migration schematic', () => { }); it('should skip injectable if the class is using constructor DI', async () => { - const initialContent = ` + writeFile( + 'service.ts', + ` import {Injectable, NgZone} from '@angular/core'; @Injectable({providedIn: 'root'}) export class MyService { constructor(private ngZone: NgZone) {} } - `; - writeFile('comp.ts', initialContent); + `, + ); await runMigration(); - const contents = tree.readContent('comp.ts'); + const contents = tree.readContent('service.ts'); expect(contents).toContain(`providedIn: 'root'`); expect(contents).toContain('@Injectable'); expect(contents).not.toContain('@Service'); }); + it('should skip injectable if a parent class is using constructor DI', async () => { + writeFile( + 'grandparent.ts', + ` + import {Injectable, NgZone} from '@angular/core'; + + @Injectable({providedIn: 'root'}) + export class Grandparent { + constructor(private ngZone: NgZone) {} + } + `, + ); + + writeFile( + 'parent.ts', + ` + import {Injectable} from '@angular/core'; + import {Grandparent} from './grandparent'; + + @Injectable({providedIn: 'root'}) + export class Parent extends Grandparent {} + `, + ); + + writeFile( + 'service.ts', + ` + import {Injectable} from '@angular/core'; + import {Parent} from './parent'; + + @Injectable({providedIn: 'root'}) + export class MyService extends Parent {} + `, + ); + + await runMigration(); + + for (const file of ['grandparent.ts', 'parent.ts', 'service.ts']) { + const contents = tree.readContent(file); + expect(contents).withContext(file).toContain(`providedIn: 'root'`); + expect(contents).withContext(file).toContain('@Injectable'); + expect(contents).withContext(file).not.toContain('@Service'); + } + }); + + it('should account for parent class that overrides a grandparent using constructor DI', async () => { + writeFile( + 'grandparent.ts', + ` + import {Injectable, NgZone} from '@angular/core'; + + @Injectable({providedIn: 'root'}) + export class Grandparent { + constructor(private ngZone: NgZone) {} + } + `, + ); + + writeFile( + 'parent.ts', + ` + import {Injectable, inject, NgZone} from '@angular/core'; + import {Grandparent} from './grandparent'; + + @Injectable({providedIn: 'root'}) + export class Parent extends Grandparent { + constructor() { + super(inject(NgZone)); + } + } + `, + ); + + writeFile( + 'service.ts', + ` + import {Injectable} from '@angular/core'; + import {Parent} from './parent'; + + @Injectable({providedIn: 'root'}) + export class MyService extends Parent {} + `, + ); + + await runMigration(); + + const grandparentContents = tree.readContent('grandparent.ts'); + expect(grandparentContents).toContain(`providedIn: 'root'`); + expect(grandparentContents).toContain('@Injectable'); + expect(grandparentContents).not.toContain('@Service'); + + for (const file of ['parent.ts', 'service.ts']) { + const contents = tree.readContent(file); + expect(contents).withContext(file).toContain('@Service()'); + expect(contents).withContext(file).not.toContain('Injectable'); + } + }); + it('should not remove Injectable import if there are skipped injectables left in the file', async () => { writeFile( - 'comp.ts', + 'service.ts', ` import {Injectable} from '@angular/core'; @@ -163,7 +263,7 @@ describe('service migration schematic', () => { ); await runMigration(); - const contents = tree.readContent('comp.ts'); + const contents = tree.readContent('service.ts'); expect(contents).toContain('@Service()'); expect(contents).toContain(`providedIn: 'platform'`); @@ -172,7 +272,7 @@ describe('service migration schematic', () => { it('should convert injectable if `providedIn` is quoted', async () => { writeFile( - 'comp.ts', + 'service.ts', ` import {Injectable} from '@angular/core'; @@ -182,7 +282,7 @@ describe('service migration schematic', () => { ); await runMigration(); - const contents = tree.readContent('comp.ts'); + const contents = tree.readContent('service.ts'); expect(contents).toContain('@Service()'); expect(contents).not.toContain('@Injectable');