/
parameter.ts
49 lines (42 loc) · 2.01 KB
/
parameter.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
import * as ts from 'typescript';
import { ReflectionFlag, ReflectionKind, ParameterReflection, SignatureReflection } from '../../models/reflections/index';
import { Context } from '../context';
import { Converter } from '../converter';
import { convertDefaultValue } from '../convert-expression';
/**
* Create a parameter reflection for the given node.
*
* @param context The context object describing the current state the converter is in.
* @param node The parameter node that should be reflected.
* @returns The newly created parameter reflection.
*/
export function createParameter(context: Context, node: ts.ParameterDeclaration): ParameterReflection | undefined {
if (!(context.scope instanceof SignatureReflection)) {
throw new Error('Expected signature reflection.');
}
const signature = context.scope;
if (!node.symbol) {
return;
}
const parameter = new ParameterReflection(node.symbol.name, ReflectionKind.Parameter, signature);
parameter.flags.setFlag(ReflectionFlag.Exported, context.scope.flags.isExported);
context.registerReflection(parameter, node);
context.withScope(parameter, () => {
if (ts.isArrayBindingPattern(node.name) || ts.isObjectBindingPattern(node.name)) {
parameter.type = context.converter.convertType(context, node.name);
parameter.name = '__namedParameters';
} else {
parameter.type = context.converter.convertType(context, node.type, context.getTypeAtLocation(node));
}
parameter.defaultValue = convertDefaultValue(node);
parameter.setFlag(ReflectionFlag.Optional, !!node.questionToken);
parameter.setFlag(ReflectionFlag.Rest, !!node.dotDotDotToken);
parameter.setFlag(ReflectionFlag.DefaultValue, !!parameter.defaultValue);
if (!signature.parameters) {
signature.parameters = [];
}
signature.parameters.push(parameter);
});
context.trigger(Converter.EVENT_CREATE_PARAMETER, parameter, node);
return parameter;
}