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
This commit is contained in:
Kristiyan Kostadinov
2024-08-14 06:22:36 +02:00
committed by Dylan Hunn
parent 7716da8ce9
commit 4ae66f25d0
2 changed files with 33 additions and 3 deletions
@@ -95,7 +95,7 @@ export function migrateFile(sourceFile: ts.SourceFile, options: MigrationOptions
const newProperty = ts.factory.createPropertyDeclaration(
cloneModifiers(property.modifiers),
property.name,
cloneName(property.name),
property.questionToken,
property.type,
initializer,
@@ -626,3 +626,26 @@ function cloneModifiers(modifiers: ts.ModifierLike[] | ts.NodeArray<ts.ModifierL
: ts.factory.createModifier(modifier.kind);
});
}
/**
* Clones the name of a property. Can be useful to strip away
* the comments of a property without modifiers.
*/
function cloneName(node: ts.PropertyName): ts.PropertyName {
switch (node.kind) {
case ts.SyntaxKind.Identifier:
return ts.factory.createIdentifier(node.text);
case ts.SyntaxKind.StringLiteral:
return ts.factory.createStringLiteral(node.text, node.getText()[0] === `'`);
case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
return ts.factory.createNoSubstitutionTemplateLiteral(node.text, node.rawText);
case ts.SyntaxKind.NumericLiteral:
return ts.factory.createNumericLiteral(node.text);
case ts.SyntaxKind.ComputedPropertyName:
return ts.factory.createComputedPropertyName(node.expression);
case ts.SyntaxKind.PrivateIdentifier:
return ts.factory.createPrivateIdentifier(node.text);
default:
return node;
}
}
@@ -1784,10 +1784,14 @@ describe('inject migration', () => {
`@Directive()`,
`class MyDir {`,
` /** Value of Foo */`,
` private value: number;`,
` private readonly value: number;`,
``,
` /** ID of Foo */`,
` id: string;`,
``,
` constructor(private foo: Foo) {`,
` this.value = this.foo.getValue();`,
` this.id = this.foo.getId();`,
` }`,
`}`,
].join('\n'),
@@ -1804,7 +1808,10 @@ describe('inject migration', () => {
` private foo = inject(Foo);`,
``,
` /** Value of Foo */`,
` private value: number = this.foo.getValue();`,
` private readonly value: number = this.foo.getValue();`,
``,
` /** ID of Foo */`,
` id: string = this.foo.getId();`,
`}`,
]);
});