Skip to content

Commit 8f68d1b

Browse files
aparzithePunderWoman
authored andcommitted
fix(core): fix ng generate @angular/core:output-migration (angular#60626)
output-migration command not keep type if @output declaring without initializer PR Close angular#60626
1 parent 7cd89ad commit 8f68d1b

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

packages/core/schematics/migrations/output-migration/output-migration.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,28 @@ describe('outputs', () => {
3333
});
3434
});
3535

36+
it('should keep type without initializer', async () => {
37+
await verifyDeclaration({
38+
before: '@Output() eventMovement: EventEmitter<IResponse> = new EventEmitter();',
39+
after: 'readonly eventMovement = output<IResponse>();',
40+
});
41+
});
42+
43+
it('should keep type with initializer', async () => {
44+
await verifyDeclaration({
45+
before: '@Output() eventMovement: EventEmitter = new EventEmitter<IResponse>();',
46+
after: 'readonly eventMovement = output<IResponse>();',
47+
});
48+
});
49+
50+
it('should keep type without initializer and with alias', async () => {
51+
await verifyDeclaration({
52+
before:
53+
"@Output('customEvent') eventMovement: EventEmitter<IResponse> = new EventEmitter();",
54+
after: "readonly eventMovement = output<IResponse>({ alias: 'customEvent' });",
55+
});
56+
});
57+
3658
it('should migrate declaration without type hint', async () => {
3759
await verifyDeclaration({
3860
before: '@Output() readonly someChange = new EventEmitter();',

packages/core/schematics/migrations/output-migration/output-replacements.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ export function calculateDeclarationReplacement(
2828
aliasParam?: string,
2929
): Replacement {
3030
const sf = node.getSourceFile();
31-
const payloadTypes =
32-
node.initializer !== undefined && ts.isNewExpression(node.initializer)
33-
? node.initializer?.typeArguments
34-
: undefined;
31+
32+
let payloadTypes: ts.NodeArray<ts.TypeNode> | undefined;
33+
34+
if (node.initializer && ts.isNewExpression(node.initializer) && node.initializer.typeArguments) {
35+
payloadTypes = node.initializer.typeArguments;
36+
} else if (node.type && ts.isTypeReferenceNode(node.type) && node.type.typeArguments) {
37+
payloadTypes = ts.factory.createNodeArray(node.type.typeArguments);
38+
}
3539

3640
const outputCall = ts.factory.createCallExpression(
3741
ts.factory.createIdentifier('output'),

0 commit comments

Comments
 (0)