xray16 1.0.17 → 1.1.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/package.json +1 -1
- package/plugins/{inject_filename.js → inject_file_meta.js} +4 -0
- package/plugins/inline.js +0 -89
- package/plugins/plugins/built_at_info.js +0 -12
- package/plugins/plugins/from_cast_utils.js +0 -27
- package/plugins/plugins/global_declarations_transform.js +0 -18
- package/plugins/plugins/inject_filename.js +0 -18
- package/plugins/plugins/strip_lua_logger.js +0 -51
- package/plugins/plugins/transform_luabind_class/plugin.js +0 -34
- package/plugins/plugins/transform_luabind_class/transformation/class_declaration.js +0 -141
- package/plugins/plugins/transform_luabind_class/transformation/constants.js +0 -4
- package/plugins/plugins/transform_luabind_class/transformation/decorators.js +0 -12
- package/plugins/plugins/transform_luabind_class/transformation/errors.js +0 -25
- package/plugins/plugins/transform_luabind_class/transformation/index.js +0 -5
- package/plugins/plugins/transform_luabind_class/transformation/members/accessors.js +0 -35
- package/plugins/plugins/transform_luabind_class/transformation/members/constructor.js +0 -56
- package/plugins/plugins/transform_luabind_class/transformation/members/fields.js +0 -36
- package/plugins/plugins/transform_luabind_class/transformation/members/method.js +0 -49
- package/plugins/plugins/transform_luabind_class/transformation/new.js +0 -8
- package/plugins/plugins/transform_luabind_class/transformation/setup.js +0 -76
- package/plugins/plugins/transform_luabind_class/transformation/super.js +0 -65
- package/plugins/plugins/transform_luabind_class/transformation/utils.js +0 -67
- package/plugins/plugins/utils/diagnostics.js +0 -17
package/package.json
CHANGED
|
@@ -4,6 +4,7 @@ const path = require("path");
|
|
|
4
4
|
const typescript_1 = require("typescript");
|
|
5
5
|
const typescript_to_lua_1 = require("typescript-to-lua");
|
|
6
6
|
const FILENAME_IDENTIFIER = "$filename";
|
|
7
|
+
const DIRNAME_IDENTIFIER = "$dirname";
|
|
7
8
|
/**
|
|
8
9
|
* Plugin that injects FILE_NAME in compile-time.
|
|
9
10
|
*/
|
|
@@ -13,6 +14,9 @@ const plugin = {
|
|
|
13
14
|
if (node.text === FILENAME_IDENTIFIER) {
|
|
14
15
|
return (0, typescript_to_lua_1.createStringLiteral)(path.parse(context.sourceFile.fileName).name);
|
|
15
16
|
}
|
|
17
|
+
if (node.text === DIRNAME_IDENTIFIER) {
|
|
18
|
+
return (0, typescript_to_lua_1.createStringLiteral)(path.basename(path.dirname(context.sourceFile.fileName)));
|
|
19
|
+
}
|
|
16
20
|
return context.superTransformExpression(node);
|
|
17
21
|
},
|
|
18
22
|
},
|
package/plugins/inline.js
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const typescript_1 = require("typescript");
|
|
4
|
-
const typescript_to_lua_1 = require("typescript-to-lua");
|
|
5
|
-
const diagnostics_1 = require("./utils/diagnostics");
|
|
6
|
-
const INLINE_MACROS = "$inline";
|
|
7
|
-
const expectedFunctionExpressionInInline = (0, diagnostics_1.createErrorDiagnosticFactory)("Expected a function expression in '__inline'.");
|
|
8
|
-
/**
|
|
9
|
-
* Plugin for transformation of casting methods.
|
|
10
|
-
* Simplifies TS/Lua testing and interoperation.
|
|
11
|
-
*
|
|
12
|
-
* Reference: https://gist.github.com/Cheatoid/ea4573c6bd1992fc4940090543ec9380
|
|
13
|
-
*/
|
|
14
|
-
const plugin = {
|
|
15
|
-
visitors: {
|
|
16
|
-
[typescript_1.SyntaxKind.ExpressionStatement]: (node, context) => {
|
|
17
|
-
const result = context.superTransformStatements(node);
|
|
18
|
-
if ((0, typescript_1.isExpressionStatement)(node)) {
|
|
19
|
-
const expr = node.expression;
|
|
20
|
-
if ((0, typescript_1.isCallExpression)(expr) && (0, typescript_1.isIdentifier)(expr.expression)) {
|
|
21
|
-
switch (expr.expression.text) {
|
|
22
|
-
case INLINE_MACROS: {
|
|
23
|
-
if (expr.arguments.length > 0) {
|
|
24
|
-
let bodyArg = expr.arguments[0];
|
|
25
|
-
if ((0, typescript_1.isIdentifier)(bodyArg)) {
|
|
26
|
-
try {
|
|
27
|
-
bodyArg = context.checker.getSymbolAtLocation(bodyArg).getDeclarations()[0];
|
|
28
|
-
}
|
|
29
|
-
catch (error) {
|
|
30
|
-
context.diagnostics.push(expectedFunctionExpressionInInline(node));
|
|
31
|
-
break;
|
|
32
|
-
}
|
|
33
|
-
if (!bodyArg) {
|
|
34
|
-
context.diagnostics.push(expectedFunctionExpressionInInline(node));
|
|
35
|
-
break;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
const paramNames = [];
|
|
39
|
-
let funcExpr;
|
|
40
|
-
if ((0, typescript_1.isFunctionLike)(bodyArg)) {
|
|
41
|
-
const bodyNode = context.transformNode(bodyArg)[0];
|
|
42
|
-
if (!bodyNode) {
|
|
43
|
-
context.diagnostics.push(expectedFunctionExpressionInInline(node));
|
|
44
|
-
break;
|
|
45
|
-
}
|
|
46
|
-
if ((0, typescript_to_lua_1.isVariableDeclarationStatement)(bodyNode) && bodyNode.right) {
|
|
47
|
-
if ((0, typescript_to_lua_1.isFunctionExpression)(bodyNode.right[0])) {
|
|
48
|
-
funcExpr = bodyNode.right[0];
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
paramNames.push(...bodyArg.parameters.map((p) => p.name.getText()));
|
|
52
|
-
}
|
|
53
|
-
for (const stmt of result) {
|
|
54
|
-
if ((0, typescript_to_lua_1.isExpressionStatement)(stmt)) {
|
|
55
|
-
const callExpr = stmt.expression;
|
|
56
|
-
if ((0, typescript_to_lua_1.isCallExpression)(callExpr) &&
|
|
57
|
-
(0, typescript_to_lua_1.isIdentifier)(callExpr.expression) &&
|
|
58
|
-
callExpr.expression.text === expr.expression.text) {
|
|
59
|
-
const paramCount = callExpr.params.length;
|
|
60
|
-
if (paramCount > 0) {
|
|
61
|
-
let body = callExpr.params[0];
|
|
62
|
-
if ((0, typescript_to_lua_1.isIdentifier)(body)) {
|
|
63
|
-
body = funcExpr;
|
|
64
|
-
}
|
|
65
|
-
if (body && (0, typescript_to_lua_1.isFunctionExpression)(body)) {
|
|
66
|
-
const statements = body.body.statements;
|
|
67
|
-
for (let index = 1; index < paramCount; ++index) {
|
|
68
|
-
// Skip the body parameter.
|
|
69
|
-
const param = callExpr.params[index];
|
|
70
|
-
statements.unshift((0, typescript_to_lua_1.createVariableDeclarationStatement)([(0, typescript_to_lua_1.createIdentifier)(paramNames[index - 1])], [param]));
|
|
71
|
-
}
|
|
72
|
-
return (0, typescript_to_lua_1.createDoStatement)(statements, node);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
context.diagnostics.push(expectedFunctionExpressionInInline(node));
|
|
80
|
-
break;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
return result;
|
|
86
|
-
},
|
|
87
|
-
},
|
|
88
|
-
};
|
|
89
|
-
exports.default = plugin;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
const comment = `-- Generated by xrf util at: ${new Date().toString()}\n\n`;
|
|
2
|
-
const plugin = {
|
|
3
|
-
beforeEmit(program, options, emitHost, result) {
|
|
4
|
-
void program;
|
|
5
|
-
void options;
|
|
6
|
-
void emitHost;
|
|
7
|
-
for (const file of result) {
|
|
8
|
-
file.code = comment + file.code;
|
|
9
|
-
}
|
|
10
|
-
},
|
|
11
|
-
};
|
|
12
|
-
export default plugin;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { isIdentifier, SyntaxKind } from "typescript";
|
|
2
|
-
import { createErrorDiagnosticFactory } from "./utils/diagnostics";
|
|
3
|
-
const FROM_CAST_METHODS = ["$fromObject", "$fromArray", "$fromLuaArray", "$fromLuaTable"];
|
|
4
|
-
/**
|
|
5
|
-
* Push generic error to notify about usage issue.
|
|
6
|
-
*/
|
|
7
|
-
const createInvalidFunctionCallError = createErrorDiagnosticFactory((name) => {
|
|
8
|
-
return `Invalid transformer call, expected function to have exactly 1 argument.`;
|
|
9
|
-
});
|
|
10
|
-
/**
|
|
11
|
-
* Plugin for transformation of casting methods.
|
|
12
|
-
* Simplifies TS/Lua testing and interoperation.
|
|
13
|
-
*/
|
|
14
|
-
const plugin = {
|
|
15
|
-
visitors: {
|
|
16
|
-
[SyntaxKind.CallExpression]: (node, context) => {
|
|
17
|
-
if (isIdentifier(node.expression) && FROM_CAST_METHODS.includes(node.expression.text)) {
|
|
18
|
-
if (node.arguments.length !== 1) {
|
|
19
|
-
context.diagnostics.push(createInvalidFunctionCallError(node));
|
|
20
|
-
}
|
|
21
|
-
return context.transformExpression(node.arguments[0]);
|
|
22
|
-
}
|
|
23
|
-
return context.superTransformExpression(node);
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
};
|
|
27
|
-
export default plugin;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { SyntaxKind } from "typescript";
|
|
2
|
-
const XRF_GLOBALS = ["xray16"];
|
|
3
|
-
/**
|
|
4
|
-
* Plugin that removes imports from 'global' libraries like engine typedefs.
|
|
5
|
-
*/
|
|
6
|
-
const plugin = {
|
|
7
|
-
visitors: {
|
|
8
|
-
[SyntaxKind.ImportDeclaration]: (node, context) => {
|
|
9
|
-
const module = node.moduleSpecifier.getText().slice(1, -1);
|
|
10
|
-
const shouldNotBeTransformed = XRF_GLOBALS.includes(module);
|
|
11
|
-
if (shouldNotBeTransformed) {
|
|
12
|
-
return undefined;
|
|
13
|
-
}
|
|
14
|
-
return context.superTransformStatements(node);
|
|
15
|
-
},
|
|
16
|
-
},
|
|
17
|
-
};
|
|
18
|
-
export default plugin;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import * as path from "path";
|
|
2
|
-
import { SyntaxKind } from "typescript";
|
|
3
|
-
import { createStringLiteral } from "typescript-to-lua";
|
|
4
|
-
const FILENAME_IDENTIFIER = "$filename";
|
|
5
|
-
/**
|
|
6
|
-
* Plugin that injects FILE_NAME in compile-time.
|
|
7
|
-
*/
|
|
8
|
-
const plugin = {
|
|
9
|
-
visitors: {
|
|
10
|
-
[SyntaxKind.Identifier]: (node, context) => {
|
|
11
|
-
if (node.text === FILENAME_IDENTIFIER) {
|
|
12
|
-
return createStringLiteral(path.parse(context.sourceFile.fileName).name);
|
|
13
|
-
}
|
|
14
|
-
return context.superTransformExpression(node);
|
|
15
|
-
},
|
|
16
|
-
},
|
|
17
|
-
};
|
|
18
|
-
export default plugin;
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { factory, SyntaxKind, } from "typescript";
|
|
2
|
-
const LUA_LOGGER_STRIP_TARGET = "LuaLogger";
|
|
3
|
-
const IS_LUA_LOGGER_DISABLED = process.argv.includes("--no-lua-logs");
|
|
4
|
-
/**
|
|
5
|
-
* Plugin that removes all LuaLogger instance creations and calls when possible.
|
|
6
|
-
*/
|
|
7
|
-
const plugin = {
|
|
8
|
-
visitors: {
|
|
9
|
-
[SyntaxKind.VariableStatement]: (statement, context) => {
|
|
10
|
-
if (IS_LUA_LOGGER_DISABLED) {
|
|
11
|
-
let elementsCount = 0;
|
|
12
|
-
const list = statement.declarationList;
|
|
13
|
-
const nodes = [];
|
|
14
|
-
list.forEachChild((it) => {
|
|
15
|
-
const checker = context.program.getTypeChecker();
|
|
16
|
-
const typeSymbol = checker.getTypeAtLocation(it);
|
|
17
|
-
if (typeSymbol.symbol?.name === LUA_LOGGER_STRIP_TARGET) {
|
|
18
|
-
// nothing
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
nodes.push(it);
|
|
22
|
-
}
|
|
23
|
-
elementsCount += 1;
|
|
24
|
-
});
|
|
25
|
-
if (nodes.length === 0) {
|
|
26
|
-
return undefined;
|
|
27
|
-
}
|
|
28
|
-
else if (nodes.length !== elementsCount) {
|
|
29
|
-
return context.superTransformStatements(factory.createVariableStatement(statement.modifiers, factory.updateVariableDeclarationList(list, nodes)));
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return context.superTransformStatements(statement);
|
|
33
|
-
},
|
|
34
|
-
[SyntaxKind.ExpressionStatement]: (statement, context) => {
|
|
35
|
-
if (IS_LUA_LOGGER_DISABLED && statement.expression?.kind === SyntaxKind.CallExpression) {
|
|
36
|
-
const expression = statement.expression;
|
|
37
|
-
const propertyAccess = expression.expression;
|
|
38
|
-
if (propertyAccess.expression?.kind === SyntaxKind.Identifier) {
|
|
39
|
-
const checker = context.program.getTypeChecker();
|
|
40
|
-
const identifier = propertyAccess.expression;
|
|
41
|
-
const typeSymbol = checker.getTypeAtLocation(identifier);
|
|
42
|
-
if (typeSymbol.symbol?.name === LUA_LOGGER_STRIP_TARGET) {
|
|
43
|
-
return undefined;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return context.superTransformStatements(statement);
|
|
48
|
-
},
|
|
49
|
-
},
|
|
50
|
-
};
|
|
51
|
-
export default plugin;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { SyntaxKind } from "typescript";
|
|
2
|
-
import { isLuabindClassSuperCall, isLuabindClassSuperMethodCall, isLuabindClassType, isLuabindDecoratedClass, transformClassSuperMethodExpression, transformLuabindClassDeclaration, transformLuabindConstructorSuperCall, transformNewCallExpression, } from "./transformation";
|
|
3
|
-
/**
|
|
4
|
-
* Plugin that transform TS classes marked with decorator to luabind class declaration.
|
|
5
|
-
*/
|
|
6
|
-
const plugin = {
|
|
7
|
-
visitors: {
|
|
8
|
-
[SyntaxKind.CallExpression]: (expression, context) => {
|
|
9
|
-
if (isLuabindClassSuperCall(expression, context)) {
|
|
10
|
-
return transformLuabindConstructorSuperCall(expression, context);
|
|
11
|
-
}
|
|
12
|
-
return context.superTransformExpression(expression);
|
|
13
|
-
},
|
|
14
|
-
[SyntaxKind.ClassDeclaration]: (declaration, context) => {
|
|
15
|
-
if (isLuabindDecoratedClass(declaration)) {
|
|
16
|
-
return transformLuabindClassDeclaration(declaration, context);
|
|
17
|
-
}
|
|
18
|
-
return context.superTransformStatements(declaration);
|
|
19
|
-
},
|
|
20
|
-
[SyntaxKind.NewExpression]: (expression, context) => {
|
|
21
|
-
if (isLuabindClassType(expression, context)) {
|
|
22
|
-
return transformNewCallExpression(expression, context);
|
|
23
|
-
}
|
|
24
|
-
return context.superTransformExpression(expression);
|
|
25
|
-
},
|
|
26
|
-
[SyntaxKind.SuperKeyword]: (expression, context) => {
|
|
27
|
-
if (isLuabindClassSuperMethodCall(expression, context)) {
|
|
28
|
-
return transformClassSuperMethodExpression(expression, context);
|
|
29
|
-
}
|
|
30
|
-
return context.superTransformExpression(expression);
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
export default plugin;
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { canHaveDecorators, factory, getDecorators, isAccessor, isConstructorDeclaration, isMethodDeclaration, isPropertyDeclaration, } from "typescript";
|
|
2
|
-
import * as tstl from "typescript-to-lua";
|
|
3
|
-
import { LuaTarget } from "typescript-to-lua";
|
|
4
|
-
import { createDefaultExportExpression, createExportedIdentifier, hasDefaultExportModifier, hasExportModifier, } from "typescript-to-lua/dist/transformation/utils/export";
|
|
5
|
-
import { createSelfIdentifier } from "typescript-to-lua/dist/transformation/utils/lua-ast";
|
|
6
|
-
import { transformInPrecedingStatementScope } from "typescript-to-lua/dist/transformation/utils/preceding-statements";
|
|
7
|
-
import { createSafeName, isUnsafeName } from "typescript-to-lua/dist/transformation/utils/safe-names";
|
|
8
|
-
import { transformIdentifier } from "typescript-to-lua/dist/transformation/visitors/identifier";
|
|
9
|
-
import { LUABIND_CONSTRUCTOR_METHOD } from "./constants";
|
|
10
|
-
import { checkLuabindClassDecoratorExpression } from "./decorators";
|
|
11
|
-
import { unsupportedStaticMethod } from "./errors";
|
|
12
|
-
import { transformAccessorDeclarations } from "./members/accessors";
|
|
13
|
-
import { createConstructorName, transformConstructorDeclaration } from "./members/constructor";
|
|
14
|
-
import { transformClassInstanceFields, verifyPropertyDecoratingExpression } from "./members/fields";
|
|
15
|
-
import { transformMethodDeclaration, verifyMethodDecoratingExpression } from "./members/method";
|
|
16
|
-
import { createClassSetup } from "./setup";
|
|
17
|
-
import { getExtendedNode, getExtendedType, isStaticNode, markTypeAsLuabind } from "./utils";
|
|
18
|
-
export const transformLuabindClassDeclaration = (declaration, context) => {
|
|
19
|
-
// If declaration is a default export, transform to export variable assignment instead
|
|
20
|
-
if (hasDefaultExportModifier(declaration)) {
|
|
21
|
-
// Class declaration including assignment to ____exports.default are in preceding statements
|
|
22
|
-
const { precedingStatements } = transformInPrecedingStatementScope(context, () => {
|
|
23
|
-
transformClassAsExpression(declaration, context);
|
|
24
|
-
return [];
|
|
25
|
-
});
|
|
26
|
-
return precedingStatements;
|
|
27
|
-
}
|
|
28
|
-
const { statements } = transformClassLikeDeclaration(declaration, context);
|
|
29
|
-
return statements;
|
|
30
|
-
};
|
|
31
|
-
export function transformClassAsExpression(expression, context) {
|
|
32
|
-
const { statements, name } = transformClassLikeDeclaration(expression, context);
|
|
33
|
-
context.addPrecedingStatements(statements);
|
|
34
|
-
return name;
|
|
35
|
-
}
|
|
36
|
-
export function transformClassLikeDeclaration(classDeclaration, context, nameOverride) {
|
|
37
|
-
let className;
|
|
38
|
-
if (nameOverride !== undefined) {
|
|
39
|
-
className = nameOverride;
|
|
40
|
-
}
|
|
41
|
-
else if (classDeclaration.name !== undefined) {
|
|
42
|
-
className = transformIdentifier(context, classDeclaration.name);
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
className = tstl.createIdentifier(context.createTempName("class"), classDeclaration);
|
|
46
|
-
}
|
|
47
|
-
markTypeAsLuabind(classDeclaration, context);
|
|
48
|
-
// Get type that is extended
|
|
49
|
-
const extendedTypeNode = getExtendedNode(classDeclaration);
|
|
50
|
-
const extendedType = getExtendedType(context, classDeclaration);
|
|
51
|
-
context.classSuperInfos.push({ className, classDeclaration: classDeclaration, extendedTypeNode });
|
|
52
|
-
// Get all properties with value
|
|
53
|
-
const properties = classDeclaration.members.filter(isPropertyDeclaration).filter((member) => member.initializer);
|
|
54
|
-
// Divide properties into static and non-static
|
|
55
|
-
const instanceFields = properties.filter((prop) => !isStaticNode(prop));
|
|
56
|
-
const result = [];
|
|
57
|
-
let localClassName;
|
|
58
|
-
if (isUnsafeName(className.text, context.options)) {
|
|
59
|
-
localClassName = tstl.createIdentifier(createSafeName(className.text), undefined, className.symbolId, className.text);
|
|
60
|
-
tstl.setNodePosition(localClassName, className);
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
localClassName = className;
|
|
64
|
-
}
|
|
65
|
-
result.push(...createClassSetup(context, classDeclaration, className, localClassName));
|
|
66
|
-
// Find first constructor with body
|
|
67
|
-
const constructor = classDeclaration.members.find((n) => isConstructorDeclaration(n) && n.body !== undefined);
|
|
68
|
-
if (constructor) {
|
|
69
|
-
// Add constructor plus initialization of instance fields
|
|
70
|
-
const constructorResult = transformConstructorDeclaration(context, constructor, localClassName, instanceFields, classDeclaration);
|
|
71
|
-
if (constructorResult)
|
|
72
|
-
result.push(constructorResult);
|
|
73
|
-
}
|
|
74
|
-
else if (!extendedType) {
|
|
75
|
-
// Generate a constructor if none was defined in a base class
|
|
76
|
-
const constructorResult = transformConstructorDeclaration(context, factory.createConstructorDeclaration([], [], factory.createBlock([], true)), localClassName, instanceFields, classDeclaration);
|
|
77
|
-
if (constructorResult)
|
|
78
|
-
result.push(constructorResult);
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
// Generate a constructor if none was defined in a class with instance fields that need initialization
|
|
82
|
-
// localClassName.__init = function(self, ...)
|
|
83
|
-
// baseClassName.__init(self, ...) // or unpack(arg) for Lua 5.0
|
|
84
|
-
// ...
|
|
85
|
-
// Also luabind always needs definition of call expression to use table as class.
|
|
86
|
-
const constructorBody = transformClassInstanceFields(context, instanceFields);
|
|
87
|
-
const argsExpression = context.luaTarget === LuaTarget.Lua50
|
|
88
|
-
? tstl.createCallExpression(tstl.createIdentifier("unpack"), [tstl.createArgLiteral()])
|
|
89
|
-
: tstl.createDotsLiteral();
|
|
90
|
-
const superCall = tstl.createExpressionStatement(tstl.createCallExpression(tstl.createTableIndexExpression(context.transformExpression(factory.createSuper()), tstl.createStringLiteral(LUABIND_CONSTRUCTOR_METHOD)), [createSelfIdentifier(), argsExpression]));
|
|
91
|
-
constructorBody.unshift(superCall);
|
|
92
|
-
const constructorFunction = tstl.createFunctionExpression(tstl.createBlock(constructorBody), [createSelfIdentifier()], tstl.createDotsLiteral(), tstl.NodeFlags.Declaration);
|
|
93
|
-
result.push(tstl.createAssignmentStatement(createConstructorName(localClassName), constructorFunction, classDeclaration));
|
|
94
|
-
}
|
|
95
|
-
// Transform accessors
|
|
96
|
-
for (const member of classDeclaration.members) {
|
|
97
|
-
if (!isAccessor(member))
|
|
98
|
-
continue;
|
|
99
|
-
const accessors = context.resolver.getAllAccessorDeclarations(member);
|
|
100
|
-
if (accessors.firstAccessor !== member)
|
|
101
|
-
continue;
|
|
102
|
-
const accessorsResult = transformAccessorDeclarations(context, accessors, localClassName);
|
|
103
|
-
if (accessorsResult) {
|
|
104
|
-
result.push(accessorsResult);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
const decorationStatements = [];
|
|
108
|
-
for (const member of classDeclaration.members) {
|
|
109
|
-
if (isAccessor(member)) {
|
|
110
|
-
verifyPropertyDecoratingExpression(context, member);
|
|
111
|
-
}
|
|
112
|
-
else if (isMethodDeclaration(member)) {
|
|
113
|
-
const statement = transformMethodDeclaration(context, member, localClassName);
|
|
114
|
-
if (statement)
|
|
115
|
-
result.push(statement);
|
|
116
|
-
if (member.body) {
|
|
117
|
-
verifyMethodDecoratingExpression(context, member);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
else if (isPropertyDeclaration(member)) {
|
|
121
|
-
if (isStaticNode(member)) {
|
|
122
|
-
context.diagnostics.push(unsupportedStaticMethod(member));
|
|
123
|
-
}
|
|
124
|
-
verifyPropertyDecoratingExpression(context, member);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
result.push(...decorationStatements);
|
|
128
|
-
// Decorate the class
|
|
129
|
-
if (canHaveDecorators(classDeclaration) && getDecorators(classDeclaration)) {
|
|
130
|
-
getDecorators(classDeclaration)?.forEach((d) => checkLuabindClassDecoratorExpression(context, d));
|
|
131
|
-
if (hasExportModifier(classDeclaration)) {
|
|
132
|
-
const exportExpression = hasDefaultExportModifier(classDeclaration)
|
|
133
|
-
? createDefaultExportExpression(classDeclaration)
|
|
134
|
-
: createExportedIdentifier(context, className);
|
|
135
|
-
const classAssignment = tstl.createAssignmentStatement(exportExpression, localClassName);
|
|
136
|
-
result.push(classAssignment);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
context.classSuperInfos.pop();
|
|
140
|
-
return { statements: result, name: className };
|
|
141
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { LUABIND_DECORATOR } from "./constants";
|
|
2
|
-
import { unsupportedClassDecorator } from "./errors";
|
|
3
|
-
/**
|
|
4
|
-
* Transform decorator call expressions for luabind class.
|
|
5
|
-
*/
|
|
6
|
-
export function checkLuabindClassDecoratorExpression(context, decorator) {
|
|
7
|
-
const expression = decorator.expression;
|
|
8
|
-
// Do not transform luabind decorator.
|
|
9
|
-
if (expression.expression.text !== LUABIND_DECORATOR) {
|
|
10
|
-
context.diagnostics.push(unsupportedClassDecorator(expression));
|
|
11
|
-
}
|
|
12
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { createErrorDiagnosticFactory } from "../../utils/diagnostics";
|
|
2
|
-
export const unsupportedStaticMethod = createErrorDiagnosticFactory((name) => {
|
|
3
|
-
const nameReference = name ? ` '${name}'` : "";
|
|
4
|
-
return `Unable transform static properties for luabind classes${nameReference}.`;
|
|
5
|
-
});
|
|
6
|
-
export const unsupportedClassDecorator = createErrorDiagnosticFactory((name) => {
|
|
7
|
-
const nameReference = name ? ` '${name}'` : "";
|
|
8
|
-
return `Unable transform class decorators for luabind classes${nameReference}.`;
|
|
9
|
-
});
|
|
10
|
-
export const unsupportedPropertyDecorator = createErrorDiagnosticFactory((name) => {
|
|
11
|
-
const nameReference = name ? ` '${name}'` : "";
|
|
12
|
-
return `Unable transform property decorator for luabind classes${nameReference}.`;
|
|
13
|
-
});
|
|
14
|
-
export const unsupportedParameterDecorator = createErrorDiagnosticFactory((name) => {
|
|
15
|
-
const nameReference = name ? ` '${name}'` : "";
|
|
16
|
-
return `Unable transform parameter decorator for luabind classes${nameReference}.`;
|
|
17
|
-
});
|
|
18
|
-
export const unsupportedMethodDecorator = createErrorDiagnosticFactory((name) => {
|
|
19
|
-
const nameReference = name ? ` '${name}'` : "";
|
|
20
|
-
return `Unable transform method decorator for luabind classes${nameReference}.`;
|
|
21
|
-
});
|
|
22
|
-
export const unsupportedStaticAccessor = createErrorDiagnosticFactory((name) => {
|
|
23
|
-
const nameReference = name ? ` '${name}'` : "";
|
|
24
|
-
return `Unable transform static accessors for luabind classes${nameReference}.`;
|
|
25
|
-
});
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { LuaLibFeature } from "typescript-to-lua";
|
|
2
|
-
import * as lua from "typescript-to-lua";
|
|
3
|
-
import { createSelfIdentifier } from "typescript-to-lua/dist/transformation/utils/lua-ast";
|
|
4
|
-
import { transformLuaLibFunction } from "typescript-to-lua/dist/transformation/utils/lualib";
|
|
5
|
-
import { transformFunctionBody, transformParameters } from "typescript-to-lua/dist/transformation/visitors/function";
|
|
6
|
-
import { transformPropertyName } from "typescript-to-lua/dist/transformation/visitors/literal";
|
|
7
|
-
import { unsupportedStaticAccessor } from "../errors";
|
|
8
|
-
import { isStaticNode } from "../utils";
|
|
9
|
-
function transformAccessor(context, node) {
|
|
10
|
-
const [params, dot, restParam] = transformParameters(context, node.parameters, createSelfIdentifier());
|
|
11
|
-
const body = node.body ? transformFunctionBody(context, node.parameters, node.body, restParam)[0] : [];
|
|
12
|
-
return lua.createFunctionExpression(lua.createBlock(body), params, dot, lua.NodeFlags.Declaration);
|
|
13
|
-
}
|
|
14
|
-
export function transformAccessorDeclarations(context, { firstAccessor, getAccessor, setAccessor }, className) {
|
|
15
|
-
const propertyName = transformPropertyName(context, firstAccessor.name);
|
|
16
|
-
const descriptor = lua.createTableExpression([]);
|
|
17
|
-
if (getAccessor) {
|
|
18
|
-
const getterFunction = transformAccessor(context, getAccessor);
|
|
19
|
-
descriptor.fields.push(lua.createTableFieldExpression(getterFunction, lua.createStringLiteral("get")));
|
|
20
|
-
}
|
|
21
|
-
if (setAccessor) {
|
|
22
|
-
const setterFunction = transformAccessor(context, setAccessor);
|
|
23
|
-
descriptor.fields.push(lua.createTableFieldExpression(setterFunction, lua.createStringLiteral("set")));
|
|
24
|
-
}
|
|
25
|
-
const isStatic = isStaticNode(firstAccessor);
|
|
26
|
-
if (isStatic) {
|
|
27
|
-
context.diagnostics.push(unsupportedStaticAccessor(firstAccessor));
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
const target = lua.cloneIdentifier(className);
|
|
31
|
-
const feature = LuaLibFeature.ObjectDefineProperty;
|
|
32
|
-
const parameters = [target, propertyName, descriptor];
|
|
33
|
-
const call = transformLuaLibFunction(context, feature, undefined, ...parameters);
|
|
34
|
-
return lua.createExpressionStatement(call);
|
|
35
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { isCallExpression, isExpressionStatement, isIdentifier, SyntaxKind, } from "typescript";
|
|
2
|
-
import * as lua from "typescript-to-lua";
|
|
3
|
-
import { createSelfIdentifier } from "typescript-to-lua/dist/transformation/utils/lua-ast";
|
|
4
|
-
import { ScopeType } from "typescript-to-lua/dist/transformation/utils/scope";
|
|
5
|
-
import { transformFunctionBodyContent, transformFunctionBodyHeader, transformParameters, } from "typescript-to-lua/dist/transformation/visitors/function";
|
|
6
|
-
import { transformIdentifier } from "typescript-to-lua/dist/transformation/visitors/identifier";
|
|
7
|
-
import { LUABIND_CONSTRUCTOR_METHOD } from "../constants";
|
|
8
|
-
import { transformClassInstanceFields } from "./fields";
|
|
9
|
-
export function createConstructorName(className) {
|
|
10
|
-
return lua.createTableIndexExpression(lua.cloneIdentifier(className), lua.createStringLiteral(LUABIND_CONSTRUCTOR_METHOD));
|
|
11
|
-
}
|
|
12
|
-
export function transformConstructorDeclaration(context, statement, className, instanceFields, classDeclaration) {
|
|
13
|
-
// Don't transform methods without body (overload declarations)
|
|
14
|
-
if (!statement.body) {
|
|
15
|
-
return undefined;
|
|
16
|
-
}
|
|
17
|
-
// Transform body
|
|
18
|
-
const scope = context.pushScope(ScopeType.Function);
|
|
19
|
-
const body = transformFunctionBodyContent(context, statement.body);
|
|
20
|
-
const [params, dotsLiteral, restParamName] = transformParameters(context, statement.parameters, createSelfIdentifier());
|
|
21
|
-
// Make sure default parameters are assigned before fields are initialized
|
|
22
|
-
const bodyWithFieldInitializers = transformFunctionBodyHeader(context, scope, statement.parameters, restParamName);
|
|
23
|
-
// Check for field declarations in constructor
|
|
24
|
-
const constructorFieldsDeclarations = statement.parameters.filter((p) => p.modifiers !== undefined);
|
|
25
|
-
const classInstanceFields = transformClassInstanceFields(context, instanceFields);
|
|
26
|
-
// If there are field initializers and the first statement is a super call,
|
|
27
|
-
// move super call between default assignments and initializers
|
|
28
|
-
if ((constructorFieldsDeclarations.length > 0 || classInstanceFields.length > 0) &&
|
|
29
|
-
statement.body &&
|
|
30
|
-
statement.body.statements.length > 0) {
|
|
31
|
-
const firstStatement = statement.body.statements[0];
|
|
32
|
-
if (isExpressionStatement(firstStatement) &&
|
|
33
|
-
isCallExpression(firstStatement.expression) &&
|
|
34
|
-
firstStatement.expression.expression.kind === SyntaxKind.SuperKeyword) {
|
|
35
|
-
const superCall = body.shift();
|
|
36
|
-
if (superCall) {
|
|
37
|
-
bodyWithFieldInitializers.push(superCall);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
// Add in instance field declarations
|
|
42
|
-
for (const declaration of constructorFieldsDeclarations) {
|
|
43
|
-
if (isIdentifier(declaration.name)) {
|
|
44
|
-
// self.declarationName = declarationName
|
|
45
|
-
const assignment = lua.createAssignmentStatement(lua.createTableIndexExpression(createSelfIdentifier(), lua.createStringLiteral(declaration.name.text)), transformIdentifier(context, declaration.name));
|
|
46
|
-
bodyWithFieldInitializers.push(assignment);
|
|
47
|
-
}
|
|
48
|
-
// else { TypeScript error: A parameter property may not be declared using a binding pattern }
|
|
49
|
-
}
|
|
50
|
-
bodyWithFieldInitializers.push(...classInstanceFields);
|
|
51
|
-
bodyWithFieldInitializers.push(...body);
|
|
52
|
-
const block = lua.createBlock(bodyWithFieldInitializers);
|
|
53
|
-
const constructorWasGenerated = statement.pos === -1;
|
|
54
|
-
context.popScope();
|
|
55
|
-
return lua.createAssignmentStatement(createConstructorName(className), lua.createFunctionExpression(block, params, dotsLiteral, lua.NodeFlags.Declaration), constructorWasGenerated ? classDeclaration : statement);
|
|
56
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { canHaveDecorators, getDecorators } from "typescript";
|
|
2
|
-
import * as tstl from "typescript-to-lua";
|
|
3
|
-
import { createSelfIdentifier } from "typescript-to-lua/dist/transformation/utils/lua-ast";
|
|
4
|
-
import { transformInPrecedingStatementScope } from "typescript-to-lua/dist/transformation/utils/preceding-statements";
|
|
5
|
-
import { transformPropertyName } from "typescript-to-lua/dist/transformation/visitors/literal";
|
|
6
|
-
import { unsupportedPropertyDecorator } from "../errors";
|
|
7
|
-
/**
|
|
8
|
-
* Verify whether decorators provided for luabind class.
|
|
9
|
-
*/
|
|
10
|
-
export function verifyPropertyDecoratingExpression(context, node) {
|
|
11
|
-
if (!canHaveDecorators(node))
|
|
12
|
-
return;
|
|
13
|
-
const decorators = getDecorators(node);
|
|
14
|
-
if (!decorators)
|
|
15
|
-
return;
|
|
16
|
-
decorators.forEach((decorator) => {
|
|
17
|
-
context.diagnostics.push(unsupportedPropertyDecorator(decorator));
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
export function transformClassInstanceFields(context, instanceFields) {
|
|
21
|
-
const statements = [];
|
|
22
|
-
for (const f of instanceFields) {
|
|
23
|
-
const { precedingStatements, result: statement } = transformInPrecedingStatementScope(context, () => {
|
|
24
|
-
// Get identifier
|
|
25
|
-
const fieldName = transformPropertyName(context, f.name);
|
|
26
|
-
const value = f.initializer ? context.transformExpression(f.initializer) : undefined;
|
|
27
|
-
// self[fieldName]
|
|
28
|
-
const selfIndex = tstl.createTableIndexExpression(createSelfIdentifier(), fieldName);
|
|
29
|
-
// self[fieldName] = value
|
|
30
|
-
const assignClassField = tstl.createAssignmentStatement(selfIndex, value, f);
|
|
31
|
-
return assignClassField;
|
|
32
|
-
});
|
|
33
|
-
statements.push(...precedingStatements, statement);
|
|
34
|
-
}
|
|
35
|
-
return statements;
|
|
36
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { getDecorators } from "typescript";
|
|
2
|
-
import * as lua from "typescript-to-lua";
|
|
3
|
-
import { transformFunctionToExpression } from "typescript-to-lua/dist/transformation/visitors/function";
|
|
4
|
-
import { transformPropertyName } from "typescript-to-lua/dist/transformation/visitors/literal";
|
|
5
|
-
import { unsupportedMethodDecorator, unsupportedParameterDecorator, unsupportedStaticMethod } from "../errors";
|
|
6
|
-
import { isStaticNode } from "../utils";
|
|
7
|
-
export function transformMemberExpressionOwnerName(node, className) {
|
|
8
|
-
return lua.cloneIdentifier(className);
|
|
9
|
-
}
|
|
10
|
-
export function transformMethodName(context, node) {
|
|
11
|
-
const methodName = transformPropertyName(context, node.name);
|
|
12
|
-
if (lua.isStringLiteral(methodName) && methodName.value === "toString") {
|
|
13
|
-
return lua.createStringLiteral("__tostring", node.name);
|
|
14
|
-
}
|
|
15
|
-
return methodName;
|
|
16
|
-
}
|
|
17
|
-
export function transformMethodDeclaration(context, node, className) {
|
|
18
|
-
// Don't transform methods without body (overload declarations)
|
|
19
|
-
if (!node.body)
|
|
20
|
-
return;
|
|
21
|
-
// Don't transform static methods for luabind classes.
|
|
22
|
-
if (isStaticNode(node)) {
|
|
23
|
-
context.diagnostics.push(unsupportedStaticMethod(node));
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
const methodTable = transformMemberExpressionOwnerName(node, className);
|
|
27
|
-
const methodName = transformMethodName(context, node);
|
|
28
|
-
const [functionExpression] = transformFunctionToExpression(context, node);
|
|
29
|
-
return lua.createAssignmentStatement(lua.createTableIndexExpression(methodTable, methodName), functionExpression, node);
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Verify that method statement is not using decorators for methods/parameters.
|
|
33
|
-
*/
|
|
34
|
-
export function verifyMethodDecoratingExpression(context, node) {
|
|
35
|
-
node.parameters.flatMap((parameter, index) => {
|
|
36
|
-
const decorators = getDecorators(parameter);
|
|
37
|
-
if (decorators?.length) {
|
|
38
|
-
decorators.forEach((decorator) => {
|
|
39
|
-
context.diagnostics.push(unsupportedParameterDecorator(decorator));
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
const decorators = getDecorators(node);
|
|
44
|
-
if (decorators?.length) {
|
|
45
|
-
decorators.forEach((decorator) => {
|
|
46
|
-
context.diagnostics.push(unsupportedMethodDecorator(decorator));
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import * as tstl from "typescript-to-lua";
|
|
2
|
-
import { transformArguments } from "typescript-to-lua/dist/transformation/visitors/call";
|
|
3
|
-
/**
|
|
4
|
-
* Transform new call for luabind class as ClassConstructor() instead of TS_NEW from tstl.
|
|
5
|
-
*/
|
|
6
|
-
export function transformNewCallExpression(expression, context) {
|
|
7
|
-
return tstl.createCallExpression(context.transformExpression(expression.expression), transformArguments(context, expression.arguments ?? []));
|
|
8
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { isIdentifier, isVariableDeclaration } from "typescript";
|
|
2
|
-
import * as tstl from "typescript-to-lua";
|
|
3
|
-
import { createDefaultExportStringLiteral, createExportedIdentifier, getIdentifierExportScope, hasDefaultExportModifier, } from "typescript-to-lua/dist/transformation/utils/export";
|
|
4
|
-
import { createExportsIdentifier, createLocalOrExportedOrGlobalDeclaration, } from "typescript-to-lua/dist/transformation/utils/lua-ast";
|
|
5
|
-
import { LUABIND_NAME_FIELD } from "./constants";
|
|
6
|
-
import { getExtendedNode } from "./utils";
|
|
7
|
-
/**
|
|
8
|
-
* Create full class setup statement with name/super calls/methods/declaration/fields etc.
|
|
9
|
-
*/
|
|
10
|
-
export function createClassSetup(context, statement, className, localClassName) {
|
|
11
|
-
const result = [];
|
|
12
|
-
// class("name")(base)
|
|
13
|
-
const classInitializer = createLuabindClassStatement(statement, context, localClassName);
|
|
14
|
-
result.push(tstl.createExpressionStatement(classInitializer));
|
|
15
|
-
const classReference = createLuabindClassGlobalClassRef(statement, context, localClassName);
|
|
16
|
-
const defaultExportLeftHandSide = hasDefaultExportModifier(statement)
|
|
17
|
-
? tstl.createTableIndexExpression(createExportsIdentifier(), createDefaultExportStringLiteral(statement))
|
|
18
|
-
: undefined;
|
|
19
|
-
// [____exports.]className = class()
|
|
20
|
-
if (defaultExportLeftHandSide) {
|
|
21
|
-
result.push(tstl.createAssignmentStatement(defaultExportLeftHandSide, classReference, statement));
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
result.push(...createLocalOrExportedOrGlobalDeclaration(context, className, classReference, statement));
|
|
25
|
-
}
|
|
26
|
-
if (defaultExportLeftHandSide) {
|
|
27
|
-
// local localClassName = ____exports.default
|
|
28
|
-
result.push(tstl.createVariableDeclarationStatement(localClassName, defaultExportLeftHandSide));
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
const exportScope = getIdentifierExportScope(context, className);
|
|
32
|
-
if (exportScope) {
|
|
33
|
-
// local localClassName = ____exports.className
|
|
34
|
-
result.push(tstl.createVariableDeclarationStatement(localClassName, createExportedIdentifier(context, tstl.cloneIdentifier(className), exportScope)));
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
// localClassName.__name = className
|
|
38
|
-
result.push(tstl.createAssignmentStatement(tstl.createTableIndexExpression(tstl.cloneIdentifier(localClassName), tstl.createStringLiteral(LUABIND_NAME_FIELD)), getReflectionClassName(statement, className), statement));
|
|
39
|
-
return result;
|
|
40
|
-
}
|
|
41
|
-
export function getReflectionClassName(declaration, className) {
|
|
42
|
-
if (declaration.name) {
|
|
43
|
-
return tstl.createStringLiteral(declaration.name.text);
|
|
44
|
-
}
|
|
45
|
-
else if (isVariableDeclaration(declaration.parent) && isIdentifier(declaration.parent.name)) {
|
|
46
|
-
return tstl.createStringLiteral(declaration.parent.name.text);
|
|
47
|
-
}
|
|
48
|
-
else if (hasDefaultExportModifier(declaration)) {
|
|
49
|
-
return tstl.createStringLiteral("default");
|
|
50
|
-
}
|
|
51
|
-
if (getExtendedNode(declaration)) {
|
|
52
|
-
return tstl.createTableIndexExpression(className, tstl.createStringLiteral("name"));
|
|
53
|
-
}
|
|
54
|
-
return tstl.createStringLiteral("");
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Creates class("Name")(base) expression for luabind classes.
|
|
58
|
-
*/
|
|
59
|
-
function createLuabindClassStatement(declaration, context, className) {
|
|
60
|
-
const extendedNode = getExtendedNode(declaration);
|
|
61
|
-
let classDeclaration = tstl.createCallExpression(tstl.createIdentifier("class"), [
|
|
62
|
-
getReflectionClassName(declaration, className),
|
|
63
|
-
]);
|
|
64
|
-
if (extendedNode) {
|
|
65
|
-
classDeclaration = tstl.createCallExpression(classDeclaration, [
|
|
66
|
-
context.transformExpression(extendedNode.expression),
|
|
67
|
-
]);
|
|
68
|
-
}
|
|
69
|
-
return classDeclaration;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Creates name expression for luabind classes.
|
|
73
|
-
*/
|
|
74
|
-
function createLuabindClassGlobalClassRef(declaration, context, className) {
|
|
75
|
-
return className;
|
|
76
|
-
}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { factory, isIdentifier, SyntaxKind } from "typescript";
|
|
2
|
-
import * as tstl from "typescript-to-lua";
|
|
3
|
-
import { isSymbolExported } from "typescript-to-lua/dist/transformation/utils/export";
|
|
4
|
-
import { getCalledExpression, transformArguments } from "typescript-to-lua/dist/transformation/visitors/call";
|
|
5
|
-
import { transformIdentifier } from "typescript-to-lua/dist/transformation/visitors/identifier";
|
|
6
|
-
import { LUABIND_CONSTRUCTOR_METHOD } from "./constants";
|
|
7
|
-
import { isLuabindClassType } from "./utils";
|
|
8
|
-
/**
|
|
9
|
-
* Transform generic methods super calls.
|
|
10
|
-
* Example: super.parentMethod(first, second).
|
|
11
|
-
*/
|
|
12
|
-
export function transformClassSuperMethodExpression(expression, context) {
|
|
13
|
-
const superInfos = context.classSuperInfos;
|
|
14
|
-
const superInfo = superInfos[superInfos.length - 1];
|
|
15
|
-
if (!superInfo.classDeclaration || !isLuabindClassType(superInfo.classDeclaration, context)) {
|
|
16
|
-
return context.superTransformExpression(expression);
|
|
17
|
-
}
|
|
18
|
-
const { extendedTypeNode } = superInfo;
|
|
19
|
-
// Using `super` without extended type node is a TypeScript error
|
|
20
|
-
const extendsExpression = extendedTypeNode?.expression;
|
|
21
|
-
let baseClassName;
|
|
22
|
-
if (extendsExpression && isIdentifier(extendsExpression)) {
|
|
23
|
-
const symbol = context.checker.getSymbolAtLocation(extendsExpression);
|
|
24
|
-
if (symbol && !isSymbolExported(context, symbol)) {
|
|
25
|
-
// Use "baseClassName" if base is a simple identifier
|
|
26
|
-
baseClassName = transformIdentifier(context, extendsExpression);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
if (!baseClassName) {
|
|
30
|
-
throw new Error("Super without identifier - not supported with luabind.");
|
|
31
|
-
}
|
|
32
|
-
return baseClassName;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Check if super() call is in luabind class target.
|
|
36
|
-
*/
|
|
37
|
-
export function isLuabindClassSuperCall(expression, context) {
|
|
38
|
-
const calledExpression = getCalledExpression(expression);
|
|
39
|
-
if (calledExpression.kind !== SyntaxKind.SuperKeyword) {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
const superInfos = context.classSuperInfos;
|
|
44
|
-
const superInfo = superInfos[superInfos.length - 1];
|
|
45
|
-
// Handle super calls properly for luabind classes.
|
|
46
|
-
return superInfo?.classDeclaration ? isLuabindClassType(superInfo.classDeclaration, context) : false;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Check if super.method() call is in luabind class target.
|
|
51
|
-
*/
|
|
52
|
-
export function isLuabindClassSuperMethodCall(expression, context) {
|
|
53
|
-
const superInfos = context.classSuperInfos;
|
|
54
|
-
const superInfo = superInfos[superInfos.length - 1];
|
|
55
|
-
// Handle super calls properly for luabind classes.
|
|
56
|
-
return superInfo?.classDeclaration ? isLuabindClassType(superInfo.classDeclaration, context) : false;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Transform super() call in luabind classes to base_class.__init(self, param).
|
|
60
|
-
*/
|
|
61
|
-
export function transformLuabindConstructorSuperCall(expression, context) {
|
|
62
|
-
const signature = context.checker.getResolvedSignature(expression);
|
|
63
|
-
const parameters = transformArguments(context, expression.arguments, signature, factory.createThis());
|
|
64
|
-
return tstl.createCallExpression(tstl.createTableIndexExpression(context.transformExpression(factory.createSuper()), tstl.createStringLiteral(LUABIND_CONSTRUCTOR_METHOD)), parameters, expression);
|
|
65
|
-
}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { getDecorators, SyntaxKind, } from "typescript";
|
|
2
|
-
import { LUABIND_DECORATOR, LUABIND_SYMBOL } from "./constants";
|
|
3
|
-
/**
|
|
4
|
-
* Whether method / field is static.
|
|
5
|
-
*/
|
|
6
|
-
export function isStaticNode(node) {
|
|
7
|
-
return node.modifiers?.some((m) => m.kind === SyntaxKind.StaticKeyword) === true;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Get class extends node.
|
|
11
|
-
*/
|
|
12
|
-
export function getExtendsClause(node) {
|
|
13
|
-
return node.heritageClauses?.find((clause) => clause.token === SyntaxKind.ExtendsKeyword);
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Get class extended node.
|
|
17
|
-
*/
|
|
18
|
-
export function getExtendedNode(node) {
|
|
19
|
-
const extendsClause = getExtendsClause(node);
|
|
20
|
-
if (!extendsClause)
|
|
21
|
-
return;
|
|
22
|
-
return extendsClause.types[0];
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Get class extended node.
|
|
26
|
-
*/
|
|
27
|
-
export function getExtendedType(context, node) {
|
|
28
|
-
const extendedNode = getExtendedNode(node);
|
|
29
|
-
return extendedNode && context.checker.getTypeAtLocation(extendedNode);
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Check if class is decorated with provided decorator name.
|
|
33
|
-
*/
|
|
34
|
-
export function isLuabindDecoratedClass(declaration) {
|
|
35
|
-
const decorators = getDecorators(declaration);
|
|
36
|
-
if (!decorators) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
return decorators.some((it) => it.expression.expression?.escapedText === LUABIND_DECORATOR);
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Mark provided class as Luabind target.
|
|
43
|
-
*/
|
|
44
|
-
export function markTypeAsLuabind(declaration, context) {
|
|
45
|
-
const typeAtLocation = context.checker.getTypeAtLocation(declaration);
|
|
46
|
-
const typeSymbol = typeAtLocation.symbol || typeAtLocation.aliasSymbol;
|
|
47
|
-
typeSymbol[LUABIND_SYMBOL] = true;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Check if provided class is specified as LuaBind.
|
|
51
|
-
*/
|
|
52
|
-
export function isLuabindClassType(declaration, context) {
|
|
53
|
-
const typeAtLocation = context.checker.getTypeAtLocation(declaration);
|
|
54
|
-
const typeSymbol = typeAtLocation.symbol || typeAtLocation.aliasSymbol;
|
|
55
|
-
if (typeSymbol) {
|
|
56
|
-
const isMarked = typeSymbol[LUABIND_SYMBOL] === true;
|
|
57
|
-
if (isMarked) {
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
return isLuabindDecoratedClass(typeSymbol.declarations[0]);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { DiagnosticCategory, getOriginalNode } from "typescript";
|
|
2
|
-
import { createSerialDiagnosticFactory } from "typescript-to-lua/dist/utils";
|
|
3
|
-
/**
|
|
4
|
-
* Create diagnostics factory to push errors when transpile lua to typescript.
|
|
5
|
-
*/
|
|
6
|
-
export function createDiagnosticFactory(category, message) {
|
|
7
|
-
return createSerialDiagnosticFactory((node, ...args) => ({
|
|
8
|
-
file: getOriginalNode(node).getSourceFile(),
|
|
9
|
-
start: getOriginalNode(node).getStart(),
|
|
10
|
-
length: getOriginalNode(node).getWidth(),
|
|
11
|
-
messageText: typeof message === "string" ? message : message(...args),
|
|
12
|
-
category,
|
|
13
|
-
}));
|
|
14
|
-
}
|
|
15
|
-
export function createErrorDiagnosticFactory(message) {
|
|
16
|
-
return createDiagnosticFactory(DiagnosticCategory.Error, message);
|
|
17
|
-
}
|