-
-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathaccess.ts
More file actions
223 lines (199 loc) · 9.14 KB
/
Copy pathaccess.ts
File metadata and controls
223 lines (199 loc) · 9.14 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
import * as ts from "typescript";
import * as lua from "../../LuaAST";
import { transformBuiltinPropertyAccessExpression } from "../builtins";
import { FunctionVisitor, TransformationContext } from "../context";
import { AnnotationKind, getTypeAnnotations } from "../utils/annotations";
import {
invalidCallExtensionUse,
invalidMultiReturnAccess,
unsupportedOptionalCompileMembersOnly,
} from "../utils/diagnostics";
import { getExtensionKindForNode } from "../utils/language-extensions";
import { addToNumericExpression, createExportsIdentifier } from "../utils/lua-ast";
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
import { isArrayType, isNumberType, isStringType } from "../utils/typescript";
import { tryGetConstEnumValue } from "./enum";
import { transformOrderedExpressions } from "./expression-list";
import { callExtensions } from "./language-extensions/call-extension";
import { isMultiReturnCall, returnsMultiType } from "./language-extensions/multi";
import {
transformOptionalChainWithCapture,
ExpressionWithThisValue,
isOptionalContinuation,
captureThisValue,
} from "./optional-chaining";
import { SyntaxKind } from "typescript";
import { getCustomNameFromSymbol } from "./identifier";
import { getSymbolExportScope, isSymbolExported } from "../utils/export";
function addOneToArrayAccessArgument(
context: TransformationContext,
node: ts.ElementAccessExpression,
index: lua.Expression
): lua.Expression {
const type = context.checker.getTypeAtLocation(node.expression);
const argumentType = context.checker.getTypeAtLocation(node.argumentExpression);
if (isArrayType(context, type) && isNumberType(context, argumentType)) {
return addToNumericExpression(index, 1);
}
return index;
}
export function transformElementAccessArgument(
context: TransformationContext,
node: ts.ElementAccessExpression
): lua.Expression {
const index = context.transformExpression(node.argumentExpression);
return addOneToArrayAccessArgument(context, node, index);
}
export const transformElementAccessExpression: FunctionVisitor<ts.ElementAccessExpression> = (node, context) =>
transformElementAccessExpressionWithCapture(context, node, undefined).expression;
export function transformElementAccessExpressionWithCapture(
context: TransformationContext,
node: ts.ElementAccessExpression,
thisValueCapture: lua.Identifier | undefined
): ExpressionWithThisValue {
const constEnumValue = tryGetConstEnumValue(context, node);
if (constEnumValue) {
return { expression: constEnumValue };
}
if (ts.isOptionalChain(node)) {
return transformOptionalChainWithCapture(context, node, thisValueCapture);
}
const [table, accessExpression] = transformOrderedExpressions(context, [node.expression, node.argumentExpression]);
const type = context.checker.getTypeAtLocation(node.expression);
const argumentType = context.checker.getTypeAtLocation(node.argumentExpression);
if (isStringType(context, type) && isNumberType(context, argumentType)) {
// strings are not callable, so ignore thisValueCapture
return {
expression: transformLuaLibFunction(context, LuaLibFeature.StringAccess, node, table, accessExpression),
};
}
const updatedAccessExpression = addOneToArrayAccessArgument(context, node, accessExpression);
if (isMultiReturnCall(context, node.expression)) {
const accessType = context.checker.getTypeAtLocation(node.argumentExpression);
if (!isNumberType(context, accessType)) {
context.diagnostics.push(invalidMultiReturnAccess(node));
}
const canOmitSelect = ts.isNumericLiteral(node.argumentExpression) && node.argumentExpression.text === "0";
if (canOmitSelect) {
// wrapping in parenthesis ensures only the first return value is used
// https://www.lua.org/manual/5.1/manual.html#2.5
return { expression: lua.createParenthesizedExpression(table) };
}
const selectIdentifier = lua.createIdentifier("select");
return { expression: lua.createCallExpression(selectIdentifier, [updatedAccessExpression, table]) };
}
if (thisValueCapture) {
const thisValue = captureThisValue(context, table, thisValueCapture, node.expression);
return {
expression: lua.createTableIndexExpression(thisValue, updatedAccessExpression, node),
thisValue,
};
}
return { expression: lua.createTableIndexExpression(table, updatedAccessExpression, node) };
}
export const transformPropertyAccessExpression: FunctionVisitor<ts.PropertyAccessExpression> = (node, context) =>
transformPropertyAccessExpressionWithCapture(context, node, undefined).expression;
export function transformPropertyAccessExpressionWithCapture(
context: TransformationContext,
node: ts.PropertyAccessExpression,
thisValueCapture: lua.Identifier | undefined
): ExpressionWithThisValue {
const type = context.checker.getTypeAtLocation(node.expression);
const isOptionalLeft = isOptionalContinuation(node.expression);
let property = node.name.text;
const symbol = context.checker.getSymbolAtLocation(node.name);
const customName = getCustomNameFromSymbol(context, symbol);
if (customName) {
property = customName;
}
const constEnumValue = tryGetConstEnumValue(context, node);
if (constEnumValue) {
return { expression: constEnumValue };
}
if (ts.isCallExpression(node.expression) && returnsMultiType(context, node.expression)) {
context.diagnostics.push(invalidMultiReturnAccess(node));
}
if (ts.isOptionalChain(node)) {
return transformOptionalChainWithCapture(context, node, thisValueCapture);
}
// Do not output path for member only enums
const annotations = getTypeAnnotations(type);
if (annotations.has(AnnotationKind.CompileMembersOnly)) {
if (isOptionalLeft) {
context.diagnostics.push(unsupportedOptionalCompileMembersOnly(node));
}
if (ts.isPropertyAccessExpression(node.expression)) {
// in case of ...x.enum.y transform to ...x.y
const expression = lua.createTableIndexExpression(
context.transformExpression(node.expression.expression),
lua.createStringLiteral(property),
node
);
return { expression };
} else {
// Check if we need to account for enum being exported int his file
if (
isSymbolExported(context, type.symbol) &&
getSymbolExportScope(context, type.symbol) === node.expression.getSourceFile()
) {
return {
expression: lua.createTableIndexExpression(
createExportsIdentifier(),
lua.createStringLiteral(property),
node
),
};
} else {
return { expression: lua.createIdentifier(property, node) };
}
}
}
const builtinResult = transformBuiltinPropertyAccessExpression(context, node);
if (builtinResult) {
// Ignore thisValueCapture.
// This assumes that nothing returned by builtin property accesses are callable.
// If this assumption is no longer true, this may need to be updated.
return { expression: builtinResult };
}
if (
ts.isIdentifier(node.expression) &&
node.parent &&
(!ts.isCallExpression(node.parent) || node.parent.expression !== node)
) {
// Check if this is a method call extension that is not used as a call
const extensionType = getExtensionKindForNode(context, node);
if (extensionType && callExtensions.has(extensionType)) {
context.diagnostics.push(invalidCallExtensionUse(node));
}
}
const table = context.transformExpression(node.expression);
if (thisValueCapture) {
const thisValue = captureThisValue(context, table, thisValueCapture, node.expression);
const expression = lua.createTableIndexExpression(thisValue, lua.createStringLiteral(property), node);
return {
expression,
thisValue,
};
}
if (node.expression.kind === SyntaxKind.SuperKeyword) {
const symbol = context.checker.getSymbolAtLocation(node);
if (symbol && symbol.flags & ts.SymbolFlags.GetAccessor) {
return {
expression: transformLuaLibFunction(
context,
LuaLibFeature.DescriptorGet,
node,
lua.createIdentifier("self"),
table,
lua.createStringLiteral(property)
),
};
}
}
return { expression: lua.createTableIndexExpression(table, lua.createStringLiteral(property), node) };
}
export const transformQualifiedName: FunctionVisitor<ts.QualifiedName> = (node, context) => {
const right = lua.createStringLiteral(node.right.text, node.right);
const left = context.transformExpression(node.left);
return lua.createTableIndexExpression(left, right, node);
};