Skip to content

Commit 4ae66f2

Browse files
crisbetodylhunn
authored andcommitted
fix(migrations): account for members with doc strings and no modifiers (#57389)
Fixes that the migration was duplicating the doc strings of members that don't have modifiers. PR Close #57389
1 parent 7716da8 commit 4ae66f2

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export function migrateFile(sourceFile: ts.SourceFile, options: MigrationOptions
9595

9696
const newProperty = ts.factory.createPropertyDeclaration(
9797
cloneModifiers(property.modifiers),
98-
property.name,
98+
cloneName(property.name),
9999
property.questionToken,
100100
property.type,
101101
initializer,
@@ -626,3 +626,26 @@ function cloneModifiers(modifiers: ts.ModifierLike[] | ts.NodeArray<ts.ModifierL
626626
: ts.factory.createModifier(modifier.kind);
627627
});
628628
}
629+
630+
/**
631+
* Clones the name of a property. Can be useful to strip away
632+
* the comments of a property without modifiers.
633+
*/
634+
function cloneName(node: ts.PropertyName): ts.PropertyName {
635+
switch (node.kind) {
636+
case ts.SyntaxKind.Identifier:
637+
return ts.factory.createIdentifier(node.text);
638+
case ts.SyntaxKind.StringLiteral:
639+
return ts.factory.createStringLiteral(node.text, node.getText()[0] === `'`);
640+
case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
641+
return ts.factory.createNoSubstitutionTemplateLiteral(node.text, node.rawText);
642+
case ts.SyntaxKind.NumericLiteral:
643+
return ts.factory.createNumericLiteral(node.text);
644+
case ts.SyntaxKind.ComputedPropertyName:
645+
return ts.factory.createComputedPropertyName(node.expression);
646+
case ts.SyntaxKind.PrivateIdentifier:
647+
return ts.factory.createPrivateIdentifier(node.text);
648+
default:
649+
return node;
650+
}
651+
}

packages/core/schematics/test/inject_migration_spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,10 +1784,14 @@ describe('inject migration', () => {
17841784
`@Directive()`,
17851785
`class MyDir {`,
17861786
` /** Value of Foo */`,
1787-
` private value: number;`,
1787+
` private readonly value: number;`,
1788+
``,
1789+
` /** ID of Foo */`,
1790+
` id: string;`,
17881791
``,
17891792
` constructor(private foo: Foo) {`,
17901793
` this.value = this.foo.getValue();`,
1794+
` this.id = this.foo.getId();`,
17911795
` }`,
17921796
`}`,
17931797
].join('\n'),
@@ -1804,7 +1808,10 @@ describe('inject migration', () => {
18041808
` private foo = inject(Foo);`,
18051809
``,
18061810
` /** Value of Foo */`,
1807-
` private value: number = this.foo.getValue();`,
1811+
` private readonly value: number = this.foo.getValue();`,
1812+
``,
1813+
` /** ID of Foo */`,
1814+
` id: string = this.foo.getId();`,
18081815
`}`,
18091816
]);
18101817
});

0 commit comments

Comments
 (0)