-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
model_function.ts
52 lines (46 loc) 路 1.61 KB
/
model_function.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import ts from 'typescript';
import {ImportedSymbolsTracker} from '../../../imports';
import {ModelMapping} from '../../../metadata';
import {ClassMember, ReflectionHost} from '../../../reflection';
import {tryParseInitializerApiMember} from './initializer_functions';
import {parseAndValidateInputAndOutputOptions} from './input_output_parse_options';
/**
* Attempts to parse a model class member. Returns the parsed model mapping if possible.
*/
export function tryParseSignalModelMapping(
member: Pick<ClassMember, 'name'|'value'>, reflector: ReflectionHost,
importTracker: ImportedSymbolsTracker): ModelMapping|null {
const model = tryParseInitializerApiMember(['model'], member, reflector, importTracker);
if (model === null) {
return null;
}
const optionsNode =
(model.isRequired ? model.call.arguments[0] : model.call.arguments[1]) as ts.Expression |
undefined;
const options =
optionsNode !== undefined ? parseAndValidateInputAndOutputOptions(optionsNode) : null;
const classPropertyName = member.name;
const bindingPropertyName = options?.alias ?? classPropertyName;
return {
call: model.call,
input: {
isSignal: true,
transform: null,
classPropertyName,
bindingPropertyName,
required: model.isRequired,
},
output: {
isSignal: false,
classPropertyName,
bindingPropertyName: bindingPropertyName + 'Change',
}
};
}