xray16 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/README.md +29 -0
  2. package/package.json +7 -4
  3. package/plugins/built_at_info.js +14 -0
  4. package/plugins/from_cast_utils.js +29 -0
  5. package/plugins/global_declarations_transform.js +20 -0
  6. package/plugins/inject_filename.js +20 -0
  7. package/plugins/plugins/built_at_info.js +12 -0
  8. package/plugins/plugins/from_cast_utils.js +27 -0
  9. package/plugins/plugins/global_declarations_transform.js +18 -0
  10. package/plugins/plugins/inject_filename.js +18 -0
  11. package/plugins/plugins/strip_lua_logger.js +51 -0
  12. package/plugins/plugins/transform_luabind_class/plugin.js +34 -0
  13. package/plugins/plugins/transform_luabind_class/transformation/class_declaration.js +141 -0
  14. package/plugins/plugins/transform_luabind_class/transformation/constants.js +4 -0
  15. package/plugins/plugins/transform_luabind_class/transformation/decorators.js +12 -0
  16. package/plugins/plugins/transform_luabind_class/transformation/errors.js +25 -0
  17. package/plugins/{transform_luabind_class/transformation/index.ts → plugins/transform_luabind_class/transformation/index.js} +5 -5
  18. package/plugins/plugins/transform_luabind_class/transformation/members/accessors.js +35 -0
  19. package/plugins/plugins/transform_luabind_class/transformation/members/constructor.js +56 -0
  20. package/plugins/plugins/transform_luabind_class/transformation/members/fields.js +36 -0
  21. package/plugins/plugins/transform_luabind_class/transformation/members/method.js +49 -0
  22. package/plugins/plugins/transform_luabind_class/transformation/new.js +8 -0
  23. package/plugins/plugins/transform_luabind_class/transformation/setup.js +76 -0
  24. package/plugins/plugins/transform_luabind_class/transformation/super.js +65 -0
  25. package/plugins/plugins/transform_luabind_class/transformation/utils.js +67 -0
  26. package/plugins/plugins/utils/diagnostics.js +17 -0
  27. package/plugins/strip_lua_logger.js +53 -0
  28. package/plugins/transform_luabind_class/plugin.js +36 -0
  29. package/plugins/transform_luabind_class/transformation/class_declaration.js +147 -0
  30. package/plugins/transform_luabind_class/transformation/constants.js +7 -0
  31. package/plugins/transform_luabind_class/transformation/decorators.js +16 -0
  32. package/plugins/transform_luabind_class/transformation/errors.js +28 -0
  33. package/plugins/transform_luabind_class/transformation/index.js +21 -0
  34. package/plugins/transform_luabind_class/transformation/members/accessors.js +39 -0
  35. package/plugins/transform_luabind_class/transformation/members/constructor.js +61 -0
  36. package/plugins/transform_luabind_class/transformation/members/fields.js +41 -0
  37. package/plugins/transform_luabind_class/transformation/members/method.js +56 -0
  38. package/plugins/transform_luabind_class/transformation/new.js +12 -0
  39. package/plugins/transform_luabind_class/transformation/setup.js +81 -0
  40. package/plugins/transform_luabind_class/transformation/super.js +72 -0
  41. package/plugins/transform_luabind_class/transformation/utils.js +77 -0
  42. package/plugins/utils/diagnostics.js +22 -0
  43. package/types/index.d.ts +63 -0
  44. package/{src → types}/xr_object/xr_client_object.d.ts +4 -1
  45. package/plugins/built_at_info.ts +0 -18
  46. package/plugins/from_cast_utils.ts +0 -34
  47. package/plugins/global_declarations_transform.ts +0 -24
  48. package/plugins/inject_filename.ts +0 -22
  49. package/plugins/strip_lua_logger.ts +0 -73
  50. package/plugins/transform_luabind_class/plugin.ts +0 -51
  51. package/plugins/transform_luabind_class/transformation/class_declaration.ts +0 -226
  52. package/plugins/transform_luabind_class/transformation/constants.ts +0 -4
  53. package/plugins/transform_luabind_class/transformation/decorators.ts +0 -16
  54. package/plugins/transform_luabind_class/transformation/errors.ts +0 -31
  55. package/plugins/transform_luabind_class/transformation/members/accessors.ts +0 -48
  56. package/plugins/transform_luabind_class/transformation/members/constructor.ts +0 -107
  57. package/plugins/transform_luabind_class/transformation/members/fields.ts +0 -53
  58. package/plugins/transform_luabind_class/transformation/members/method.ts +0 -70
  59. package/plugins/transform_luabind_class/transformation/new.ts +0 -14
  60. package/plugins/transform_luabind_class/transformation/setup.ts +0 -128
  61. package/plugins/transform_luabind_class/transformation/super.ts +0 -92
  62. package/plugins/transform_luabind_class/transformation/utils.ts +0 -90
  63. package/plugins/utils/diagnostics.ts +0 -24
  64. package/src/index.d.ts +0 -63
  65. /package/{src → types}/xr_constant.d.ts +0 -0
  66. /package/{src → types}/xr_core.d.ts +0 -0
  67. /package/{src → types}/xr_global.d.ts +0 -0
  68. /package/{src → types}/xr_lib/xr_fs.d.ts +0 -0
  69. /package/{src → types}/xr_lib/xr_math.d.ts +0 -0
  70. /package/{src → types}/xr_lib/xr_utils.d.ts +0 -0
  71. /package/{src → types}/xr_luabind.d.ts +0 -0
  72. /package/{src → types}/xr_map/xr_map.d.ts +0 -0
  73. /package/{src → types}/xr_object/xr_action.d.ts +0 -0
  74. /package/{src → types}/xr_object/xr_alife.d.ts +0 -0
  75. /package/{src → types}/xr_object/xr_anomaly.d.ts +0 -0
  76. /package/{src → types}/xr_object/xr_artefact.d.ts +0 -0
  77. /package/{src → types}/xr_object/xr_creature.d.ts +0 -0
  78. /package/{src → types}/xr_object/xr_dialog.d.ts +0 -0
  79. /package/{src → types}/xr_object/xr_item.d.ts +0 -0
  80. /package/{src → types}/xr_object/xr_level.d.ts +0 -0
  81. /package/{src → types}/xr_object/xr_physic.d.ts +0 -0
  82. /package/{src → types}/xr_object/xr_quest.d.ts +0 -0
  83. /package/{src → types}/xr_object/xr_server_object.d.ts +0 -0
  84. /package/{src → types}/xr_online/xr_multiplayer.d.ts +0 -0
  85. /package/{src → types}/xr_sound/xr_sound.d.ts +0 -0
  86. /package/{src → types}/xr_type/xr_enums.d.ts +0 -0
  87. /package/{src → types}/xr_type/xr_type.d.ts +0 -0
  88. /package/{src → types}/xr_ui/README.md +0 -0
  89. /package/{src → types}/xr_ui/demo/CServerList.png +0 -0
  90. /package/{src → types}/xr_ui/demo/CUI3tButton.png +0 -0
  91. /package/{src → types}/xr_ui/demo/CUICheckButton.png +0 -0
  92. /package/{src → types}/xr_ui/demo/CUIComboBox.png +0 -0
  93. /package/{src → types}/xr_ui/demo/CUICustomEdit.png +0 -0
  94. /package/{src → types}/xr_ui/demo/CUIStatic.png +0 -0
  95. /package/{src → types}/xr_ui/demo/CUITrackBar.png +0 -0
  96. /package/{src → types}/xr_ui/xr_ui_core.d.ts +0 -0
  97. /package/{src → types}/xr_ui/xr_ui_interface.d.ts +0 -0
