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`;