xray16 1.0.9 → 1.0.10
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/inline.js +89 -0
- package/types/index.d.ts +2 -0
- package/types/xrf_plugin.d.ts +44 -0
package/package.json
CHANGED
|
@@ -0,0 +1,89 @@
|
|
|
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;
|
package/types/index.d.ts
CHANGED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
/**
|
|
3
|
+
* Utility to get current filename, similar to __filename in nodejs.
|
|
4
|
+
*
|
|
5
|
+
* @group xrf_plugin
|
|
6
|
+
*/
|
|
7
|
+
const $filename: string;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Utility to transform TS provided array to a lua one.
|
|
11
|
+
* Just wrapper that is stripped compile time, but simplifies unit testing with TS.
|
|
12
|
+
*
|
|
13
|
+
* @group xrf_plugin
|
|
14
|
+
*/
|
|
15
|
+
function $fromArray<T>(array: Array<T>): LuaTable<number, T>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Utility to transform LUA array to JS array.
|
|
19
|
+
* Just wrapper that is stripped compile time, but simplifies unit testing with TS.
|
|
20
|
+
*
|
|
21
|
+
* @group xrf_plugin
|
|
22
|
+
*/
|
|
23
|
+
function $fromLuaArray<T>(array: LuaTable<number, T>): Array<T>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Utility to transform TS provided object to a lua table.
|
|
27
|
+
* Just wrapper that is stripped compile time, but simplifies unit testing with TS.
|
|
28
|
+
*
|
|
29
|
+
* @group xrf_plugin
|
|
30
|
+
*/
|
|
31
|
+
function $fromObject<K extends string | number, T>(object: Record<K, T>): LuaTable<K, T>;
|
|
32
|
+
function $fromObject<D>(object: D): LuaTable<keyof D, D[keyof D]>;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Utility to transform LUA provided table to a TS one.
|
|
36
|
+
* Just wrapper that is stripped compile time, but simplifies unit testing with TS.
|
|
37
|
+
*
|
|
38
|
+
* @group xrf_plugin
|
|
39
|
+
*/
|
|
40
|
+
function $fromLuaTable<K extends string | number, T>(object: LuaTable<K, T>): Record<K, T>;
|
|
41
|
+
function $fromLuaTable<D>(object: LuaTable<keyof D, D[keyof D]>): D;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export {};
|