@@ -0,0 +1,49 @@
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
+ }
@@ -0,0 +1,8 @@
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
+ }
@@ -0,0 +1,76 @@
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
+ }
@@ -0,0 +1,65 @@
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
+ }
@@ -0,0 +1,67 @@
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
+ }
@@ -0,0 +1,17 @@
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
+ }
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const typescript_1 = require("typescript");
4
+ const LUA_LOGGER_STRIP_TARGET = "LuaLogger";
5
+ const IS_LUA_LOGGER_DISABLED = process.argv.includes("--no-lua-logs");
6
+ /**
7
+ * Plugin that removes all LuaLogger instance creations and calls when possible.
8
+ */
9
+ const plugin = {
10
+ visitors: {
11
+ [typescript_1.SyntaxKind.VariableStatement]: (statement, context) => {
12
+ if (IS_LUA_LOGGER_DISABLED) {
13
+ let elementsCount = 0;
14
+ const list = statement.declarationList;
15
+ const nodes = [];
16
+ list.forEachChild((it) => {
17
+ const checker = context.program.getTypeChecker();
18
+ const typeSymbol = checker.getTypeAtLocation(it);
19
+ if (typeSymbol.symbol?.name === LUA_LOGGER_STRIP_TARGET) {
20
+ // nothing
21
+ }
22
+ else {
23
+ nodes.push(it);
24
+ }
25
+ elementsCount += 1;
26
+ });
27
+ if (nodes.length === 0) {
28
+ return undefined;
29
+ }
30
+ else if (nodes.length !== elementsCount) {
31
+ return context.superTransformStatements(typescript_1.factory.createVariableStatement(statement.modifiers, typescript_1.factory.updateVariableDeclarationList(list, nodes)));
32
+ }
33
+ }
34
+ return context.superTransformStatements(statement);
35
+ },
36
+ [typescript_1.SyntaxKind.ExpressionStatement]: (statement, context) => {
37
+ if (IS_LUA_LOGGER_DISABLED && statement.expression?.kind === typescript_1.SyntaxKind.CallExpression) {
38
+ const expression = statement.expression;
39
+ const propertyAccess = expression.expression;
40
+ if (propertyAccess.expression?.kind === typescript_1.SyntaxKind.Identifier) {
41
+ const checker = context.program.getTypeChecker();
42
+ const identifier = propertyAccess.expression;
43
+ const typeSymbol = checker.getTypeAtLocation(identifier);
44
+ if (typeSymbol.symbol?.name === LUA_LOGGER_STRIP_TARGET) {
45
+ return undefined;
46
+ }
47
+ }
48
+ }
49
+ return context.superTransformStatements(statement);
50
+ },
51
+ },
52
+ };
53
+ exports.default = plugin;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const typescript_1 = require("typescript");
4
+ const transformation_1 = require("./transformation");
5
+ /**
6
+ * Plugin that transform TS classes marked with decorator to luabind class declaration.
7
+ */
8
+ const plugin = {
9
+ visitors: {
10
+ [typescript_1.SyntaxKind.CallExpression]: (expression, context) => {
11
+ if ((0, transformation_1.isLuabindClassSuperCall)(expression, context)) {
12
+ return (0, transformation_1.transformLuabindConstructorSuperCall)(expression, context);
13
+ }
14
+ return context.superTransformExpression(expression);
15
+ },
16
+ [typescript_1.SyntaxKind.ClassDeclaration]: (declaration, context) => {
17
+ if ((0, transformation_1.isLuabindDecoratedClass)(declaration)) {
18
+ return (0, transformation_1.transformLuabindClassDeclaration)(declaration, context);
19
+ }
20
+ return context.superTransformStatements(declaration);
21
+ },
22
+ [typescript_1.SyntaxKind.NewExpression]: (expression, context) => {
23
+ if ((0, transformation_1.isLuabindClassType)(expression, context)) {
24
+ return (0, transformation_1.transformNewCallExpression)(expression, context);
25
+ }
26
+ return context.superTransformExpression(expression);
27
+ },
28
+ [typescript_1.SyntaxKind.SuperKeyword]: (expression, context) => {
29
+ if ((0, transformation_1.isLuabindClassSuperMethodCall)(expression, context)) {
30
+ return (0, transformation_1.transformClassSuperMethodExpression)(expression, context);
31
+ }
32
+ return context.superTransformExpression(expression);
33
+ },
34
+ },
35
+ };
36
+ exports.default = plugin;
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.transformClassLikeDeclaration = exports.transformClassAsExpression = exports.transformLuabindClassDeclaration = void 0;
4
+ const typescript_1 = require("typescript");
5
+ const tstl = require("typescript-to-lua");
6
+ const typescript_to_lua_1 = require("typescript-to-lua");
7
+ const export_1 = require("typescript-to-lua/dist/transformation/utils/export");
8
+ const lua_ast_1 = require("typescript-to-lua/dist/transformation/utils/lua-ast");
9
+ const preceding_statements_1 = require("typescript-to-lua/dist/transformation/utils/preceding-statements");
10
+ const safe_names_1 = require("typescript-to-lua/dist/transformation/utils/safe-names");
11
+ const identifier_1 = require("typescript-to-lua/dist/transformation/visitors/identifier");
12
+ const constants_1 = require("./constants");
13
+ const decorators_1 = require("./decorators");
14
+ const errors_1 = require("./errors");
15
+ const accessors_1 = require("./members/accessors");
16
+ const constructor_1 = require("./members/constructor");
17
+ const fields_1 = require("./members/fields");
18
+ const method_1 = require("./members/method");
19
+ const setup_1 = require("./setup");
20
+ const utils_1 = require("./utils");
21
+ const transformLuabindClassDeclaration = (declaration, context) => {
22
+ // If declaration is a default export, transform to export variable assignment instead
23
+ if ((0, export_1.hasDefaultExportModifier)(declaration)) {
24
+ // Class declaration including assignment to ____exports.default are in preceding statements
25
+ const { precedingStatements } = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => {
26
+ transformClassAsExpression(declaration, context);
27
+ return [];
28
+ });
29
+ return precedingStatements;
30
+ }
31
+ const { statements } = transformClassLikeDeclaration(declaration, context);
32
+ return statements;
33
+ };
34
+ exports.transformLuabindClassDeclaration = transformLuabindClassDeclaration;
35
+ function transformClassAsExpression(expression, context) {
36
+ const { statements, name } = transformClassLikeDeclaration(expression, context);
37
+ context.addPrecedingStatements(statements);
38
+ return name;
39
+ }
40
+ exports.transformClassAsExpression = transformClassAsExpression;
41
+ function transformClassLikeDeclaration(classDeclaration, context, nameOverride) {
42
+ let className;
43
+ if (nameOverride !== undefined) {
44
+ className = nameOverride;
45
+ }
46
+ else if (classDeclaration.name !== undefined) {
47
+ className = (0, identifier_1.transformIdentifier)(context, classDeclaration.name);
48
+ }
49
+ else {
50
+ className = tstl.createIdentifier(context.createTempName("class"), classDeclaration);
51
+ }
52
+ (0, utils_1.markTypeAsLuabind)(classDeclaration, context);
53
+ // Get type that is extended
54
+ const extendedTypeNode = (0, utils_1.getExtendedNode)(classDeclaration);
55
+ const extendedType = (0, utils_1.getExtendedType)(context, classDeclaration);
56
+ context.classSuperInfos.push({ className, classDeclaration: classDeclaration, extendedTypeNode });
57
+ // Get all properties with value
58
+ const properties = classDeclaration.members.filter(typescript_1.isPropertyDeclaration).filter((member) => member.initializer);
59
+ // Divide properties into static and non-static
60
+ const instanceFields = properties.filter((prop) => !(0, utils_1.isStaticNode)(prop));
61
+ const result = [];
62
+ let localClassName;
63
+ if ((0, safe_names_1.isUnsafeName)(className.text, context.options)) {
64
+ localClassName = tstl.createIdentifier((0, safe_names_1.createSafeName)(className.text), undefined, className.symbolId, className.text);
65
+ tstl.setNodePosition(localClassName, className);
66
+ }
67
+ else {
68
+ localClassName = className;
69
+ }
70
+ result.push(...(0, setup_1.createClassSetup)(context, classDeclaration, className, localClassName));
71
+ // Find first constructor with body
72
+ const constructor = classDeclaration.members.find((n) => (0, typescript_1.isConstructorDeclaration)(n) && n.body !== undefined);
73
+ if (constructor) {
74
+ // Add constructor plus initialization of instance fields
75
+ const constructorResult = (0, constructor_1.transformConstructorDeclaration)(context, constructor, localClassName, instanceFields, classDeclaration);
76
+ if (constructorResult)
77
+ result.push(constructorResult);
78
+ }
79
+ else if (!extendedType) {
80
+ // Generate a constructor if none was defined in a base class
81
+ const constructorResult = (0, constructor_1.transformConstructorDeclaration)(context, typescript_1.factory.createConstructorDeclaration([], [], typescript_1.factory.createBlock([], true)), localClassName, instanceFields, classDeclaration);
82
+ if (constructorResult)
83
+ result.push(constructorResult);
84
+ }
85
+ else {
86
+ // Generate a constructor if none was defined in a class with instance fields that need initialization
87
+ // localClassName.__init = function(self, ...)
88
+ // baseClassName.__init(self, ...) // or unpack(arg) for Lua 5.0
89
+ // ...
90
+ // Also luabind always needs definition of call expression to use table as class.
91
+ const constructorBody = (0, fields_1.transformClassInstanceFields)(context, instanceFields);
92
+ const argsExpression = context.luaTarget === typescript_to_lua_1.LuaTarget.Lua50
93
+ ? tstl.createCallExpression(tstl.createIdentifier("unpack"), [tstl.createArgLiteral()])
94
+ : tstl.createDotsLiteral();
95
+ const superCall = tstl.createExpressionStatement(tstl.createCallExpression(tstl.createTableIndexExpression(context.transformExpression(typescript_1.factory.createSuper()), tstl.createStringLiteral(constants_1.LUABIND_CONSTRUCTOR_METHOD)), [(0, lua_ast_1.createSelfIdentifier)(), argsExpression]));
96
+ constructorBody.unshift(superCall);
97
+ const constructorFunction = tstl.createFunctionExpression(tstl.createBlock(constructorBody), [(0, lua_ast_1.createSelfIdentifier)()], tstl.createDotsLiteral(), tstl.NodeFlags.Declaration);
98
+ result.push(tstl.createAssignmentStatement((0, constructor_1.createConstructorName)(localClassName), constructorFunction, classDeclaration));
99
+ }
100
+ // Transform accessors
101
+ for (const member of classDeclaration.members) {
102
+ if (!(0, typescript_1.isAccessor)(member))
103
+ continue;
104
+ const accessors = context.resolver.getAllAccessorDeclarations(member);
105
+ if (accessors.firstAccessor !== member)
106
+ continue;
107
+ const accessorsResult = (0, accessors_1.transformAccessorDeclarations)(context, accessors, localClassName);
108
+ if (accessorsResult) {
109
+ result.push(accessorsResult);
110
+ }
111
+ }
112
+ const decorationStatements = [];
113
+ for (const member of classDeclaration.members) {
114
+ if ((0, typescript_1.isAccessor)(member)) {
115
+ (0, fields_1.verifyPropertyDecoratingExpression)(context, member);
116
+ }
117
+ else if ((0, typescript_1.isMethodDeclaration)(member)) {
118
+ const statement = (0, method_1.transformMethodDeclaration)(context, member, localClassName);
119
+ if (statement)
120
+ result.push(statement);
121
+ if (member.body) {
122
+ (0, method_1.verifyMethodDecoratingExpression)(context, member);
123
+ }
124
+ }
125
+ else if ((0, typescript_1.isPropertyDeclaration)(member)) {
126
+ if ((0, utils_1.isStaticNode)(member)) {
127
+ context.diagnostics.push((0, errors_1.unsupportedStaticMethod)(member));
128
+ }
129
+ (0, fields_1.verifyPropertyDecoratingExpression)(context, member);
130
+ }
131
+ }
132
+ result.push(...decorationStatements);
133
+ // Decorate the class
134
+ if ((0, typescript_1.canHaveDecorators)(classDeclaration) && (0, typescript_1.getDecorators)(classDeclaration)) {
135
+ (0, typescript_1.getDecorators)(classDeclaration)?.forEach((d) => (0, decorators_1.checkLuabindClassDecoratorExpression)(context, d));
136
+ if ((0, export_1.hasExportModifier)(classDeclaration)) {
137
+ const exportExpression = (0, export_1.hasDefaultExportModifier)(classDeclaration)
138
+ ? (0, export_1.createDefaultExportExpression)(classDeclaration)
139
+ : (0, export_1.createExportedIdentifier)(context, className);
140
+ const classAssignment = tstl.createAssignmentStatement(exportExpression, localClassName);
141
+ result.push(classAssignment);
142
+ }
143
+ }
144
+ context.classSuperInfos.pop();
145
+ return { statements: result, name: className };
146
+ }
147
+ exports.transformClassLikeDeclaration = transformClassLikeDeclaration;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LUABIND_SYMBOL = exports.LUABIND_CONSTRUCTOR_METHOD = exports.LUABIND_DECORATOR = exports.LUABIND_NAME_FIELD = void 0;
4
+ exports.LUABIND_NAME_FIELD = "__name";
5
+ exports.LUABIND_DECORATOR = "LuabindClass";
6
+ exports.LUABIND_CONSTRUCTOR_METHOD = "__init";
7
+ exports.LUABIND_SYMBOL = Symbol("IS_LUABIND_CLASS");
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkLuabindClassDecoratorExpression = void 0;
4
+ const constants_1 = require("./constants");
5
+ const errors_1 = require("./errors");
6
+ /**
7
+ * Transform decorator call expressions for luabind class.
8
+ */
9
+ function checkLuabindClassDecoratorExpression(context, decorator) {
10
+ const expression = decorator.expression;
11
+ // Do not transform luabind decorator.
12
+ if (expression.expression.text !== constants_1.LUABIND_DECORATOR) {
13
+ context.diagnostics.push((0, errors_1.unsupportedClassDecorator)(expression));
14
+ }
15
+ }
16
+ exports.checkLuabindClassDecoratorExpression = checkLuabindClassDecoratorExpression;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.unsupportedStaticAccessor = exports.unsupportedMethodDecorator = exports.unsupportedParameterDecorator = exports.unsupportedPropertyDecorator = exports.unsupportedClassDecorator = exports.unsupportedStaticMethod = void 0;
4
+ const diagnostics_1 = require("../../utils/diagnostics");
5
+ exports.unsupportedStaticMethod = (0, diagnostics_1.createErrorDiagnosticFactory)((name) => {
6
+ const nameReference = name ? ` '${name}'` : "";
7
+ return `Unable transform static properties for luabind classes${nameReference}.`;
8
+ });
9
+ exports.unsupportedClassDecorator = (0, diagnostics_1.createErrorDiagnosticFactory)((name) => {
10
+ const nameReference = name ? ` '${name}'` : "";
11
+ return `Unable transform class decorators for luabind classes${nameReference}.`;
12
+ });
13
+ exports.unsupportedPropertyDecorator = (0, diagnostics_1.createErrorDiagnosticFactory)((name) => {
14
+ const nameReference = name ? ` '${name}'` : "";
15
+ return `Unable transform property decorator for luabind classes${nameReference}.`;
16
+ });
17
+ exports.unsupportedParameterDecorator = (0, diagnostics_1.createErrorDiagnosticFactory)((name) => {
18
+ const nameReference = name ? ` '${name}'` : "";
19
+ return `Unable transform parameter decorator for luabind classes${nameReference}.`;
20
+ });
21
+ exports.unsupportedMethodDecorator = (0, diagnostics_1.createErrorDiagnosticFactory)((name) => {
22
+ const nameReference = name ? ` '${name}'` : "";
23
+ return `Unable transform method decorator for luabind classes${nameReference}.`;
24
+ });
25
+ exports.unsupportedStaticAccessor = (0, diagnostics_1.createErrorDiagnosticFactory)((name) => {
26
+ const nameReference = name ? ` '${name}'` : "";
27
+ return `Unable transform static accessors for luabind classes${nameReference}.`;
28
+ });
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./constants"), exports);
18
+ __exportStar(require("./utils"), exports);
19
+ __exportStar(require("./class_declaration"), exports);
20
+ __exportStar(require("./super"), exports);
21
+ __exportStar(require("./new"), exports);
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.transformAccessorDeclarations = void 0;
4
+ const typescript_to_lua_1 = require("typescript-to-lua");
5
+ const lua = require("typescript-to-lua");
6
+ const lua_ast_1 = require("typescript-to-lua/dist/transformation/utils/lua-ast");
7
+ const lualib_1 = require("typescript-to-lua/dist/transformation/utils/lualib");
8
+ const function_1 = require("typescript-to-lua/dist/transformation/visitors/function");
9
+ const literal_1 = require("typescript-to-lua/dist/transformation/visitors/literal");
10
+ const errors_1 = require("../errors");
11
+ const utils_1 = require("../utils");
12
+ function transformAccessor(context, node) {
13
+ const [params, dot, restParam] = (0, function_1.transformParameters)(context, node.parameters, (0, lua_ast_1.createSelfIdentifier)());
14
+ const body = node.body ? (0, function_1.transformFunctionBody)(context, node.parameters, node.body, restParam)[0] : [];
15
+ return lua.createFunctionExpression(lua.createBlock(body), params, dot, lua.NodeFlags.Declaration);
16
+ }
17
+ function transformAccessorDeclarations(context, { firstAccessor, getAccessor, setAccessor }, className) {
18
+ const propertyName = (0, literal_1.transformPropertyName)(context, firstAccessor.name);
19
+ const descriptor = lua.createTableExpression([]);
20
+ if (getAccessor) {
21
+ const getterFunction = transformAccessor(context, getAccessor);
22
+ descriptor.fields.push(lua.createTableFieldExpression(getterFunction, lua.createStringLiteral("get")));
23
+ }
24
+ if (setAccessor) {
25
+ const setterFunction = transformAccessor(context, setAccessor);
26
+ descriptor.fields.push(lua.createTableFieldExpression(setterFunction, lua.createStringLiteral("set")));
27
+ }
28
+ const isStatic = (0, utils_1.isStaticNode)(firstAccessor);
29
+ if (isStatic) {
30
+ context.diagnostics.push((0, errors_1.unsupportedStaticAccessor)(firstAccessor));
31
+ return;
32
+ }
33
+ const target = lua.cloneIdentifier(className);
34
+ const feature = typescript_to_lua_1.LuaLibFeature.ObjectDefineProperty;
35
+ const parameters = [target, propertyName, descriptor];
36
+ const call = (0, lualib_1.transformLuaLibFunction)(context, feature, undefined, ...parameters);
37
+ return lua.createExpressionStatement(call);
38
+ }
39
+ exports.transformAccessorDeclarations = transformAccessorDeclarations;