@boristype/bt-cli 0.1.0-alpha.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/LICENSE +21 -0
- package/README.md +66 -0
- package/build/builder/config.js +88 -0
- package/build/cli/commands/artifact.js +14 -0
- package/build/cli/commands/build.js +25 -0
- package/build/cli/commands/dev.js +190 -0
- package/build/cli/commands/index.js +18 -0
- package/build/cli/commands/init.js +15 -0
- package/build/cli/commands/link.js +18 -0
- package/build/cli/commands/push.js +28 -0
- package/build/cli/index.js +14 -0
- package/build/cli/types.js +2 -0
- package/build/core/artifacting/context.js +54 -0
- package/build/core/artifacting/index.js +37 -0
- package/build/core/artifacting/stages/index.js +10 -0
- package/build/core/artifacting/stages/main-archive.js +72 -0
- package/build/core/artifacting/stages/validate.js +70 -0
- package/build/core/artifacting/types.js +6 -0
- package/build/core/artifacting/utils/index.js +10 -0
- package/build/core/artifacting/utils/zip.js +94 -0
- package/build/core/babel.js +96 -0
- package/build/core/btconfig.types.js +6 -0
- package/build/core/build.js +280 -0
- package/build/core/building/compile-mode.js +146 -0
- package/build/core/building/compiler.js +281 -0
- package/build/core/building/coordinator.js +71 -0
- package/build/core/building/files.js +290 -0
- package/build/core/building/index.js +102 -0
- package/build/core/building/output.js +92 -0
- package/build/core/building/transformers.js +110 -0
- package/build/core/building/types.js +19 -0
- package/build/core/config.js +157 -0
- package/build/core/dependencies.js +223 -0
- package/build/core/linking/cache.js +260 -0
- package/build/core/linking/context.js +149 -0
- package/build/core/linking/dependencies.js +240 -0
- package/build/core/linking/executables.js +61 -0
- package/build/core/linking/generators/api-ext.js +57 -0
- package/build/core/linking/generators/component.js +83 -0
- package/build/core/linking/generators/filemap.js +53 -0
- package/build/core/linking/generators/index.js +21 -0
- package/build/core/linking/generators/init-xml.js +37 -0
- package/build/core/linking/generators/package-json.js +50 -0
- package/build/core/linking/index.js +213 -0
- package/build/core/linking/linkers/component.js +175 -0
- package/build/core/linking/linkers/index.js +69 -0
- package/build/core/linking/linkers/standalone.js +144 -0
- package/build/core/linking/linkers/system.js +86 -0
- package/build/core/linking/parsers.js +278 -0
- package/build/core/linking/types.js +6 -0
- package/build/core/linking/utils/copy.js +101 -0
- package/build/core/linking/utils/index.js +26 -0
- package/build/core/linking/utils/node-modules.js +226 -0
- package/build/core/linking/utils/package-type.js +101 -0
- package/build/core/linking/utils/url.js +73 -0
- package/build/core/linking/utils/write.js +91 -0
- package/build/core/logger.js +10 -0
- package/build/core/pushing/config.js +90 -0
- package/build/core/pushing/index.js +96 -0
- package/build/core/pushing/init-scripts.js +173 -0
- package/build/core/pushing/queue.js +95 -0
- package/build/core/pushing/reinit.js +61 -0
- package/build/core/pushing/session.js +167 -0
- package/build/core/pushing/types.js +6 -0
- package/build/core/pushing/upload.js +35 -0
- package/build/core/tsconfig.js +78 -0
- package/build/core/utils/index.js +17 -0
- package/build/core/utils/logger.js +46 -0
- package/build/core/utils/properties.js +81 -0
- package/build/core/utils/xml.js +44 -0
- package/build/core/utils.js +59 -0
- package/build/index.js +76 -0
- package/build/plugins/destructuring.js +83 -0
- package/build/plugins/forOfToForIn.js +14 -0
- package/build/plugins/loopHoistVariables.js +160 -0
- package/build/plugins/precedence.js +172 -0
- package/build/plugins/removeImportExport.js +42 -0
- package/build/plugins/replaceDollar.js +16 -0
- package/build/plugins/spreadArray.js +42 -0
- package/build/plugins/spreadObject.js +91 -0
- package/build/transformers/arrayFunctional.js +467 -0
- package/build/transformers/arrayGeneral.js +222 -0
- package/build/transformers/blockScoping.js +212 -0
- package/build/transformers/destructuring.js +133 -0
- package/build/transformers/dirname.js +79 -0
- package/build/transformers/enumsToObjects.js +25 -0
- package/build/transformers/execObj.js +220 -0
- package/build/transformers/forOfToForIn.js +45 -0
- package/build/transformers/funcSemantic.js +113 -0
- package/build/transformers/functions.js +270 -0
- package/build/transformers/globalCache.js +34 -0
- package/build/transformers/loopHoistVariables.js +352 -0
- package/build/transformers/math.js +39 -0
- package/build/transformers/namespaces.js +22 -0
- package/build/transformers/numericSeparator.js +46 -0
- package/build/transformers/objectProperties.js +54 -0
- package/build/transformers/precedence.js +192 -0
- package/build/transformers/propSemantic.js +467 -0
- package/build/transformers/remodule.js +620 -0
- package/build/transformers/removeImportExport.js +135 -0
- package/build/transformers/replaceDollar.js +46 -0
- package/build/transformers/shorthandProperties.js +34 -0
- package/build/transformers/spreadArray.js +68 -0
- package/build/transformers/spreadObject.js +134 -0
- package/build/transformers/string.js +138 -0
- package/build/transformers/templateLiterals.js +104 -0
- package/build/transformers/tocodelibrary.js +178 -0
- package/build/transformers/utils.js +202 -0
- package/build/wshcm/client.js +193 -0
- package/build/wshcm/evaluator.js +111 -0
- package/build/wshcm/exceptions.js +25 -0
- package/build/wshcm/index.js +20 -0
- package/build/wshcm/soap-utils.js +228 -0
- package/build/wshcm/types.js +2 -0
- package/build/wshcm/uploader.js +320 -0
- package/package.json +51 -0
|
@@ -0,0 +1,467 @@
|
|
|
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 () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.default = arrayFunctionalTransformer;
|
|
37
|
+
const ts = __importStar(require("typescript"));
|
|
38
|
+
const utils_1 = require("./utils");
|
|
39
|
+
function arrayFunctionalTransformer(program) {
|
|
40
|
+
return (context) => (file) => {
|
|
41
|
+
const typeChecker = program.getTypeChecker();
|
|
42
|
+
const arrayReturningMethods = new Set(["map", "filter", "flatMap"]);
|
|
43
|
+
const booleanReturningMethods = new Set(["every", "some"]);
|
|
44
|
+
const findMethods = new Set(["find", "findIndex", "findLast", "findLastIndex"]);
|
|
45
|
+
const reduceMethods = new Set(["reduce", "reduceRight"]);
|
|
46
|
+
const supportedMethods = new Set([
|
|
47
|
+
...arrayReturningMethods,
|
|
48
|
+
...booleanReturningMethods,
|
|
49
|
+
...findMethods,
|
|
50
|
+
"forEach",
|
|
51
|
+
...reduceMethods,
|
|
52
|
+
]);
|
|
53
|
+
function containsSupportedMethod(expr) {
|
|
54
|
+
if (ts.isCallExpression(expr)) {
|
|
55
|
+
const callee = expr.expression;
|
|
56
|
+
if (ts.isPropertyAccessExpression(callee)) {
|
|
57
|
+
if (supportedMethods.has(callee.name.text))
|
|
58
|
+
return true;
|
|
59
|
+
return containsSupportedMethod(callee.expression);
|
|
60
|
+
}
|
|
61
|
+
if (ts.isCallExpression(callee))
|
|
62
|
+
return containsSupportedMethod(callee);
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
if (ts.isPropertyAccessExpression(expr)) {
|
|
66
|
+
return containsSupportedMethod(expr.expression);
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
function getRootExpression(expr) {
|
|
71
|
+
if (ts.isCallExpression(expr)) {
|
|
72
|
+
const callee = expr.expression;
|
|
73
|
+
if (ts.isPropertyAccessExpression(callee) && arrayReturningMethods.has(callee.name.text)) {
|
|
74
|
+
return callee.expression; // Returns obj.arr for obj.arr.map, filter, flatMap
|
|
75
|
+
}
|
|
76
|
+
return getRootExpression(callee);
|
|
77
|
+
}
|
|
78
|
+
if (ts.isPropertyAccessExpression(expr)) {
|
|
79
|
+
return getRootExpression(expr.expression);
|
|
80
|
+
}
|
|
81
|
+
return expr;
|
|
82
|
+
}
|
|
83
|
+
function transformChain(expression, statements, tempVarPrefix, index) {
|
|
84
|
+
if (ts.isCallExpression(expression) && ts.isPropertyAccessExpression(expression.expression)) {
|
|
85
|
+
const methodName = expression.expression.name.text;
|
|
86
|
+
const objExpr = expression.expression.expression;
|
|
87
|
+
const base = transformChain(objExpr, statements, tempVarPrefix, index + 1);
|
|
88
|
+
const baseExpr = base.expr;
|
|
89
|
+
const accStatements = base.statements;
|
|
90
|
+
if (supportedMethods.has(methodName)) {
|
|
91
|
+
const callback = expression.arguments[0];
|
|
92
|
+
const iVar = ts.factory.createUniqueName(`i_${index}`);
|
|
93
|
+
const loopBody = [];
|
|
94
|
+
let callbackBody;
|
|
95
|
+
let callbackParams;
|
|
96
|
+
if (callback && (ts.isArrowFunction(callback) || ts.isFunctionExpression(callback))) {
|
|
97
|
+
callbackParams = callback.parameters;
|
|
98
|
+
callbackBody = callback.body;
|
|
99
|
+
}
|
|
100
|
+
const elementExpr = ts.factory.createElementAccessExpression(baseExpr, iVar);
|
|
101
|
+
const indexExpr = iVar;
|
|
102
|
+
const arrayExpr = baseExpr;
|
|
103
|
+
const resultVar = ts.factory.createUniqueName(`result_${index}`);
|
|
104
|
+
if (reduceMethods.has(methodName)) {
|
|
105
|
+
const isRight = methodName === "reduceRight";
|
|
106
|
+
const tempResult = ts.factory.createUniqueName(`${tempVarPrefix}_${index}`);
|
|
107
|
+
const initialValue = expression.arguments[1] || ts.factory.createIdentifier("undefined");
|
|
108
|
+
// Define tempResult variable
|
|
109
|
+
const tempDecl = ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
110
|
+
ts.factory.createVariableDeclaration(tempResult, undefined, undefined, initialValue),
|
|
111
|
+
], ts.NodeFlags.Let));
|
|
112
|
+
// Handle callback parameters
|
|
113
|
+
if (callbackParams && callbackParams.length >= 1) {
|
|
114
|
+
const accumulatorParam = callbackParams[0].name;
|
|
115
|
+
loopBody.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
116
|
+
ts.factory.createVariableDeclaration(accumulatorParam, undefined, undefined, tempResult // Use tempResult consistently
|
|
117
|
+
),
|
|
118
|
+
], ts.NodeFlags.Const)));
|
|
119
|
+
}
|
|
120
|
+
if (callbackParams && callbackParams.length >= 2) {
|
|
121
|
+
const currentParam = callbackParams[1].name;
|
|
122
|
+
loopBody.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
123
|
+
ts.factory.createVariableDeclaration(currentParam, undefined, undefined, elementExpr),
|
|
124
|
+
], ts.NodeFlags.Const)));
|
|
125
|
+
}
|
|
126
|
+
if (callbackParams && callbackParams.length >= 3) {
|
|
127
|
+
const indexParam = callbackParams[2].name;
|
|
128
|
+
loopBody.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
129
|
+
ts.factory.createVariableDeclaration(indexParam, undefined, undefined, indexExpr),
|
|
130
|
+
], ts.NodeFlags.Const)));
|
|
131
|
+
}
|
|
132
|
+
if (callbackParams && callbackParams.length >= 4) {
|
|
133
|
+
const arrayParam = callbackParams[3].name;
|
|
134
|
+
loopBody.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
135
|
+
ts.factory.createVariableDeclaration(arrayParam, undefined, undefined, arrayExpr),
|
|
136
|
+
], ts.NodeFlags.Const)));
|
|
137
|
+
}
|
|
138
|
+
// Handle callback body
|
|
139
|
+
if (callbackBody) {
|
|
140
|
+
if (ts.isBlock(callbackBody)) {
|
|
141
|
+
const nonReturn = callbackBody.statements.filter((s) => !ts.isReturnStatement(s));
|
|
142
|
+
loopBody.push(...nonReturn);
|
|
143
|
+
const ret = callbackBody.statements.find(ts.isReturnStatement);
|
|
144
|
+
if (ret) {
|
|
145
|
+
loopBody.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
146
|
+
ts.factory.createVariableDeclaration(resultVar, undefined, undefined, ret.expression ?? ts.factory.createIdentifier("undefined")),
|
|
147
|
+
], ts.NodeFlags.Const)));
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
loopBody.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([ts.factory.createVariableDeclaration(resultVar, undefined, undefined, ts.factory.createIdentifier("undefined"))], ts.NodeFlags.Const)));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
loopBody.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([ts.factory.createVariableDeclaration(resultVar, undefined, undefined, callbackBody)], ts.NodeFlags.Const)));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
loopBody.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
159
|
+
ts.factory.createVariableDeclaration(resultVar, undefined, undefined, elementExpr),
|
|
160
|
+
], ts.NodeFlags.Const)));
|
|
161
|
+
}
|
|
162
|
+
// Update tempResult with resultVar
|
|
163
|
+
loopBody.push(ts.factory.createExpressionStatement(ts.factory.createBinaryExpression(tempResult, ts.SyntaxKind.EqualsToken, resultVar)));
|
|
164
|
+
// Create for loop
|
|
165
|
+
const initializer = isRight
|
|
166
|
+
? ts.factory.createBinaryExpression((0, utils_1.markUntouchable)(ts.factory.createPropertyAccessExpression(baseExpr, "length")), ts.SyntaxKind.MinusToken, ts.factory.createNumericLiteral("1"))
|
|
167
|
+
: ts.factory.createNumericLiteral("0");
|
|
168
|
+
const conditionOp = isRight
|
|
169
|
+
? ts.factory.createBinaryExpression(iVar, ts.SyntaxKind.GreaterThanEqualsToken, ts.factory.createNumericLiteral("0"))
|
|
170
|
+
: ts.factory.createBinaryExpression(iVar, ts.SyntaxKind.LessThanToken, (0, utils_1.markUntouchable)(ts.factory.createPropertyAccessExpression(baseExpr, "length")));
|
|
171
|
+
const increment = isRight
|
|
172
|
+
? ts.factory.createPrefixUnaryExpression(ts.SyntaxKind.MinusMinusToken, iVar)
|
|
173
|
+
: ts.factory.createPostfixUnaryExpression(iVar, ts.SyntaxKind.PlusPlusToken);
|
|
174
|
+
const forLoop = ts.factory.createForStatement(ts.factory.createVariableDeclarationList([ts.factory.createVariableDeclaration(iVar, undefined, undefined, initializer)], ts.NodeFlags.Let), conditionOp, increment, ts.factory.createBlock(loopBody, true));
|
|
175
|
+
return {
|
|
176
|
+
expr: tempResult,
|
|
177
|
+
statements: [...accStatements, tempDecl, forLoop],
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
// Handle non-reduce methods (map, filter, flatMap, forEach, etc.)
|
|
182
|
+
if (callbackParams && callbackParams.length >= 1) {
|
|
183
|
+
const elementParam = callbackParams[0].name;
|
|
184
|
+
loopBody.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
185
|
+
ts.factory.createVariableDeclaration(elementParam, undefined, undefined, elementExpr),
|
|
186
|
+
], ts.NodeFlags.Const)));
|
|
187
|
+
}
|
|
188
|
+
if (callbackParams && callbackParams.length >= 2) {
|
|
189
|
+
const indexParam = callbackParams[1].name;
|
|
190
|
+
loopBody.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
191
|
+
ts.factory.createVariableDeclaration(indexParam, undefined, undefined, indexExpr),
|
|
192
|
+
], ts.NodeFlags.Const)));
|
|
193
|
+
}
|
|
194
|
+
if (callbackParams && callbackParams.length >= 3) {
|
|
195
|
+
const arrayParam = callbackParams[2].name;
|
|
196
|
+
loopBody.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
197
|
+
ts.factory.createVariableDeclaration(arrayParam, undefined, undefined, arrayExpr),
|
|
198
|
+
], ts.NodeFlags.Const)));
|
|
199
|
+
}
|
|
200
|
+
// Handle callback body
|
|
201
|
+
if (callbackBody) {
|
|
202
|
+
if (ts.isBlock(callbackBody)) {
|
|
203
|
+
const nonReturn = callbackBody.statements.filter((s) => !ts.isReturnStatement(s));
|
|
204
|
+
loopBody.push(...nonReturn);
|
|
205
|
+
const ret = callbackBody.statements.find(ts.isReturnStatement);
|
|
206
|
+
if (ret) {
|
|
207
|
+
loopBody.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
208
|
+
ts.factory.createVariableDeclaration(resultVar, undefined, undefined, ret.expression ?? ts.factory.createIdentifier("undefined")),
|
|
209
|
+
], ts.NodeFlags.Const)));
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
loopBody.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([ts.factory.createVariableDeclaration(resultVar, undefined, undefined, ts.factory.createIdentifier("undefined"))], ts.NodeFlags.Const)));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
loopBody.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([ts.factory.createVariableDeclaration(resultVar, undefined, undefined, callbackBody)], ts.NodeFlags.Const)));
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
loopBody.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
221
|
+
ts.factory.createVariableDeclaration(resultVar, undefined, undefined, elementExpr),
|
|
222
|
+
], ts.NodeFlags.Const)));
|
|
223
|
+
}
|
|
224
|
+
let tempDecl;
|
|
225
|
+
let forLoop;
|
|
226
|
+
let expr;
|
|
227
|
+
if (methodName === "forEach") {
|
|
228
|
+
forLoop = ts.factory.createForStatement(ts.factory.createVariableDeclarationList([ts.factory.createVariableDeclaration(iVar, undefined, undefined, ts.factory.createNumericLiteral("0"))], ts.NodeFlags.Let), ts.factory.createBinaryExpression(iVar, ts.SyntaxKind.LessThanToken, (0, utils_1.markUntouchable)(ts.factory.createPropertyAccessExpression(baseExpr, "length"))), ts.factory.createPostfixUnaryExpression(iVar, ts.SyntaxKind.PlusPlusToken), ts.factory.createBlock(loopBody, true));
|
|
229
|
+
expr = ts.factory.createIdentifier("undefined"); // forEach returns undefined
|
|
230
|
+
}
|
|
231
|
+
else if (booleanReturningMethods.has(methodName)) {
|
|
232
|
+
const isEvery = methodName === "every";
|
|
233
|
+
const tempResult = ts.factory.createUniqueName(`${tempVarPrefix}_${index}`);
|
|
234
|
+
tempDecl = ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
235
|
+
ts.factory.createVariableDeclaration(tempResult, undefined, undefined, isEvery ? ts.factory.createTrue() : ts.factory.createFalse()),
|
|
236
|
+
], ts.NodeFlags.Let));
|
|
237
|
+
loopBody.push(ts.factory.createIfStatement(isEvery
|
|
238
|
+
? ts.factory.createPrefixUnaryExpression(ts.SyntaxKind.ExclamationToken, resultVar)
|
|
239
|
+
: resultVar, ts.factory.createBlock([
|
|
240
|
+
ts.factory.createExpressionStatement(ts.factory.createBinaryExpression(tempResult, ts.SyntaxKind.EqualsToken, isEvery ? ts.factory.createFalse() : ts.factory.createTrue())),
|
|
241
|
+
ts.factory.createBreakStatement(),
|
|
242
|
+
], true)));
|
|
243
|
+
forLoop = ts.factory.createForStatement(ts.factory.createVariableDeclarationList([ts.factory.createVariableDeclaration(iVar, undefined, undefined, ts.factory.createNumericLiteral("0"))], ts.NodeFlags.Let), ts.factory.createBinaryExpression(iVar, ts.SyntaxKind.LessThanToken, (0, utils_1.markUntouchable)(ts.factory.createPropertyAccessExpression(baseExpr, "length"))), ts.factory.createPostfixUnaryExpression(iVar, ts.SyntaxKind.PlusPlusToken), ts.factory.createBlock(loopBody, true));
|
|
244
|
+
expr = tempResult;
|
|
245
|
+
}
|
|
246
|
+
else if (findMethods.has(methodName)) {
|
|
247
|
+
const isIndex = methodName.endsWith("Index");
|
|
248
|
+
const isLast = methodName.startsWith("findLast");
|
|
249
|
+
const tempResult = ts.factory.createUniqueName(`${tempVarPrefix}_${index}`);
|
|
250
|
+
tempDecl = ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
251
|
+
ts.factory.createVariableDeclaration(tempResult, undefined, undefined, isIndex ? ts.factory.createPrefixUnaryExpression(ts.SyntaxKind.MinusToken, ts.factory.createNumericLiteral("1")) : ts.factory.createIdentifier("undefined")),
|
|
252
|
+
], ts.NodeFlags.Let));
|
|
253
|
+
loopBody.push(ts.factory.createIfStatement(resultVar, ts.factory.createBlock([
|
|
254
|
+
ts.factory.createExpressionStatement(ts.factory.createBinaryExpression(tempResult, ts.SyntaxKind.EqualsToken, isIndex ? iVar : ts.factory.createElementAccessExpression(baseExpr, iVar))),
|
|
255
|
+
ts.factory.createBreakStatement(),
|
|
256
|
+
], true)));
|
|
257
|
+
const initializer = isLast
|
|
258
|
+
? ts.factory.createBinaryExpression((0, utils_1.markUntouchable)(ts.factory.createPropertyAccessExpression(baseExpr, "length")), ts.SyntaxKind.MinusToken, ts.factory.createNumericLiteral("1"))
|
|
259
|
+
: ts.factory.createNumericLiteral("0");
|
|
260
|
+
const conditionOp = isLast
|
|
261
|
+
? ts.factory.createBinaryExpression(iVar, ts.SyntaxKind.GreaterThanEqualsToken, ts.factory.createNumericLiteral("0"))
|
|
262
|
+
: ts.factory.createBinaryExpression(iVar, ts.SyntaxKind.LessThanToken, (0, utils_1.markUntouchable)(ts.factory.createPropertyAccessExpression(baseExpr, "length")));
|
|
263
|
+
const increment = isLast
|
|
264
|
+
? ts.factory.createPrefixUnaryExpression(ts.SyntaxKind.MinusMinusToken, iVar)
|
|
265
|
+
: ts.factory.createPostfixUnaryExpression(iVar, ts.SyntaxKind.PlusPlusToken);
|
|
266
|
+
forLoop = ts.factory.createForStatement(ts.factory.createVariableDeclarationList([ts.factory.createVariableDeclaration(iVar, undefined, undefined, initializer)], ts.NodeFlags.Let), conditionOp, increment, ts.factory.createBlock(loopBody, true));
|
|
267
|
+
expr = tempResult;
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
const tempArr = ts.factory.createUniqueName(`${tempVarPrefix}_${index}`);
|
|
271
|
+
tempDecl = ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
272
|
+
ts.factory.createVariableDeclaration(tempArr, undefined, undefined, ts.factory.createArrayLiteralExpression([], false)),
|
|
273
|
+
], ts.NodeFlags.Const));
|
|
274
|
+
let pushStatement;
|
|
275
|
+
if (methodName === "map") {
|
|
276
|
+
const newPropertyAccessExpression = (0, utils_1.markUntouchable)(ts.factory.createPropertyAccessExpression(tempArr, "push"));
|
|
277
|
+
const newCallExpression = ts.factory.createCallExpression(newPropertyAccessExpression, undefined, [resultVar]);
|
|
278
|
+
// workaround for a TypeScript behavior where the parent is not set automatically
|
|
279
|
+
newPropertyAccessExpression.parent = newCallExpression;
|
|
280
|
+
pushStatement = ts.factory.createExpressionStatement(newCallExpression);
|
|
281
|
+
}
|
|
282
|
+
else if (methodName === "filter") {
|
|
283
|
+
const newPropertyAccessExpression = (0, utils_1.markUntouchable)(ts.factory.createPropertyAccessExpression(tempArr, "push"));
|
|
284
|
+
const newCallExpression = ts.factory.createCallExpression(newPropertyAccessExpression, undefined, [ts.factory.createElementAccessExpression(baseExpr, iVar)]);
|
|
285
|
+
// workaround for a TypeScript behavior where the parent is not set automatically
|
|
286
|
+
newPropertyAccessExpression.parent = newCallExpression;
|
|
287
|
+
pushStatement = ts.factory.createIfStatement(resultVar, ts.factory.createExpressionStatement(newCallExpression));
|
|
288
|
+
}
|
|
289
|
+
else { // flatMap
|
|
290
|
+
const jVar = ts.factory.createUniqueName(`j_${index}`);
|
|
291
|
+
const newPropertyAccessExpression = (0, utils_1.markUntouchable)(ts.factory.createPropertyAccessExpression(tempArr, "push"));
|
|
292
|
+
const newCallExpression = ts.factory.createCallExpression(newPropertyAccessExpression, undefined, [ts.factory.createElementAccessExpression(resultVar, jVar)]);
|
|
293
|
+
// workaround for a TypeScript behavior where the parent is not set automatically
|
|
294
|
+
newPropertyAccessExpression.parent = newCallExpression;
|
|
295
|
+
pushStatement = ts.factory.createForStatement(ts.factory.createVariableDeclarationList([ts.factory.createVariableDeclaration(jVar, undefined, undefined, ts.factory.createNumericLiteral("0"))], ts.NodeFlags.Let), ts.factory.createBinaryExpression(jVar, ts.SyntaxKind.LessThanToken, (0, utils_1.markUntouchable)(ts.factory.createPropertyAccessExpression(resultVar, "length"))), ts.factory.createPostfixUnaryExpression(jVar, ts.SyntaxKind.PlusPlusToken), ts.factory.createBlock([
|
|
296
|
+
ts.factory.createExpressionStatement(newCallExpression),
|
|
297
|
+
], true));
|
|
298
|
+
}
|
|
299
|
+
loopBody.push(pushStatement);
|
|
300
|
+
forLoop = ts.factory.createForStatement(ts.factory.createVariableDeclarationList([ts.factory.createVariableDeclaration(iVar, undefined, undefined, ts.factory.createNumericLiteral("0"))], ts.NodeFlags.Let), ts.factory.createBinaryExpression(iVar, ts.SyntaxKind.LessThanToken, (0, utils_1.markUntouchable)(ts.factory.createPropertyAccessExpression(baseExpr, "length"))), ts.factory.createPostfixUnaryExpression(iVar, ts.SyntaxKind.PlusPlusToken), ts.factory.createBlock(loopBody, true));
|
|
301
|
+
expr = tempArr;
|
|
302
|
+
}
|
|
303
|
+
return {
|
|
304
|
+
expr: expr,
|
|
305
|
+
statements: [...accStatements, ...(tempDecl ? [tempDecl] : []), ...(forLoop ? [forLoop] : [])],
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
const nextTemp = ts.factory.createUniqueName(`${tempVarPrefix}_${index}_${methodName}`);
|
|
311
|
+
const newPropertyAccessExpression = ts.factory.createPropertyAccessExpression(baseExpr, methodName);
|
|
312
|
+
const newCallExpression = ts.factory.createCallExpression(newPropertyAccessExpression, undefined, expression.arguments);
|
|
313
|
+
// workaround for a TypeScript behavior where the parent is not set automatically
|
|
314
|
+
newPropertyAccessExpression.parent = newCallExpression;
|
|
315
|
+
const methodCallDecl = ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
316
|
+
ts.factory.createVariableDeclaration(nextTemp, undefined, undefined, newCallExpression),
|
|
317
|
+
], ts.NodeFlags.Const));
|
|
318
|
+
return {
|
|
319
|
+
expr: nextTemp,
|
|
320
|
+
statements: [...accStatements, methodCallDecl],
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (ts.isPropertyAccessExpression(expression)) {
|
|
325
|
+
const base = transformChain(expression.expression, statements, tempVarPrefix, index + 1);
|
|
326
|
+
return {
|
|
327
|
+
expr: ts.factory.createPropertyAccessExpression(base.expr, expression.name),
|
|
328
|
+
statements: base.statements,
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
return { expr: expression, statements };
|
|
332
|
+
}
|
|
333
|
+
function transformExpression(node, tempVarPrefix) {
|
|
334
|
+
if ((ts.isCallExpression(node) || ts.isPropertyAccessExpression(node)) && containsSupportedMethod(node)) {
|
|
335
|
+
const root = getRootExpression(node);
|
|
336
|
+
const rootType = typeChecker.getTypeAtLocation(root);
|
|
337
|
+
if (rootType && (typeChecker.isArrayType(rootType) || typeChecker.isTupleType(rootType))) {
|
|
338
|
+
return transformChain(node, [], tempVarPrefix, 1);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
if (ts.isCallExpression(node)) {
|
|
342
|
+
const transformedArgs = [];
|
|
343
|
+
const allStatements = [];
|
|
344
|
+
// Transform the callee expression
|
|
345
|
+
const calleeResult = transformExpression(node.expression, `${tempVarPrefix}_callee`);
|
|
346
|
+
allStatements.push(...calleeResult.statements);
|
|
347
|
+
const transformedCallee = calleeResult.expr;
|
|
348
|
+
// Transform each argument
|
|
349
|
+
node.arguments.forEach((arg, index) => {
|
|
350
|
+
const result = transformExpression(arg, `${tempVarPrefix}_arg${index}`);
|
|
351
|
+
transformedArgs.push(result.expr);
|
|
352
|
+
allStatements.push(...result.statements);
|
|
353
|
+
});
|
|
354
|
+
return {
|
|
355
|
+
expr: ts.factory.createCallExpression(transformedCallee, node.typeArguments, transformedArgs),
|
|
356
|
+
statements: allStatements,
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
360
|
+
const transformedElements = [];
|
|
361
|
+
const allStatements = [];
|
|
362
|
+
node.elements.forEach((element, index) => {
|
|
363
|
+
if (ts.isSpreadElement(element)) {
|
|
364
|
+
const result = transformExpression(element.expression, `${tempVarPrefix}_elem${index}`);
|
|
365
|
+
transformedElements.push(ts.factory.createSpreadElement(result.expr));
|
|
366
|
+
allStatements.push(...result.statements);
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
const result = transformExpression(element, `${tempVarPrefix}_elem${index}`);
|
|
370
|
+
transformedElements.push(result.expr);
|
|
371
|
+
allStatements.push(...result.statements);
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
return {
|
|
375
|
+
expr: ts.factory.createArrayLiteralExpression(transformedElements),
|
|
376
|
+
statements: allStatements,
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
380
|
+
const transformedProperties = [];
|
|
381
|
+
const allStatements = [];
|
|
382
|
+
node.properties.forEach((property, index) => {
|
|
383
|
+
if (ts.isPropertyAssignment(property) && property.initializer) {
|
|
384
|
+
const result = transformExpression(property.initializer, `${tempVarPrefix}_prop${index}`);
|
|
385
|
+
transformedProperties.push(ts.factory.createPropertyAssignment(property.name, result.expr));
|
|
386
|
+
allStatements.push(...result.statements);
|
|
387
|
+
}
|
|
388
|
+
else if (ts.isShorthandPropertyAssignment(property)) {
|
|
389
|
+
const result = transformExpression(ts.factory.createIdentifier(property.name.text), `${tempVarPrefix}_prop${index}`);
|
|
390
|
+
if (ts.isIdentifier(result.expr)) {
|
|
391
|
+
transformedProperties.push(ts.factory.createShorthandPropertyAssignment(result.expr.text, property.objectAssignmentInitializer));
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
transformedProperties.push(ts.factory.createPropertyAssignment(property.name, result.expr));
|
|
395
|
+
}
|
|
396
|
+
allStatements.push(...result.statements);
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
transformedProperties.push(property);
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
return {
|
|
403
|
+
expr: ts.factory.createObjectLiteralExpression(transformedProperties),
|
|
404
|
+
statements: allStatements,
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
return { expr: node, statements: [] };
|
|
408
|
+
}
|
|
409
|
+
function visit(node) {
|
|
410
|
+
if (ts.isVariableStatement(node)) {
|
|
411
|
+
const declList = node.declarationList;
|
|
412
|
+
if (declList.declarations.length === 1) {
|
|
413
|
+
const decl = declList.declarations[0];
|
|
414
|
+
const init = decl.initializer;
|
|
415
|
+
if (init) {
|
|
416
|
+
const { expr: finalExpr, statements } = transformExpression(init, `temp_${decl.name.getText()}`);
|
|
417
|
+
if (statements.length > 0) {
|
|
418
|
+
const assignStatement = ts.factory.createVariableStatement(node.modifiers, ts.factory.createVariableDeclarationList([
|
|
419
|
+
ts.factory.createVariableDeclaration(decl.name, decl.exclamationToken, decl.type, finalExpr),
|
|
420
|
+
], declList.flags));
|
|
421
|
+
return [...statements, assignStatement];
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (ts.isExpressionStatement(node) && ts.isBinaryExpression(node.expression) &&
|
|
427
|
+
node.expression.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
|
|
428
|
+
const left = node.expression.left;
|
|
429
|
+
const right = node.expression.right;
|
|
430
|
+
// ошибка
|
|
431
|
+
const { expr: finalExpr, statements } = transformExpression(right, `temp_${ts.isIdentifier(left) ? left.text : "temp"}`);
|
|
432
|
+
if (statements.length > 0) {
|
|
433
|
+
const assignStatement = ts.factory.createExpressionStatement(ts.factory.createBinaryExpression(left, ts.SyntaxKind.EqualsToken, finalExpr));
|
|
434
|
+
return [...statements, assignStatement];
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
if (ts.isExpressionStatement(node) &&
|
|
438
|
+
(ts.isCallExpression(node.expression) ||
|
|
439
|
+
ts.isPropertyAccessExpression(node.expression) ||
|
|
440
|
+
ts.isArrayLiteralExpression(node.expression) ||
|
|
441
|
+
ts.isObjectLiteralExpression(node.expression))) {
|
|
442
|
+
const { expr: finalExpr, statements } = transformExpression(node.expression, "temp_standalone");
|
|
443
|
+
if (statements.length > 0 && !ts.isCallExpression(finalExpr)) {
|
|
444
|
+
return statements;
|
|
445
|
+
}
|
|
446
|
+
if (statements.length > 0) {
|
|
447
|
+
return [...statements, ts.factory.createExpressionStatement(finalExpr)];
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
if (ts.isExpressionStatement(node) && ts.isCallExpression(node.expression)) {
|
|
451
|
+
const callExpr = node.expression;
|
|
452
|
+
const { expr: transformedCall, statements } = transformExpression(callExpr, "temp_call");
|
|
453
|
+
if (statements.length > 0) {
|
|
454
|
+
return [...statements, ts.factory.createExpressionStatement(transformedCall)];
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
if (ts.isReturnStatement(node) && node.expression) {
|
|
458
|
+
const { expr: finalExpr, statements } = transformExpression(node.expression, "temp_return");
|
|
459
|
+
if (statements.length > 0) {
|
|
460
|
+
return [...statements, ts.factory.createReturnStatement(finalExpr)];
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return ts.visitEachChild(node, visit, context);
|
|
464
|
+
}
|
|
465
|
+
return ts.visitNode(file, visit);
|
|
466
|
+
};
|
|
467
|
+
}
|