Skip to content

Commit b1a9d0f

Browse files
crisbetoAndrewKushnir
authored andcommitted
fix(migrations): avoid duplicating comments when generating properties (#57367)
Updates the logic that generates new component properties to avoid duplicating their doc strings. PR Close #57367
1 parent 5d76401 commit b1a9d0f

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

packages/core/schematics/ng-generate/inject-migration/migration.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,8 @@ export function migrateFile(sourceFile: ts.SourceFile, options: MigrationOptions
9393
return;
9494
}
9595

96-
const newProperty = ts.factory.updatePropertyDeclaration(
97-
property,
98-
property.modifiers,
96+
const newProperty = ts.factory.createPropertyDeclaration(
97+
cloneModifiers(property.modifiers),
9998
property.name,
10099
property.questionToken,
101100
property.type,

packages/core/schematics/test/inject_migration_spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,5 +1773,40 @@ describe('inject migration', () => {
17731773
`}`,
17741774
]);
17751775
});
1776+
1777+
it('should account for doc strings when inlining initializers', async () => {
1778+
writeFile(
1779+
'/dir.ts',
1780+
[
1781+
`import { Directive } from '@angular/core';`,
1782+
`import { Foo } from 'foo';`,
1783+
``,
1784+
`@Directive()`,
1785+
`class MyDir {`,
1786+
` /** Value of Foo */`,
1787+
` private value: number;`,
1788+
``,
1789+
` constructor(private foo: Foo) {`,
1790+
` this.value = this.foo.getValue();`,
1791+
` }`,
1792+
`}`,
1793+
].join('\n'),
1794+
);
1795+
1796+
await runInternalMigration();
1797+
1798+
expect(tree.readContent('/dir.ts').split('\n')).toEqual([
1799+
`import { Directive, inject } from '@angular/core';`,
1800+
`import { Foo } from 'foo';`,
1801+
``,
1802+
`@Directive()`,
1803+
`class MyDir {`,
1804+
` private foo = inject(Foo);`,
1805+
``,
1806+
` /** Value of Foo */`,
1807+
` private value: number = this.foo.getValue();`,
1808+
`}`,
1809+
]);
1810+
});
17761811
});
17771812
});

0 commit comments

Comments
 (0)