Skip to content

Commit

Permalink
fix(migrations): account for members with doc strings and no modifiers (
Browse files Browse the repository at this point in the history
angular#57389)

Fixes that the migration was duplicating the doc strings of members that don't have modifiers.

PR Close angular#57389
  • Loading branch information
crisbeto authored and dylhunn committed Aug 15, 2024
1 parent 7716da8 commit 4ae66f2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}
11 changes: 9 additions & 2 deletions packages/core/schematics/test/inject_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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();`,
`}`,
]);
});
Expand Down

0 comments on commit 4ae66f2

Please sign in to comment.