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
158 lines (136 loc) · 5.74 KB
/
function-context.ts
File metadata and controls
158 lines (136 loc) · 5.74 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
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,
Void,
NonVoid,
Mixed,
}
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 {
return signatureDeclaration.parameters.find(
param => ts.isIdentifier(param.name) && param.name.originalKeywordKind === ts.SyntaxKind.ThisKeyword
);
}
export function getDeclarationContextType(
{ program }: TransformationContext,
signatureDeclaration: ts.SignatureDeclaration
): ContextType {
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) {
return ContextType.NonVoid;
}
if (getNodeAnnotations(scopeDeclaration).has(AnnotationKind.NoSelf)) {
return ContextType.Void;
}
return ContextType.NonVoid;
}
// When using --noImplicitSelf and the signature is defined in a file targeted by the program apply the @noSelf rule.
const options = program.getCompilerOptions() as CompilerOptions;
if (options.noImplicitSelf && program.getRootFileNames().includes(signatureDeclaration.getSourceFile().fileName)) {
return ContextType.Void;
}
// Walk up to find @noSelf or @noSelfInFile
if (hasNoSelfAncestor(signatureDeclaration)) {
return ContextType.Void;
}
return ContextType.NonVoid;
}
function reduceContextTypes(contexts: ContextType[]): ContextType {
const reducer = (a: ContextType, b: ContextType) => {
if (a === ContextType.None) {
return b;
} else if (b === ContextType.None) {
return a;
} else if (a !== b) {
return ContextType.Mixed;
} else {
return a;
}
};
return contexts.reduce(reducer, ContextType.None);
}
function getSignatureDeclarations(
context: TransformationContext,
signatures: readonly ts.Signature[]
): ts.SignatureDeclaration[] {
return signatures.flatMap(signature => {
const signatureDeclaration = signature.getDeclaration();
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;
});
}
export function getFunctionContextType(context: TransformationContext, type: ts.Type): ContextType {
if (type.isTypeParameter()) {
type = type.getConstraint() ?? type;
}
if (type.isUnion()) {
return reduceContextTypes(type.types.map(t => getFunctionContextType(context, t)));
}
const signatures = context.checker.getSignaturesOfType(type, ts.SignatureKind.Call);
if (signatures.length === 0) {
return ContextType.None;
}
const signatureDeclarations = getSignatureDeclarations(context, signatures);
return reduceContextTypes(signatureDeclarations.map(s => getDeclarationContextType(context, s)));
}