fix(core): fix ng generate @angular/core:output-migration (#60626)

output-migration command not keep type if @output declaring without initializer

PR Close #60626
This commit is contained in:
aparziale
2025-03-31 19:31:34 +02:00
committed by Jessica Janiuk
parent 4546d4fb39
commit cdbc6e8ec1
2 changed files with 30 additions and 4 deletions
@@ -33,6 +33,28 @@ describe('outputs', () => {
});
});
it('should keep type without initializer', async () => {
await verifyDeclaration({
before: '@Output() eventMovement: EventEmitter<IResponse> = new EventEmitter();',
after: 'readonly eventMovement = output<IResponse>();',
});
});
it('should keep type with initializer', async () => {
await verifyDeclaration({
before: '@Output() eventMovement: EventEmitter = new EventEmitter<IResponse>();',
after: 'readonly eventMovement = output<IResponse>();',
});
});
it('should keep type without initializer and with alias', async () => {
await verifyDeclaration({
before:
"@Output('customEvent') eventMovement: EventEmitter<IResponse> = new EventEmitter();",
after: "readonly eventMovement = output<IResponse>({ alias: 'customEvent' });",
});
});
it('should migrate declaration without type hint', async () => {
await verifyDeclaration({
before: '@Output() readonly someChange = new EventEmitter();',
@@ -28,10 +28,14 @@ export function calculateDeclarationReplacement(
aliasParam?: string,
): Replacement {
const sf = node.getSourceFile();
const payloadTypes =
node.initializer !== undefined && ts.isNewExpression(node.initializer)
? node.initializer?.typeArguments
: undefined;
let payloadTypes: ts.NodeArray<ts.TypeNode> | undefined;
if (node.initializer && ts.isNewExpression(node.initializer) && node.initializer.typeArguments) {
payloadTypes = node.initializer.typeArguments;
} else if (node.type && ts.isTypeReferenceNode(node.type) && node.type.typeArguments) {
payloadTypes = ts.factory.createNodeArray(node.type.typeArguments);
}
const outputCall = ts.factory.createCallExpression(
ts.factory.createIdentifier('output'),