hackmud-script-manager-ng 0.26.5 → 0.26.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.
Files changed (69) hide show
  1. package/bin/hsm.d.ts +2 -0
  2. package/bin/hsm.js +493 -0
  3. package/constants.d.ts +2 -0
  4. package/constants.js +3 -0
  5. package/generateTypeDeclaration.d.ts +1 -0
  6. package/generateTypeDeclaration.js +70 -0
  7. package/index.d.ts +16 -0
  8. package/index.js +52 -0
  9. package/package.json +3 -22
  10. package/processScript/index.d.ts +34 -0
  11. package/processScript/index.js +362 -0
  12. package/processScript/minify.d.ts +20 -0
  13. package/processScript/minify.js +431 -0
  14. package/processScript/postprocess.d.ts +1 -0
  15. package/processScript/postprocess.js +21 -0
  16. package/processScript/preprocess.d.ts +11 -0
  17. package/processScript/preprocess.js +107 -0
  18. package/processScript/shared.d.ts +5 -0
  19. package/processScript/shared.js +32 -0
  20. package/processScript/transform.d.ts +24 -0
  21. package/processScript/transform.js +803 -0
  22. package/pull.d.ts +7 -0
  23. package/pull.js +11 -0
  24. package/push.d.ts +42 -0
  25. package/push.js +164 -0
  26. package/syncMacros.d.ts +4 -0
  27. package/syncMacros.js +42 -0
  28. package/watch.d.ts +19 -0
  29. package/watch.js +220 -0
  30. package/.envrc +0 -1
  31. package/.github/FUNDING.yml +0 -14
  32. package/.github/workflows/publish.yml +0 -27
  33. package/.github/workflows/test.yml +0 -18
  34. package/.vscode/settings.json +0 -55
  35. package/CONTRIBUTING.md +0 -68
  36. package/eslint.config.js +0 -81
  37. package/game-scripts-tests/array-rest-works.ts +0 -6
  38. package/game-scripts-tests/this_array_in_object.ts +0 -16
  39. package/game-scripts-tests/this_function_array.ts +0 -9
  40. package/game-scripts-tests/this_in_default_argument.ts +0 -35
  41. package/game-scripts-tests/this_in_lambda_in_object.ts +0 -26
  42. package/pnpm-workspace.yaml +0 -2
  43. package/rollup.config.js +0 -69
  44. package/scripts/dev.sh +0 -3
  45. package/scripts/emit-declarations.sh +0 -3
  46. package/scripts/emit-package-json.js +0 -9
  47. package/scripts/lint.sh +0 -5
  48. package/scripts/package.sh +0 -7
  49. package/scripts/update.sh +0 -4
  50. package/scripts/version-dev.js +0 -14
  51. package/src/bin/hsm.ts +0 -754
  52. package/src/constants.ts +0 -2
  53. package/src/env.d.ts +0 -13
  54. package/src/generateTypeDeclaration.ts +0 -119
  55. package/src/index.ts +0 -13
  56. package/src/processScript/__snapshots__/index.ts.snap +0 -8
  57. package/src/processScript/index.ts +0 -474
  58. package/src/processScript/minify.ts +0 -594
  59. package/src/processScript/postprocess.ts +0 -19
  60. package/src/processScript/preprocess.ts +0 -130
  61. package/src/processScript/shared.ts +0 -33
  62. package/src/processScript/transform.ts +0 -978
  63. package/src/pull.ts +0 -20
  64. package/src/push.ts +0 -200
  65. package/src/syncMacros.ts +0 -53
  66. package/src/tsconfig.json +0 -14
  67. package/src/watch.ts +0 -258
  68. package/tsconfig.json +0 -23
  69. package/vitest.config.ts +0 -19
