/
DecoratorPlugin.ts
130 lines (117 loc) · 4.49 KB
/
DecoratorPlugin.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import * as ts from "typescript";
import { ReferenceType } from "../../models/types";
import type { Reflection, Decorator } from "../../models/reflections/index";
import { Component, ConverterComponent } from "../components";
import { Converter } from "../converter";
import type { Context } from "../context";
/**
* A plugin that detects decorators.
*/
@Component({ name: "decorator" })
export class DecoratorPlugin extends ConverterComponent {
private readonly usages = new Map<ts.Symbol, ReferenceType[]>();
/**
* Create a new ImplementsPlugin instance.
*/
override initialize() {
this.listenTo(this.owner, {
[Converter.EVENT_CREATE_DECLARATION]: this.onDeclaration,
[Converter.EVENT_CREATE_PARAMETER]: this.onDeclaration,
[Converter.EVENT_RESOLVE]: this.onBeginResolve,
[Converter.EVENT_END]: () => this.usages.clear(),
});
}
/**
* Create an object describing the arguments a decorator is set with.
*
* @param args The arguments resolved from the decorator's call expression.
* @param signature The signature definition of the decorator being used.
* @returns An object describing the decorator parameters,
*/
private extractArguments(
args: ts.NodeArray<ts.Expression>,
signature: ts.Signature
): { [name: string]: string | string[] } {
const result: any = {};
args.forEach((arg: ts.Expression, index: number) => {
if (index < signature.parameters.length) {
const parameter = signature.parameters[index];
result[parameter.name] = arg.getText();
} else {
if (!result["..."]) {
result["..."] = [];
}
result["..."].push(arg.getText());
}
});
return result;
}
/**
* Triggered when the converter has created a declaration or signature reflection.
*
* @param context The context object describing the current state the converter is in.
* @param reflection The reflection that is currently processed.
* @param node The node that is currently processed if available.
*/
private onDeclaration(
context: Context,
reflection: Reflection,
node?: ts.Node
) {
node?.decorators?.forEach((decorator) => {
let callExpression: ts.CallExpression | undefined;
let identifier: ts.Expression;
switch (decorator.expression.kind) {
case ts.SyntaxKind.Identifier:
identifier = decorator.expression;
break;
case ts.SyntaxKind.CallExpression:
callExpression = <ts.CallExpression>decorator.expression;
identifier = callExpression.expression;
break;
default:
return;
}
const info: Decorator = {
name: identifier.getText(),
};
const type = context.checker.getTypeAtLocation(identifier);
if (type && type.symbol) {
info.type = ReferenceType.createSymbolReference(
context.resolveAliasedSymbol(type.symbol),
context,
info.name
);
if (callExpression && callExpression.arguments) {
const signature =
context.checker.getResolvedSignature(callExpression);
if (signature) {
info.arguments = this.extractArguments(
callExpression.arguments,
signature
);
}
}
const usages = this.usages.get(type.symbol) ?? [];
usages.push(
ReferenceType.createResolvedReference(
reflection.name,
reflection,
context.project
)
);
this.usages.set(type.symbol, usages);
}
reflection.decorators ??= [];
reflection.decorators.push(info);
});
}
private onBeginResolve(context: Context) {
for (const [symbol, ref] of this.usages) {
const reflection = context.project.getReflectionFromSymbol(symbol);
if (reflection) {
reflection.decorates = ref;
}
}
}
}