typescript-to-lua 1.0.1 → 1.3.0
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 +46 -0
- package/dist/CompilerOptions.d.ts +4 -3
- package/dist/LuaAST.d.ts +1 -0
- package/dist/LuaAST.js +14 -6
- package/dist/LuaLib.d.ts +4 -3
- package/dist/LuaLib.js +6 -5
- package/dist/LuaPrinter.d.ts +15 -4
- package/dist/LuaPrinter.js +81 -29
- package/dist/cli/diagnostics.js +2 -2
- package/dist/cli/parse.d.ts +1 -1
- package/dist/cli/parse.js +14 -0
- package/dist/cli/report.js +1 -1
- package/dist/cli/tsconfig.js +4 -4
- 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 +43 -19
- 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 +44 -26
- package/dist/lualib/Set.lua +119 -123
- package/dist/lualib/SetDescriptor.lua +7 -1
- package/dist/lualib/SourceMapTraceBack.lua +19 -9
- 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 +12 -11
- package/dist/lualib/StringReplaceAll.lua +35 -0
- 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 +876 -745
- package/dist/transformation/builtins/array.js +29 -30
- package/dist/transformation/builtins/console.js +2 -2
- package/dist/transformation/builtins/function.js +14 -12
- package/dist/transformation/builtins/global.js +8 -8
- package/dist/transformation/builtins/index.d.ts +1 -1
- package/dist/transformation/builtins/index.js +69 -36
- package/dist/transformation/builtins/math.js +4 -4
- package/dist/transformation/builtins/number.js +7 -7
- package/dist/transformation/builtins/object.d.ts +2 -1
- package/dist/transformation/builtins/object.js +12 -13
- package/dist/transformation/builtins/promise.js +9 -9
- package/dist/transformation/builtins/string.js +32 -31
- package/dist/transformation/builtins/symbol.js +3 -3
- package/dist/transformation/context/context.d.ts +12 -0
- package/dist/transformation/context/context.js +105 -6
- package/dist/transformation/index.js +1 -1
- package/dist/transformation/utils/annotations.d.ts +1 -3
- package/dist/transformation/utils/annotations.js +0 -2
- package/dist/transformation/utils/assignment-validation.js +8 -7
- package/dist/transformation/utils/diagnostics.d.ts +9 -0
- package/dist/transformation/utils/diagnostics.js +8 -5
- package/dist/transformation/utils/export.d.ts +3 -2
- package/dist/transformation/utils/export.js +14 -7
- package/dist/transformation/utils/function-context.js +8 -8
- 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 +25 -35
- package/dist/transformation/utils/lualib.js +1 -1
- 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 +13 -7
- package/dist/transformation/utils/scope.d.ts +9 -1
- package/dist/transformation/utils/scope.js +78 -40
- package/dist/transformation/utils/symbols.js +6 -6
- package/dist/transformation/utils/typescript/index.d.ts +1 -0
- package/dist/transformation/utils/typescript/index.js +16 -1
- package/dist/transformation/utils/typescript/nodes.d.ts +2 -0
- package/dist/transformation/utils/typescript/nodes.js +21 -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 +74 -47
- package/dist/transformation/visitors/async-await.js +5 -4
- package/dist/transformation/visitors/binary-expression/assignments.d.ts +3 -2
- package/dist/transformation/visitors/binary-expression/assignments.js +86 -77
- package/dist/transformation/visitors/binary-expression/bit.js +4 -4
- package/dist/transformation/visitors/binary-expression/compound.d.ts +5 -4
- package/dist/transformation/visitors/binary-expression/compound.js +69 -71
- package/dist/transformation/visitors/binary-expression/destructuring-assignments.d.ts +2 -2
- package/dist/transformation/visitors/binary-expression/destructuring-assignments.js +65 -36
- package/dist/transformation/visitors/binary-expression/index.d.ts +3 -1
- package/dist/transformation/visitors/binary-expression/index.js +84 -42
- package/dist/transformation/visitors/block.js +6 -6
- package/dist/transformation/visitors/break-continue.js +2 -2
- package/dist/transformation/visitors/call.d.ts +3 -2
- package/dist/transformation/visitors/call.js +130 -143
- package/dist/transformation/visitors/class/decorators.js +3 -3
- package/dist/transformation/visitors/class/index.js +43 -39
- package/dist/transformation/visitors/class/members/accessors.js +6 -6
- package/dist/transformation/visitors/class/members/constructor.js +7 -7
- package/dist/transformation/visitors/class/members/fields.js +6 -6
- package/dist/transformation/visitors/class/members/method.js +6 -6
- package/dist/transformation/visitors/class/new.js +14 -16
- package/dist/transformation/visitors/class/setup.js +11 -11
- package/dist/transformation/visitors/class/utils.js +2 -2
- package/dist/transformation/visitors/conditional.js +36 -43
- package/dist/transformation/visitors/delete.js +8 -4
- package/dist/transformation/visitors/enum.js +8 -8
- package/dist/transformation/visitors/errors.js +35 -33
- 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.js +10 -6
- package/dist/transformation/visitors/function.d.ts +2 -0
- package/dist/transformation/visitors/function.js +80 -33
- package/dist/transformation/visitors/identifier.js +26 -22
- package/dist/transformation/visitors/index.js +2 -2
- package/dist/transformation/visitors/language-extensions/iterable.js +7 -7
- package/dist/transformation/visitors/language-extensions/multi.d.ts +1 -1
- package/dist/transformation/visitors/language-extensions/multi.js +3 -3
- package/dist/transformation/visitors/language-extensions/operators.js +6 -6
- 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/range.js +6 -6
- package/dist/transformation/visitors/language-extensions/table.js +5 -5
- package/dist/transformation/visitors/literal.d.ts +1 -2
- package/dist/transformation/visitors/literal.js +87 -57
- package/dist/transformation/visitors/loops/do-while.js +35 -9
- package/dist/transformation/visitors/loops/for-in.js +4 -4
- package/dist/transformation/visitors/loops/for-of.js +17 -13
- package/dist/transformation/visitors/loops/for.js +28 -8
- package/dist/transformation/visitors/loops/utils.d.ts +1 -0
- package/dist/transformation/visitors/loops/utils.js +26 -10
- package/dist/transformation/visitors/modules/export.js +14 -15
- package/dist/transformation/visitors/modules/import.js +10 -10
- package/dist/transformation/visitors/namespace.js +13 -13
- package/dist/transformation/visitors/optional-chaining.d.ts +16 -0
- package/dist/transformation/visitors/optional-chaining.js +172 -0
- package/dist/transformation/visitors/return.d.ts +1 -0
- package/dist/transformation/visitors/return.js +40 -26
- package/dist/transformation/visitors/sourceFile.js +12 -9
- package/dist/transformation/visitors/spread.js +23 -16
- package/dist/transformation/visitors/switch.js +63 -20
- package/dist/transformation/visitors/template.js +10 -7
- package/dist/transformation/visitors/typeof.js +4 -2
- package/dist/transformation/visitors/typescript.js +1 -1
- package/dist/transformation/visitors/unary-expression.js +9 -9
- package/dist/transformation/visitors/variable-declaration.js +55 -32
- package/dist/transformation/visitors/void.d.ts +6 -0
- package/dist/transformation/visitors/void.js +20 -0
- package/dist/transpilation/bundle.d.ts +3 -0
- package/dist/transpilation/bundle.js +52 -10
- package/dist/transpilation/diagnostics.js +3 -3
- package/dist/transpilation/index.js +2 -2
- package/dist/transpilation/output-collector.js +2 -2
- package/dist/transpilation/plugins.js +1 -1
- package/dist/transpilation/resolve.js +20 -13
- package/dist/transpilation/transformers.js +45 -25
- package/dist/transpilation/transpile.js +6 -6
- package/dist/transpilation/transpiler.js +8 -8
- package/dist/transpilation/utils.js +5 -3
- package/dist/tstl.js +8 -8
- package/dist/utils.js +2 -2
- package/language-extensions/index.d.ts +11 -1
- package/package.json +15 -12
- 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
- package/dist/transformation/visitors/jsx/jsx.d.ts +0 -9
- package/dist/transformation/visitors/jsx/jsx.js +0 -237
- package/dist/transformation/visitors/jsx/xhtml.d.ts +0 -3
- package/dist/transformation/visitors/jsx/xhtml.js +0 -260
|
@@ -6,6 +6,7 @@ export declare enum ExtensionKind {
|
|
|
6
6
|
RangeFunction = "RangeFunction",
|
|
7
7
|
VarargConstant = "VarargConstant",
|
|
8
8
|
IterableType = "IterableType",
|
|
9
|
+
PairsIterableType = "PairsIterableType",
|
|
9
10
|
AdditionOperatorType = "AdditionOperatorType",
|
|
10
11
|
AdditionOperatorMethodType = "AdditionOperatorMethodType",
|
|
11
12
|
SubtractionOperatorType = "SubtractionOperatorType",
|
|
@@ -53,4 +54,5 @@ export declare enum ExtensionKind {
|
|
|
53
54
|
TableSetMethodType = "TableSetMethodType"
|
|
54
55
|
}
|
|
55
56
|
export declare function isExtensionType(type: ts.Type, extensionKind: ExtensionKind): boolean;
|
|
57
|
+
export declare function getExtensionKinds(type: ts.Type): ExtensionKind[];
|
|
56
58
|
export declare function isExtensionValue(context: TransformationContext, symbol: ts.Symbol, extensionKind: ExtensionKind): boolean;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isExtensionValue = exports.isExtensionType = exports.ExtensionKind = void 0;
|
|
3
|
+
exports.isExtensionValue = exports.getExtensionKinds = exports.isExtensionType = exports.ExtensionKind = void 0;
|
|
4
4
|
var ExtensionKind;
|
|
5
5
|
(function (ExtensionKind) {
|
|
6
6
|
ExtensionKind["MultiFunction"] = "MultiFunction";
|
|
@@ -8,6 +8,7 @@ var ExtensionKind;
|
|
|
8
8
|
ExtensionKind["RangeFunction"] = "RangeFunction";
|
|
9
9
|
ExtensionKind["VarargConstant"] = "VarargConstant";
|
|
10
10
|
ExtensionKind["IterableType"] = "IterableType";
|
|
11
|
+
ExtensionKind["PairsIterableType"] = "PairsIterableType";
|
|
11
12
|
ExtensionKind["AdditionOperatorType"] = "AdditionOperatorType";
|
|
12
13
|
ExtensionKind["AdditionOperatorMethodType"] = "AdditionOperatorMethodType";
|
|
13
14
|
ExtensionKind["SubtractionOperatorType"] = "SubtractionOperatorType";
|
|
@@ -65,6 +66,7 @@ const extensionKindToTypeBrand = {
|
|
|
65
66
|
[ExtensionKind.RangeFunction]: "__luaRangeFunctionBrand",
|
|
66
67
|
[ExtensionKind.VarargConstant]: "__luaVarargConstantBrand",
|
|
67
68
|
[ExtensionKind.IterableType]: "__luaIterableBrand",
|
|
69
|
+
[ExtensionKind.PairsIterableType]: "__luaPairsIterableBrand",
|
|
68
70
|
[ExtensionKind.AdditionOperatorType]: "__luaAdditionBrand",
|
|
69
71
|
[ExtensionKind.AdditionOperatorMethodType]: "__luaAdditionMethodBrand",
|
|
70
72
|
[ExtensionKind.SubtractionOperatorType]: "__luaSubtractionBrand",
|
|
@@ -116,6 +118,10 @@ function isExtensionType(type, extensionKind) {
|
|
|
116
118
|
return typeBrand !== undefined && type.getProperty(typeBrand) !== undefined;
|
|
117
119
|
}
|
|
118
120
|
exports.isExtensionType = isExtensionType;
|
|
121
|
+
function getExtensionKinds(type) {
|
|
122
|
+
return Object.keys(extensionKindToTypeBrand).filter(e => type.getProperty(extensionKindToTypeBrand[e]) !== undefined);
|
|
123
|
+
}
|
|
124
|
+
exports.getExtensionKinds = getExtensionKinds;
|
|
119
125
|
function isExtensionValue(context, symbol, extensionKind) {
|
|
120
126
|
var _a;
|
|
121
127
|
return (symbol.getName() === extensionKindToValueName[extensionKind] &&
|
|
@@ -7,7 +7,6 @@ export declare function createSelfIdentifier(tsOriginal?: ts.Node): lua.Identifi
|
|
|
7
7
|
export declare function createExportsIdentifier(): lua.Identifier;
|
|
8
8
|
export declare function addToNumericExpression(expression: lua.Expression, change: number): lua.Expression;
|
|
9
9
|
export declare function getNumberLiteralValue(expression?: lua.Expression): number | undefined;
|
|
10
|
-
export declare function createImmediatelyInvokedFunctionExpression(statements: lua.Statement[], result: lua.Expression | lua.Expression[], tsOriginal?: ts.Node): lua.CallExpression;
|
|
11
10
|
export declare function createUnpackCall(context: TransformationContext, expression: lua.Expression, tsOriginal?: ts.Node): lua.Expression;
|
|
12
11
|
export declare function wrapInTable(...expressions: lua.Expression[]): lua.TableExpression;
|
|
13
12
|
export declare function wrapInToStringForConcat(expression: lua.Expression): lua.Expression;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createNaN = exports.createLocalOrExportedOrGlobalDeclaration = exports.createHoistableVariableDeclarationStatement = exports.wrapInToStringForConcat = exports.wrapInTable = exports.createUnpackCall = exports.
|
|
3
|
+
exports.createNaN = exports.createLocalOrExportedOrGlobalDeclaration = exports.createHoistableVariableDeclarationStatement = exports.wrapInToStringForConcat = exports.wrapInTable = exports.createUnpackCall = exports.getNumberLiteralValue = exports.addToNumericExpression = exports.createExportsIdentifier = exports.createSelfIdentifier = exports.unwrapVisitorResult = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const CompilerOptions_1 = require("../../CompilerOptions");
|
|
6
6
|
const lua = require("../../LuaAST");
|
|
@@ -10,7 +10,7 @@ const scope_1 = require("./scope");
|
|
|
10
10
|
const lualib_1 = require("./lualib");
|
|
11
11
|
const LuaLib_1 = require("../../LuaLib");
|
|
12
12
|
function unwrapVisitorResult(result) {
|
|
13
|
-
return result === undefined ? [] : utils_1.castArray(result);
|
|
13
|
+
return result === undefined ? [] : (0, utils_1.castArray)(result);
|
|
14
14
|
}
|
|
15
15
|
exports.unwrapVisitorResult = unwrapVisitorResult;
|
|
16
16
|
function createSelfIdentifier(tsOriginal) {
|
|
@@ -55,18 +55,9 @@ function getNumberLiteralValue(expression) {
|
|
|
55
55
|
return undefined;
|
|
56
56
|
}
|
|
57
57
|
exports.getNumberLiteralValue = getNumberLiteralValue;
|
|
58
|
-
// Prefer use of transformToImmediatelyInvokedFunctionExpression to maintain correct scope. If you use this directly,
|
|
59
|
-
// ensure you push/pop a function scope appropriately to avoid incorrect vararg optimization.
|
|
60
|
-
function createImmediatelyInvokedFunctionExpression(statements, result, tsOriginal) {
|
|
61
|
-
const body = [...statements, lua.createReturnStatement(utils_1.castArray(result))];
|
|
62
|
-
const flags = statements.length === 0 ? lua.FunctionExpressionFlags.Inline : lua.FunctionExpressionFlags.None;
|
|
63
|
-
const iife = lua.createFunctionExpression(lua.createBlock(body), undefined, undefined, flags);
|
|
64
|
-
return lua.createCallExpression(iife, [], tsOriginal);
|
|
65
|
-
}
|
|
66
|
-
exports.createImmediatelyInvokedFunctionExpression = createImmediatelyInvokedFunctionExpression;
|
|
67
58
|
function createUnpackCall(context, expression, tsOriginal) {
|
|
68
59
|
if (context.luaTarget === CompilerOptions_1.LuaTarget.Universal) {
|
|
69
|
-
return lualib_1.transformLuaLibFunction(context, LuaLib_1.LuaLibFeature.Unpack, tsOriginal, expression);
|
|
60
|
+
return (0, lualib_1.transformLuaLibFunction)(context, LuaLib_1.LuaLibFeature.Unpack, tsOriginal, expression);
|
|
70
61
|
}
|
|
71
62
|
const unpack = context.luaTarget === CompilerOptions_1.LuaTarget.Lua51 || context.luaTarget === CompilerOptions_1.LuaTarget.LuaJIT
|
|
72
63
|
? lua.createIdentifier("unpack")
|
|
@@ -91,12 +82,9 @@ exports.wrapInToStringForConcat = wrapInToStringForConcat;
|
|
|
91
82
|
function createHoistableVariableDeclarationStatement(context, identifier, initializer, tsOriginal) {
|
|
92
83
|
const declaration = lua.createVariableDeclarationStatement(identifier, initializer, tsOriginal);
|
|
93
84
|
if (identifier.symbolId !== undefined) {
|
|
94
|
-
const scope = scope_1.peekScope(context);
|
|
95
|
-
utils_1.assert(scope.type !== scope_1.ScopeType.Switch);
|
|
96
|
-
|
|
97
|
-
scope.variableDeclarations = [];
|
|
98
|
-
}
|
|
99
|
-
scope.variableDeclarations.push(declaration);
|
|
85
|
+
const scope = (0, scope_1.peekScope)(context);
|
|
86
|
+
(0, utils_1.assert)(scope.type !== scope_1.ScopeType.Switch);
|
|
87
|
+
(0, scope_1.addScopeVariableDeclaration)(scope, declaration);
|
|
100
88
|
}
|
|
101
89
|
return declaration;
|
|
102
90
|
}
|
|
@@ -106,48 +94,50 @@ function hasMultipleReferences(scope, identifiers) {
|
|
|
106
94
|
if (!scopeSymbols) {
|
|
107
95
|
return false;
|
|
108
96
|
}
|
|
109
|
-
const referenceLists = utils_1.castArray(identifiers).map(i => i.symbolId && scopeSymbols.get(i.symbolId));
|
|
97
|
+
const referenceLists = (0, utils_1.castArray)(identifiers).map(i => i.symbolId && scopeSymbols.get(i.symbolId));
|
|
110
98
|
return referenceLists.some(symbolRefs => symbolRefs && symbolRefs.length > 1);
|
|
111
99
|
}
|
|
112
100
|
function createLocalOrExportedOrGlobalDeclaration(context, lhs, rhs, tsOriginal, overrideExportScope) {
|
|
113
101
|
let declaration;
|
|
114
102
|
let assignment;
|
|
115
103
|
const isFunctionDeclaration = tsOriginal !== undefined && ts.isFunctionDeclaration(tsOriginal);
|
|
116
|
-
const identifiers = utils_1.castArray(lhs);
|
|
104
|
+
const identifiers = (0, utils_1.castArray)(lhs);
|
|
117
105
|
if (identifiers.length === 0) {
|
|
118
106
|
return [];
|
|
119
107
|
}
|
|
120
|
-
const exportScope = overrideExportScope !== null && overrideExportScope !== void 0 ? overrideExportScope : export_1.getIdentifierExportScope(context, identifiers[0]);
|
|
108
|
+
const exportScope = overrideExportScope !== null && overrideExportScope !== void 0 ? overrideExportScope : (0, export_1.getIdentifierExportScope)(context, identifiers[0]);
|
|
121
109
|
if (exportScope) {
|
|
122
110
|
// exported
|
|
123
111
|
if (!rhs) {
|
|
124
112
|
return [];
|
|
125
113
|
}
|
|
126
114
|
else {
|
|
127
|
-
assignment = lua.createAssignmentStatement(identifiers.map(identifier => export_1.createExportedIdentifier(context, identifier, exportScope)), rhs, tsOriginal);
|
|
115
|
+
assignment = lua.createAssignmentStatement(identifiers.map(identifier => (0, export_1.createExportedIdentifier)(context, identifier, exportScope)), rhs, tsOriginal);
|
|
128
116
|
}
|
|
129
117
|
}
|
|
130
118
|
else {
|
|
131
|
-
const scope = scope_1.peekScope(context);
|
|
119
|
+
const scope = (0, scope_1.peekScope)(context);
|
|
132
120
|
const isTopLevelVariable = scope.type === scope_1.ScopeType.File;
|
|
133
121
|
if (context.isModule || !isTopLevelVariable) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
122
|
+
const isLuaFunctionExpression = rhs && !Array.isArray(rhs) && lua.isFunctionExpression(rhs);
|
|
123
|
+
const isSafeRecursiveFunctionDeclaration = isFunctionDeclaration && isLuaFunctionExpression;
|
|
124
|
+
if (!isSafeRecursiveFunctionDeclaration && hasMultipleReferences(scope, lhs)) {
|
|
125
|
+
// Split declaration and assignment of identifiers that reference themselves in their declaration.
|
|
126
|
+
// Put declaration above preceding statements in case the identifier is referenced in those.
|
|
127
|
+
const precedingDeclaration = lua.createVariableDeclarationStatement(lhs, undefined, tsOriginal);
|
|
128
|
+
context.prependPrecedingStatements(precedingDeclaration);
|
|
137
129
|
if (rhs) {
|
|
138
130
|
assignment = lua.createAssignmentStatement(lhs, rhs, tsOriginal);
|
|
139
131
|
}
|
|
132
|
+
// Remember local variable declarations for hoisting later
|
|
133
|
+
(0, scope_1.addScopeVariableDeclaration)(scope, precedingDeclaration);
|
|
140
134
|
}
|
|
141
135
|
else {
|
|
142
136
|
declaration = lua.createVariableDeclarationStatement(lhs, rhs, tsOriginal);
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
scope.variableDeclarations.push(declaration);
|
|
149
|
-
if (scope.type === scope_1.ScopeType.Switch) {
|
|
150
|
-
declaration = undefined;
|
|
137
|
+
if (!isFunctionDeclaration) {
|
|
138
|
+
// Remember local variable declarations for hoisting later
|
|
139
|
+
(0, scope_1.addScopeVariableDeclaration)(scope, declaration);
|
|
140
|
+
}
|
|
151
141
|
}
|
|
152
142
|
}
|
|
153
143
|
else if (rhs) {
|
|
@@ -161,7 +151,7 @@ function createLocalOrExportedOrGlobalDeclaration(context, lhs, rhs, tsOriginal,
|
|
|
161
151
|
if (isFunctionDeclaration) {
|
|
162
152
|
// Remember function definitions for hoisting later
|
|
163
153
|
const functionSymbolId = lhs.symbolId;
|
|
164
|
-
const scope = scope_1.peekScope(context);
|
|
154
|
+
const scope = (0, scope_1.peekScope)(context);
|
|
165
155
|
if (functionSymbolId && scope.functionDefinitions) {
|
|
166
156
|
const definitions = scope.functionDefinitions.get(functionSymbolId);
|
|
167
157
|
if (definitions) {
|
|
@@ -7,7 +7,7 @@ Object.defineProperty(exports, "LuaLibFeature", { enumerable: true, get: functio
|
|
|
7
7
|
const utils_1 = require("../../utils");
|
|
8
8
|
const luaLibFeatures = new WeakMap();
|
|
9
9
|
function getUsedLuaLibFeatures(context) {
|
|
10
|
-
return utils_1.getOrUpdate(luaLibFeatures, context, () => new Set());
|
|
10
|
+
return (0, utils_1.getOrUpdate)(luaLibFeatures, context, () => new Set());
|
|
11
11
|
}
|
|
12
12
|
exports.getUsedLuaLibFeatures = getUsedLuaLibFeatures;
|
|
13
13
|
function importLuaLibFeature(context, feature) {
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import * as lua from "../../LuaAST";
|
|
2
|
+
import { TransformationContext } from "../context";
|
|
3
|
+
export declare function transformInPrecedingStatementScope<TReturn extends lua.Statement | lua.Statement[] | lua.Expression | lua.Expression[]>(context: TransformationContext, transformer: () => TReturn): [lua.Statement[], TReturn];
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transformInPrecedingStatementScope = void 0;
|
|
4
|
+
function transformInPrecedingStatementScope(context, transformer) {
|
|
5
|
+
context.pushPrecedingStatements();
|
|
6
|
+
const statementOrStatements = transformer();
|
|
7
|
+
const precedingStatements = context.popPrecedingStatements();
|
|
8
|
+
return [precedingStatements, statementOrStatements];
|
|
9
|
+
}
|
|
10
|
+
exports.transformInPrecedingStatementScope = transformInPrecedingStatementScope;
|
|
11
|
+
//# sourceMappingURL=preceding-statements.js.map
|
|
@@ -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,27 +57,27 @@ const luaBuiltins = new Set([
|
|
|
51
57
|
"type",
|
|
52
58
|
"unpack",
|
|
53
59
|
]);
|
|
54
|
-
const isUnsafeName = (name) => !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 = !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 !== "") {
|
|
61
|
-
context.diagnostics.push(diagnostics_1.invalidAmbientIdentifierName(node, name));
|
|
67
|
+
context.diagnostics.push((0, diagnostics_1.invalidAmbientIdentifierName)(node, name));
|
|
62
68
|
}
|
|
63
69
|
}
|
|
64
70
|
return isInvalid;
|
|
65
71
|
}
|
|
66
72
|
function hasUnsafeSymbolName(context, symbol, tsOriginal) {
|
|
67
73
|
var _a, _b;
|
|
68
|
-
const isAmbient = (_b = (_a = symbol.declarations) === null || _a === void 0 ? void 0 : _a.some(d => typescript_1.isAmbientNode(d))) !== null && _b !== void 0 ? _b : false;
|
|
74
|
+
const isAmbient = (_b = (_a = symbol.declarations) === null || _a === void 0 ? void 0 : _a.some(d => (0, typescript_1.isAmbientNode)(d))) !== null && _b !== void 0 ? _b : false;
|
|
69
75
|
// Catch ambient declarations of identifiers with bad names
|
|
70
76
|
if (isAmbient && checkName(context, symbol.name, tsOriginal)) {
|
|
71
77
|
return true;
|
|
72
78
|
}
|
|
73
79
|
// only unsafe when non-ambient and not exported
|
|
74
|
-
return exports.isUnsafeName(symbol.name) && !isAmbient && !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[]>;
|
|
@@ -26,16 +27,23 @@ export interface Scope {
|
|
|
26
27
|
loopContinued?: boolean;
|
|
27
28
|
functionReturned?: boolean;
|
|
28
29
|
}
|
|
30
|
+
export interface HoistingResult {
|
|
31
|
+
statements: lua.Statement[];
|
|
32
|
+
hoistedStatements: lua.Statement[];
|
|
33
|
+
hoistedIdentifiers: lua.Identifier[];
|
|
34
|
+
}
|
|
29
35
|
export declare function walkScopesUp(context: TransformationContext): IterableIterator<Scope>;
|
|
30
36
|
export declare function markSymbolAsReferencedInCurrentScopes(context: TransformationContext, symbolId: lua.SymbolId, identifier: ts.Identifier): void;
|
|
31
37
|
export declare function peekScope(context: TransformationContext): Scope;
|
|
32
38
|
export declare function findScope(context: TransformationContext, scopeTypes: ScopeType): Scope | undefined;
|
|
33
39
|
export declare function pushScope(context: TransformationContext, scopeType: ScopeType): Scope;
|
|
34
40
|
export declare function popScope(context: TransformationContext): Scope;
|
|
41
|
+
export declare function addScopeVariableDeclaration(scope: Scope, declaration: lua.VariableDeclarationStatement): void;
|
|
35
42
|
export declare function hasReferencedUndefinedLocalFunction(context: TransformationContext, scope: Scope): boolean;
|
|
36
43
|
export declare function hasReferencedSymbol(context: TransformationContext, scope: Scope, symbol: ts.Symbol): boolean | undefined;
|
|
37
44
|
export declare function isFunctionScopeWithDefinition(scope: Scope): scope is Scope & {
|
|
38
45
|
node: ts.SignatureDeclaration;
|
|
39
46
|
};
|
|
47
|
+
export declare function separateHoistedStatements(context: TransformationContext, statements: lua.Statement[]): HoistingResult;
|
|
40
48
|
export declare function performHoisting(context: TransformationContext, statements: lua.Statement[]): lua.Statement[];
|
|
41
49
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.performHoisting = 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,10 +16,11 @@ 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) {
|
|
22
|
-
return utils_1.getOrUpdate(scopeStacks, context, () => []);
|
|
23
|
+
return (0, utils_1.getOrUpdate)(scopeStacks, context, () => []);
|
|
23
24
|
}
|
|
24
25
|
function* walkScopesUp(context) {
|
|
25
26
|
const scopeStack = getScopeStack(context);
|
|
@@ -34,7 +35,7 @@ function markSymbolAsReferencedInCurrentScopes(context, symbolId, identifier) {
|
|
|
34
35
|
if (!scope.referencedSymbols) {
|
|
35
36
|
scope.referencedSymbols = new Map();
|
|
36
37
|
}
|
|
37
|
-
const references = utils_1.getOrUpdate(scope.referencedSymbols, symbolId, () => []);
|
|
38
|
+
const references = (0, utils_1.getOrUpdate)(scope.referencedSymbols, symbolId, () => []);
|
|
38
39
|
references.push(identifier);
|
|
39
40
|
}
|
|
40
41
|
}
|
|
@@ -42,7 +43,7 @@ exports.markSymbolAsReferencedInCurrentScopes = markSymbolAsReferencedInCurrentS
|
|
|
42
43
|
function peekScope(context) {
|
|
43
44
|
const scopeStack = getScopeStack(context);
|
|
44
45
|
const scope = scopeStack[scopeStack.length - 1];
|
|
45
|
-
utils_1.assert(scope);
|
|
46
|
+
(0, utils_1.assert)(scope);
|
|
46
47
|
return scope;
|
|
47
48
|
}
|
|
48
49
|
exports.peekScope = peekScope;
|
|
@@ -64,13 +65,20 @@ exports.pushScope = pushScope;
|
|
|
64
65
|
function popScope(context) {
|
|
65
66
|
const scopeStack = getScopeStack(context);
|
|
66
67
|
const scope = scopeStack.pop();
|
|
67
|
-
utils_1.assert(scope);
|
|
68
|
+
(0, utils_1.assert)(scope);
|
|
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
|
-
return (_a = symbol === null || symbol === void 0 ? void 0 : symbol.declarations) === null || _a === void 0 ? void 0 : _a.some(d => ts.isFunctionDeclaration(d) && typescript_1.findFirstNodeAbove(d, (n) => n === scopeNode));
|
|
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));
|
|
74
82
|
}
|
|
75
83
|
// Checks for references to local functions which haven't been defined yet,
|
|
76
84
|
// and thus will be hoisted above the current position.
|
|
@@ -106,21 +114,43 @@ function isFunctionScopeWithDefinition(scope) {
|
|
|
106
114
|
return scope.node !== undefined && ts.isFunctionLike(scope.node);
|
|
107
115
|
}
|
|
108
116
|
exports.isFunctionScopeWithDefinition = isFunctionScopeWithDefinition;
|
|
109
|
-
function
|
|
117
|
+
function separateHoistedStatements(context, statements) {
|
|
110
118
|
const scope = peekScope(context);
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
119
|
+
const allHoistedStatments = [];
|
|
120
|
+
const allHoistedIdentifiers = [];
|
|
121
|
+
let { unhoistedStatements, hoistedStatements, hoistedIdentifiers } = hoistFunctionDefinitions(context, scope, statements);
|
|
122
|
+
allHoistedStatments.push(...hoistedStatements);
|
|
123
|
+
allHoistedIdentifiers.push(...hoistedIdentifiers);
|
|
124
|
+
({ unhoistedStatements, hoistedIdentifiers } = hoistVariableDeclarations(context, scope, unhoistedStatements));
|
|
125
|
+
allHoistedIdentifiers.push(...hoistedIdentifiers);
|
|
126
|
+
({ unhoistedStatements, hoistedStatements } = hoistImportStatements(scope, unhoistedStatements));
|
|
127
|
+
allHoistedStatments.unshift(...hoistedStatements);
|
|
128
|
+
return {
|
|
129
|
+
statements: unhoistedStatements,
|
|
130
|
+
hoistedStatements: allHoistedStatments,
|
|
131
|
+
hoistedIdentifiers: allHoistedIdentifiers,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
exports.separateHoistedStatements = separateHoistedStatements;
|
|
135
|
+
function performHoisting(context, statements) {
|
|
136
|
+
const result = separateHoistedStatements(context, statements);
|
|
137
|
+
const modifiedStatements = [...result.hoistedStatements, ...result.statements];
|
|
138
|
+
if (result.hoistedIdentifiers.length > 0) {
|
|
139
|
+
modifiedStatements.unshift(lua.createVariableDeclarationStatement(result.hoistedIdentifiers));
|
|
140
|
+
}
|
|
141
|
+
return modifiedStatements;
|
|
116
142
|
}
|
|
117
143
|
exports.performHoisting = performHoisting;
|
|
118
144
|
function shouldHoistSymbol(context, symbolId, scope) {
|
|
119
|
-
|
|
145
|
+
// Always hoist in top-level of switch statements
|
|
146
|
+
if (scope.type === ScopeType.Switch) {
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
const symbolInfo = (0, symbols_1.getSymbolInfo)(context, symbolId);
|
|
120
150
|
if (!symbolInfo) {
|
|
121
151
|
return false;
|
|
122
152
|
}
|
|
123
|
-
const declaration = typescript_1.getFirstDeclarationInFile(symbolInfo.symbol, context.sourceFile);
|
|
153
|
+
const declaration = (0, typescript_1.getFirstDeclarationInFile)(symbolInfo.symbol, context.sourceFile);
|
|
124
154
|
if (!declaration) {
|
|
125
155
|
return false;
|
|
126
156
|
}
|
|
@@ -129,7 +159,7 @@ function shouldHoistSymbol(context, symbolId, scope) {
|
|
|
129
159
|
}
|
|
130
160
|
if (scope.functionDefinitions) {
|
|
131
161
|
for (const [functionSymbolId, functionDefinition] of scope.functionDefinitions) {
|
|
132
|
-
utils_1.assert(functionDefinition.definition);
|
|
162
|
+
(0, utils_1.assert)(functionDefinition.definition);
|
|
133
163
|
const { line, column } = lua.getOriginalPos(functionDefinition.definition);
|
|
134
164
|
if (line !== undefined && column !== undefined) {
|
|
135
165
|
const definitionPos = ts.getPositionOfLineAndCharacter(context.sourceFile, line, column);
|
|
@@ -146,52 +176,60 @@ function shouldHoistSymbol(context, symbolId, scope) {
|
|
|
146
176
|
}
|
|
147
177
|
function hoistVariableDeclarations(context, scope, statements) {
|
|
148
178
|
if (!scope.variableDeclarations) {
|
|
149
|
-
return statements;
|
|
179
|
+
return { unhoistedStatements: statements, hoistedIdentifiers: [] };
|
|
150
180
|
}
|
|
151
|
-
const
|
|
152
|
-
const
|
|
181
|
+
const unhoistedStatements = [...statements];
|
|
182
|
+
const hoistedIdentifiers = [];
|
|
153
183
|
for (const declaration of scope.variableDeclarations) {
|
|
154
184
|
const symbols = declaration.left.map(i => i.symbolId).filter(utils_1.isNonNull);
|
|
155
185
|
if (symbols.some(s => shouldHoistSymbol(context, s, scope))) {
|
|
156
|
-
const index =
|
|
157
|
-
|
|
186
|
+
const index = unhoistedStatements.indexOf(declaration);
|
|
187
|
+
if (index < 0) {
|
|
188
|
+
continue; // statements array may not contain all statements in the scope (switch-case)
|
|
189
|
+
}
|
|
158
190
|
if (declaration.right) {
|
|
159
191
|
const assignment = lua.createAssignmentStatement(declaration.left, declaration.right);
|
|
160
192
|
lua.setNodePosition(assignment, declaration); // Preserve position info for sourcemap
|
|
161
|
-
|
|
193
|
+
unhoistedStatements.splice(index, 1, assignment);
|
|
162
194
|
}
|
|
163
195
|
else {
|
|
164
|
-
|
|
196
|
+
unhoistedStatements.splice(index, 1);
|
|
165
197
|
}
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
else if (scope.type === ScopeType.Switch) {
|
|
169
|
-
utils_1.assert(!declaration.right);
|
|
170
|
-
hoistedLocals.push(...declaration.left);
|
|
198
|
+
hoistedIdentifiers.push(...declaration.left);
|
|
171
199
|
}
|
|
172
200
|
}
|
|
173
|
-
|
|
174
|
-
result.unshift(lua.createVariableDeclarationStatement(hoistedLocals));
|
|
175
|
-
}
|
|
176
|
-
return result;
|
|
201
|
+
return { unhoistedStatements, hoistedIdentifiers };
|
|
177
202
|
}
|
|
178
203
|
function hoistFunctionDefinitions(context, scope, statements) {
|
|
179
204
|
if (!scope.functionDefinitions) {
|
|
180
|
-
return statements;
|
|
205
|
+
return { unhoistedStatements: statements, hoistedStatements: [], hoistedIdentifiers: [] };
|
|
181
206
|
}
|
|
182
|
-
const
|
|
183
|
-
const
|
|
207
|
+
const unhoistedStatements = [...statements];
|
|
208
|
+
const hoistedStatements = [];
|
|
209
|
+
const hoistedIdentifiers = [];
|
|
184
210
|
for (const [functionSymbolId, functionDefinition] of scope.functionDefinitions) {
|
|
185
|
-
utils_1.assert(functionDefinition.definition);
|
|
211
|
+
(0, utils_1.assert)(functionDefinition.definition);
|
|
186
212
|
if (shouldHoistSymbol(context, functionSymbolId, scope)) {
|
|
187
|
-
const index =
|
|
188
|
-
|
|
189
|
-
|
|
213
|
+
const index = unhoistedStatements.indexOf(functionDefinition.definition);
|
|
214
|
+
if (index < 0) {
|
|
215
|
+
continue; // statements array may not contain all statements in the scope (switch-case)
|
|
216
|
+
}
|
|
217
|
+
unhoistedStatements.splice(index, 1);
|
|
218
|
+
if (lua.isVariableDeclarationStatement(functionDefinition.definition)) {
|
|
219
|
+
// Separate function definition and variable declaration
|
|
220
|
+
(0, utils_1.assert)(functionDefinition.definition.right);
|
|
221
|
+
hoistedIdentifiers.push(...functionDefinition.definition.left);
|
|
222
|
+
hoistedStatements.push(lua.createAssignmentStatement(functionDefinition.definition.left, functionDefinition.definition.right));
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
hoistedStatements.push(functionDefinition.definition);
|
|
226
|
+
}
|
|
190
227
|
}
|
|
191
228
|
}
|
|
192
|
-
return
|
|
229
|
+
return { unhoistedStatements, hoistedStatements, hoistedIdentifiers };
|
|
193
230
|
}
|
|
194
231
|
function hoistImportStatements(scope, statements) {
|
|
195
|
-
|
|
232
|
+
var _a;
|
|
233
|
+
return { unhoistedStatements: statements, hoistedStatements: (_a = scope.importStatements) !== null && _a !== void 0 ? _a : [] };
|
|
196
234
|
}
|
|
197
235
|
//# sourceMappingURL=scope.js.map
|
|
@@ -14,27 +14,27 @@ function nextSymbolId(context) {
|
|
|
14
14
|
const symbolInfoMap = new WeakMap();
|
|
15
15
|
const symbolIdMaps = new WeakMap();
|
|
16
16
|
function getSymbolInfo(context, symbolId) {
|
|
17
|
-
return utils_1.getOrUpdate(symbolInfoMap, context, () => new Map()).get(symbolId);
|
|
17
|
+
return (0, utils_1.getOrUpdate)(symbolInfoMap, context, () => new Map()).get(symbolId);
|
|
18
18
|
}
|
|
19
19
|
exports.getSymbolInfo = getSymbolInfo;
|
|
20
20
|
function getSymbolIdOfSymbol(context, symbol) {
|
|
21
|
-
return utils_1.getOrUpdate(symbolIdMaps, context, () => new Map()).get(symbol);
|
|
21
|
+
return (0, utils_1.getOrUpdate)(symbolIdMaps, context, () => new Map()).get(symbol);
|
|
22
22
|
}
|
|
23
23
|
exports.getSymbolIdOfSymbol = getSymbolIdOfSymbol;
|
|
24
24
|
function trackSymbolReference(context, symbol, identifier) {
|
|
25
|
-
const symbolIds = utils_1.getOrUpdate(symbolIdMaps, context, () => new Map());
|
|
25
|
+
const symbolIds = (0, utils_1.getOrUpdate)(symbolIdMaps, context, () => new Map());
|
|
26
26
|
// Track first time symbols are seen
|
|
27
27
|
let symbolId = symbolIds.get(symbol);
|
|
28
28
|
if (symbolId === undefined) {
|
|
29
29
|
symbolId = nextSymbolId(context);
|
|
30
30
|
symbolIds.set(symbol, symbolId);
|
|
31
|
-
const symbolInfo = utils_1.getOrUpdate(symbolInfoMap, context, () => new Map());
|
|
31
|
+
const symbolInfo = (0, utils_1.getOrUpdate)(symbolInfoMap, context, () => new Map());
|
|
32
32
|
symbolInfo.set(symbolId, { symbol, firstSeenAtPos: identifier.pos });
|
|
33
33
|
}
|
|
34
34
|
// If isOptimizedVarArgSpread returns true, the identifier will not appear in the resulting Lua.
|
|
35
35
|
// Only the optimized ellipses (...) will be used.
|
|
36
|
-
if (!spread_1.isOptimizedVarArgSpread(context, symbol, identifier)) {
|
|
37
|
-
scope_1.markSymbolAsReferencedInCurrentScopes(context, symbolId, identifier);
|
|
36
|
+
if (!(0, spread_1.isOptimizedVarArgSpread)(context, symbol, identifier)) {
|
|
37
|
+
(0, scope_1.markSymbolAsReferencedInCurrentScopes)(context, symbolId, identifier);
|
|
38
38
|
}
|
|
39
39
|
return symbolId;
|
|
40
40
|
}
|
|
@@ -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
|
|
@@ -4,6 +4,8 @@ export declare function isAssignmentPattern(node: ts.Node): node is ts.Assignmen
|
|
|
4
4
|
export declare function isDestructuringAssignment(node: ts.Node): node is ts.DestructuringAssignment;
|
|
5
5
|
export declare function isAmbientNode(node: ts.Declaration): boolean;
|
|
6
6
|
export declare function isInDestructingAssignment(node: ts.Node): boolean;
|
|
7
|
+
export declare function isInAsyncFunction(node: ts.Node): boolean;
|
|
8
|
+
export declare function isInGeneratorFunction(node: ts.Node): boolean;
|
|
7
9
|
/**
|
|
8
10
|
* Quite hacky, avoid unless absolutely necessary!
|
|
9
11
|
*/
|