@@ -0,0 +1,803 @@
1
+ import w5m3g6w58my4352jqaq8zmhm from "@babel/traverse"
2
+ import t from "@babel/types"
3
+ import { assert } from "@samual/lib/assert"
4
+ import { clearObject } from "@samual/lib/clearObject"
5
+ import { validDBMethods } from "../constants.js"
6
+ import { getReferencePathsToGlobal } from "./shared.js"
7
+ const traverse = w5m3g6w58my4352jqaq8zmhm.default,
8
+ globalFunctionsUnder7Characters = [
9
+ "Map",
10
+ "Set",
11
+ "Date",
12
+ "JSON",
13
+ "Math",
14
+ "Array",
15
+ "Error",
16
+ "isNaN",
17
+ "Number",
18
+ "Object",
19
+ "RegExp",
20
+ "String",
21
+ "Symbol",
22
+ "BigInt"
23
+ ]
24
+ function transform(file, sourceCode, { uniqueId = "00000000000", scriptUser, scriptName, seclevel = 4 }) {
25
+ const warnings = [],
26
+ topFunctionName = `_${uniqueId}_SCRIPT_`
27
+ let program
28
+ traverse(file, {
29
+ Program(path) {
30
+ program = path
31
+ path.skip()
32
+ }
33
+ })
34
+ if (program.scope.hasGlobal("_SOURCE"))
35
+ for (const referencePath of getReferencePathsToGlobal("_SOURCE", program))
36
+ referencePath.replaceWith(t.stringLiteral(sourceCode))
37
+ if (program.scope.hasGlobal("_BUILD_DATE"))
38
+ for (const referencePath of getReferencePathsToGlobal("_BUILD_DATE", program))
39
+ referencePath.replaceWith(t.numericLiteral(Date.now()))
40
+ let uniqueIdScriptUserNeeded = !1
41
+ if (program.scope.hasGlobal("_SCRIPT_USER"))
42
+ for (const referencePath of getReferencePathsToGlobal("_SCRIPT_USER", program))
43
+ if (null == scriptUser) {
44
+ uniqueIdScriptUserNeeded = !0
45
+ referencePath.replaceWith(t.identifier(`_${uniqueId}_SCRIPT_USER_`))
46
+ } else
47
+ referencePath.replaceWith(t.stringLiteral(1 == scriptUser ? `$${uniqueId}$SCRIPT_USER$` : scriptUser))
48
+ if (program.scope.hasGlobal("_SCRIPT_NAME")) {
49
+ warnings.push({
50
+ message:
51
+ "Global _SCRIPT_NAME is deprecated and will be removed in the next minor release of HSM, use _SCRIPT_SUBNAME instead"
52
+ })
53
+ for (const referencePath of getReferencePathsToGlobal("_SCRIPT_NAME", program))
54
+ referencePath.replaceWith(t.stringLiteral(1 == scriptName ? `$${uniqueId}$SCRIPT_NAME$` : scriptName))
55
+ }
56
+ if (program.scope.hasGlobal("_SCRIPT_SUBNAME"))
57
+ for (const referencePath of getReferencePathsToGlobal("_SCRIPT_SUBNAME", program))
58
+ referencePath.replaceWith(t.stringLiteral(1 == scriptName ? `$${uniqueId}$SCRIPT_NAME$` : scriptName))
59
+ if (program.scope.hasGlobal("_FULL_SCRIPT_NAME"))
60
+ for (const referencePath of getReferencePathsToGlobal("_FULL_SCRIPT_NAME", program))
61
+ if (1 == scriptUser || 1 == scriptName)
62
+ referencePath.replaceWith(t.stringLiteral(`$${uniqueId}$FULL_SCRIPT_NAME$`))
63
+ else if (null == scriptUser) {
64
+ uniqueIdScriptUserNeeded = !0
65
+ referencePath.replaceWith(
66
+ t.binaryExpression(
67
+ "+",
68
+ t.identifier(`_${uniqueId}_SCRIPT_USER_`),
69
+ t.stringLiteral("." + scriptName)
70
+ )
71
+ )
72
+ } else referencePath.replaceWith(t.stringLiteral(`${scriptUser}.${scriptName}`))
73
+ let functionDotPrototypeIsReferencedMultipleTimes = !1
74
+ if (program.scope.hasGlobal("Function")) {
75
+ const FunctionReferencePaths = getReferencePathsToGlobal("Function", program)
76
+ if (1 == FunctionReferencePaths.length) {
77
+ const referencePath = FunctionReferencePaths[0]
78
+ assert(
79
+ "MemberExpression" == referencePath.parent.type,
80
+ "src/processScript/transform.ts:111:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
81
+ )
82
+ assert(
83
+ "Identifier" == referencePath.parent.property.type,
84
+ "src/processScript/transform.ts:116:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
85
+ )
86
+ assert(
87
+ "prototype" == referencePath.parent.property.name,
88
+ "src/processScript/transform.ts:121:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
89
+ )
90
+ referencePath.parentPath.replaceWith(createGetFunctionPrototypeNode())
91
+ } else {
92
+ for (const referencePath of FunctionReferencePaths) {
93
+ assert(
94
+ "MemberExpression" == referencePath.parent.type,
95
+ "src/processScript/transform.ts:129:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
96
+ )
97
+ assert(
98
+ "Identifier" == referencePath.parent.property.type,
99
+ "src/processScript/transform.ts:134:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
100
+ )
101
+ assert(
102
+ "prototype" == referencePath.parent.property.name,
103
+ "src/processScript/transform.ts:139:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
104
+ )
105
+ functionDotPrototypeIsReferencedMultipleTimes = !0
106
+ referencePath.parentPath.replaceWith(t.identifier(`_${uniqueId}_FUNCTION_DOT_PROTOTYPE_`))
107
+ }
108
+ functionDotPrototypeIsReferencedMultipleTimes = !0
109
+ }
110
+ }
111
+ const neededSubscriptLets = new Map()
112
+ let detectedSeclevel = 4
113
+ program.scope.hasGlobal("$s") &&
114
+ warnings.push({
115
+ message:
116
+ "Subscripts in the form of $s.foo.bar() and #s.foo.bar() are deprecated. Use explicit seclevels instead."
117
+ })
118
+ for (const fakeSubscriptObjectName of ["$fs", "$4s", "$s"])
119
+ program.scope.hasGlobal(fakeSubscriptObjectName) && processFakeSubscriptObject(fakeSubscriptObjectName, 4)
120
+ for (const fakeSubscriptObjectName of ["$hs", "$3s"])
121
+ if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
122
+ detectedSeclevel = 3
123
+ processFakeSubscriptObject(fakeSubscriptObjectName, 3)
124
+ }
125
+ for (const fakeSubscriptObjectName of ["$ms", "$2s"])
126
+ if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
127
+ detectedSeclevel = 2
128
+ processFakeSubscriptObject(fakeSubscriptObjectName, 2)
129
+ }
130
+ for (const fakeSubscriptObjectName of ["$ls", "$1s"])
131
+ if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
132
+ detectedSeclevel = 1
133
+ processFakeSubscriptObject(fakeSubscriptObjectName, 1)
134
+ }
135
+ for (const fakeSubscriptObjectName of ["$ns", "$0s"])
136
+ if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
137
+ detectedSeclevel = 0
138
+ processFakeSubscriptObject(fakeSubscriptObjectName, 0)
139
+ }
140
+ seclevel = Math.min(seclevel, detectedSeclevel)
141
+ const neededDbMethodLets = new Set()
142
+ if (program.scope.hasGlobal("$db"))
143
+ for (const referencePath of getReferencePathsToGlobal("$db", program)) {
144
+ assert("MemberExpression" == referencePath.parentPath.node.type, "src/processScript/transform.ts:198:69")
145
+ assert("Identifier" == referencePath.parentPath.node.property.type, "src/processScript/transform.ts:199:72")
146
+ const databaseOpMethodName = referencePath.parentPath.node.property.name
147
+ assert(
148
+ validDBMethods.includes(databaseOpMethodName),
149
+ `src/processScript/transform.ts:205:8 invalid db method "${databaseOpMethodName}", valid db methods are "${validDBMethods.join('", "')}"`
150
+ )
151
+ if ("CallExpression" == referencePath.parentPath.parentPath?.type)
152
+ referencePath.parentPath.replaceWith(t.identifier(`$${uniqueId}$DB$${databaseOpMethodName}$`))
153
+ else {
154
+ referencePath.parentPath.replaceWith(
155
+ t.identifier(`_${uniqueId}_CONSOLE_METHOD_${databaseOpMethodName}_`)
156
+ )
157
+ neededDbMethodLets.add(databaseOpMethodName)
158
+ }
159
+ }
160
+ let needDebugLet = !1
161
+ if (program.scope.hasGlobal("$D"))
162
+ for (const referencePath of getReferencePathsToGlobal("$D", program))
163
+ if ("CallExpression" == referencePath.parentPath.type)
164
+ referencePath.replaceWith(t.identifier(`$${uniqueId}$DEBUG$`))
165
+ else {
166
+ referencePath.replaceWith(t.identifier(`_${uniqueId}_DEBUG_`))
167
+ needDebugLet = !0
168
+ }
169
+ if (program.scope.hasGlobal("$FMCL"))
170
+ for (const referencePath of getReferencePathsToGlobal("$FMCL", program))
171
+ referencePath.replaceWith(t.identifier(`$${uniqueId}$FMCL$`))
172
+ let needG = program.scope.hasGlobal("$G")
173
+ if (needG)
174
+ for (const referencePath of getReferencePathsToGlobal("$G", program))
175
+ referencePath.replaceWith(t.identifier(`_${uniqueId}_G_`))
176
+ if (program.scope.hasGlobal("_SECLEVEL"))
177
+ for (const referencePath of getReferencePathsToGlobal("_SECLEVEL", program))
178
+ referencePath.replaceWith(t.numericLiteral(seclevel))
179
+ let needGetPrototypeOf = !1,
180
+ needHasOwn = !1
181
+ if (program.scope.hasGlobal("Object"))
182
+ for (const referencePath of getReferencePathsToGlobal("Object", program))
183
+ if ("MemberExpression" == referencePath.parent.type && !referencePath.parent.computed) {
184
+ assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:255:64")
185
+ if ("getPrototypeOf" == referencePath.parent.property.name) {
186
+ referencePath.parentPath.replaceWith(t.identifier(`_${uniqueId}_GET_PROTOTYPE_OF_`))
187
+ needGetPrototypeOf = !0
188
+ } else if ("hasOwn" == referencePath.parent.property.name) {
189
+ referencePath.parentPath.replaceWith(t.identifier(`_${uniqueId}_HAS_OWN_`))
190
+ needHasOwn = !0
191
+ }
192
+ }
193
+ const consoleMethodsReferenced = new Set()
194
+ if (program.scope.hasGlobal("console"))
195
+ for (const referencePath of getReferencePathsToGlobal("console", program))
196
+ if ("MemberExpression" == referencePath.parent.type && !referencePath.parent.computed) {
197
+ assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:273:64")
198
+ referencePath.parentPath.replaceWith(
199
+ t.identifier(`_${uniqueId}_CONSOLE_METHOD_${referencePath.parent.property.name}_`)
200
+ )
201
+ consoleMethodsReferenced.add(referencePath.parent.property.name)
202
+ }
203
+ const lastStatement = program.node.body.at(-1)
204
+ let exportDefaultName
205
+ assert(lastStatement, "src/processScript/transform.ts:287:27 program is empty")
206
+ if ("ExportNamedDeclaration" == lastStatement.type) {
207
+ program.node.body.pop()
208
+ for (const specifier of lastStatement.specifiers) {
209
+ assert(
210
+ "ExportSpecifier" == specifier.type,
211
+ `src/processScript/transform.ts:293:51 ${specifier.type} is currently unsupported`
212
+ )
213
+ if (
214
+ "default" !=
215
+ ("Identifier" == specifier.exported.type ? specifier.exported.name : specifier.exported.value)
216
+ )
217
+ throw Error("Only default exports are supported")
218
+ exportDefaultName = specifier.local.name
219
+ }
220
+ }
221
+ const globalBlock = t.blockStatement([])
222
+ let mainFunction
223
+ for (const statement of program.node.body)
224
+ if ("VariableDeclaration" == statement.type)
225
+ for (const declarator of statement.declarations)
226
+ if (
227
+ "Identifier" != declarator.id.type ||
228
+ declarator.id.name != exportDefaultName ||
229
+ !declarator.init ||
230
+ ("FunctionExpression" != declarator.init.type &&
231
+ "ArrowFunctionExpression" != declarator.init.type) ||
232
+ declarator.init.async ||
233
+ declarator.init.generator
234
+ ) {
235
+ for (const identifierName in t.getBindingIdentifiers(declarator.id)) {
236
+ identifierName == exportDefaultName &&
237
+ (mainFunction = t.functionDeclaration(
238
+ t.identifier(topFunctionName),
239
+ [t.identifier("context"), t.identifier("args")],
240
+ t.blockStatement([
241
+ t.returnStatement(t.callExpression(t.identifier(exportDefaultName), []))
242
+ ])
243
+ ))
244
+ globalBlock.body.push(
245
+ t.variableDeclaration("let", [t.variableDeclarator(t.identifier(identifierName))])
246
+ )
247
+ }
248
+ declarator.init &&
249
+ globalBlock.body.push(
250
+ t.expressionStatement(t.assignmentExpression("=", declarator.id, declarator.init))
251
+ )
252
+ } else
253
+ mainFunction = t.functionDeclaration(
254
+ t.identifier(topFunctionName),
255
+ declarator.init.params,
256
+ "BlockStatement" == declarator.init.body.type
257
+ ? declarator.init.body
258
+ : t.blockStatement([t.returnStatement(declarator.init.body)])
259
+ )
260
+ else
261
+ "FunctionDeclaration" == statement.type
262
+ ? statement.id.name == exportDefaultName
263
+ ? (mainFunction = statement)
264
+ : globalBlock.body.push(
265
+ t.variableDeclaration("let", [
266
+ t.variableDeclarator(
267
+ statement.id,
268
+ t.functionExpression(
269
+ void 0,
270
+ statement.params,
271
+ statement.body,
272
+ statement.generator,
273
+ statement.async
274
+ )
275
+ )
276
+ ])
277
+ )
278
+ : globalBlock.body.push(statement)
279
+ mainFunction ||= t.functionDeclaration(
280
+ t.identifier(topFunctionName),
281
+ [t.identifier("context"), t.identifier("args")],
282
+ t.blockStatement([])
283
+ )
284
+ if (uniqueIdScriptUserNeeded) {
285
+ const mainFunctionParams = mainFunction.params
286
+ mainFunction.params = [t.restElement(t.identifier(`_${uniqueId}_PARAMS_`))]
287
+ mainFunction.body.body.unshift(
288
+ t.variableDeclaration("let", [
289
+ t.variableDeclarator(t.arrayPattern(mainFunctionParams), t.identifier(`_${uniqueId}_PARAMS_`)),
290
+ t.variableDeclarator(
291
+ t.arrayPattern([t.identifier(`_${uniqueId}_SCRIPT_USER_`)]),
292
+ t.callExpression(
293
+ t.memberExpression(
294
+ t.memberExpression(
295
+ t.memberExpression(t.identifier(`_${uniqueId}_PARAMS_`), t.numericLiteral(0), !0),
296
+ t.identifier("this_script")
297
+ ),
298
+ t.identifier("split")
299
+ ),
300
+ [t.stringLiteral(".")]
301
+ )
302
+ )
303
+ ])
304
+ )
305
+ }
306
+ program.node.body = [mainFunction]
307
+ if (globalBlock.body.length) {
308
+ program.scope.crawl()
309
+ const globalBlockVariables = new Set()
310
+ let hoistedGlobalBlockFunctions = 0
311
+ for (const [globalBlockIndex, globalBlockStatement] of [...globalBlock.body.entries()].reverse())
312
+ if ("VariableDeclaration" == globalBlockStatement.type) {
313
+ assert(1 == globalBlockStatement.declarations.length, "src/processScript/transform.ts:406:59")
314
+ const declarator = globalBlockStatement.declarations[0]
315
+ assert(
316
+ "Identifier" == declarator.id.type,
317
+ `src/processScript/transform.ts:410:51 declarator.id.type was "${declarator.id.type}"`
318
+ )
319
+ program.scope.crawl()
320
+ if (program.scope.hasGlobal(declarator.id.name)) {
321
+ globalBlock.body.splice(globalBlockIndex, 1)
322
+ const [globalBlockPath] = program.unshiftContainer("body", globalBlock),
323
+ [globalBlockStatementPath] = program.unshiftContainer("body", globalBlockStatement)
324
+ program.scope.crawl()
325
+ if (
326
+ !declarator.init ||
327
+ ("FunctionExpression" != declarator.init.type &&
328
+ "ArrowFunctionExpression" != declarator.init.type) ||
329
+ Object.keys(program.scope.globals).some(global => globalBlockVariables.has(global))
330
+ ) {
331
+ const binding = program.scope.getBinding(declarator.id.name)
332
+ assert(binding, "src/processScript/transform.ts:429:23")
333
+ for (const referencePath of binding.referencePaths) {
334
+ assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:432:56")
335
+ referencePath.replaceWith(
336
+ t.memberExpression(
337
+ t.identifier(`_${uniqueId}_G_`),
338
+ t.identifier(referencePath.node.name)
339
+ )
340
+ )
341
+ needG = !0
342
+ }
343
+ for (const referencePath of binding.constantViolations)
344
+ if ("AssignmentExpression" == referencePath.node.type)
345
+ for (const [name, node] of Object.entries(t.getBindingIdentifiers(referencePath.node)))
346
+ if (name == declarator.id.name) {
347
+ clearObject(node)
348
+ Object.assign(
349
+ node,
350
+ t.memberExpression(t.identifier(`_${uniqueId}_G_`), t.identifier(name))
351
+ )
352
+ needG = !0
353
+ }
354
+ globalBlockPath.remove()
355
+ globalBlockStatementPath.remove()
356
+ if (declarator.init) {
357
+ globalBlock.body.splice(
358
+ globalBlockIndex,
359
+ 0,
360
+ t.expressionStatement(
361
+ t.assignmentExpression(
362
+ "=",
363
+ t.memberExpression(
364
+ t.identifier(`_${uniqueId}_G_`),
365
+ t.identifier(declarator.id.name)
366
+ ),
367
+ declarator.init
368
+ )
369
+ )
370
+ )
371
+ needG = !0
372
+ }
373
+ } else {
374
+ globalBlockPath.remove()
375
+ globalBlockStatementPath.remove()
376
+ mainFunction.body.body.unshift(globalBlockStatement)
377
+ hoistedGlobalBlockFunctions++
378
+ }
379
+ } else globalBlockVariables.add(declarator.id.name)
380
+ } else if ("ClassDeclaration" == globalBlockStatement.type) {
381
+ program.scope.crawl()
382
+ assert(globalBlockStatement.id, "src/processScript/transform.ts:489:37")
383
+ if (program.scope.hasGlobal(globalBlockStatement.id.name)) {
384
+ globalBlock.body.splice(globalBlockIndex, 1)
385
+ const [globalBlockPath] = program.unshiftContainer("body", globalBlock),
386
+ [globalBlockStatementPath] = program.unshiftContainer("body", globalBlockStatement)
387
+ program.scope.crawl()
388
+ const binding = program.scope.getBinding(globalBlockStatement.id.name)
389
+ assert(binding, "src/processScript/transform.ts:501:22")
390
+ for (const referencePath of binding.referencePaths) {
391
+ assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:504:55")
392
+ referencePath.replaceWith(
393
+ t.memberExpression(t.identifier(`_${uniqueId}_G_`), t.identifier(referencePath.node.name))
394
+ )
395
+ needG = !0
396
+ }
397
+ globalBlockPath.remove()
398
+ globalBlockStatementPath.remove()
399
+ globalBlock.body.splice(
400
+ globalBlockIndex,
401
+ 0,
402
+ t.expressionStatement(
403
+ t.assignmentExpression(
404
+ "=",
405
+ t.memberExpression(
406
+ t.identifier(`_${uniqueId}_G_`),
407
+ t.identifier(globalBlockStatement.id.name)
408
+ ),
409
+ t.classExpression(
410
+ void 0,
411
+ globalBlockStatement.superClass,
412
+ globalBlockStatement.body,
413
+ globalBlockStatement.decorators
414
+ )
415
+ )
416
+ )
417
+ )
418
+ needG = !0
419
+ } else globalBlockVariables.add(globalBlockStatement.id.name)
420
+ }
421
+ globalBlock.body.length &&
422
+ mainFunction.body.body.splice(
423
+ hoistedGlobalBlockFunctions,
424
+ 0,
425
+ t.ifStatement(t.unaryExpression("!", t.identifier(`$${uniqueId}$FMCL$`)), globalBlock)
426
+ )
427
+ }
428
+ functionDotPrototypeIsReferencedMultipleTimes &&
429
+ mainFunction.body.body.unshift(
430
+ t.variableDeclaration("let", [
431
+ t.variableDeclarator(
432
+ t.identifier(`_${uniqueId}_FUNCTION_DOT_PROTOTYPE_`),
433
+ createGetFunctionPrototypeNode()
434
+ )
435
+ ])
436
+ )
437
+ needGetPrototypeOf &&
438
+ mainFunction.body.body.unshift(
439
+ t.variableDeclaration("let", [
440
+ t.variableDeclarator(
441
+ t.objectPattern([
442
+ t.objectProperty(t.identifier("get"), t.identifier(`_${uniqueId}_DUNDER_PROTO_GETTER_`))
443
+ ]),
444
+ t.callExpression(
445
+ t.memberExpression(t.identifier("Object"), t.identifier("getOwnPropertyDescriptor")),
446
+ [
447
+ t.memberExpression(t.identifier("Object"), t.identifier("prototype")),
448
+ t.stringLiteral("__proto__")
449
+ ]
450
+ )
451
+ ),
452
+ t.variableDeclarator(
453
+ t.identifier(`_${uniqueId}_GET_PROTOTYPE_OF_`),
454
+ t.callExpression(
455
+ t.memberExpression(
456
+ t.memberExpression(
457
+ t.identifier(
458
+ globalFunctionsUnder7Characters.find(name => !program.scope.hasOwnBinding(name))
459
+ ),
460
+ t.identifier("call")
461
+ ),
462
+ t.identifier("bind")
463
+ ),
464
+ [t.identifier(`_${uniqueId}_DUNDER_PROTO_GETTER_`)]
465
+ )
466
+ )
467
+ ])
468
+ )
469
+ needHasOwn &&
470
+ mainFunction.body.body.unshift(
471
+ t.variableDeclaration("let", [
472
+ t.variableDeclarator(
473
+ t.identifier(`_${uniqueId}_HAS_OWN_`),
474
+ t.callExpression(
475
+ t.memberExpression(
476
+ t.memberExpression(
477
+ t.identifier(
478
+ globalFunctionsUnder7Characters.find(name => !program.scope.hasOwnBinding(name))
479
+ ),
480
+ t.identifier("call")
481
+ ),
482
+ t.identifier("bind")
483
+ ),
484
+ [
485
+ t.memberExpression(
486
+ t.memberExpression(t.identifier("Object"), t.identifier("prototype")),
487
+ t.identifier("hasOwnProperty")
488
+ )
489
+ ]
490
+ )
491
+ )
492
+ ])
493
+ )
494
+ consoleMethodsReferenced.size &&
495
+ mainFunction.body.body.unshift(
496
+ t.variableDeclaration(
497
+ "let",
498
+ [...consoleMethodsReferenced].map(name =>
499
+ t.variableDeclarator(
500
+ t.identifier(`_${uniqueId}_CONSOLE_METHOD_${name}_`),
501
+ t.arrowFunctionExpression(
502
+ [t.restElement(t.identifier("args"))],
503
+ t.unaryExpression(
504
+ "void",
505
+ t.callExpression(t.identifier(`$${uniqueId}$DEBUG$`), [t.identifier("args")])
506
+ )
507
+ )
508
+ )
509
+ )
510
+ )
511
+ )
512
+ neededDbMethodLets.size &&
513
+ mainFunction.body.body.unshift(
514
+ t.variableDeclaration(
515
+ "let",
516
+ [...neededDbMethodLets].map(name => {
517
+ const getArgs = () =>
518
+ "ObjectId" == name
519
+ ? []
520
+ : "i" == name || "r" == name
521
+ ? [t.identifier("a")]
522
+ : [t.identifier("a"), t.identifier("b")]
523
+ return t.variableDeclarator(
524
+ t.identifier(`_${uniqueId}_CONSOLE_METHOD_${name}_`),
525
+ t.arrowFunctionExpression(
526
+ getArgs(),
527
+ t.callExpression(t.identifier(`$${uniqueId}$DB$${name}$`), getArgs())
528
+ )
529
+ )
530
+ })
531
+ )
532
+ )
533
+ needDebugLet &&
534
+ mainFunction.body.body.unshift(
535
+ t.variableDeclaration("let", [
536
+ t.variableDeclarator(
537
+ t.identifier(`_${uniqueId}_DEBUG_`),
538
+ t.callExpression(t.identifier(`$${uniqueId}$DEBUG$`), [t.identifier("a")])
539
+ )
540
+ ])
541
+ )
542
+ neededSubscriptLets.size &&
543
+ mainFunction.body.body.unshift(
544
+ t.variableDeclaration(
545
+ "let",
546
+ [...neededSubscriptLets].map(([name, seclevel]) =>
547
+ t.variableDeclarator(
548
+ t.identifier(`_${uniqueId}_SUBSCRIPT_${name}_`),
549
+ t.arrowFunctionExpression(
550
+ [t.restElement(t.identifier("args"))],
551
+ t.callExpression(t.identifier(`$${uniqueId}$${seclevel}$SUBSCRIPT$${name}$`), [
552
+ t.spreadElement(t.identifier("args"))
553
+ ])
554
+ )
555
+ )
556
+ )
557
+ )
558
+ )
559
+ needG &&
560
+ mainFunction.body.body.unshift(
561
+ t.variableDeclaration("let", [
562
+ t.variableDeclarator(t.identifier(`_${uniqueId}_G_`), t.identifier(`$${uniqueId}$GLOBAL$`))
563
+ ])
564
+ )
565
+ const replaceAllThisWith = (node, scope, thisId) => {
566
+ let thisIsReferenced = !1
567
+ traverse(
568
+ node,
569
+ {
570
+ ThisExpression(path) {
571
+ thisIsReferenced = !0
572
+ path.replaceWith(t.identifier(thisId))
573
+ },
574
+ Function(path) {
575
+ "ArrowFunctionExpression" != path.node.type && path.skip()
576
+ }
577
+ },
578
+ scope
579
+ )
580
+ return thisIsReferenced
581
+ },
582
+ replaceThisInObjectLikeDefinition = path => {
583
+ const { node: object, scope, parent } = path,
584
+ evenMoreUniqueId = Math.floor(Math.random() * 2 ** 52)
585
+ .toString(36)
586
+ .padStart(11, "0"),
587
+ reuseDeclaredName =
588
+ "VariableDeclarator" == parent.type &&
589
+ "VariableDeclaration" == path.parentPath?.parentPath?.node?.type &&
590
+ "const" == path.parentPath?.parentPath?.node?.kind &&
591
+ "Identifier" == parent.id.type,
592
+ thisId = reuseDeclaredName ? parent.id.name : `_${evenMoreUniqueId}_THIS_`
593
+ let thisIsReferenced = !1
594
+ if ("ObjectExpression" == object.type)
595
+ for (const property of object.properties)
596
+ if ("ObjectMethod" != property.type) {
597
+ if ("ObjectProperty" == property.type && "FunctionExpression" == property.value.type) {
598
+ for (const param of property.value.params)
599
+ thisIsReferenced ||= replaceAllThisWith(param, scope, thisId)
600
+ thisIsReferenced ||= replaceAllThisWith(property.value.body, scope, thisId)
601
+ }
602
+ } else thisIsReferenced ||= replaceAllThisWith(property, scope, thisId)
603
+ else
604
+ for (const element of object.elements)
605
+ null != element && (thisIsReferenced ||= replaceAllThisWith(element, scope, thisId))
606
+ if (!thisIsReferenced) return
607
+ if (reuseDeclaredName) return
608
+ path.replaceWith(t.assignmentExpression("=", t.identifier(thisId), object))
609
+ const parentBlock = (path => {
610
+ let someBlock = null,
611
+ currentParent = path
612
+ for (; currentParent && currentParent && currentParent.node; ) {
613
+ if (t.isBlock(currentParent.node)) {
614
+ someBlock = currentParent.node
615
+ break
616
+ }
617
+ if (t.isArrowFunctionExpression(currentParent.parentPath?.node)) {
618
+ currentParent.replaceWith(t.blockStatement([t.returnStatement(currentParent.node)]))
619
+ someBlock = currentParent.node
620
+ break
621
+ }
622
+ currentParent = currentParent.parentPath
623
+ }
624
+ assert(null != someBlock, "src/processScript/transform.ts:710:29")
625
+ return someBlock
626
+ })(path)
627
+ parentBlock.body.unshift(t.variableDeclaration("let", [t.variableDeclarator(t.identifier(thisId), null)]))
628
+ }
629
+ traverse(file, {
630
+ BlockStatement({ node: blockStatement }) {
631
+ for (const [index, functionDeclaration] of blockStatement.body.entries())
632
+ if ("FunctionDeclaration" == functionDeclaration.type && !functionDeclaration.generator) {
633
+ blockStatement.body.splice(index, 1)
634
+ blockStatement.body.unshift(
635
+ t.variableDeclaration("let", [
636
+ t.variableDeclarator(
637
+ functionDeclaration.id,
638
+ t.arrowFunctionExpression(
639
+ functionDeclaration.params,
640
+ functionDeclaration.body,
641
+ functionDeclaration.async
642
+ )
643
+ )
644
+ ])
645
+ )
646
+ }
647
+ },
648
+ ObjectExpression(path) {
649
+ replaceThisInObjectLikeDefinition(path)
650
+ },
651
+ ArrayExpression(path) {
652
+ replaceThisInObjectLikeDefinition(path)
653
+ },
654
+ ClassBody({ node: classBody, scope, parent }) {
655
+ assert(t.isClass(parent), "src/processScript/transform.ts:824:30")
656
+ let thisIsReferenced = !1
657
+ for (const classMethod of classBody.body) {
658
+ if ("ClassMethod" != classMethod.type) continue
659
+ let methodReferencesThis = !1
660
+ for (const param of classMethod.params)
661
+ traverse(
662
+ param,
663
+ {
664
+ ThisExpression(path) {
665
+ path.replaceWith(
666
+ t.callExpression(t.memberExpression(t.super(), t.identifier("valueOf")), [])
667
+ )
668
+ }
669
+ },
670
+ scope
671
+ )
672
+ traverse(
673
+ classMethod.body,
674
+ {
675
+ ThisExpression(path) {
676
+ methodReferencesThis = !0
677
+ thisIsReferenced = !0
678
+ path.replaceWith(t.identifier(`_${uniqueId}_THIS_`))
679
+ },
680
+ Function(path) {
681
+ "ArrowFunctionExpression" != path.node.type && path.skip()
682
+ }
683
+ },
684
+ scope
685
+ )
686
+ if (methodReferencesThis) {
687
+ if ("constructor" == classMethod.kind) {
688
+ const superCalls = []
689
+ traverse(
690
+ classMethod.body,
691
+ {
692
+ CallExpression(path) {
693
+ "Super" == path.node.callee.type && superCalls.push(path)
694
+ }
695
+ },
696
+ scope
697
+ )
698
+ if (superCalls.length)
699
+ if (
700
+ 1 == superCalls.length &&
701
+ "ExpressionStatement" == superCalls[0].parent.type &&
702
+ superCalls[0].parentPath.parentPath.parent == classMethod
703
+ )
704
+ superCalls[0].parentPath.replaceWith(
705
+ t.variableDeclaration("let", [
706
+ t.variableDeclarator(t.identifier(`_${uniqueId}_THIS_`), superCalls[0].node)
707
+ ])
708
+ )
709
+ else {
710
+ for (const path of superCalls)
711
+ path.replaceWith(
712
+ t.assignmentExpression("=", t.identifier(`_${uniqueId}_THIS_`), path.node)
713
+ )
714
+ classMethod.body.body.unshift(
715
+ t.variableDeclaration("let", [
716
+ t.variableDeclarator(t.identifier(`_${uniqueId}_THIS_`))
717
+ ])
718
+ )
719
+ }
720
+ else
721
+ classMethod.body.body.unshift(
722
+ t.variableDeclaration("let", [
723
+ t.variableDeclarator(
724
+ t.identifier(`_${uniqueId}_THIS_`),
725
+ t.callExpression(t.super(), [])
726
+ )
727
+ ])
728
+ )
729
+ continue
730
+ }
731
+ classMethod.body.body.unshift(
732
+ t.variableDeclaration("let", [
733
+ t.variableDeclarator(
734
+ t.identifier(`_${uniqueId}_THIS_`),
735
+ t.callExpression(t.memberExpression(t.super(), t.identifier("valueOf")), [])
736
+ )
737
+ ])
738
+ )
739
+ }
740
+ }
741
+ !parent.superClass && thisIsReferenced && (parent.superClass = t.identifier("Object"))
742
+ },
743
+ VariableDeclaration({ node: variableDeclaration }) {
744
+ "const" == variableDeclaration.kind && (variableDeclaration.kind = "let")
745
+ },
746
+ ThisExpression: path => {
747
+ path.replaceWith(t.identifier("undefined"))
748
+ },
749
+ BigIntLiteral(path) {
750
+ const bigIntAsNumber = Number(path.node.value)
751
+ path.replaceWith(
752
+ t.callExpression(t.identifier("BigInt"), [
753
+ BigInt(bigIntAsNumber) == BigInt(path.node.value)
754
+ ? t.numericLiteral(bigIntAsNumber)
755
+ : t.stringLiteral(path.node.value)
756
+ ])
757
+ )
758
+ }
759
+ })
760
+ return { file, seclevel, warnings }
761
+ function createGetFunctionPrototypeNode() {
762
+ const name = globalFunctionsUnder7Characters.find(name => !program.scope.hasOwnBinding(name))
763
+ return t.memberExpression(
764
+ name ? t.identifier(name) : t.arrowFunctionExpression([t.identifier("_")], t.identifier("_")),
765
+ t.identifier("__proto__")
766
+ )
767
+ }
768
+ function processFakeSubscriptObject(fakeSubscriptObjectName, seclevel) {
769
+ for (const referencePath of getReferencePathsToGlobal(fakeSubscriptObjectName, program)) {
770
+ assert("MemberExpression" == referencePath.parent.type, "src/processScript/transform.ts:947:60")
771
+ assert("Identifier" == referencePath.parent.property.type)
772
+ assert(
773
+ "MemberExpression" == referencePath.parentPath.parentPath?.node.type,
774
+ "src/processScript/transform.ts:949:81"
775
+ )
776
+ assert(
777
+ "Identifier" == referencePath.parentPath.parentPath.node.property.type,
778
+ "src/processScript/transform.ts:950:83"
779
+ )
780
+ assert(
781
+ /^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parent.property.name),
782
+ `src/processScript/transform.ts:954:8 invalid user "${referencePath.parent.property.name}" in subscript`
783
+ )
784
+ assert(
785
+ /^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parentPath.parentPath.node.property.name),
786
+ `src/processScript/transform.ts:959:8 invalid script name "${referencePath.parentPath.parentPath.node.property.name}" in subscript`
787
+ )
788
+ if ("CallExpression" == referencePath.parentPath.parentPath.parentPath?.type)
789
+ referencePath.parentPath.parentPath.replaceWith(
790
+ t.identifier(
791
+ `$${uniqueId}$${seclevel}$SUBSCRIPT$${referencePath.parent.property.name}$${referencePath.parentPath.parentPath.node.property.name}$`
792
+ )
793
+ )
794
+ else {
795
+ const name = `${referencePath.parent.property.name}$${referencePath.parentPath.parentPath.node.property.name}`
796
+ referencePath.parentPath.parentPath.replaceWith(t.identifier(`_${uniqueId}_SUBSCRIPT_${name}_`))
797
+ const maxSecLevel = Math.max(neededSubscriptLets.get(name) || 0, seclevel)
798
+ neededSubscriptLets.set(name, maxSecLevel)
799
+ }
800
+ }
801
+ }
802
+ }
803
+ export { transform }