forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction-context.ts
More file actions
237 lines (201 loc) · 8.93 KB
/
function-context.ts
File metadata and controls
237 lines (201 loc) · 8.93 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import * as ts from "typescript";
import { CompilerOptions } from "../../CompilerOptions";
import { TransformationContext } from "../context";
import { AnnotationKind, getFileAnnotations, getNodeAnnotations } from "./annotations";
import { findFirstNodeAbove, getAllCallSignatures, inferAssignedType } from "./typescript";
export enum ContextType {
None = 0,
Void = 1 << 0,
NonVoid = 1 << 1,
Mixed = Void | NonVoid,
}
function hasNoSelfAncestor(declaration: ts.Declaration): boolean {
const scopeDeclaration = findFirstNodeAbove(
declaration,
(node): node is ts.SourceFile | ts.ModuleDeclaration => ts.isSourceFile(node) || ts.isModuleDeclaration(node)
);
if (!scopeDeclaration) {
return false;
} else if (ts.isSourceFile(scopeDeclaration)) {
return getFileAnnotations(scopeDeclaration).has(AnnotationKind.NoSelfInFile);
} else if (getNodeAnnotations(scopeDeclaration).has(AnnotationKind.NoSelf)) {
return true;
} else {
return hasNoSelfAncestor(scopeDeclaration);
}
}
function getExplicitThisParameter(signatureDeclaration: ts.SignatureDeclaration): ts.ParameterDeclaration | undefined {
const param = signatureDeclaration.parameters[0];
if (param && ts.isIdentifier(param.name) && ts.identifierToKeywordKind(param.name) === ts.SyntaxKind.ThisKeyword) {
return param;
}
}
const callContextTypes = new WeakMap<ts.CallLikeExpression, ContextType>();
export function getCallContextType(context: TransformationContext, callExpression: ts.CallLikeExpression): ContextType {
const known = callContextTypes.get(callExpression);
if (known !== undefined) return known;
const signature = context.checker.getResolvedSignature(callExpression);
const signatureDeclaration = signature?.getDeclaration();
let contextType = ContextType.None;
if (signatureDeclaration) {
contextType = computeDeclarationContextType(context, signatureDeclaration);
} else {
// No signature declaration could be resolved, so instead try to see if the declaration is in a
// noSelfInFile file
const declarations = findRootDeclarations(context, callExpression);
contextType = declarations.some(d => getFileAnnotations(d.getSourceFile()).has(AnnotationKind.NoSelfInFile))
? ContextType.Void
: context.options.noImplicitSelf
? ContextType.Void
: ContextType.NonVoid;
}
callContextTypes.set(callExpression, contextType);
return contextType;
}
const signatureDeclarationContextTypes = new WeakMap<ts.SignatureDeclaration, ContextType>();
function getSignatureContextType(
context: TransformationContext,
signatureDeclaration: ts.SignatureDeclaration
): ContextType {
const known = signatureDeclarationContextTypes.get(signatureDeclaration);
if (known !== undefined) return known;
const contextType = computeDeclarationContextType(context, signatureDeclaration);
signatureDeclarationContextTypes.set(signatureDeclaration, contextType);
return contextType;
}
function findRootDeclarations(context: TransformationContext, callExpression: ts.CallLikeExpression): ts.Declaration[] {
const calledExpression = ts.isTaggedTemplateExpression(callExpression)
? callExpression.tag
: ts.isJsxSelfClosingElement(callExpression)
? callExpression.tagName
: ts.isJsxOpeningElement(callExpression)
? callExpression.tagName
: ts.isCallExpression(callExpression)
? callExpression.expression
: undefined;
if (!calledExpression) return [];
const calledSymbol = context.checker.getSymbolAtLocation(calledExpression);
if (calledSymbol === undefined) return [];
return (
calledSymbol.getDeclarations()?.flatMap(d => {
if (ts.isImportSpecifier(d)) {
const aliasSymbol = context.checker.getAliasedSymbol(calledSymbol);
return aliasSymbol.getDeclarations() ?? [];
} else {
return [d];
}
}) ?? []
);
}
function computeDeclarationContextType(context: TransformationContext, signatureDeclaration: ts.SignatureDeclaration) {
const thisParameter = getExplicitThisParameter(signatureDeclaration);
if (thisParameter) {
// Explicit 'this'
return thisParameter.type && thisParameter.type.kind === ts.SyntaxKind.VoidKeyword
? ContextType.Void
: ContextType.NonVoid;
}
// noSelf declaration on function signature
if (getNodeAnnotations(signatureDeclaration).has(AnnotationKind.NoSelf)) {
return ContextType.Void;
}
if (
ts.isMethodSignature(signatureDeclaration) ||
ts.isMethodDeclaration(signatureDeclaration) ||
ts.isConstructSignatureDeclaration(signatureDeclaration) ||
ts.isConstructorDeclaration(signatureDeclaration) ||
(signatureDeclaration.parent && ts.isPropertyDeclaration(signatureDeclaration.parent)) ||
(signatureDeclaration.parent && ts.isPropertySignature(signatureDeclaration.parent))
) {
// Class/interface methods only respect @noSelf on their parent
const scopeDeclaration = findFirstNodeAbove(
signatureDeclaration,
(n): n is ts.ClassLikeDeclaration | ts.InterfaceDeclaration =>
ts.isClassDeclaration(n) || ts.isClassExpression(n) || ts.isInterfaceDeclaration(n)
);
if (scopeDeclaration !== undefined && getNodeAnnotations(scopeDeclaration).has(AnnotationKind.NoSelf)) {
return ContextType.Void;
}
return ContextType.NonVoid;
}
if (signatureDeclaration.parent && ts.isTypeParameterDeclaration(signatureDeclaration.parent)) {
return ContextType.NonVoid;
}
// When using --noImplicitSelf and the signature is defined in a file targeted by the program apply the @noSelf rule.
const program = context.program;
const options = program.getCompilerOptions() as CompilerOptions;
if (options.noImplicitSelf) {
const sourceFile = program.getSourceFile(signatureDeclaration.getSourceFile().fileName);
if (
sourceFile !== undefined &&
!program.isSourceFileDefaultLibrary(sourceFile) &&
!program.isSourceFileFromExternalLibrary(sourceFile)
) {
return ContextType.Void;
}
}
// Walk up to find @noSelf or @noSelfInFile
if (hasNoSelfAncestor(signatureDeclaration)) {
return ContextType.Void;
}
return ContextType.NonVoid;
}
function reduceContextTypes(contexts: ContextType[]): ContextType {
let type = ContextType.None;
for (const context of contexts) {
type |= context;
if (type === ContextType.Mixed) break;
}
return type;
}
function getSignatureDeclarations(context: TransformationContext, signature: ts.Signature): ts.SignatureDeclaration[] {
if (signature.compositeSignatures) {
return signature.compositeSignatures.flatMap(s => getSignatureDeclarations(context, s));
}
const signatureDeclaration = signature.getDeclaration();
if (signatureDeclaration === undefined) {
return [];
}
let inferredType: ts.Type | undefined;
if (
ts.isMethodDeclaration(signatureDeclaration) &&
ts.isObjectLiteralExpression(signatureDeclaration.parent) &&
!getExplicitThisParameter(signatureDeclaration)
) {
inferredType = context.checker.getContextualTypeForObjectLiteralElement(signatureDeclaration);
} else if (
(ts.isFunctionExpression(signatureDeclaration) || ts.isArrowFunction(signatureDeclaration)) &&
!getExplicitThisParameter(signatureDeclaration)
) {
// Infer type of function expressions/arrow functions
inferredType = inferAssignedType(context, signatureDeclaration);
}
if (inferredType) {
const inferredSignatures = getAllCallSignatures(inferredType);
if (inferredSignatures.length > 0) {
return inferredSignatures.map(s => s.getDeclaration());
}
}
return [signatureDeclaration];
}
const typeContextTypes = new WeakMap<ts.Type, ContextType>();
export function getFunctionContextType(context: TransformationContext, type: ts.Type): ContextType {
const known = typeContextTypes.get(type);
if (known !== undefined) return known;
const contextType = computeFunctionContextType(context, type);
typeContextTypes.set(type, contextType);
return contextType;
}
function computeFunctionContextType(context: TransformationContext, type: ts.Type): ContextType {
if (type.isTypeParameter()) {
const constraint = type.getConstraint();
if (constraint) return getFunctionContextType(context, constraint);
}
const signatures = context.checker.getSignaturesOfType(type, ts.SignatureKind.Call);
if (signatures.length === 0) {
return ContextType.None;
}
return reduceContextTypes(
signatures.flatMap(s => getSignatureDeclarations(context, s)).map(s => getSignatureContextType(context, s))
);
}