Skip to content

Commit f01901d

Browse files
crisbetoatscott
authored andcommitted
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.
1 parent a6941ad commit f01901d

File tree

3 files changed

+5
-9
lines changed

3 files changed

+5
-9
lines changed

packages/core/schematics/migrations/change-detection-eager/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ts_project(
2222
"//packages/core/schematics/utils",
2323
"//packages/core/schematics/utils/tsurge",
2424
"//packages/core/schematics/utils/tsurge/helpers/angular_devkit",
25+
"//packages/core/schematics/utils/tsurge/helpers/ast",
2526
],
2627
)
2728

packages/core/schematics/migrations/change-detection-eager/migration.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe('ChangeDetectionEager migration', () => {
106106

107107
const content = fs.readFile(absoluteFrom('/index.ts'));
108108
expect(content).toMatch(
109-
/standalone: true,\n\s+changeDetection: ChangeDetectionStrategy\.Eager/,
109+
/template: '',\n\s+changeDetection: ChangeDetectionStrategy\.Eager,\n\s+standalone: true/,
110110
);
111111
});
112112

packages/core/schematics/migrations/change-detection-eager/migration.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
TsurgeFunnelMigration,
2020
} from '../../utils/tsurge';
2121
import {applyImportManagerChanges} from '../../utils/tsurge/helpers/apply_import_manager';
22+
import {getLeadingLineWhitespaceOfNode} from '../../utils/tsurge/helpers/ast/leading_space';
2223

2324
export interface ChangeDetectionEagerMigrationPhase1Data {
2425
replacements: Replacement[];
@@ -105,14 +106,8 @@ export class ChangeDetectionEagerMigration extends TsurgeFunnelMigration<
105106

106107
if (properties.length > 0) {
107108
const lastProp = properties[properties.length - 1];
108-
insertPos = lastProp.getEnd();
109-
110-
// Simpler approach: check comma after last property.
111-
const textAfter = sf.text.substring(lastProp.getEnd());
112-
const hasComma = /^\s*,/.test(textAfter);
113-
const prefix = hasComma ? '' : ',';
114-
115-
toInsert = `${prefix}\n changeDetection: ${exprText}.Eager`;
109+
insertPos = lastProp.getStart();
110+
toInsert = `changeDetection: ${exprText}.Eager,\n${getLeadingLineWhitespaceOfNode(lastProp)}`;
116111
} else {
117112
insertPos = metadata.getStart() + 1;
118113
toInsert = `\n changeDetection: ${exprText}.Eager\n`;

0 commit comments

Comments
 (0)