typescript-to-lua 1.1.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/dist/LuaAST.d.ts +1 -0
- package/dist/LuaAST.js +10 -2
- package/dist/LuaLib.d.ts +3 -3
- package/dist/LuaLib.js +5 -5
- package/dist/LuaPrinter.d.ts +11 -4
- package/dist/LuaPrinter.js +58 -15
- package/dist/cli/parse.d.ts +1 -1
- package/dist/cli/parse.js +14 -0
- package/dist/lualib/ArrayFlat.lua +1 -1
- package/dist/lualib/ArrayFlatMap.lua +1 -1
- package/dist/lualib/ArrayIsArray.lua +1 -1
- package/dist/lualib/ArrayReduce.lua +7 -1
- package/dist/lualib/ArrayReduceRight.lua +7 -1
- package/dist/lualib/ArraySetLength.lua +1 -1
- package/dist/lualib/ArraySplice.lua +5 -5
- package/dist/lualib/Await.lua +21 -13
- package/dist/lualib/CloneDescriptor.lua +7 -7
- package/dist/lualib/Decorate.lua +1 -1
- package/dist/lualib/DelegatedYield.lua +1 -3
- package/dist/lualib/Delete.lua +6 -6
- package/dist/lualib/Error.lua +47 -44
- package/dist/lualib/Generator.lua +1 -5
- package/dist/lualib/InstanceOf.lua +1 -1
- package/dist/lualib/InstanceOfObject.lua +1 -1
- package/dist/lualib/Map.lua +130 -134
- package/dist/lualib/Number.lua +1 -1
- package/dist/lualib/NumberIsFinite.lua +1 -1
- package/dist/lualib/NumberToString.lua +3 -5
- package/dist/lualib/ObjectDefineProperty.lua +40 -8
- package/dist/lualib/ParseFloat.lua +9 -5
- package/dist/lualib/ParseInt.lua +15 -3
- package/dist/lualib/Promise.lua +28 -24
- package/dist/lualib/Set.lua +119 -123
- package/dist/lualib/SetDescriptor.lua +7 -1
- package/dist/lualib/SourceMapTraceBack.lua +1 -1
- package/dist/lualib/SparseArrayNew.lua +5 -0
- package/dist/lualib/SparseArrayPush.lua +9 -0
- package/dist/lualib/SparseArraySpread.lua +4 -0
- package/dist/lualib/StringAccess.lua +1 -1
- package/dist/lualib/StringCharCodeAt.lua +1 -1
- package/dist/lualib/StringEndsWith.lua +2 -2
- package/dist/lualib/StringPadEnd.lua +2 -2
- package/dist/lualib/StringPadStart.lua +2 -2
- package/dist/lualib/StringReplace.lua +7 -1
- package/dist/lualib/StringSlice.lua +2 -2
- package/dist/lualib/StringSplit.lua +3 -3
- package/dist/lualib/StringStartsWith.lua +1 -1
- package/dist/lualib/StringSubstr.lua +1 -1
- package/dist/lualib/StringSubstring.lua +2 -2
- package/dist/lualib/Symbol.lua +3 -5
- package/dist/lualib/WeakMap.lua +40 -42
- package/dist/lualib/WeakSet.lua +36 -38
- package/dist/lualib/lualib_bundle.lua +795 -735
- package/dist/transformation/builtins/array.js +1 -2
- package/dist/transformation/builtins/function.js +6 -4
- package/dist/transformation/builtins/index.d.ts +1 -1
- package/dist/transformation/builtins/index.js +41 -6
- package/dist/transformation/builtins/object.d.ts +2 -1
- package/dist/transformation/builtins/object.js +1 -2
- package/dist/transformation/builtins/string.js +1 -2
- package/dist/transformation/context/context.d.ts +12 -0
- package/dist/transformation/context/context.js +102 -3
- package/dist/transformation/utils/diagnostics.d.ts +9 -6
- package/dist/transformation/utils/diagnostics.js +4 -3
- package/dist/transformation/utils/export.d.ts +3 -2
- package/dist/transformation/utils/export.js +10 -3
- package/dist/transformation/utils/language-extensions.d.ts +2 -0
- package/dist/transformation/utils/language-extensions.js +7 -1
- package/dist/transformation/utils/lua-ast.d.ts +0 -1
- package/dist/transformation/utils/lua-ast.js +14 -23
- package/dist/transformation/utils/preceding-statements.d.ts +3 -0
- package/dist/transformation/utils/preceding-statements.js +11 -0
- package/dist/transformation/utils/safe-names.d.ts +4 -2
- package/dist/transformation/utils/safe-names.js +11 -5
- package/dist/transformation/utils/scope.d.ts +3 -1
- package/dist/transformation/utils/scope.js +9 -1
- package/dist/transformation/utils/typescript/index.d.ts +1 -0
- package/dist/transformation/utils/typescript/index.js +16 -1
- package/dist/transformation/utils/typescript/types.d.ts +3 -1
- package/dist/transformation/utils/typescript/types.js +40 -13
- package/dist/transformation/visitors/access.d.ts +3 -0
- package/dist/transformation/visitors/access.js +63 -36
- package/dist/transformation/visitors/binary-expression/assignments.d.ts +3 -2
- package/dist/transformation/visitors/binary-expression/assignments.js +66 -57
- package/dist/transformation/visitors/binary-expression/compound.d.ts +5 -4
- package/dist/transformation/visitors/binary-expression/compound.js +66 -68
- package/dist/transformation/visitors/binary-expression/destructuring-assignments.d.ts +2 -2
- package/dist/transformation/visitors/binary-expression/destructuring-assignments.js +54 -25
- package/dist/transformation/visitors/binary-expression/index.d.ts +3 -1
- package/dist/transformation/visitors/binary-expression/index.js +65 -23
- package/dist/transformation/visitors/call.d.ts +3 -2
- package/dist/transformation/visitors/call.js +95 -141
- package/dist/transformation/visitors/class/index.js +13 -9
- package/dist/transformation/visitors/class/new.js +4 -6
- package/dist/transformation/visitors/conditional.js +30 -37
- package/dist/transformation/visitors/delete.js +4 -0
- package/dist/transformation/visitors/errors.js +11 -9
- package/dist/transformation/visitors/expression-list.d.ts +7 -0
- package/dist/transformation/visitors/expression-list.js +120 -0
- package/dist/transformation/visitors/expression-statement.d.ts +3 -1
- package/dist/transformation/visitors/expression-statement.js +21 -19
- package/dist/transformation/visitors/function.d.ts +2 -0
- package/dist/transformation/visitors/function.js +63 -13
- package/dist/transformation/visitors/identifier.js +5 -0
- package/dist/transformation/visitors/language-extensions/index.d.ts +4 -0
- package/dist/transformation/visitors/language-extensions/index.js +17 -0
- package/dist/transformation/visitors/language-extensions/operators.d.ts +1 -1
- package/dist/transformation/visitors/language-extensions/operators.js +7 -2
- package/dist/transformation/visitors/language-extensions/pairsIterable.d.ts +5 -0
- package/dist/transformation/visitors/language-extensions/pairsIterable.js +53 -0
- package/dist/transformation/visitors/language-extensions/table.d.ts +1 -8
- package/dist/transformation/visitors/language-extensions/table.js +55 -53
- package/dist/transformation/visitors/literal.js +69 -16
- package/dist/transformation/visitors/loops/do-while.js +34 -8
- package/dist/transformation/visitors/loops/for-of.js +4 -0
- package/dist/transformation/visitors/loops/for.js +25 -5
- package/dist/transformation/visitors/loops/utils.d.ts +1 -0
- package/dist/transformation/visitors/loops/utils.js +19 -3
- package/dist/transformation/visitors/modules/export.js +4 -5
- package/dist/transformation/visitors/namespace.js +1 -1
- package/dist/transformation/visitors/optional-chaining.d.ts +16 -0
- package/dist/transformation/visitors/optional-chaining.js +172 -0
- package/dist/transformation/visitors/sourceFile.js +4 -1
- package/dist/transformation/visitors/spread.js +14 -7
- package/dist/transformation/visitors/switch.js +29 -9
- package/dist/transformation/visitors/template.js +5 -2
- package/dist/transformation/visitors/typeof.js +3 -1
- package/dist/transformation/visitors/variable-declaration.js +37 -8
- package/dist/transformation/visitors/void.d.ts +1 -4
- package/dist/transformation/visitors/void.js +7 -12
- package/dist/transpilation/resolve.js +1 -1
- package/dist/transpilation/transformers.js +4 -1
- package/dist/transpilation/utils.js +2 -1
- package/language-extensions/index.d.ts +11 -1
- package/package.json +5 -5
- package/dist/lualib/OptionalChainAccess.lua +0 -6
- package/dist/lualib/OptionalFunctionCall.lua +0 -6
- package/dist/lualib/OptionalMethodCall.lua +0 -11
- package/dist/transformation/utils/transform.d.ts +0 -8
- package/dist/transformation/utils/transform.js +0 -14
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import * as ts from "typescript";
|
|
2
|
+
import { CompilerOptions } from "../..";
|
|
2
3
|
import { TransformationContext } from "../context";
|
|
3
|
-
export declare const
|
|
4
|
+
export declare const shouldAllowUnicode: (options: CompilerOptions) => boolean;
|
|
5
|
+
export declare const isValidLuaIdentifier: (name: string, options: CompilerOptions) => boolean;
|
|
4
6
|
export declare const luaKeywords: ReadonlySet<string>;
|
|
5
|
-
export declare const isUnsafeName: (name: string) => boolean;
|
|
7
|
+
export declare const isUnsafeName: (name: string, options: CompilerOptions) => boolean;
|
|
6
8
|
export declare function hasUnsafeSymbolName(context: TransformationContext, symbol: ts.Symbol, tsOriginal: ts.Identifier): boolean;
|
|
7
9
|
export declare function hasUnsafeIdentifierName(context: TransformationContext, identifier: ts.Identifier, checkSymbol?: boolean): boolean;
|
|
8
10
|
export declare const createSafeName: (name: string) => string;
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createSafeName = exports.hasUnsafeIdentifierName = exports.hasUnsafeSymbolName = exports.isUnsafeName = exports.luaKeywords = exports.isValidLuaIdentifier = void 0;
|
|
3
|
+
exports.createSafeName = exports.hasUnsafeIdentifierName = exports.hasUnsafeSymbolName = exports.isUnsafeName = exports.luaKeywords = exports.isValidLuaIdentifier = exports.shouldAllowUnicode = void 0;
|
|
4
|
+
const __1 = require("../..");
|
|
4
5
|
const diagnostics_1 = require("./diagnostics");
|
|
5
6
|
const export_1 = require("./export");
|
|
6
7
|
const typescript_1 = require("./typescript");
|
|
7
|
-
const
|
|
8
|
+
const shouldAllowUnicode = (options) => options.luaTarget === __1.LuaTarget.LuaJIT;
|
|
9
|
+
exports.shouldAllowUnicode = shouldAllowUnicode;
|
|
10
|
+
const isValidLuaIdentifier = (name, options) => !exports.luaKeywords.has(name) &&
|
|
11
|
+
((0, exports.shouldAllowUnicode)(options)
|
|
12
|
+
? /^[a-zA-Z_\u007F-\uFFFD][a-zA-Z0-9_\u007F-\uFFFD]*$/
|
|
13
|
+
: /^[a-zA-Z_][a-zA-Z0-9_]*$/).test(name);
|
|
8
14
|
exports.isValidLuaIdentifier = isValidLuaIdentifier;
|
|
9
15
|
exports.luaKeywords = new Set([
|
|
10
16
|
"and",
|
|
@@ -51,10 +57,10 @@ const luaBuiltins = new Set([
|
|
|
51
57
|
"type",
|
|
52
58
|
"unpack",
|
|
53
59
|
]);
|
|
54
|
-
const isUnsafeName = (name) => !(0, exports.isValidLuaIdentifier)(name) || luaBuiltins.has(name);
|
|
60
|
+
const isUnsafeName = (name, options) => !(0, exports.isValidLuaIdentifier)(name, options) || luaBuiltins.has(name);
|
|
55
61
|
exports.isUnsafeName = isUnsafeName;
|
|
56
62
|
function checkName(context, name, node) {
|
|
57
|
-
const isInvalid = !(0, exports.isValidLuaIdentifier)(name);
|
|
63
|
+
const isInvalid = !(0, exports.isValidLuaIdentifier)(name, context.options);
|
|
58
64
|
if (isInvalid) {
|
|
59
65
|
// Empty identifier is a TypeScript error
|
|
60
66
|
if (name !== "") {
|
|
@@ -71,7 +77,7 @@ function hasUnsafeSymbolName(context, symbol, tsOriginal) {
|
|
|
71
77
|
return true;
|
|
72
78
|
}
|
|
73
79
|
// only unsafe when non-ambient and not exported
|
|
74
|
-
return (0, exports.isUnsafeName)(symbol.name) && !isAmbient && !(0, export_1.isSymbolExported)(context, symbol);
|
|
80
|
+
return (0, exports.isUnsafeName)(symbol.name, context.options) && !isAmbient && !(0, export_1.isSymbolExported)(context, symbol);
|
|
75
81
|
}
|
|
76
82
|
exports.hasUnsafeSymbolName = hasUnsafeSymbolName;
|
|
77
83
|
function hasUnsafeIdentifierName(context, identifier, checkSymbol = true) {
|
|
@@ -9,7 +9,8 @@ export declare enum ScopeType {
|
|
|
9
9
|
Conditional = 16,
|
|
10
10
|
Block = 32,
|
|
11
11
|
Try = 64,
|
|
12
|
-
Catch = 128
|
|
12
|
+
Catch = 128,
|
|
13
|
+
LoopInitializer = 256
|
|
13
14
|
}
|
|
14
15
|
interface FunctionDefinitionInfo {
|
|
15
16
|
referencedSymbols: Map<lua.SymbolId, ts.Node[]>;
|
|
@@ -37,6 +38,7 @@ export declare function peekScope(context: TransformationContext): Scope;
|
|
|
37
38
|
export declare function findScope(context: TransformationContext, scopeTypes: ScopeType): Scope | undefined;
|
|
38
39
|
export declare function pushScope(context: TransformationContext, scopeType: ScopeType): Scope;
|
|
39
40
|
export declare function popScope(context: TransformationContext): Scope;
|
|
41
|
+
export declare function addScopeVariableDeclaration(scope: Scope, declaration: lua.VariableDeclarationStatement): void;
|
|
40
42
|
export declare function hasReferencedUndefinedLocalFunction(context: TransformationContext, scope: Scope): boolean;
|
|
41
43
|
export declare function hasReferencedSymbol(context: TransformationContext, scope: Scope, symbol: ts.Symbol): boolean | undefined;
|
|
42
44
|
export declare function isFunctionScopeWithDefinition(scope: Scope): scope is Scope & {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.performHoisting = exports.separateHoistedStatements = exports.isFunctionScopeWithDefinition = exports.hasReferencedSymbol = exports.hasReferencedUndefinedLocalFunction = exports.popScope = exports.pushScope = exports.findScope = exports.peekScope = exports.markSymbolAsReferencedInCurrentScopes = exports.walkScopesUp = exports.ScopeType = void 0;
|
|
3
|
+
exports.performHoisting = exports.separateHoistedStatements = exports.isFunctionScopeWithDefinition = exports.hasReferencedSymbol = exports.hasReferencedUndefinedLocalFunction = exports.addScopeVariableDeclaration = exports.popScope = exports.pushScope = exports.findScope = exports.peekScope = exports.markSymbolAsReferencedInCurrentScopes = exports.walkScopesUp = exports.ScopeType = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const lua = require("../../LuaAST");
|
|
6
6
|
const utils_1 = require("../../utils");
|
|
@@ -16,6 +16,7 @@ var ScopeType;
|
|
|
16
16
|
ScopeType[ScopeType["Block"] = 32] = "Block";
|
|
17
17
|
ScopeType[ScopeType["Try"] = 64] = "Try";
|
|
18
18
|
ScopeType[ScopeType["Catch"] = 128] = "Catch";
|
|
19
|
+
ScopeType[ScopeType["LoopInitializer"] = 256] = "LoopInitializer";
|
|
19
20
|
})(ScopeType = exports.ScopeType || (exports.ScopeType = {}));
|
|
20
21
|
const scopeStacks = new WeakMap();
|
|
21
22
|
function getScopeStack(context) {
|
|
@@ -68,6 +69,13 @@ function popScope(context) {
|
|
|
68
69
|
return scope;
|
|
69
70
|
}
|
|
70
71
|
exports.popScope = popScope;
|
|
72
|
+
function addScopeVariableDeclaration(scope, declaration) {
|
|
73
|
+
if (!scope.variableDeclarations) {
|
|
74
|
+
scope.variableDeclarations = [];
|
|
75
|
+
}
|
|
76
|
+
scope.variableDeclarations.push(declaration);
|
|
77
|
+
}
|
|
78
|
+
exports.addScopeVariableDeclaration = addScopeVariableDeclaration;
|
|
71
79
|
function isHoistableFunctionDeclaredInScope(symbol, scopeNode) {
|
|
72
80
|
var _a;
|
|
73
81
|
return (_a = symbol === null || symbol === void 0 ? void 0 : symbol.declarations) === null || _a === void 0 ? void 0 : _a.some(d => ts.isFunctionDeclaration(d) && (0, typescript_1.findFirstNodeAbove)(d, (n) => n === scopeNode));
|
|
@@ -14,3 +14,4 @@ export declare function inferAssignedType(context: TransformationContext, expres
|
|
|
14
14
|
export declare function getAllCallSignatures(type: ts.Type): readonly ts.Signature[];
|
|
15
15
|
export declare function isExpressionWithEvaluationEffect(node: ts.Expression): boolean;
|
|
16
16
|
export declare function getFunctionTypeForCall(context: TransformationContext, node: ts.CallExpression): ts.Type | undefined;
|
|
17
|
+
export declare function isConstIdentifier(context: TransformationContext, node: ts.Node): boolean;
|
|
@@ -10,7 +10,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
10
10
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.getFunctionTypeForCall = exports.isExpressionWithEvaluationEffect = exports.getAllCallSignatures = exports.inferAssignedType = exports.hasStandardLibrarySignature = exports.isStandardLibraryType = exports.getFirstDeclarationInFile = exports.findFirstNodeAbove = exports.hasExportEquals = void 0;
|
|
13
|
+
exports.isConstIdentifier = exports.getFunctionTypeForCall = exports.isExpressionWithEvaluationEffect = exports.getAllCallSignatures = exports.inferAssignedType = exports.hasStandardLibrarySignature = exports.isStandardLibraryType = exports.getFirstDeclarationInFile = exports.findFirstNodeAbove = exports.hasExportEquals = void 0;
|
|
14
14
|
const ts = require("typescript");
|
|
15
15
|
__exportStar(require("./nodes"), exports);
|
|
16
16
|
__exportStar(require("./types"), exports);
|
|
@@ -94,4 +94,19 @@ function getFunctionTypeForCall(context, node) {
|
|
|
94
94
|
return context.checker.getTypeFromTypeNode(typeDeclaration.type);
|
|
95
95
|
}
|
|
96
96
|
exports.getFunctionTypeForCall = getFunctionTypeForCall;
|
|
97
|
+
function isConstIdentifier(context, node) {
|
|
98
|
+
let identifier = node;
|
|
99
|
+
if (ts.isComputedPropertyName(identifier)) {
|
|
100
|
+
identifier = identifier.expression;
|
|
101
|
+
}
|
|
102
|
+
if (!ts.isIdentifier(identifier)) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
const symbol = context.checker.getSymbolAtLocation(identifier);
|
|
106
|
+
if (!symbol || !symbol.declarations) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
return symbol.declarations.some(d => ts.isVariableDeclarationList(d.parent) && (d.parent.flags & ts.NodeFlags.Const) !== 0);
|
|
110
|
+
}
|
|
111
|
+
exports.isConstIdentifier = isConstIdentifier;
|
|
97
112
|
//# sourceMappingURL=index.js.map
|
|
@@ -10,4 +10,6 @@ export declare function isNumberType(context: TransformationContext, type: ts.Ty
|
|
|
10
10
|
*/
|
|
11
11
|
export declare function forTypeOrAnySupertype(context: TransformationContext, type: ts.Type, predicate: (type: ts.Type) => boolean): boolean;
|
|
12
12
|
export declare function isArrayType(context: TransformationContext, type: ts.Type): boolean;
|
|
13
|
-
export declare function isFunctionType(
|
|
13
|
+
export declare function isFunctionType(type: ts.Type): boolean;
|
|
14
|
+
export declare function canBeFalsy(context: TransformationContext, type: ts.Type): boolean;
|
|
15
|
+
export declare function canBeFalsyWhenNotNull(context: TransformationContext, type: ts.Type): boolean;
|
|
@@ -1,21 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isFunctionType = exports.isArrayType = exports.forTypeOrAnySupertype = exports.isNumberType = exports.isStringType = exports.typeCanSatisfy = exports.typeAlwaysSatisfies = exports.isTypeWithFlags = void 0;
|
|
3
|
+
exports.canBeFalsyWhenNotNull = exports.canBeFalsy = exports.isFunctionType = exports.isArrayType = exports.forTypeOrAnySupertype = exports.isNumberType = exports.isStringType = exports.typeCanSatisfy = exports.typeAlwaysSatisfies = exports.isTypeWithFlags = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
function isTypeWithFlags(context, type, flags) {
|
|
6
|
-
const predicate = (type) =>
|
|
7
|
-
if (type.symbol) {
|
|
8
|
-
const baseConstraint = context.checker.getBaseConstraintOfType(type);
|
|
9
|
-
if (baseConstraint && baseConstraint !== type) {
|
|
10
|
-
return isTypeWithFlags(context, baseConstraint, flags);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
return (type.flags & flags) !== 0;
|
|
14
|
-
};
|
|
6
|
+
const predicate = (type) => (type.flags & flags) !== 0;
|
|
15
7
|
return typeAlwaysSatisfies(context, type, predicate);
|
|
16
8
|
}
|
|
17
9
|
exports.isTypeWithFlags = isTypeWithFlags;
|
|
18
10
|
function typeAlwaysSatisfies(context, type, predicate) {
|
|
11
|
+
const baseConstraint = context.checker.getBaseConstraintOfType(type);
|
|
12
|
+
if (baseConstraint) {
|
|
13
|
+
type = baseConstraint;
|
|
14
|
+
}
|
|
19
15
|
if (predicate(type)) {
|
|
20
16
|
return true;
|
|
21
17
|
}
|
|
@@ -29,6 +25,15 @@ function typeAlwaysSatisfies(context, type, predicate) {
|
|
|
29
25
|
}
|
|
30
26
|
exports.typeAlwaysSatisfies = typeAlwaysSatisfies;
|
|
31
27
|
function typeCanSatisfy(context, type, predicate) {
|
|
28
|
+
const baseConstraint = context.checker.getBaseConstraintOfType(type);
|
|
29
|
+
if (!baseConstraint) {
|
|
30
|
+
// type parameter with no constraint can be anything, assume it might satisfy predicate
|
|
31
|
+
if (type.isTypeParameter())
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
type = baseConstraint;
|
|
36
|
+
}
|
|
32
37
|
if (predicate(type)) {
|
|
33
38
|
return true;
|
|
34
39
|
}
|
|
@@ -84,9 +89,31 @@ function isArrayType(context, type) {
|
|
|
84
89
|
return forTypeOrAnySupertype(context, type, t => isExplicitArrayType(context, t));
|
|
85
90
|
}
|
|
86
91
|
exports.isArrayType = isArrayType;
|
|
87
|
-
function isFunctionType(
|
|
88
|
-
|
|
89
|
-
return typeNode !== undefined && ts.isFunctionTypeNode(typeNode);
|
|
92
|
+
function isFunctionType(type) {
|
|
93
|
+
return type.getCallSignatures().length > 0;
|
|
90
94
|
}
|
|
91
95
|
exports.isFunctionType = isFunctionType;
|
|
96
|
+
function canBeFalsy(context, type) {
|
|
97
|
+
const strictNullChecks = context.options.strict === true || context.options.strictNullChecks === true;
|
|
98
|
+
const falsyFlags = ts.TypeFlags.Boolean |
|
|
99
|
+
ts.TypeFlags.BooleanLiteral |
|
|
100
|
+
ts.TypeFlags.Never |
|
|
101
|
+
ts.TypeFlags.Void |
|
|
102
|
+
ts.TypeFlags.Unknown |
|
|
103
|
+
ts.TypeFlags.Any |
|
|
104
|
+
ts.TypeFlags.Undefined |
|
|
105
|
+
ts.TypeFlags.Null;
|
|
106
|
+
return typeCanSatisfy(context, type, type => (type.flags & falsyFlags) !== 0 || (!strictNullChecks && !type.isLiteral()));
|
|
107
|
+
}
|
|
108
|
+
exports.canBeFalsy = canBeFalsy;
|
|
109
|
+
function canBeFalsyWhenNotNull(context, type) {
|
|
110
|
+
const falsyFlags = ts.TypeFlags.Boolean |
|
|
111
|
+
ts.TypeFlags.BooleanLiteral |
|
|
112
|
+
ts.TypeFlags.Never |
|
|
113
|
+
ts.TypeFlags.Void |
|
|
114
|
+
ts.TypeFlags.Unknown |
|
|
115
|
+
ts.TypeFlags.Any;
|
|
116
|
+
return typeCanSatisfy(context, type, type => (type.flags & falsyFlags) !== 0);
|
|
117
|
+
}
|
|
118
|
+
exports.canBeFalsyWhenNotNull = canBeFalsyWhenNotNull;
|
|
92
119
|
//# sourceMappingURL=types.js.map
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import * as ts from "typescript";
|
|
2
2
|
import * as lua from "../../LuaAST";
|
|
3
3
|
import { FunctionVisitor, TransformationContext } from "../context";
|
|
4
|
+
import { ExpressionWithThisValue } from "./optional-chaining";
|
|
4
5
|
export declare function transformElementAccessArgument(context: TransformationContext, node: ts.ElementAccessExpression): lua.Expression;
|
|
5
6
|
export declare const transformElementAccessExpression: FunctionVisitor<ts.ElementAccessExpression>;
|
|
7
|
+
export declare function transformElementAccessExpressionWithCapture(context: TransformationContext, node: ts.ElementAccessExpression, thisValueCapture: lua.Identifier | undefined): ExpressionWithThisValue;
|
|
6
8
|
export declare const transformPropertyAccessExpression: FunctionVisitor<ts.PropertyAccessExpression>;
|
|
9
|
+
export declare function transformPropertyAccessExpressionWithCapture(context: TransformationContext, node: ts.PropertyAccessExpression, thisValueCapture: lua.Identifier | undefined): ExpressionWithThisValue;
|
|
7
10
|
export declare const transformQualifiedName: FunctionVisitor<ts.QualifiedName>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.transformQualifiedName = exports.transformPropertyAccessExpression = exports.transformElementAccessExpression = exports.transformElementAccessArgument = void 0;
|
|
3
|
+
exports.transformQualifiedName = exports.transformPropertyAccessExpressionWithCapture = exports.transformPropertyAccessExpression = exports.transformElementAccessExpressionWithCapture = exports.transformElementAccessExpression = exports.transformElementAccessArgument = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const lua = require("../../LuaAST");
|
|
6
6
|
const builtins_1 = require("../builtins");
|
|
@@ -10,9 +10,10 @@ const lua_ast_1 = require("../utils/lua-ast");
|
|
|
10
10
|
const lualib_1 = require("../utils/lualib");
|
|
11
11
|
const typescript_1 = require("../utils/typescript");
|
|
12
12
|
const enum_1 = require("./enum");
|
|
13
|
+
const expression_list_1 = require("./expression-list");
|
|
13
14
|
const multi_1 = require("./language-extensions/multi");
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
const optional_chaining_1 = require("./optional-chaining");
|
|
16
|
+
function addOneToArrayAccessArgument(context, node, index) {
|
|
16
17
|
const type = context.checker.getTypeAtLocation(node.expression);
|
|
17
18
|
const argumentType = context.checker.getTypeAtLocation(node.argumentExpression);
|
|
18
19
|
if ((0, typescript_1.isArrayType)(context, type) && (0, typescript_1.isNumberType)(context, argumentType)) {
|
|
@@ -20,20 +21,31 @@ function transformElementAccessArgument(context, node) {
|
|
|
20
21
|
}
|
|
21
22
|
return index;
|
|
22
23
|
}
|
|
24
|
+
function transformElementAccessArgument(context, node) {
|
|
25
|
+
const index = context.transformExpression(node.argumentExpression);
|
|
26
|
+
return addOneToArrayAccessArgument(context, node, index);
|
|
27
|
+
}
|
|
23
28
|
exports.transformElementAccessArgument = transformElementAccessArgument;
|
|
24
|
-
const transformElementAccessExpression = (node, context) =>
|
|
29
|
+
const transformElementAccessExpression = (node, context) => transformElementAccessExpressionWithCapture(context, node, undefined).expression;
|
|
30
|
+
exports.transformElementAccessExpression = transformElementAccessExpression;
|
|
31
|
+
function transformElementAccessExpressionWithCapture(context, node, thisValueCapture) {
|
|
25
32
|
const constEnumValue = (0, enum_1.tryGetConstEnumValue)(context, node);
|
|
26
33
|
if (constEnumValue) {
|
|
27
|
-
return constEnumValue;
|
|
34
|
+
return { expression: constEnumValue };
|
|
28
35
|
}
|
|
29
|
-
|
|
36
|
+
if (ts.isOptionalChain(node)) {
|
|
37
|
+
return (0, optional_chaining_1.transformOptionalChainWithCapture)(context, node, thisValueCapture);
|
|
38
|
+
}
|
|
39
|
+
const [table, accessExpression] = (0, expression_list_1.transformOrderedExpressions)(context, [node.expression, node.argumentExpression]);
|
|
30
40
|
const type = context.checker.getTypeAtLocation(node.expression);
|
|
31
41
|
const argumentType = context.checker.getTypeAtLocation(node.argumentExpression);
|
|
32
42
|
if ((0, typescript_1.isStringType)(context, type) && (0, typescript_1.isNumberType)(context, argumentType)) {
|
|
33
|
-
|
|
34
|
-
return
|
|
43
|
+
// strings are not callable, so ignore thisValueCapture
|
|
44
|
+
return {
|
|
45
|
+
expression: (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.StringAccess, node, table, accessExpression),
|
|
46
|
+
};
|
|
35
47
|
}
|
|
36
|
-
const
|
|
48
|
+
const updatedAccessExpression = addOneToArrayAccessArgument(context, node, accessExpression);
|
|
37
49
|
if ((0, multi_1.isMultiReturnCall)(context, node.expression)) {
|
|
38
50
|
const accessType = context.checker.getTypeAtLocation(node.argumentExpression);
|
|
39
51
|
if (!(0, typescript_1.isNumberType)(context, accessType)) {
|
|
@@ -41,61 +53,76 @@ const transformElementAccessExpression = (node, context) => {
|
|
|
41
53
|
}
|
|
42
54
|
// When selecting the first element, we can shortcut
|
|
43
55
|
if (ts.isNumericLiteral(node.argumentExpression) && node.argumentExpression.text === "0") {
|
|
44
|
-
return table;
|
|
56
|
+
return { expression: table };
|
|
45
57
|
}
|
|
46
58
|
else {
|
|
47
59
|
const selectIdentifier = lua.createIdentifier("select");
|
|
48
|
-
|
|
49
|
-
return selectCall;
|
|
60
|
+
return { expression: lua.createCallExpression(selectIdentifier, [updatedAccessExpression, table]) };
|
|
50
61
|
}
|
|
51
62
|
}
|
|
52
|
-
if (
|
|
53
|
-
|
|
63
|
+
if (thisValueCapture) {
|
|
64
|
+
const thisValue = (0, optional_chaining_1.captureThisValue)(context, table, thisValueCapture, node.expression);
|
|
65
|
+
return {
|
|
66
|
+
expression: lua.createTableIndexExpression(thisValue, updatedAccessExpression, node),
|
|
67
|
+
thisValue,
|
|
68
|
+
};
|
|
54
69
|
}
|
|
55
|
-
return lua.createTableIndexExpression(table,
|
|
56
|
-
}
|
|
57
|
-
exports.
|
|
58
|
-
const transformPropertyAccessExpression = (node, context) =>
|
|
70
|
+
return { expression: lua.createTableIndexExpression(table, updatedAccessExpression, node) };
|
|
71
|
+
}
|
|
72
|
+
exports.transformElementAccessExpressionWithCapture = transformElementAccessExpressionWithCapture;
|
|
73
|
+
const transformPropertyAccessExpression = (node, context) => transformPropertyAccessExpressionWithCapture(context, node, undefined).expression;
|
|
74
|
+
exports.transformPropertyAccessExpression = transformPropertyAccessExpression;
|
|
75
|
+
function transformPropertyAccessExpressionWithCapture(context, node, thisValueCapture) {
|
|
59
76
|
const property = node.name.text;
|
|
60
77
|
const type = context.checker.getTypeAtLocation(node.expression);
|
|
78
|
+
const isOptionalLeft = (0, optional_chaining_1.isOptionalContinuation)(node.expression);
|
|
61
79
|
const annotations = (0, annotations_1.getTypeAnnotations)(type);
|
|
62
80
|
if (annotations.has(annotations_1.AnnotationKind.LuaTable)) {
|
|
63
81
|
context.diagnostics.push((0, diagnostics_1.annotationRemoved)(node, annotations_1.AnnotationKind.LuaTable));
|
|
64
82
|
}
|
|
65
83
|
const constEnumValue = (0, enum_1.tryGetConstEnumValue)(context, node);
|
|
66
84
|
if (constEnumValue) {
|
|
67
|
-
return constEnumValue;
|
|
68
|
-
}
|
|
69
|
-
const builtinResult = (0, builtins_1.transformBuiltinPropertyAccessExpression)(context, node);
|
|
70
|
-
if (builtinResult) {
|
|
71
|
-
return builtinResult;
|
|
85
|
+
return { expression: constEnumValue };
|
|
72
86
|
}
|
|
73
87
|
if (ts.isCallExpression(node.expression) && (0, multi_1.returnsMultiType)(context, node.expression)) {
|
|
74
88
|
context.diagnostics.push((0, diagnostics_1.invalidMultiReturnAccess)(node));
|
|
75
89
|
}
|
|
90
|
+
if (ts.isOptionalChain(node)) {
|
|
91
|
+
return (0, optional_chaining_1.transformOptionalChainWithCapture)(context, node, thisValueCapture);
|
|
92
|
+
}
|
|
76
93
|
// Do not output path for member only enums
|
|
77
94
|
if (annotations.has(annotations_1.AnnotationKind.CompileMembersOnly)) {
|
|
95
|
+
if (isOptionalLeft) {
|
|
96
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedOptionalCompileMembersOnly)(node));
|
|
97
|
+
}
|
|
78
98
|
if (ts.isPropertyAccessExpression(node.expression)) {
|
|
79
99
|
// in case of ...x.enum.y transform to ...x.y
|
|
80
|
-
|
|
100
|
+
const expression = lua.createTableIndexExpression(context.transformExpression(node.expression.expression), lua.createStringLiteral(property), node);
|
|
101
|
+
return { expression };
|
|
81
102
|
}
|
|
82
103
|
else {
|
|
83
|
-
return lua.createIdentifier(property, node);
|
|
104
|
+
return { expression: lua.createIdentifier(property, node) };
|
|
84
105
|
}
|
|
85
106
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
107
|
+
const builtinResult = (0, builtins_1.transformBuiltinPropertyAccessExpression)(context, node);
|
|
108
|
+
if (builtinResult) {
|
|
109
|
+
// Ignore thisValueCapture.
|
|
110
|
+
// This assumes that nothing returned by builtin property accesses are callable.
|
|
111
|
+
// If this assumption is no longer true, this may need to be updated.
|
|
112
|
+
return { expression: builtinResult };
|
|
89
113
|
}
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
114
|
+
const table = context.transformExpression(node.expression);
|
|
115
|
+
if (thisValueCapture) {
|
|
116
|
+
const thisValue = (0, optional_chaining_1.captureThisValue)(context, table, thisValueCapture, node.expression);
|
|
117
|
+
const expression = lua.createTableIndexExpression(thisValue, lua.createStringLiteral(property), node);
|
|
118
|
+
return {
|
|
119
|
+
expression,
|
|
120
|
+
thisValue,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return { expression: lua.createTableIndexExpression(table, lua.createStringLiteral(property), node) };
|
|
98
124
|
}
|
|
125
|
+
exports.transformPropertyAccessExpressionWithCapture = transformPropertyAccessExpressionWithCapture;
|
|
99
126
|
const transformQualifiedName = (node, context) => {
|
|
100
127
|
const right = lua.createStringLiteral(node.right.text, node.right);
|
|
101
128
|
const left = context.transformExpression(node.left);
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import * as ts from "typescript";
|
|
2
2
|
import * as lua from "../../../LuaAST";
|
|
3
3
|
import { TransformationContext } from "../../context";
|
|
4
|
-
export declare function transformAssignmentLeftHandSideExpression(context: TransformationContext, node: ts.Expression): lua.AssignmentLeftHandSideExpression;
|
|
5
|
-
export declare function transformAssignment(context: TransformationContext, lhs: ts.Expression, right: lua.Expression, parent?: ts.Expression): lua.Statement[];
|
|
4
|
+
export declare function transformAssignmentLeftHandSideExpression(context: TransformationContext, node: ts.Expression, rightHasPrecedingStatements?: boolean): lua.AssignmentLeftHandSideExpression;
|
|
5
|
+
export declare function transformAssignment(context: TransformationContext, lhs: ts.Expression, right: lua.Expression, rightHasPrecedingStatements?: boolean, parent?: ts.Expression): lua.Statement[];
|
|
6
|
+
export declare function transformAssignmentWithRightPrecedingStatements(context: TransformationContext, lhs: ts.Expression, right: lua.Expression, rightPrecedingStatements: lua.Statement[], parent?: ts.Expression): lua.Statement[];
|
|
6
7
|
export declare function transformAssignmentExpression(context: TransformationContext, expression: ts.AssignmentExpression<ts.EqualsToken>): lua.Expression;
|
|
7
8
|
export declare function transformAssignmentStatement(context: TransformationContext, expression: ts.AssignmentExpression<ts.EqualsToken>): lua.Statement[];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.transformAssignmentStatement = exports.transformAssignmentExpression = exports.transformAssignment = exports.transformAssignmentLeftHandSideExpression = void 0;
|
|
3
|
+
exports.transformAssignmentStatement = exports.transformAssignmentExpression = exports.transformAssignmentWithRightPrecedingStatements = exports.transformAssignment = exports.transformAssignmentLeftHandSideExpression = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const lua = require("../../../LuaAST");
|
|
6
6
|
const utils_1 = require("../../../utils");
|
|
@@ -9,13 +9,27 @@ const export_1 = require("../../utils/export");
|
|
|
9
9
|
const lua_ast_1 = require("../../utils/lua-ast");
|
|
10
10
|
const lualib_1 = require("../../utils/lualib");
|
|
11
11
|
const typescript_1 = require("../../utils/typescript");
|
|
12
|
-
const access_1 = require("../access");
|
|
13
12
|
const destructuring_assignments_1 = require("./destructuring-assignments");
|
|
14
13
|
const multi_1 = require("../language-extensions/multi");
|
|
15
|
-
const scope_1 = require("../../utils/scope");
|
|
16
|
-
const transform_1 = require("../../utils/transform");
|
|
17
14
|
const diagnostics_1 = require("../../utils/diagnostics");
|
|
18
|
-
|
|
15
|
+
const access_1 = require("../access");
|
|
16
|
+
const expression_list_1 = require("../expression-list");
|
|
17
|
+
const preceding_statements_1 = require("../../utils/preceding-statements");
|
|
18
|
+
function transformAssignmentLeftHandSideExpression(context, node, rightHasPrecedingStatements) {
|
|
19
|
+
// Access expressions need the components of the left side cached in temps before the right side's preceding statements
|
|
20
|
+
if (rightHasPrecedingStatements && (ts.isElementAccessExpression(node) || ts.isPropertyAccessExpression(node))) {
|
|
21
|
+
let table = context.transformExpression(node.expression);
|
|
22
|
+
table = (0, expression_list_1.moveToPrecedingTemp)(context, table, node.expression);
|
|
23
|
+
let index;
|
|
24
|
+
if (ts.isElementAccessExpression(node)) {
|
|
25
|
+
index = (0, access_1.transformElementAccessArgument)(context, node);
|
|
26
|
+
index = (0, expression_list_1.moveToPrecedingTemp)(context, index, node.argumentExpression);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
index = lua.createStringLiteral(node.name.text, node.name);
|
|
30
|
+
}
|
|
31
|
+
return lua.createTableIndexExpression(table, index, node);
|
|
32
|
+
}
|
|
19
33
|
const symbol = context.checker.getSymbolAtLocation(node);
|
|
20
34
|
const left = context.transformExpression(node);
|
|
21
35
|
return lua.isIdentifier(left) && symbol && (0, export_1.isSymbolExported)(context, symbol)
|
|
@@ -25,7 +39,7 @@ function transformAssignmentLeftHandSideExpression(context, node) {
|
|
|
25
39
|
exports.transformAssignmentLeftHandSideExpression = transformAssignmentLeftHandSideExpression;
|
|
26
40
|
function transformAssignment(context,
|
|
27
41
|
// TODO: Change type to ts.LeftHandSideExpression?
|
|
28
|
-
lhs, right, parent) {
|
|
42
|
+
lhs, right, rightHasPrecedingStatements, parent) {
|
|
29
43
|
if (ts.isOptionalChain(lhs)) {
|
|
30
44
|
context.diagnostics.push((0, diagnostics_1.notAllowedOptionalAssignment)(lhs));
|
|
31
45
|
return [];
|
|
@@ -34,11 +48,11 @@ lhs, right, parent) {
|
|
|
34
48
|
const arrayLengthAssignment = lua.createExpressionStatement((0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ArraySetLength, parent, context.transformExpression(lhs.expression), right));
|
|
35
49
|
return [arrayLengthAssignment];
|
|
36
50
|
}
|
|
37
|
-
const symbol = ts.isShorthandPropertyAssignment(lhs.parent)
|
|
51
|
+
const symbol = lhs.parent && ts.isShorthandPropertyAssignment(lhs.parent)
|
|
38
52
|
? context.checker.getShorthandAssignmentValueSymbol(lhs.parent)
|
|
39
53
|
: context.checker.getSymbolAtLocation(lhs);
|
|
40
54
|
const dependentSymbols = symbol ? (0, export_1.getDependenciesOfSymbol)(context, symbol) : [];
|
|
41
|
-
const left = transformAssignmentLeftHandSideExpression(context, lhs);
|
|
55
|
+
const left = transformAssignmentLeftHandSideExpression(context, lhs, rightHasPrecedingStatements);
|
|
42
56
|
const rootAssignment = lua.createAssignmentStatement(left, right, lhs.parent);
|
|
43
57
|
return [
|
|
44
58
|
rootAssignment,
|
|
@@ -50,15 +64,23 @@ lhs, right, parent) {
|
|
|
50
64
|
];
|
|
51
65
|
}
|
|
52
66
|
exports.transformAssignment = transformAssignment;
|
|
67
|
+
function transformAssignmentWithRightPrecedingStatements(context, lhs, right, rightPrecedingStatements, parent) {
|
|
68
|
+
return [
|
|
69
|
+
...rightPrecedingStatements,
|
|
70
|
+
...transformAssignment(context, lhs, right, rightPrecedingStatements.length > 0, parent),
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
exports.transformAssignmentWithRightPrecedingStatements = transformAssignmentWithRightPrecedingStatements;
|
|
53
74
|
function transformDestructuredAssignmentExpression(context, expression) {
|
|
54
|
-
const rootIdentifier =
|
|
55
|
-
let right = context.transformExpression(expression.right);
|
|
75
|
+
const rootIdentifier = context.createTempNameForNode(expression.right);
|
|
76
|
+
let [rightPrecedingStatements, right] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(expression.right));
|
|
77
|
+
context.addPrecedingStatements(rightPrecedingStatements);
|
|
56
78
|
if ((0, multi_1.isMultiReturnCall)(context, expression.right)) {
|
|
57
79
|
right = (0, lua_ast_1.wrapInTable)(right);
|
|
58
80
|
}
|
|
59
81
|
const statements = [
|
|
60
82
|
lua.createVariableDeclarationStatement(rootIdentifier, right),
|
|
61
|
-
...(0, destructuring_assignments_1.transformDestructuringAssignment)(context, expression, rootIdentifier),
|
|
83
|
+
...(0, destructuring_assignments_1.transformDestructuringAssignment)(context, expression, rootIdentifier, rightPrecedingStatements.length > 0),
|
|
62
84
|
];
|
|
63
85
|
return { statements, result: rootIdentifier };
|
|
64
86
|
}
|
|
@@ -72,48 +94,28 @@ function transformAssignmentExpression(context, expression) {
|
|
|
72
94
|
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ArraySetLength, expression, context.transformExpression(expression.left.expression), context.transformExpression(expression.right));
|
|
73
95
|
}
|
|
74
96
|
if ((0, typescript_1.isDestructuringAssignment)(expression)) {
|
|
75
|
-
|
|
97
|
+
const { statements, result } = transformDestructuredAssignmentExpression(context, expression);
|
|
98
|
+
context.addPrecedingStatements(statements);
|
|
99
|
+
return result;
|
|
76
100
|
}
|
|
77
101
|
if (ts.isPropertyAccessExpression(expression.left) || ts.isElementAccessExpression(expression.left)) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
lua.createAssignmentStatement(indexStatement, valueParameter),
|
|
86
|
-
lua.createReturnStatement([valueParameter]),
|
|
87
|
-
];
|
|
88
|
-
const iife = lua.createFunctionExpression(lua.createBlock(statements), [
|
|
89
|
-
objParameter,
|
|
90
|
-
indexParameter,
|
|
91
|
-
valueParameter,
|
|
102
|
+
const tempVar = context.createTempNameForNode(expression.right);
|
|
103
|
+
const [precedingStatements, right] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(expression.right));
|
|
104
|
+
const left = transformAssignmentLeftHandSideExpression(context, expression.left, precedingStatements.length > 0);
|
|
105
|
+
context.addPrecedingStatements([
|
|
106
|
+
...precedingStatements,
|
|
107
|
+
lua.createVariableDeclarationStatement(tempVar, right, expression.right),
|
|
108
|
+
lua.createAssignmentStatement(left, lua.cloneIdentifier(tempVar), expression.left),
|
|
92
109
|
]);
|
|
93
|
-
|
|
94
|
-
const objExpression = context.transformExpression(expression.left.expression);
|
|
95
|
-
let indexExpression;
|
|
96
|
-
if (ts.isPropertyAccessExpression(expression.left)) {
|
|
97
|
-
// Property access
|
|
98
|
-
indexExpression = lua.createStringLiteral(expression.left.name.text);
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
// Element access
|
|
102
|
-
indexExpression = (0, access_1.transformElementAccessArgument)(context, expression.left);
|
|
103
|
-
}
|
|
104
|
-
const args = [objExpression, indexExpression, context.transformExpression(expression.right)];
|
|
105
|
-
(0, scope_1.popScope)(context);
|
|
106
|
-
return lua.createCallExpression(iife, args, expression);
|
|
110
|
+
return lua.cloneIdentifier(tempVar);
|
|
107
111
|
}
|
|
108
112
|
else {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return { statements, result: left };
|
|
116
|
-
}, expression);
|
|
113
|
+
// Simple assignment
|
|
114
|
+
// ${left} = ${right}; return ${left}
|
|
115
|
+
const left = context.transformExpression(expression.left);
|
|
116
|
+
const right = context.transformExpression(expression.right);
|
|
117
|
+
context.addPrecedingStatements(transformAssignment(context, expression.left, right));
|
|
118
|
+
return left;
|
|
117
119
|
}
|
|
118
120
|
}
|
|
119
121
|
exports.transformAssignmentExpression = transformAssignmentExpression;
|
|
@@ -123,7 +125,9 @@ const canBeTransformedToLuaAssignmentStatement = (context, node) => ts.isArrayLi
|
|
|
123
125
|
return false;
|
|
124
126
|
}
|
|
125
127
|
if (ts.isPropertyAccessExpression(element) || ts.isElementAccessExpression(element)) {
|
|
126
|
-
|
|
128
|
+
// Lua's execution order for multi-assignments is not the same as JS's, so we should always
|
|
129
|
+
// break these down when the left side may have side effects.
|
|
130
|
+
return false;
|
|
127
131
|
}
|
|
128
132
|
if (ts.isIdentifier(element)) {
|
|
129
133
|
const symbol = context.checker.getSymbolAtLocation(element);
|
|
@@ -141,28 +145,33 @@ function transformAssignmentStatement(context, expression) {
|
|
|
141
145
|
if ((0, typescript_1.isDestructuringAssignment)(expression)) {
|
|
142
146
|
if (canBeTransformedToLuaAssignmentStatement(context, expression)) {
|
|
143
147
|
const rightType = context.checker.getTypeAtLocation(expression.right);
|
|
144
|
-
let right
|
|
148
|
+
let right;
|
|
145
149
|
if (ts.isArrayLiteralExpression(expression.right)) {
|
|
146
|
-
right = expression.right.elements
|
|
150
|
+
right = (0, expression_list_1.transformExpressionList)(context, expression.right.elements);
|
|
147
151
|
}
|
|
148
|
-
else
|
|
149
|
-
right =
|
|
152
|
+
else {
|
|
153
|
+
right = context.transformExpression(expression.right);
|
|
154
|
+
if (!(0, multi_1.isMultiReturnCall)(context, expression.right) && (0, typescript_1.isArrayType)(context, rightType)) {
|
|
155
|
+
right = (0, lua_ast_1.createUnpackCall)(context, right, expression.right);
|
|
156
|
+
}
|
|
150
157
|
}
|
|
151
158
|
const left = expression.left.elements.map(e => transformAssignmentLeftHandSideExpression(context, e));
|
|
152
159
|
return [lua.createAssignmentStatement(left, right, expression)];
|
|
153
160
|
}
|
|
154
|
-
let right = context.transformExpression(expression.right);
|
|
161
|
+
let [rightPrecedingStatements, right] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(expression.right));
|
|
162
|
+
context.addPrecedingStatements(rightPrecedingStatements);
|
|
155
163
|
if ((0, multi_1.isMultiReturnCall)(context, expression.right)) {
|
|
156
164
|
right = (0, lua_ast_1.wrapInTable)(right);
|
|
157
165
|
}
|
|
158
|
-
const rootIdentifier =
|
|
166
|
+
const rootIdentifier = context.createTempNameForNode(expression.left);
|
|
159
167
|
return [
|
|
160
168
|
lua.createVariableDeclarationStatement(rootIdentifier, right),
|
|
161
|
-
...(0, destructuring_assignments_1.transformDestructuringAssignment)(context, expression, rootIdentifier),
|
|
169
|
+
...(0, destructuring_assignments_1.transformDestructuringAssignment)(context, expression, rootIdentifier, rightPrecedingStatements.length > 0),
|
|
162
170
|
];
|
|
163
171
|
}
|
|
164
172
|
else {
|
|
165
|
-
|
|
173
|
+
const [precedingStatements, right] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(expression.right));
|
|
174
|
+
return transformAssignmentWithRightPrecedingStatements(context, expression.left, right, precedingStatements);
|
|
166
175
|
}
|
|
167
176
|
}
|
|
168
177
|
exports.transformAssignmentStatement = transformAssignmentStatement;
|