Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a symptom of not using updatePropertyDeclaration?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was using updatePropertyDeclaration earlier, but it had the same issue where it was duplicating the comments. The problem is that TS attaches the comments to the first node within the property declaration. E.g. if it has modifiers, it'll be on the first modifier, otherwise it's on the name.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. How will the comment be preserved if there are no modifiers, and the name is "cloned" (as a synthetic node)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment gets preserved because the ChangeTracker removes between Node.getStart() and Node.getEnd() which doesn't include the comment ranges.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. I was only seeing the getFullStart below.

property.questionToken,
property.type,
initializer,
Expand Down Expand Up @@ -526,6 +526,18 @@ function migrateInjectDecorator(
injectedType = arrowFn.body.getText();
}
}
} else if (
// Pass the type for cases like `@Inject(FOO_TOKEN) foo: Foo`, because:
// 1. It guarantees that the type stays the same as before.
// 2. Avoids leaving unused imports behind.
// We only do this for type references since the `@Inject` pattern above is fairly common and
// apps don't necessarily type their injection tokens correctly, whereas doing it for literal
// types will add a lot of noise to the generated code.
type &&
(ts.isTypeReferenceNode(type) ||
(ts.isUnionTypeNode(type) && type.types.some(ts.isTypeReferenceNode)))
) {
typeArguments = [type];
}

return {injectedType, typeArguments};
Expand Down Expand Up @@ -626,3 +638,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;
}
}
21 changes: 14 additions & 7 deletions packages/core/schematics/test/inject_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('inject migration', () => {
``,
`@Directive()`,
`class MyDir {`,
` private foo = inject(FOO_TOKEN);`,
` private foo = inject<Foo>(FOO_TOKEN);`,
`}`,
]);
});
Expand Down Expand Up @@ -271,7 +271,7 @@ describe('inject migration', () => {
` ]`,
`})`,
`class MyDir {`,
` private foo = inject(FOO_TOKEN, { optional: true });`,
` private foo = inject<Foo>(FOO_TOKEN, { optional: true });`,
`}`,
]);
});
Expand Down Expand Up @@ -1431,7 +1431,7 @@ describe('inject migration', () => {
`@Directive()`,
`class MyDir {`,
` private foo = inject(Foo);`,
` readonly bar = inject(BAR_TOKEN);`,
` readonly bar = inject<Bar>(BAR_TOKEN);`,
``,
` private value: number = this.foo.getValue();`,
` private otherValue: string = this.bar.getOtherValue();`,
Expand Down Expand Up @@ -1470,7 +1470,7 @@ describe('inject migration', () => {
`@Directive()`,
`class MyDir {`,
` private foo = inject(Foo);`,
` readonly bar = inject(BAR_TOKEN);`,
` readonly bar = inject<Bar>(BAR_TOKEN);`,
``,
` private value: number = this.foo.getValue();`,
` private otherValue: string;`,
Expand Down Expand Up @@ -1599,7 +1599,7 @@ describe('inject migration', () => {
`@Directive()`,
`class MyDir {`,
` private foo = inject(Foo);`,
` readonly bar = inject(BAR_TOKEN);`,
` readonly bar = inject<Bar>(BAR_TOKEN);`,
``,
` private 'my-value': number = this.foo.getValue();`,
` private 'my-other-value': string = this.bar.getOtherValue();`,
Expand Down 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