forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotations.ts
More file actions
192 lines (160 loc) · 6.81 KB
/
annotations.ts
File metadata and controls
192 lines (160 loc) · 6.81 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import * as ts from "typescript";
import { TransformationContext } from "../context";
import { findFirstNodeAbove, inferAssignedType } from "./typescript";
export enum AnnotationKind {
Extension = "extension",
MetaExtension = "metaExtension",
CustomConstructor = "customConstructor",
CompileMembersOnly = "compileMembersOnly",
NoResolution = "noResolution",
PureAbstract = "pureAbstract",
Phantom = "phantom",
TupleReturn = "tupleReturn",
LuaIterator = "luaIterator",
LuaTable = "luaTable",
NoSelf = "noSelf",
NoSelfInFile = "noSelfInFile",
Vararg = "vararg",
ForRange = "forRange",
}
export interface Annotation {
kind: AnnotationKind;
args: string[];
}
function createAnnotation(name: string, args: string[]): Annotation | undefined {
const kind = Object.values(AnnotationKind).find(k => k.toLowerCase() === name.toLowerCase());
if (kind !== undefined) {
return { kind, args };
}
}
export type AnnotationsMap = Map<AnnotationKind, Annotation>;
function collectAnnotations(source: ts.Symbol | ts.Signature, annotationsMap: AnnotationsMap): void {
for (const tag of source.getJsDocTags()) {
const annotation = createAnnotation(tag.name, tag.text?.map(p => p.text) ?? []);
if (annotation) {
annotationsMap.set(annotation.kind, annotation);
}
}
}
export function getSymbolAnnotations(symbol: ts.Symbol): AnnotationsMap {
const annotationsMap: AnnotationsMap = new Map();
collectAnnotations(symbol, annotationsMap);
return annotationsMap;
}
export function getTypeAnnotations(type: ts.Type): AnnotationsMap {
const annotationsMap: AnnotationsMap = new Map();
if (type.symbol) collectAnnotations(type.symbol, annotationsMap);
if (type.aliasSymbol) collectAnnotations(type.aliasSymbol, annotationsMap);
return annotationsMap;
}
export function getNodeAnnotations(node: ts.Node): AnnotationsMap {
const annotationsMap: AnnotationsMap = new Map();
for (const tag of ts.getJSDocTags(node)) {
const tagName = tag.tagName.text;
const annotation = createAnnotation(tagName, getTagArgsFromComment(tag));
if (annotation) {
annotationsMap.set(annotation.kind, annotation);
}
}
return annotationsMap;
}
export function getFileAnnotations(sourceFile: ts.SourceFile): AnnotationsMap {
const annotationsMap: AnnotationsMap = new Map();
if (sourceFile.statements.length > 0) {
// Manually collect jsDoc because `getJSDocTags` includes tags only from closest comment
const jsDoc = sourceFile.statements[0].jsDoc;
if (jsDoc) {
for (const tag of jsDoc.flatMap(x => x.tags ?? [])) {
const tagName = tag.tagName.text;
const annotation = createAnnotation(tagName, getTagArgsFromComment(tag));
if (annotation) {
annotationsMap.set(annotation.kind, annotation);
}
}
}
}
return annotationsMap;
}
export function getSignatureAnnotations(context: TransformationContext, signature: ts.Signature): AnnotationsMap {
const annotationsMap: AnnotationsMap = new Map();
collectAnnotations(signature, annotationsMap);
// Function properties on interfaces have the JSDoc tags on the parent PropertySignature
const declaration = signature.getDeclaration();
if (declaration?.parent && ts.isPropertySignature(declaration.parent)) {
const symbol = context.checker.getSymbolAtLocation(declaration.parent.name);
if (symbol) {
collectAnnotations(symbol, annotationsMap);
}
}
return annotationsMap;
}
export function isTupleReturnCall(context: TransformationContext, node: ts.Node): boolean {
if (!ts.isCallExpression(node)) {
return false;
}
const signature = context.checker.getResolvedSignature(node);
if (signature) {
if (getSignatureAnnotations(context, signature).has(AnnotationKind.TupleReturn)) {
return true;
}
// Only check function type for directive if it is declared as an interface or type alias
const declaration = signature.getDeclaration();
const isInterfaceOrAlias =
declaration?.parent &&
((ts.isInterfaceDeclaration(declaration.parent) && ts.isCallSignatureDeclaration(declaration)) ||
ts.isTypeAliasDeclaration(declaration.parent));
if (!isInterfaceOrAlias) {
return false;
}
}
const type = context.checker.getTypeAtLocation(node.expression);
return getTypeAnnotations(type).has(AnnotationKind.TupleReturn);
}
export function isInTupleReturnFunction(context: TransformationContext, node: ts.Node): boolean {
const declaration = findFirstNodeAbove(node, ts.isFunctionLike);
if (!declaration) {
return false;
}
let functionType: ts.Type | undefined;
if (ts.isFunctionExpression(declaration) || ts.isArrowFunction(declaration)) {
functionType = inferAssignedType(context, declaration);
} else if (ts.isMethodDeclaration(declaration) && ts.isObjectLiteralExpression(declaration.parent)) {
// Manually lookup type for object literal properties declared with method syntax
const interfaceType = inferAssignedType(context, declaration.parent);
const propertySymbol = interfaceType.getProperty(declaration.name.getText());
if (propertySymbol) {
functionType = context.checker.getTypeOfSymbolAtLocation(propertySymbol, declaration);
}
}
if (functionType === undefined) {
functionType = context.checker.getTypeAtLocation(declaration);
}
// Check all overloads for directive
const signatures = functionType.getCallSignatures();
if (signatures?.some(s => getSignatureAnnotations(context, s).has(AnnotationKind.TupleReturn))) {
return true;
}
return getTypeAnnotations(functionType).has(AnnotationKind.TupleReturn);
}
export function isLuaIteratorType(context: TransformationContext, node: ts.Node): boolean {
const type = context.checker.getTypeAtLocation(node);
return getTypeAnnotations(type).has(AnnotationKind.LuaIterator);
}
export function isVarargType(context: TransformationContext, node: ts.Node): boolean {
const type = context.checker.getTypeAtLocation(node);
return getTypeAnnotations(type).has(AnnotationKind.Vararg);
}
export function isForRangeType(context: TransformationContext, node: ts.Node): boolean {
const type = context.checker.getTypeAtLocation(node);
return getTypeAnnotations(type).has(AnnotationKind.ForRange);
}
export function getTagArgsFromComment(tag: ts.JSDocTag): string[] {
if (tag.comment) {
if (typeof tag.comment === "string") {
return tag.comment.split(" ");
} else {
return tag.comment.map(part => part.text);
}
}
return [];
}