-
Notifications
You must be signed in to change notification settings - Fork 11
/
processDecorator.ts
48 lines (39 loc) · 1.91 KB
/
processDecorator.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
import * as ts from "typescript";
import { Context } from "./contexts/Context";
import { FunctionLikeDeclarationGenericParametersDetail } from "./FunctionLikeDeclarationGenericParametersDetail";
import { getGenericParametersDetails } from "./getGenericParametersDetails";
import getTypeCall from "./getTypeCall";
import { updateCallExpression } from "./updateCallExpression";
export function processDecorator(node: ts.Decorator, decoratorType: ts.Type, context: Context): ts.Decorator | undefined
{
if (!ts.isCallExpression(node.expression))
{
return undefined;
}
// Method/function declaration
const declaration = decoratorType.symbol.declarations?.[0] as ts.FunctionLikeDeclarationBase;
if (!declaration)
{
return undefined;
}
// Try to get State
const state: FunctionLikeDeclarationGenericParametersDetail = getGenericParametersDetails(declaration, context, []);
if (!state || !state.usedGenericParameters || !state.indexesOfGenericParameters || !state.requestedGenericsReflection)
{
return undefined;
}
// Decorator has no generic parameters in nature; we just abusing it so only one generic parameter makes sense
const genericParamName = state.usedGenericParameters[0];
const genericTypeNode = node.parent; // TODO: mělo by asi být node.parent.type
const genericType = context.typeChecker.getTypeAtLocation(genericTypeNode);
const genericTypeSymbol = genericType.getSymbol();
return ts.factory.updateDecorator(node, updateCallExpression(node.expression, state, [{
genericTypeName: genericParamName,
reflectedType: getTypeCall(
genericType,
genericTypeSymbol,
context,
genericTypeNode.name
)
}]));
}