|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import ts from 'typescript'; |
| 10 | + |
| 11 | +import {isAngularDecorator, tryParseSignalModelMapping} from '../../../ngtsc/annotations'; |
| 12 | +import {ImportManager} from '../../../ngtsc/translator'; |
| 13 | + |
| 14 | +import {PropertyTransform} from './transform_api'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Transform that automatically adds `@Input` and `@Output` to members initialized as `model()`. |
| 18 | + * It is useful for JIT environments where models can't be recognized based on the initializer. |
| 19 | + */ |
| 20 | +export const signalModelTransform: PropertyTransform = ( |
| 21 | + member, |
| 22 | + host, |
| 23 | + factory, |
| 24 | + importManager, |
| 25 | + decorator, |
| 26 | + isCore, |
| 27 | + ) => { |
| 28 | + if (host.getDecoratorsOfDeclaration(member)?.some(d => { |
| 29 | + return isAngularDecorator(d, 'Input', isCore) || isAngularDecorator(d, 'Output', isCore); |
| 30 | + })) { |
| 31 | + return member; |
| 32 | + } |
| 33 | + |
| 34 | + const modelMapping = tryParseSignalModelMapping( |
| 35 | + {name: member.name.text, value: member.initializer ?? null}, |
| 36 | + host, |
| 37 | + isCore, |
| 38 | + ); |
| 39 | + |
| 40 | + if (modelMapping === null) { |
| 41 | + return member; |
| 42 | + } |
| 43 | + |
| 44 | + const classDecoratorIdentifier = ts.isIdentifier(decorator.identifier) ? |
| 45 | + decorator.identifier : |
| 46 | + decorator.identifier.expression; |
| 47 | + |
| 48 | + const inputConfig = factory.createObjectLiteralExpression([ |
| 49 | + factory.createPropertyAssignment( |
| 50 | + 'isSignal', modelMapping.input.isSignal ? factory.createTrue() : factory.createFalse()), |
| 51 | + factory.createPropertyAssignment( |
| 52 | + 'alias', factory.createStringLiteral(modelMapping.input.bindingPropertyName)), |
| 53 | + factory.createPropertyAssignment( |
| 54 | + 'required', modelMapping.input.required ? factory.createTrue() : factory.createFalse()), |
| 55 | + ]); |
| 56 | + |
| 57 | + const inputDecorator = createDecorator( |
| 58 | + 'Input', |
| 59 | + // Config is cast to `any` because `isSignal` will be private, and in case this |
| 60 | + // transform is used directly as a pre-compilation step, the decorator should |
| 61 | + // not fail. It is already validated now due to us parsing the input metadata. |
| 62 | + factory.createAsExpression( |
| 63 | + inputConfig, factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword)), |
| 64 | + classDecoratorIdentifier, factory, importManager); |
| 65 | + |
| 66 | + const outputDecorator = createDecorator( |
| 67 | + 'Output', factory.createStringLiteral(modelMapping.output.bindingPropertyName), |
| 68 | + classDecoratorIdentifier, factory, importManager); |
| 69 | + |
| 70 | + return factory.updatePropertyDeclaration( |
| 71 | + member, |
| 72 | + [inputDecorator, outputDecorator, ...(member.modifiers ?? [])], |
| 73 | + member.name, |
| 74 | + member.questionToken, |
| 75 | + member.type, |
| 76 | + member.initializer, |
| 77 | + ); |
| 78 | +}; |
| 79 | + |
| 80 | +function createDecorator( |
| 81 | + name: string, config: ts.Expression, classDecoratorIdentifier: ts.Identifier, |
| 82 | + factory: ts.NodeFactory, importManager: ImportManager): ts.Decorator { |
| 83 | + const callTarget = factory.createPropertyAccessExpression( |
| 84 | + importManager.generateNamespaceImport('@angular/core'), |
| 85 | + // The synthetic identifier may be checked later by the downlevel decorators |
| 86 | + // transform to resolve to an Angular import using `getSymbolAtLocation`. We trick |
| 87 | + // the transform to think it's not synthetic and comes from Angular core. |
| 88 | + ts.setOriginalNode(factory.createIdentifier(name), classDecoratorIdentifier)); |
| 89 | + |
| 90 | + return factory.createDecorator(factory.createCallExpression(callTarget, undefined, [config])); |
| 91 | +} |
0 commit comments