crankscript 0.9.5 → 0.9.6
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/assets/plugin.js
ADDED
@@ -0,0 +1,204 @@
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
+
if (mod && mod.__esModule) return mod;
|
20
|
+
var result = {};
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
+
__setModuleDefault(result, mod);
|
23
|
+
return result;
|
24
|
+
};
|
25
|
+
var _a;
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
27
|
+
exports.transformSuperExpression = exports.transformClassDeclaration = void 0;
|
28
|
+
exports.transformPropertyName = transformPropertyName;
|
29
|
+
var ts = __importStar(require("typescript"));
|
30
|
+
var tstl = __importStar(require("typescript-to-lua"));
|
31
|
+
var lua = __importStar(require("typescript-to-lua/dist/LuaAST"));
|
32
|
+
var scope_1 = require("typescript-to-lua/dist/transformation/utils/scope");
|
33
|
+
var call_1 = require("typescript-to-lua/dist/transformation/visitors/call");
|
34
|
+
var fields_1 = require("typescript-to-lua/dist/transformation/visitors/class/members/fields");
|
35
|
+
var utils_1 = require("typescript-to-lua/dist/transformation/visitors/class/utils");
|
36
|
+
var function_1 = require("typescript-to-lua/dist/transformation/visitors/function");
|
37
|
+
function createClassCall(context, className, extendsNode) {
|
38
|
+
// class('X')
|
39
|
+
var classCall = tstl.createCallExpression(tstl.createIdentifier('class'), [tstl.createStringLiteral(className.text)]);
|
40
|
+
var classCreationExpression;
|
41
|
+
if (extendsNode) {
|
42
|
+
// class('X').extends(Blah)
|
43
|
+
classCreationExpression = tstl.createCallExpression(tstl.createTableIndexExpression(classCall, tstl.createStringLiteral('extends')), [context.transformExpression(extendsNode.expression)]);
|
44
|
+
}
|
45
|
+
else {
|
46
|
+
classCreationExpression = tstl.createCallExpression(tstl.createTableIndexExpression(classCall, tstl.createStringLiteral('extends')), [tstl.createIdentifier('Object')]);
|
47
|
+
}
|
48
|
+
return tstl.createExpressionStatement(classCreationExpression);
|
49
|
+
}
|
50
|
+
function transformPropertyName(context, node) {
|
51
|
+
if (ts.isComputedPropertyName(node)) {
|
52
|
+
return context.transformExpression(node.expression);
|
53
|
+
}
|
54
|
+
else if (ts.isIdentifier(node)) {
|
55
|
+
return tstl.createStringLiteral(node.text);
|
56
|
+
}
|
57
|
+
else if (ts.isPrivateIdentifier(node)) {
|
58
|
+
throw new Error('PrivateIdentifier is not supported');
|
59
|
+
}
|
60
|
+
else {
|
61
|
+
return context.transformExpression(node);
|
62
|
+
}
|
63
|
+
}
|
64
|
+
function transformConstructor(context, className, instanceFields, constructor) {
|
65
|
+
var methodName = 'init';
|
66
|
+
context.pushScope(scope_1.ScopeType.Function);
|
67
|
+
var bodyStatements = [];
|
68
|
+
var params;
|
69
|
+
if (constructor) {
|
70
|
+
params = (0, function_1.transformParameters)(context, constructor === null || constructor === void 0 ? void 0 : constructor.parameters, tstl.createIdentifier('self'))[0];
|
71
|
+
}
|
72
|
+
else {
|
73
|
+
params = [tstl.createIdentifier('self')];
|
74
|
+
}
|
75
|
+
bodyStatements.push(tstl.createExpressionStatement(tstl.createCallExpression(tstl.createTableIndexExpression(tstl.createTableIndexExpression(className, tstl.createStringLiteral('super')), tstl.createStringLiteral('init')), params)));
|
76
|
+
var classInstanceFields = (0, fields_1.transformClassInstanceFields)(context, instanceFields);
|
77
|
+
// initializers have to come before any body of the constructor
|
78
|
+
bodyStatements.push.apply(bodyStatements, classInstanceFields);
|
79
|
+
if (constructor === null || constructor === void 0 ? void 0 : constructor.body) {
|
80
|
+
var body = (0, function_1.transformFunctionBodyContent)(context, constructor.body);
|
81
|
+
// if the first expression in the body is a super call, ignore it, because we have
|
82
|
+
// constructed our own super call.
|
83
|
+
// if it's not, make sure to include the entire body.
|
84
|
+
var firstStatement = constructor.body.statements[0];
|
85
|
+
if (firstStatement &&
|
86
|
+
ts.isExpressionStatement(firstStatement) &&
|
87
|
+
ts.isCallExpression(firstStatement.expression) &&
|
88
|
+
firstStatement.expression.expression.kind ===
|
89
|
+
ts.SyntaxKind.SuperKeyword) {
|
90
|
+
bodyStatements.push.apply(bodyStatements, body.slice(1));
|
91
|
+
}
|
92
|
+
else {
|
93
|
+
bodyStatements.push.apply(bodyStatements, body);
|
94
|
+
}
|
95
|
+
}
|
96
|
+
context.popScope();
|
97
|
+
return tstl.createAssignmentStatement(tstl.createTableIndexExpression(className, tstl.createStringLiteral(methodName)), tstl.createFunctionExpression(tstl.createBlock(bodyStatements), params));
|
98
|
+
}
|
99
|
+
function transformMethodDeclaration(context, node, className) {
|
100
|
+
var functionExpression = (0, function_1.transformFunctionToExpression)(context, node)[0];
|
101
|
+
return tstl.createAssignmentStatement(tstl.createTableIndexExpression(className, transformPropertyName(context, node.name)), functionExpression);
|
102
|
+
}
|
103
|
+
var transformClassDeclaration = function (declaration, context) {
|
104
|
+
var className;
|
105
|
+
if (declaration.name) {
|
106
|
+
className = tstl.createIdentifier(declaration.name.text);
|
107
|
+
}
|
108
|
+
else {
|
109
|
+
className = tstl.createIdentifier(context.createTempName('class'), declaration);
|
110
|
+
}
|
111
|
+
var extension = (0, utils_1.getExtendedNode)(declaration);
|
112
|
+
if (context.classSuperInfos) {
|
113
|
+
context.classSuperInfos.push({
|
114
|
+
className: className,
|
115
|
+
extendedTypeNode: extension,
|
116
|
+
});
|
117
|
+
}
|
118
|
+
else {
|
119
|
+
context.classSuperInfos = [{ className: className, extendedTypeNode: extension }];
|
120
|
+
}
|
121
|
+
// Get all properties with value
|
122
|
+
var properties = declaration.members
|
123
|
+
.filter(ts.isPropertyDeclaration)
|
124
|
+
.filter(function (member) { return member.initializer; });
|
125
|
+
// Divide properties into static and non-static
|
126
|
+
var instanceFields = properties.filter(function (prop) { return !(0, utils_1.isStaticNode)(prop); });
|
127
|
+
var statements = [];
|
128
|
+
// class('X')
|
129
|
+
statements.push(createClassCall(context, className, extension));
|
130
|
+
// function X:init()
|
131
|
+
// X.super.init(self)
|
132
|
+
// end
|
133
|
+
var constructor = declaration.members.find(function (n) {
|
134
|
+
return ts.isConstructorDeclaration(n) && n.body !== undefined;
|
135
|
+
});
|
136
|
+
var transformedConstructor = transformConstructor(context, className, instanceFields, constructor);
|
137
|
+
if (transformedConstructor) {
|
138
|
+
statements.push(transformedConstructor);
|
139
|
+
}
|
140
|
+
var methods = declaration.members
|
141
|
+
.filter(ts.isMethodDeclaration)
|
142
|
+
.map(function (method) { return transformMethodDeclaration(context, method, className); })
|
143
|
+
.filter(function (method) { return method !== undefined; });
|
144
|
+
statements.push.apply(statements, methods);
|
145
|
+
return statements;
|
146
|
+
};
|
147
|
+
exports.transformClassDeclaration = transformClassDeclaration;
|
148
|
+
var transformNewExpression = function (node, context) {
|
149
|
+
var _a;
|
150
|
+
var signature = context.checker.getResolvedSignature(node);
|
151
|
+
var _b = (0, call_1.transformCallAndArguments)(context, node.expression, (_a = node.arguments) !== null && _a !== void 0 ? _a : [ts.factory.createTrue()], signature), name = _b[0], params = _b[1];
|
152
|
+
return tstl.createCallExpression(name, params);
|
153
|
+
};
|
154
|
+
var transformSuperExpression = function (expression, context) {
|
155
|
+
var superInfos = context.classSuperInfos;
|
156
|
+
var superInfo = undefined;
|
157
|
+
if (superInfos) {
|
158
|
+
superInfo = superInfos[superInfos.length - 1];
|
159
|
+
}
|
160
|
+
if (!superInfo)
|
161
|
+
return lua.createAnonymousIdentifier(expression);
|
162
|
+
var className = superInfo.className;
|
163
|
+
// Using `super` without extended type node is a TypeScript error
|
164
|
+
// const extendsExpression = extendedTypeNode?.expression;
|
165
|
+
// let baseClassName: lua.AssignmentLeftHandSideExpression | undefined;
|
166
|
+
// if (extendsExpression && ts.isIdentifier(extendsExpression)) {
|
167
|
+
// const symbol = context.checker.getSymbolAtLocation(extendsExpression);
|
168
|
+
// if (symbol && !isSymbolExported(context, symbol)) {
|
169
|
+
// // Use "baseClassName" if base is a simple identifier
|
170
|
+
// baseClassName = transformIdentifier(context, extendsExpression);
|
171
|
+
// }
|
172
|
+
// }
|
173
|
+
// if (!baseClassName) {
|
174
|
+
// // Use "className.____super" if the base is not a simple identifier
|
175
|
+
// baseClassName = lua.createTableIndexExpression(
|
176
|
+
// className,
|
177
|
+
// lua.createStringLiteral('____super'),
|
178
|
+
// expression
|
179
|
+
// );
|
180
|
+
// }
|
181
|
+
return lua.createTableIndexExpression(className, lua.createStringLiteral('super'));
|
182
|
+
};
|
183
|
+
exports.transformSuperExpression = transformSuperExpression;
|
184
|
+
var plugin = {
|
185
|
+
visitors: (_a = {},
|
186
|
+
_a[ts.SyntaxKind.ClassDeclaration] = exports.transformClassDeclaration,
|
187
|
+
_a[ts.SyntaxKind.SuperKeyword] = exports.transformSuperExpression,
|
188
|
+
_a[ts.SyntaxKind.NewExpression] = transformNewExpression,
|
189
|
+
_a[ts.SyntaxKind.CallExpression] = function (node, context) {
|
190
|
+
if (ts.isIdentifier(node.expression) &&
|
191
|
+
node.expression.escapedText === 'require') {
|
192
|
+
console.log(1);
|
193
|
+
var normalNode = context.superTransformExpression(node);
|
194
|
+
normalNode.expression.text = 'import';
|
195
|
+
normalNode.expression.originalName = 'import';
|
196
|
+
return normalNode;
|
197
|
+
}
|
198
|
+
else {
|
199
|
+
return context.superTransformExpression(node);
|
200
|
+
}
|
201
|
+
},
|
202
|
+
_a),
|
203
|
+
};
|
204
|
+
exports.default = plugin;
|
package/package.json
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
{
|
2
2
|
"name": "crankscript",
|
3
|
-
"version": "0.9.
|
3
|
+
"version": "0.9.6",
|
4
4
|
"scripts": {
|
5
5
|
"dev": "tsx src/index.ts",
|
6
|
-
"post-build": "tsc-alias --project tsconfig.json"
|
6
|
+
"post-build": "tsc-alias --project tsconfig.json",
|
7
|
+
"prepare-assets": "npx tsc -p assets/tsconfig.json"
|
7
8
|
},
|
8
9
|
"bin": {
|
9
10
|
"crankscript": "./src/index.js"
|
@@ -17,9 +18,7 @@
|
|
17
18
|
"ink": "^5.0.1",
|
18
19
|
"react": "^18.3.1",
|
19
20
|
"ts-morph": "^23.0.0",
|
20
|
-
"ts-node": "^10.9.2",
|
21
21
|
"typanion": "^3.14.0",
|
22
|
-
"typescript": "~5.6.2",
|
23
22
|
"typescript-to-lua": "^1.27.0"
|
24
23
|
},
|
25
24
|
"type": "module",
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../../../../../libs/cli/src/commands/TranspileCommand/components/Transpile.tsx"],"sourcesContent":["import { join } from 'node:path';\nimport React from 'react';\nimport { useMemo } from 'react';\nimport * as tstl from 'typescript-to-lua';\nimport { LuaTarget } from 'typescript-to-lua';\nimport { CheckList } from '@/cli/components/CheckList/index.js';\nimport { RootFolder } from '@/cli/constants.js';\nimport { CheckListItem } from '@/cli/types.js';\n\nconst transpile = (path: string) => {\n tstl.transpileProject(join(path, 'tsconfig.json'), {\n luaTarget: LuaTarget.Lua54,\n outDir: join(path, 'Source'),\n luaBundle: 'main.lua',\n luaBundleEntry: join(path, 'src', 'index.ts'),\n luaPlugins: [\n {\n name: join(RootFolder, 'assets', 'plugin.
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/cli/src/commands/TranspileCommand/components/Transpile.tsx"],"sourcesContent":["import { join } from 'node:path';\nimport React from 'react';\nimport { useMemo } from 'react';\nimport * as tstl from 'typescript-to-lua';\nimport { LuaTarget } from 'typescript-to-lua';\nimport { CheckList } from '@/cli/components/CheckList/index.js';\nimport { RootFolder } from '@/cli/constants.js';\nimport { CheckListItem } from '@/cli/types.js';\n\nconst transpile = (path: string) => {\n tstl.transpileProject(join(path, 'tsconfig.json'), {\n luaTarget: LuaTarget.Lua54,\n outDir: join(path, 'Source'),\n luaBundle: 'main.lua',\n luaBundleEntry: join(path, 'src', 'index.ts'),\n luaPlugins: [\n {\n name: join(RootFolder, 'assets', 'plugin.js'),\n },\n ],\n });\n};\n\nexport const Transpile = ({ path }: { path: string }) => {\n const items = useMemo(\n () => [\n {\n waitingDescription: 'Waiting to transpile code...',\n errorDescription: 'Could not transpile code',\n runningDescription: 'Transpiling code...',\n finishedDescription: () => 'Code transpiled',\n runner: async () => {\n transpile(path);\n },\n ready: true,\n },\n ],\n []\n ) as CheckListItem<unknown>[];\n\n return <CheckList items={items} onFinish={process.exit} />;\n};\n"],"names":["join","React","useMemo","tstl","LuaTarget","CheckList","RootFolder","transpile","path","transpileProject","luaTarget","Lua54","outDir","luaBundle","luaBundleEntry","luaPlugins","name","Transpile","items","waitingDescription","errorDescription","runningDescription","finishedDescription","runner","ready","onFinish","process","exit"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,SAASA,IAAI,QAAQ,YAAY;AACjC,OAAOC,WAAW,QAAQ;AAC1B,SAASC,OAAO,QAAQ,QAAQ;AAChC,YAAYC,UAAU,oBAAoB;AAC1C,SAASC,SAAS,QAAQ,oBAAoB;AAC9C,SAASC,SAAS,QAAQ,sCAAsC;AAChE,SAASC,UAAU,QAAQ,qBAAqB;AAGhD,MAAMC,YAAY,CAACC;IACfL,KAAKM,gBAAgB,CAACT,KAAKQ,MAAM,kBAAkB;QAC/CE,WAAWN,UAAUO,KAAK;QAC1BC,QAAQZ,KAAKQ,MAAM;QACnBK,WAAW;QACXC,gBAAgBd,KAAKQ,MAAM,OAAO;QAClCO,YAAY;YACR;gBACIC,MAAMhB,KAAKM,YAAY,UAAU;YACrC;SACH;IACL;AACJ;AAEA,OAAO,MAAMW,YAAY,CAAC,EAAET,IAAI,EAAoB;IAChD,MAAMU,QAAQhB,QACV,IAAM;YACF;gBACIiB,oBAAoB;gBACpBC,kBAAkB;gBAClBC,oBAAoB;gBACpBC,qBAAqB,IAAM;gBAC3BC,QAAQ;oBACJhB,UAAUC;gBACd;gBACAgB,OAAO;YACX;SACH,EACD,EAAE;IAGN,qBAAO,oBAACnB;QAAUa,OAAOA;QAAOO,UAAUC,QAAQC,IAAI;;AAC1D,EAAE"}
|