From f01901d7668ab926bd7a786f43dbb18f2bb8a5b7 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Thu, 5 Mar 2026 12:35:52 +0100 Subject: [PATCH] fix(migrations): avoid generating invalid code in ChangeDetectionStrategy.Eager migration Currently the migration that add `ChangeDetectionStrategy.Eager` to components tries to add the properties as last in the object literal. This can be tricky, because TS doesn't reflect the commas in the AST so we need have to do brittle string lookups to know where to insert it. These changes switch to inserting the property before the last pre-existing property which should be a bit more robust. --- .../migrations/change-detection-eager/BUILD.bazel | 1 + .../change-detection-eager/migration.spec.ts | 2 +- .../migrations/change-detection-eager/migration.ts | 11 +++-------- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/packages/core/schematics/migrations/change-detection-eager/BUILD.bazel b/packages/core/schematics/migrations/change-detection-eager/BUILD.bazel index b3c37574d1f..4ce45be4809 100644 --- a/packages/core/schematics/migrations/change-detection-eager/BUILD.bazel +++ b/packages/core/schematics/migrations/change-detection-eager/BUILD.bazel @@ -22,6 +22,7 @@ ts_project( "//packages/core/schematics/utils", "//packages/core/schematics/utils/tsurge", "//packages/core/schematics/utils/tsurge/helpers/angular_devkit", + "//packages/core/schematics/utils/tsurge/helpers/ast", ], ) diff --git a/packages/core/schematics/migrations/change-detection-eager/migration.spec.ts b/packages/core/schematics/migrations/change-detection-eager/migration.spec.ts index ad962a98aa5..3f23b935f3d 100644 --- a/packages/core/schematics/migrations/change-detection-eager/migration.spec.ts +++ b/packages/core/schematics/migrations/change-detection-eager/migration.spec.ts @@ -106,7 +106,7 @@ describe('ChangeDetectionEager migration', () => { const content = fs.readFile(absoluteFrom('/index.ts')); expect(content).toMatch( - /standalone: true,\n\s+changeDetection: ChangeDetectionStrategy\.Eager/, + /template: '',\n\s+changeDetection: ChangeDetectionStrategy\.Eager,\n\s+standalone: true/, ); }); diff --git a/packages/core/schematics/migrations/change-detection-eager/migration.ts b/packages/core/schematics/migrations/change-detection-eager/migration.ts index 7130085fb0c..ddf72dfd94b 100644 --- a/packages/core/schematics/migrations/change-detection-eager/migration.ts +++ b/packages/core/schematics/migrations/change-detection-eager/migration.ts @@ -19,6 +19,7 @@ import { TsurgeFunnelMigration, } from '../../utils/tsurge'; import {applyImportManagerChanges} from '../../utils/tsurge/helpers/apply_import_manager'; +import {getLeadingLineWhitespaceOfNode} from '../../utils/tsurge/helpers/ast/leading_space'; export interface ChangeDetectionEagerMigrationPhase1Data { replacements: Replacement[]; @@ -105,14 +106,8 @@ export class ChangeDetectionEagerMigration extends TsurgeFunnelMigration< if (properties.length > 0) { const lastProp = properties[properties.length - 1]; - insertPos = lastProp.getEnd(); - - // Simpler approach: check comma after last property. - const textAfter = sf.text.substring(lastProp.getEnd()); - const hasComma = /^\s*,/.test(textAfter); - const prefix = hasComma ? '' : ','; - - toInsert = `${prefix}\n changeDetection: ${exprText}.Eager`; + insertPos = lastProp.getStart(); + toInsert = `changeDetection: ${exprText}.Eager,\n${getLeadingLineWhitespaceOfNode(lastProp)}`; } else { insertPos = metadata.getStart() + 1; toInsert = `\n changeDetection: ${exprText}.Eager\n`;