@casual-simulation/aux-runtime 3.2.17 → 3.2.18-alpha.8349491134
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 +9 -9
- package/runtime/AuxCompiler.d.ts +11 -0
- package/runtime/AuxCompiler.js +72 -23
- package/runtime/AuxCompiler.js.map +1 -1
- package/runtime/AuxGlobalContext.js.map +1 -1
- package/runtime/AuxLibrary.d.ts +6 -5
- package/runtime/AuxLibrary.js +8 -7
- package/runtime/AuxLibrary.js.map +1 -1
- package/runtime/AuxPartitionRealtimeEditModeProvider.js.map +1 -1
- package/runtime/AuxRuntime.d.ts +27 -1
- package/runtime/AuxRuntime.js +387 -14
- package/runtime/AuxRuntime.js.map +1 -1
- package/runtime/CasualOSError.js.map +1 -1
- package/runtime/CompiledBot.d.ts +11 -2
- package/runtime/CompiledBot.js +6 -1
- package/runtime/CompiledBot.js.map +1 -1
- package/runtime/PerformanceNowPolyfill.js.map +1 -1
- package/runtime/RuntimeBot.js.map +1 -1
- package/runtime/RuntimeStateVersion.js.map +1 -1
- package/runtime/Transpiler.d.ts +33 -0
- package/runtime/Transpiler.js +463 -44
- package/runtime/Transpiler.js.map +1 -1
- package/runtime/TranspilerUtils.js.map +1 -1
- package/runtime/Utils.d.ts +5 -0
- package/runtime/Utils.js +13 -0
- package/runtime/Utils.js.map +1 -1
- package/runtime/test/TestScriptBotFactory.js.map +1 -1
package/runtime/Transpiler.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import * as Acorn from 'acorn';
|
|
2
|
-
import AcornJSX from 'acorn-jsx';
|
|
3
2
|
import { generate, GENERATOR } from 'astring';
|
|
4
3
|
import LRUCache from 'lru-cache';
|
|
5
|
-
import { traverse } from 'estraverse';
|
|
4
|
+
import { traverse, VisitorKeys } from 'estraverse';
|
|
6
5
|
import { createAbsolutePositionFromRelativePosition, createRelativePositionFromTypeIndex, getItem, Doc, } from 'yjs';
|
|
7
6
|
import { createAbsolutePositionFromStateVector, createRelativePositionFromStateVector, getClock, } from '@casual-simulation/aux-common/yjs/YjsHelpers';
|
|
8
7
|
import { calculateIndexFromLocation, calculateLocationFromIndex, } from './TranspilerUtils';
|
|
8
|
+
import { tsPlugin } from 'acorn-typescript';
|
|
9
9
|
/**
|
|
10
10
|
* The symbol that is used in script dependencies to represent any argument.
|
|
11
11
|
*/
|
|
@@ -17,6 +17,87 @@ const exJsGenerator = Object.assign({}, GENERATOR, {
|
|
|
17
17
|
state.write(')');
|
|
18
18
|
},
|
|
19
19
|
});
|
|
20
|
+
const TypeScriptVisistorKeys = {
|
|
21
|
+
TSTypeParameterDeclaration: [],
|
|
22
|
+
TSCallSignatureDeclaration: [],
|
|
23
|
+
TSConstructSignatureDeclaration: [],
|
|
24
|
+
TSInterfaceDeclaration: [],
|
|
25
|
+
TSModuleDeclaration: [],
|
|
26
|
+
TSEnumDeclaration: [],
|
|
27
|
+
TSTypeAliasDeclaration: [],
|
|
28
|
+
TSDeclareFunction: [],
|
|
29
|
+
TSExternalModuleReference: [],
|
|
30
|
+
TSQualifiedName: [],
|
|
31
|
+
TSEnumMember: [],
|
|
32
|
+
TSModuleBlock: [],
|
|
33
|
+
TSTypePredicate: [],
|
|
34
|
+
TSThisType: [],
|
|
35
|
+
TSTypeAnnotation: [],
|
|
36
|
+
TSLiteralType: [],
|
|
37
|
+
TSVoidKeyword: [],
|
|
38
|
+
TSNeverKeyword: [],
|
|
39
|
+
TSNumberKeyword: [],
|
|
40
|
+
TSAnyKeyword: [],
|
|
41
|
+
TSBooleanKeyword: [],
|
|
42
|
+
TSBigIntKeyword: [],
|
|
43
|
+
TSObjectKeyword: [],
|
|
44
|
+
TSStringKeyword: [],
|
|
45
|
+
TSSymbolKeyword: [],
|
|
46
|
+
TSUndefinedKeyword: [],
|
|
47
|
+
TSUnknownKeyword: [],
|
|
48
|
+
TSFunctionType: [],
|
|
49
|
+
TSConstructorType: [],
|
|
50
|
+
TSUnionType: [],
|
|
51
|
+
TSIntersectionType: [],
|
|
52
|
+
TSInferType: [],
|
|
53
|
+
TSImportType: [],
|
|
54
|
+
TSTypeQuery: [],
|
|
55
|
+
TSTypeParameter: [],
|
|
56
|
+
TSMappedType: [],
|
|
57
|
+
TSTypeLiteral: [],
|
|
58
|
+
TSTupleType: [],
|
|
59
|
+
TSNamedTupleMember: [],
|
|
60
|
+
TSRestType: [],
|
|
61
|
+
TSOptionalType: [],
|
|
62
|
+
TSTypeReference: [],
|
|
63
|
+
TSParenthesizedType: [],
|
|
64
|
+
TSArrayType: [],
|
|
65
|
+
TSIndexedAccessType: [],
|
|
66
|
+
TSConditionalType: [],
|
|
67
|
+
TSIndexSignature: [],
|
|
68
|
+
TSTypeParameterInstantiation: [],
|
|
69
|
+
TSExpressionWithTypeArguments: [],
|
|
70
|
+
TSInterfaceBody: [],
|
|
71
|
+
TSIntrinsicKeyword: [],
|
|
72
|
+
TSImportEqualsDeclaration: [],
|
|
73
|
+
TSNonNullExpression: [],
|
|
74
|
+
TSTypeOperator: [],
|
|
75
|
+
TSMethodSignature: [],
|
|
76
|
+
TSPropertySignature: [],
|
|
77
|
+
TSAsExpression: [],
|
|
78
|
+
ClassDeclaration: [
|
|
79
|
+
...VisitorKeys.ClassDeclaration,
|
|
80
|
+
'implements',
|
|
81
|
+
'typeParameters',
|
|
82
|
+
],
|
|
83
|
+
ClassExpression: [
|
|
84
|
+
...VisitorKeys.ClassExpression,
|
|
85
|
+
'implements',
|
|
86
|
+
'typeParameters',
|
|
87
|
+
],
|
|
88
|
+
VariableDeclarator: [...VisitorKeys.VariableDeclarator, 'typeAnnotation'],
|
|
89
|
+
FunctionDeclaration: [
|
|
90
|
+
...VisitorKeys.FunctionDeclaration,
|
|
91
|
+
'returnType',
|
|
92
|
+
'typeParameters',
|
|
93
|
+
],
|
|
94
|
+
FunctionExpression: [
|
|
95
|
+
...VisitorKeys.FunctionExpression,
|
|
96
|
+
'returnType',
|
|
97
|
+
'typeParameters',
|
|
98
|
+
],
|
|
99
|
+
Identifier: [...VisitorKeys.Identifier, 'typeAnnotation'],
|
|
100
|
+
};
|
|
20
101
|
/**
|
|
21
102
|
* The list of macros that the sandbox uses on the input code before transpiling it.
|
|
22
103
|
*/
|
|
@@ -25,14 +106,6 @@ const MACROS = [
|
|
|
25
106
|
test: /^(?:\🧬)/g,
|
|
26
107
|
replacement: (val) => '',
|
|
27
108
|
},
|
|
28
|
-
{
|
|
29
|
-
test: /(?:[“”])/g,
|
|
30
|
-
replacement: (val) => '"',
|
|
31
|
-
},
|
|
32
|
-
{
|
|
33
|
-
test: /(?:[‘’])/g,
|
|
34
|
-
replacement: (val) => "'",
|
|
35
|
-
},
|
|
36
109
|
];
|
|
37
110
|
/**
|
|
38
111
|
* Replaces macros in the given text.
|
|
@@ -61,14 +134,31 @@ export class Transpiler {
|
|
|
61
134
|
this._forceSync = value;
|
|
62
135
|
}
|
|
63
136
|
constructor(options) {
|
|
64
|
-
var _a, _b, _c;
|
|
137
|
+
var _a, _b, _c, _d, _e, _f;
|
|
65
138
|
this._cache = new LRUCache({
|
|
66
139
|
max: 1000,
|
|
67
140
|
});
|
|
68
|
-
this._parser = Acorn.Parser.extend(
|
|
141
|
+
this._parser = Acorn.Parser.extend(tsPlugin());
|
|
142
|
+
this._parser.prototype.parseReturnStatement = function (node) {
|
|
143
|
+
this.next();
|
|
144
|
+
// In `return` (and `break`/`continue`), the keywords with
|
|
145
|
+
// optional arguments, we eagerly look for a semicolon or the
|
|
146
|
+
// possibility to insert one.
|
|
147
|
+
if (this.eat(Acorn.tokTypes.semi) || this.insertSemicolon()) {
|
|
148
|
+
node.argument = null;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
node.argument = this.parseExpression();
|
|
152
|
+
this.semicolon();
|
|
153
|
+
}
|
|
154
|
+
return this.finishNode(node, 'ReturnStatement');
|
|
155
|
+
};
|
|
69
156
|
this._jsxFactory = (_a = options === null || options === void 0 ? void 0 : options.jsxFactory) !== null && _a !== void 0 ? _a : 'h';
|
|
70
157
|
this._jsxFragment = (_b = options === null || options === void 0 ? void 0 : options.jsxFragment) !== null && _b !== void 0 ? _b : 'Fragment';
|
|
71
|
-
this.
|
|
158
|
+
this._importFactory = (_c = options === null || options === void 0 ? void 0 : options.importFactory) !== null && _c !== void 0 ? _c : 'importModule';
|
|
159
|
+
this._importMetaFactory = (_d = options === null || options === void 0 ? void 0 : options.importMetaFactory) !== null && _d !== void 0 ? _d : 'importMeta';
|
|
160
|
+
this._exportFactory = (_e = options === null || options === void 0 ? void 0 : options.exportFactory) !== null && _e !== void 0 ? _e : 'exports';
|
|
161
|
+
this._forceSync = (_f = options === null || options === void 0 ? void 0 : options.forceSync) !== null && _f !== void 0 ? _f : false;
|
|
72
162
|
}
|
|
73
163
|
parse(code) {
|
|
74
164
|
const macroed = replaceMacros(code);
|
|
@@ -108,15 +198,17 @@ export class Transpiler {
|
|
|
108
198
|
doc.clientID = 0;
|
|
109
199
|
const text = doc.getText();
|
|
110
200
|
text.insert(0, code);
|
|
111
|
-
|
|
201
|
+
let metadata = {
|
|
202
|
+
doc,
|
|
203
|
+
text,
|
|
204
|
+
isModule: false,
|
|
205
|
+
};
|
|
206
|
+
this._replace(node, doc, text, metadata);
|
|
112
207
|
const finalCode = text.toString();
|
|
113
208
|
const result = {
|
|
114
209
|
code: finalCode,
|
|
115
210
|
original: macroed,
|
|
116
|
-
metadata
|
|
117
|
-
doc,
|
|
118
|
-
text,
|
|
119
|
-
},
|
|
211
|
+
metadata,
|
|
120
212
|
};
|
|
121
213
|
this._cache.set(code, result);
|
|
122
214
|
return result;
|
|
@@ -127,8 +219,9 @@ export class Transpiler {
|
|
|
127
219
|
*/
|
|
128
220
|
_parse(code) {
|
|
129
221
|
const node = this._parser.parse(code, {
|
|
130
|
-
ecmaVersion:
|
|
222
|
+
ecmaVersion: 14,
|
|
131
223
|
locations: true,
|
|
224
|
+
sourceType: 'module',
|
|
132
225
|
});
|
|
133
226
|
return node;
|
|
134
227
|
}
|
|
@@ -166,13 +259,34 @@ export class Transpiler {
|
|
|
166
259
|
generator: exJsGenerator,
|
|
167
260
|
});
|
|
168
261
|
}
|
|
169
|
-
_replace(node, doc, text) {
|
|
262
|
+
_replace(node, doc, text, metadata) {
|
|
170
263
|
if (!node) {
|
|
171
264
|
return;
|
|
172
265
|
}
|
|
173
266
|
traverse(node, {
|
|
174
267
|
enter: ((n, parent) => {
|
|
175
|
-
|
|
268
|
+
var _a;
|
|
269
|
+
if (n.type === 'ImportDeclaration') {
|
|
270
|
+
this._replaceImportDeclaration(n, parent, doc, text, metadata);
|
|
271
|
+
}
|
|
272
|
+
else if (n.type === 'ImportExpression') {
|
|
273
|
+
this._replaceImportExpression(n, parent, doc, text, metadata);
|
|
274
|
+
}
|
|
275
|
+
else if (n.type === 'ExportNamedDeclaration') {
|
|
276
|
+
this._replaceExportNamedDeclaration(n, parent, doc, text, metadata);
|
|
277
|
+
}
|
|
278
|
+
else if (n.type === 'ExportDefaultDeclaration') {
|
|
279
|
+
this._replaceExportDefaultDeclaration(n, parent, doc, text, metadata);
|
|
280
|
+
}
|
|
281
|
+
else if (n.type === 'ExportAllDeclaration') {
|
|
282
|
+
this._replaceExportAllDeclaration(n, parent, doc, text, metadata);
|
|
283
|
+
}
|
|
284
|
+
else if (n.type === 'MetaProperty' &&
|
|
285
|
+
n.meta.name === 'import' &&
|
|
286
|
+
n.property.name === 'meta') {
|
|
287
|
+
this._replaceImportMeta(n, parent, doc, text, metadata);
|
|
288
|
+
}
|
|
289
|
+
else if (n.type === 'WhileStatement') {
|
|
176
290
|
this._replaceWhileStatement(n, doc, text);
|
|
177
291
|
}
|
|
178
292
|
else if (n.type === 'DoWhileStatement') {
|
|
@@ -188,7 +302,7 @@ export class Transpiler {
|
|
|
188
302
|
this._replaceForOfStatement(n, doc, text);
|
|
189
303
|
}
|
|
190
304
|
else if (n.type === 'JSXElement') {
|
|
191
|
-
this._replaceJSXElement(n, doc, text);
|
|
305
|
+
this._replaceJSXElement(n, doc, text, metadata);
|
|
192
306
|
}
|
|
193
307
|
else if (n.type === 'JSXText') {
|
|
194
308
|
this._replaceJSXText(n, doc, text);
|
|
@@ -197,7 +311,7 @@ export class Transpiler {
|
|
|
197
311
|
this._replaceJSXExpressionContainer(n, doc, text);
|
|
198
312
|
}
|
|
199
313
|
else if (n.type === 'JSXFragment') {
|
|
200
|
-
this._replaceJSXFragment(n, doc, text);
|
|
314
|
+
this._replaceJSXFragment(n, doc, text, metadata);
|
|
201
315
|
}
|
|
202
316
|
else if (n.type === 'JSXEmptyExpression') {
|
|
203
317
|
this._replaceJSXEmptyExpression(n, doc, text);
|
|
@@ -225,18 +339,264 @@ export class Transpiler {
|
|
|
225
339
|
else if (this._forceSync && n.type === 'AwaitExpression') {
|
|
226
340
|
this._replaceAwaitExpression(n, doc, text);
|
|
227
341
|
}
|
|
342
|
+
else if (n.type === 'ClassDeclaration' ||
|
|
343
|
+
n.type === 'ClassExpression') {
|
|
344
|
+
if (((_a = n.implements) === null || _a === void 0 ? void 0 : _a.length) > 0) {
|
|
345
|
+
this._removeClassImplements(n, doc, text);
|
|
346
|
+
}
|
|
347
|
+
if (n.abstract === true) {
|
|
348
|
+
this._removeClassAbstract(n, doc, text);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
else if (n.type === 'TSAsExpression') {
|
|
352
|
+
this._removeAsExpression(n, doc, text);
|
|
353
|
+
}
|
|
354
|
+
else if (n.type === 'Identifier' && n.optional === true) {
|
|
355
|
+
this._removeOptionalFromIdentifier(n, doc, text);
|
|
356
|
+
}
|
|
357
|
+
else if (n.type.startsWith('TS')) {
|
|
358
|
+
this._removeNodeOrReplaceWithUndefined(n, doc, text);
|
|
359
|
+
}
|
|
228
360
|
}),
|
|
229
|
-
keys: {
|
|
230
|
-
JSXElement: [],
|
|
231
|
-
JSXFragment: [],
|
|
232
|
-
JSXOpeningElement: [],
|
|
233
|
-
JSXClosingElement: [],
|
|
234
|
-
JSXText: [],
|
|
235
|
-
JSXExpressionContainer: ['expression'],
|
|
236
|
-
JSXEmptyExpression: [],
|
|
237
|
-
},
|
|
361
|
+
keys: Object.assign({ JSXElement: [], JSXFragment: [], JSXOpeningElement: [], JSXClosingElement: [], JSXText: [], JSXExpressionContainer: ['expression'], JSXEmptyExpression: [] }, TypeScriptVisistorKeys),
|
|
238
362
|
});
|
|
239
363
|
}
|
|
364
|
+
_replaceImportDeclaration(node, parent, doc, text, metadata) {
|
|
365
|
+
metadata.isModule = true;
|
|
366
|
+
doc.clientID += 1;
|
|
367
|
+
const version = { '0': getClock(doc, 0) };
|
|
368
|
+
const absoluteStart = createAbsolutePositionFromStateVector(doc, text, version, node.start, undefined, true);
|
|
369
|
+
const statementEnd = createRelativePositionFromStateVector(text, version, node.source.end, 1, true);
|
|
370
|
+
const sourceStart = createRelativePositionFromStateVector(text, version, node.source.start, -1, true);
|
|
371
|
+
const sourceEnd = createRelativePositionFromStateVector(text, version, node.source.end, 1, true);
|
|
372
|
+
let currentIndex = absoluteStart.index;
|
|
373
|
+
let importCall = node.specifiers.length > 0 ? `const ` : '';
|
|
374
|
+
const namespaceImport = node.specifiers.find((s) => s.type === 'ImportNamespaceSpecifier');
|
|
375
|
+
const defaultImport = node.specifiers.find((s) => s.type === 'ImportDefaultSpecifier');
|
|
376
|
+
if (namespaceImport) {
|
|
377
|
+
importCall += `${namespaceImport.local.name} = `;
|
|
378
|
+
}
|
|
379
|
+
else {
|
|
380
|
+
let addedBraces = false;
|
|
381
|
+
for (let specifier of node.specifiers) {
|
|
382
|
+
if (specifier.type === 'ImportSpecifier') {
|
|
383
|
+
if (!addedBraces) {
|
|
384
|
+
addedBraces = true;
|
|
385
|
+
importCall += `{ `;
|
|
386
|
+
}
|
|
387
|
+
if (specifier.local === specifier.imported) {
|
|
388
|
+
importCall += `${specifier.local.name}, `;
|
|
389
|
+
}
|
|
390
|
+
else {
|
|
391
|
+
importCall += `${specifier.imported.name}: ${specifier.local.name}, `;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
else if (specifier.type === 'ImportDefaultSpecifier') {
|
|
395
|
+
if (!addedBraces) {
|
|
396
|
+
addedBraces = true;
|
|
397
|
+
importCall += `{ `;
|
|
398
|
+
}
|
|
399
|
+
importCall += `default: ${specifier.local.name}, `;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
if (addedBraces) {
|
|
403
|
+
importCall += `}`;
|
|
404
|
+
}
|
|
405
|
+
if (node.specifiers.length > 0) {
|
|
406
|
+
importCall += ` = `;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
if (node.importKind === 'type') {
|
|
410
|
+
importCall += `{}`;
|
|
411
|
+
}
|
|
412
|
+
else {
|
|
413
|
+
importCall += `await ${this._importFactory}(`;
|
|
414
|
+
}
|
|
415
|
+
text.insert(currentIndex, importCall);
|
|
416
|
+
currentIndex += importCall.length;
|
|
417
|
+
if (node.importKind !== 'type') {
|
|
418
|
+
const absoluteSourceEnd = createAbsolutePositionFromRelativePosition(sourceEnd, doc);
|
|
419
|
+
text.insert(absoluteSourceEnd.index, `, ${this._importMetaFactory})`);
|
|
420
|
+
if (namespaceImport && defaultImport) {
|
|
421
|
+
const absoluteEnd = createAbsolutePositionFromRelativePosition(statementEnd, doc);
|
|
422
|
+
let defaultImportSource = `\nconst { default: ${defaultImport.local.name} } = ${namespaceImport.local.name};`;
|
|
423
|
+
text.insert(absoluteEnd.index + 1, defaultImportSource);
|
|
424
|
+
}
|
|
425
|
+
const absoluteSourceStart = createAbsolutePositionFromRelativePosition(sourceStart, doc);
|
|
426
|
+
text.delete(currentIndex, absoluteSourceStart.index - currentIndex);
|
|
427
|
+
}
|
|
428
|
+
else {
|
|
429
|
+
const absoluteSourceEnd = createAbsolutePositionFromRelativePosition(sourceEnd, doc);
|
|
430
|
+
text.delete(currentIndex, absoluteSourceEnd.index - currentIndex);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
_replaceImportExpression(node, parent, doc, text, metadata) {
|
|
434
|
+
metadata.isModule = true;
|
|
435
|
+
doc.clientID += 1;
|
|
436
|
+
const version = { '0': getClock(doc, 0) };
|
|
437
|
+
const absoluteStart = createAbsolutePositionFromStateVector(doc, text, version, node.start, undefined, true);
|
|
438
|
+
const sourceStart = createRelativePositionFromStateVector(text, version, node.source.start, -1, true);
|
|
439
|
+
const sourceEnd = createRelativePositionFromStateVector(text, version, node.source.end, 1, true);
|
|
440
|
+
let currentIndex = absoluteStart.index;
|
|
441
|
+
let importCall = `${this._importFactory}(`;
|
|
442
|
+
text.insert(currentIndex, importCall);
|
|
443
|
+
currentIndex += importCall.length;
|
|
444
|
+
const absoluteSourceEnd = createAbsolutePositionFromRelativePosition(sourceEnd, doc);
|
|
445
|
+
text.insert(absoluteSourceEnd.index, `, ${this._importMetaFactory}`);
|
|
446
|
+
const absoluteSourceStart = createAbsolutePositionFromRelativePosition(sourceStart, doc);
|
|
447
|
+
text.delete(currentIndex, absoluteSourceStart.index - currentIndex);
|
|
448
|
+
}
|
|
449
|
+
_replaceImportMeta(node, parent, doc, text, metadata) {
|
|
450
|
+
metadata.isModule = true;
|
|
451
|
+
doc.clientID += 1;
|
|
452
|
+
const version = { '0': getClock(doc, 0) };
|
|
453
|
+
const absoluteStart = createAbsolutePositionFromStateVector(doc, text, version, node.start, undefined, true);
|
|
454
|
+
const absoluteEnd = createAbsolutePositionFromStateVector(doc, text, version, node.end, 1, true);
|
|
455
|
+
text.insert(absoluteEnd.index, `${this._importMetaFactory}`);
|
|
456
|
+
text.delete(absoluteStart.index, absoluteEnd.index - absoluteStart.index);
|
|
457
|
+
}
|
|
458
|
+
_replaceExportNamedDeclaration(node, parent, doc, text, metadata) {
|
|
459
|
+
if (node.declaration) {
|
|
460
|
+
this._replaceExportVariableDeclaration(node, parent, doc, text, metadata);
|
|
461
|
+
}
|
|
462
|
+
else if (node.source) {
|
|
463
|
+
this._replaceExportFromSourceDeclaration(node, parent, doc, text, metadata);
|
|
464
|
+
}
|
|
465
|
+
else {
|
|
466
|
+
this._replaceExportSpecifiersDeclaration(node, parent, doc, text, metadata);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
_replaceExportVariableDeclaration(node, parent, doc, text, metadata) {
|
|
470
|
+
metadata.isModule = true;
|
|
471
|
+
doc.clientID += 1;
|
|
472
|
+
const version = { '0': getClock(doc, 0) };
|
|
473
|
+
const absoluteStart = createAbsolutePositionFromStateVector(doc, text, version, node.start, undefined, true);
|
|
474
|
+
const declarationStart = createAbsolutePositionFromStateVector(doc, text, version, node.declaration.start, undefined, true);
|
|
475
|
+
const declarationEnd = createRelativePositionFromStateVector(text, version, node.declaration.end, -1, true);
|
|
476
|
+
text.delete(absoluteStart.index, declarationStart.index - absoluteStart.index);
|
|
477
|
+
const absoluteDeclarationEnd = createAbsolutePositionFromRelativePosition(declarationEnd, doc);
|
|
478
|
+
let exportCall = `\nawait ${this._exportFactory}({ `;
|
|
479
|
+
if (node.declaration.type === 'VariableDeclaration') {
|
|
480
|
+
for (let declaration of node.declaration.declarations) {
|
|
481
|
+
exportCall += `${declaration.id.name}, `;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
else {
|
|
485
|
+
exportCall += `${node.declaration.id.name}, `;
|
|
486
|
+
}
|
|
487
|
+
exportCall += `});`;
|
|
488
|
+
text.insert(absoluteDeclarationEnd.index + 1, exportCall);
|
|
489
|
+
}
|
|
490
|
+
_replaceExportSpecifiersDeclaration(node, parent, doc, text, metadata) {
|
|
491
|
+
metadata.isModule = true;
|
|
492
|
+
doc.clientID += 1;
|
|
493
|
+
const version = { '0': getClock(doc, 0) };
|
|
494
|
+
const absoluteStart = createAbsolutePositionFromStateVector(doc, text, version, node.start, undefined, true);
|
|
495
|
+
const relativeEnd = createRelativePositionFromStateVector(text, version, node.end, -1, true);
|
|
496
|
+
if (node.specifiers.length > 0) {
|
|
497
|
+
let exportCall = `await ${this._exportFactory}({ `;
|
|
498
|
+
for (let specifier of node.specifiers) {
|
|
499
|
+
if (specifier.local === specifier.exported) {
|
|
500
|
+
exportCall += `${specifier.local.name}, `;
|
|
501
|
+
}
|
|
502
|
+
else {
|
|
503
|
+
exportCall += `${specifier.exported.name}: ${specifier.local.name}, `;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
exportCall += `});`;
|
|
507
|
+
let currentIndex = absoluteStart.index;
|
|
508
|
+
text.insert(currentIndex, exportCall);
|
|
509
|
+
currentIndex += exportCall.length;
|
|
510
|
+
const absoluteEnd = createAbsolutePositionFromRelativePosition(relativeEnd, doc);
|
|
511
|
+
text.delete(currentIndex, absoluteEnd.index);
|
|
512
|
+
}
|
|
513
|
+
else {
|
|
514
|
+
let exportCall = `await ${this._exportFactory}({});`;
|
|
515
|
+
let currentIndex = absoluteStart.index;
|
|
516
|
+
text.insert(currentIndex, exportCall);
|
|
517
|
+
currentIndex += exportCall.length;
|
|
518
|
+
const absoluteEnd = createAbsolutePositionFromRelativePosition(relativeEnd, doc);
|
|
519
|
+
text.delete(currentIndex, absoluteEnd.index);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
_replaceExportFromSourceDeclaration(node, parent, doc, text, metadata) {
|
|
523
|
+
metadata.isModule = true;
|
|
524
|
+
doc.clientID += 1;
|
|
525
|
+
const version = { '0': getClock(doc, 0) };
|
|
526
|
+
const absoluteStart = createAbsolutePositionFromStateVector(doc, text, version, node.start, undefined, true);
|
|
527
|
+
const relativeSourceStart = createRelativePositionFromStateVector(text, version, node.source.start, -1, true);
|
|
528
|
+
const relativeSourceEnd = createRelativePositionFromStateVector(text, version, node.source.end, -1, true);
|
|
529
|
+
const relativeEnd = createRelativePositionFromStateVector(text, version, node.end, -1, true);
|
|
530
|
+
if (node.specifiers.length > 0) {
|
|
531
|
+
let exportCall = `await ${this._exportFactory}(`;
|
|
532
|
+
let specifiers = ', [';
|
|
533
|
+
for (let specifier of node.specifiers) {
|
|
534
|
+
if (specifier.local === specifier.exported) {
|
|
535
|
+
specifiers += `'${specifier.local.name}', `;
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
538
|
+
specifiers += `['${specifier.local.name}', '${specifier.exported.name}'], `;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
specifiers += ']);';
|
|
542
|
+
let currentIndex = absoluteStart.index;
|
|
543
|
+
text.insert(currentIndex, exportCall);
|
|
544
|
+
currentIndex += exportCall.length;
|
|
545
|
+
const sourceEnd = createAbsolutePositionFromRelativePosition(relativeSourceEnd, doc);
|
|
546
|
+
text.insert(sourceEnd.index, specifiers);
|
|
547
|
+
const sourceStart = createAbsolutePositionFromRelativePosition(relativeSourceStart, doc);
|
|
548
|
+
text.delete(currentIndex, sourceStart.index - currentIndex);
|
|
549
|
+
const absoluteEnd = createAbsolutePositionFromRelativePosition(relativeEnd, doc);
|
|
550
|
+
const finalSourceEnd = createAbsolutePositionFromRelativePosition(relativeSourceEnd, doc);
|
|
551
|
+
text.delete(absoluteEnd.index - 1, absoluteEnd.index - finalSourceEnd.index);
|
|
552
|
+
}
|
|
553
|
+
else {
|
|
554
|
+
let exportCall = `await ${this._exportFactory}({});`;
|
|
555
|
+
let currentIndex = absoluteStart.index;
|
|
556
|
+
text.insert(currentIndex, exportCall);
|
|
557
|
+
currentIndex += exportCall.length;
|
|
558
|
+
const absoluteEnd = createAbsolutePositionFromRelativePosition(relativeEnd, doc);
|
|
559
|
+
text.delete(currentIndex, absoluteEnd.index);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
_replaceExportDefaultDeclaration(node, parent, doc, text, metadata) {
|
|
563
|
+
metadata.isModule = true;
|
|
564
|
+
doc.clientID += 1;
|
|
565
|
+
const version = { '0': getClock(doc, 0) };
|
|
566
|
+
const absoluteStart = createAbsolutePositionFromStateVector(doc, text, version, node.start, undefined, true);
|
|
567
|
+
const declarationStart = createRelativePositionFromStateVector(text, version, node.declaration.start, -1, true);
|
|
568
|
+
const declarationEnd = createRelativePositionFromStateVector(text, version, node.declaration.end, -1, true);
|
|
569
|
+
const relativeEnd = createRelativePositionFromStateVector(text, version, node.end, -1, true);
|
|
570
|
+
let exportCall = `await ${this._exportFactory}({ default: `;
|
|
571
|
+
let currentIndex = absoluteStart.index;
|
|
572
|
+
text.insert(currentIndex, exportCall);
|
|
573
|
+
currentIndex += exportCall.length;
|
|
574
|
+
const absoluteDeclarationStart = createAbsolutePositionFromRelativePosition(declarationStart, doc);
|
|
575
|
+
text.delete(currentIndex, absoluteDeclarationStart.index - currentIndex);
|
|
576
|
+
const absoluteDeclarationEnd = createAbsolutePositionFromRelativePosition(declarationEnd, doc);
|
|
577
|
+
text.insert(absoluteDeclarationEnd.index, ' });');
|
|
578
|
+
const absoulteEnd = createAbsolutePositionFromRelativePosition(relativeEnd, doc);
|
|
579
|
+
text.delete(absoulteEnd.index - 1, 1);
|
|
580
|
+
}
|
|
581
|
+
_replaceExportAllDeclaration(node, parent, doc, text, metadata) {
|
|
582
|
+
metadata.isModule = true;
|
|
583
|
+
doc.clientID += 1;
|
|
584
|
+
const version = { '0': getClock(doc, 0) };
|
|
585
|
+
const absoluteStart = createAbsolutePositionFromStateVector(doc, text, version, node.start, undefined, true);
|
|
586
|
+
const sourceStart = createRelativePositionFromStateVector(text, version, node.source.start, -1, true);
|
|
587
|
+
const sourceEnd = createRelativePositionFromStateVector(text, version, node.source.end, -1, true);
|
|
588
|
+
const relativeEnd = createRelativePositionFromStateVector(text, version, node.end, -1, true);
|
|
589
|
+
let exportCall = `await ${this._exportFactory}(`;
|
|
590
|
+
let currentIndex = absoluteStart.index;
|
|
591
|
+
text.insert(currentIndex, exportCall);
|
|
592
|
+
currentIndex += exportCall.length;
|
|
593
|
+
const absolutesourceStart = createAbsolutePositionFromRelativePosition(sourceStart, doc);
|
|
594
|
+
text.delete(currentIndex, absolutesourceStart.index - currentIndex);
|
|
595
|
+
const absoluteSourceEnd = createAbsolutePositionFromRelativePosition(sourceEnd, doc);
|
|
596
|
+
text.insert(absoluteSourceEnd.index, ');');
|
|
597
|
+
const absoulteEnd = createAbsolutePositionFromRelativePosition(relativeEnd, doc);
|
|
598
|
+
text.delete(absoulteEnd.index - 1, 1);
|
|
599
|
+
}
|
|
240
600
|
_replaceWhileStatement(node, doc, text) {
|
|
241
601
|
this._insertEnergyCheckIntoStatement(doc, text, node.body);
|
|
242
602
|
}
|
|
@@ -252,12 +612,71 @@ export class Transpiler {
|
|
|
252
612
|
_replaceForOfStatement(node, doc, text) {
|
|
253
613
|
this._insertEnergyCheckIntoStatement(doc, text, node.body);
|
|
254
614
|
}
|
|
255
|
-
|
|
256
|
-
|
|
615
|
+
_removeClassImplements(node, doc, text) {
|
|
616
|
+
doc.clientID += 1;
|
|
617
|
+
const version = { '0': getClock(doc, 0) };
|
|
618
|
+
const firstImplemented = node.implements[0];
|
|
619
|
+
const lastImplemented = node.implements[node.implements.length - 1];
|
|
620
|
+
const implementedStart = createAbsolutePositionFromStateVector(doc, text, version, firstImplemented.start - 'implements '.length, undefined, true);
|
|
621
|
+
const implementedEnd = createAbsolutePositionFromStateVector(doc, text, version, lastImplemented.end, -1, true);
|
|
622
|
+
text.delete(implementedStart.index, implementedEnd.index - implementedStart.index);
|
|
623
|
+
}
|
|
624
|
+
_removeClassAbstract(node, doc, text) {
|
|
625
|
+
doc.clientID += 1;
|
|
626
|
+
const version = { '0': getClock(doc, 0) };
|
|
627
|
+
const abstractStart = createAbsolutePositionFromStateVector(doc, text, version, node.start, undefined, true);
|
|
628
|
+
const abstractEnd = createAbsolutePositionFromStateVector(doc, text, version, node.start + 'abstract '.length, -1, true);
|
|
629
|
+
text.delete(abstractStart.index, abstractEnd.index - abstractStart.index);
|
|
630
|
+
}
|
|
631
|
+
_removeAsExpression(node, doc, text) {
|
|
632
|
+
doc.clientID += 1;
|
|
633
|
+
const version = { '0': getClock(doc, 0) };
|
|
634
|
+
const expressionEnd = createAbsolutePositionFromStateVector(doc, text, version, node.expression.end, -1, true);
|
|
635
|
+
const absoluteEnd = createAbsolutePositionFromStateVector(doc, text, version, node.end, -1, true);
|
|
636
|
+
text.delete(expressionEnd.index, absoluteEnd.index - expressionEnd.index);
|
|
637
|
+
}
|
|
638
|
+
_removeNodeOrReplaceWithUndefined(node, doc, text) {
|
|
639
|
+
doc.clientID += 1;
|
|
640
|
+
const version = { '0': getClock(doc, 0) };
|
|
641
|
+
if (node.type === 'TSInterfaceDeclaration' ||
|
|
642
|
+
node.type === 'TSCallSignatureDeclaration' ||
|
|
643
|
+
node.type === 'TSEnumDeclaration' ||
|
|
644
|
+
node.type === 'TSTypeAliasDeclaration') {
|
|
645
|
+
// replace with const Identifier = void 0; instead of deleting
|
|
646
|
+
// This is because these declarations might be referenced in a later export {} declaration,
|
|
647
|
+
// so we need to keep the identifier around.
|
|
648
|
+
// In the future, we might optimize this to also delete TypeScript-sepecific declarations from those exports.
|
|
649
|
+
const absoluteStart = createAbsolutePositionFromStateVector(doc, text, version, node.start, undefined, true);
|
|
650
|
+
const identifierStart = createRelativePositionFromStateVector(text, version, node.id.start, undefined, true);
|
|
651
|
+
const identifierEnd = createRelativePositionFromStateVector(text, version, node.id.end, -1, true);
|
|
652
|
+
const end = createRelativePositionFromStateVector(text, version, node.end, -1, true);
|
|
653
|
+
const absoluteIdentifierStart = createAbsolutePositionFromRelativePosition(identifierStart, doc);
|
|
654
|
+
text.insert(absoluteIdentifierStart.index, 'const ');
|
|
655
|
+
text.delete(absoluteStart.index, absoluteIdentifierStart.index - absoluteStart.index);
|
|
656
|
+
const absoluteIdentifierEnd = createAbsolutePositionFromRelativePosition(identifierEnd, doc);
|
|
657
|
+
let str = ` = void 0;`;
|
|
658
|
+
text.insert(absoluteIdentifierEnd.index, str);
|
|
659
|
+
const absoluteEnd = createAbsolutePositionFromRelativePosition(end, doc);
|
|
660
|
+
text.delete(absoluteIdentifierEnd.index + str.length, absoluteEnd.index - absoluteIdentifierEnd.index - str.length);
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
const absoluteStart = createAbsolutePositionFromStateVector(doc, text, version, node.start, undefined, true);
|
|
664
|
+
const absoluteEnd = createAbsolutePositionFromStateVector(doc, text, version, node.end, -1, true);
|
|
665
|
+
text.delete(absoluteStart.index, absoluteEnd.index - absoluteStart.index);
|
|
666
|
+
}
|
|
667
|
+
_removeOptionalFromIdentifier(node, doc, text) {
|
|
668
|
+
var _a, _b;
|
|
669
|
+
doc.clientID += 1;
|
|
670
|
+
const version = { '0': getClock(doc, 0) };
|
|
671
|
+
const absoluteEnd = createAbsolutePositionFromStateVector(doc, text, version, (_b = (_a = node.typeAnnotation) === null || _a === void 0 ? void 0 : _a.start) !== null && _b !== void 0 ? _b : node.end, -1, true);
|
|
672
|
+
text.delete(absoluteEnd.index - 1, 1);
|
|
673
|
+
}
|
|
674
|
+
_replaceJSXElement(node, doc, text, metadata) {
|
|
675
|
+
this._insertJSXFactoryCall(node, node.openingElement, node.closingElement, doc, text, metadata);
|
|
257
676
|
this._removeTag(node, node.openingElement, node.closingElement, doc, text);
|
|
258
677
|
}
|
|
259
|
-
_replaceJSXFragment(node, doc, text) {
|
|
260
|
-
this._insertJSXFactoryCall(node, node.openingFragment, node.closingFragment, doc, text);
|
|
678
|
+
_replaceJSXFragment(node, doc, text, metadata) {
|
|
679
|
+
this._insertJSXFactoryCall(node, node.openingFragment, node.closingFragment, doc, text, metadata);
|
|
261
680
|
this._removeTag(node, node.openingFragment, node.closingFragment, doc, text);
|
|
262
681
|
}
|
|
263
682
|
_replaceJSXEmptyExpression(node, doc, text) {
|
|
@@ -273,7 +692,7 @@ export class Transpiler {
|
|
|
273
692
|
text.delete(absoluteValueStartBegin.index, length);
|
|
274
693
|
text.insert(absoluteValueEndEnd.index, "''");
|
|
275
694
|
}
|
|
276
|
-
_insertJSXFactoryCall(node, openElement, closeElement, doc, text) {
|
|
695
|
+
_insertJSXFactoryCall(node, openElement, closeElement, doc, text, metadata) {
|
|
277
696
|
doc.clientID += 1;
|
|
278
697
|
const version = { '0': getClock(doc, 0) };
|
|
279
698
|
const openingFunctionCall = `${this._jsxFactory}(`;
|
|
@@ -319,18 +738,18 @@ export class Transpiler {
|
|
|
319
738
|
currentIndex += nodeName.length;
|
|
320
739
|
}
|
|
321
740
|
if (openElement.attributes.length > 0) {
|
|
322
|
-
this._replaceJSXElementAttributes(node, openElement, openElement.attributes, doc, text);
|
|
741
|
+
this._replaceJSXElementAttributes(node, openElement, openElement.attributes, doc, text, metadata);
|
|
323
742
|
}
|
|
324
743
|
else {
|
|
325
744
|
const props = `null,`;
|
|
326
745
|
text.insert(currentIndex, props);
|
|
327
746
|
}
|
|
328
|
-
this._replaceJSXElementChildren(node, node.children, doc, text);
|
|
747
|
+
this._replaceJSXElementChildren(node, node.children, doc, text, metadata);
|
|
329
748
|
const absoluteEnd = createAbsolutePositionFromRelativePosition(end, doc);
|
|
330
749
|
doc.clientID += 1;
|
|
331
750
|
text.insert(absoluteEnd.index, ')');
|
|
332
751
|
}
|
|
333
|
-
_replaceJSXElementAttributes(node, openElement, attributes, doc, text) {
|
|
752
|
+
_replaceJSXElementAttributes(node, openElement, attributes, doc, text, metadata) {
|
|
334
753
|
var _a, _b;
|
|
335
754
|
// doc.clientID += 1;
|
|
336
755
|
const version = { '0': getClock(doc, 0) };
|
|
@@ -363,10 +782,10 @@ export class Transpiler {
|
|
|
363
782
|
text.insert(pos.index, val);
|
|
364
783
|
index++;
|
|
365
784
|
if (attr.type === 'JSXSpreadAttribute') {
|
|
366
|
-
this._replace(attr.argument, doc, text);
|
|
785
|
+
this._replace(attr.argument, doc, text, metadata);
|
|
367
786
|
}
|
|
368
787
|
else {
|
|
369
|
-
this._replace(attr.value, doc, text);
|
|
788
|
+
this._replace(attr.value, doc, text, metadata);
|
|
370
789
|
}
|
|
371
790
|
}
|
|
372
791
|
const startAbsolute = createAbsolutePositionFromRelativePosition(start, doc);
|
|
@@ -374,11 +793,11 @@ export class Transpiler {
|
|
|
374
793
|
const endAbsolute = createAbsolutePositionFromRelativePosition(end, doc);
|
|
375
794
|
text.insert(endAbsolute.index, '},');
|
|
376
795
|
}
|
|
377
|
-
_replaceJSXElementChildren(node, children, doc, text) {
|
|
796
|
+
_replaceJSXElementChildren(node, children, doc, text, metadata) {
|
|
378
797
|
const version = { '0': getClock(doc, 0) };
|
|
379
798
|
for (let child of children) {
|
|
380
799
|
const pos = createRelativePositionFromStateVector(text, version, child.end, undefined, true);
|
|
381
|
-
this._replace(child, doc, text);
|
|
800
|
+
this._replace(child, doc, text, metadata);
|
|
382
801
|
doc.clientID += 1;
|
|
383
802
|
const absoluteEnd = createAbsolutePositionFromRelativePosition(pos, doc);
|
|
384
803
|
text.insert(absoluteEnd.index, ',');
|