hackmud-script-manager 0.19.1-98e81f8 → 0.19.1-b040eb5

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.
@@ -20,406 +20,322 @@ const { default: traverse } = babelTraverse,
20
20
  "String",
21
21
  "Symbol",
22
22
  "BigInt"
23
- ],
24
- transform = (
25
- file,
26
- sourceCode,
27
- { uniqueID = "00000000000", scriptUser = "UNKNOWN", scriptName = "UNKNOWN", seclevel = 4 } = {}
28
- ) => {
29
- const topFunctionName = `_${uniqueID}_SCRIPT_`,
30
- exports = new Map(),
31
- liveExports = new Map()
32
- let program
33
- traverse(file, {
34
- Program(path) {
35
- program = path
36
- path.skip()
37
- }
38
- })
39
- if (program.scope.hasGlobal("_SOURCE"))
40
- for (const referencePath of getReferencePathsToGlobal("_SOURCE", program))
41
- referencePath.replaceWith(t.stringLiteral(sourceCode))
42
- if (program.scope.hasGlobal("_BUILD_DATE"))
43
- for (const referencePath of getReferencePathsToGlobal("_BUILD_DATE", program))
44
- referencePath.replaceWith(t.numericLiteral(Date.now()))
45
- if (program.scope.hasGlobal("_SCRIPT_USER"))
46
- for (const referencePath of getReferencePathsToGlobal("_SCRIPT_USER", program))
47
- 1 == scriptUser ?
48
- referencePath.replaceWith(t.stringLiteral(`$${uniqueID}$SCRIPT_USER$`))
49
- : referencePath.replaceWith(t.stringLiteral(scriptUser))
50
- if (program.scope.hasGlobal("_SCRIPT_NAME"))
51
- for (const referencePath of getReferencePathsToGlobal("_SCRIPT_NAME", program))
52
- 1 == scriptName ?
53
- referencePath.replaceWith(t.stringLiteral(`$${uniqueID}$SCRIPT_NAME$`))
54
- : referencePath.replaceWith(t.stringLiteral(scriptName))
55
- if (program.scope.hasGlobal("_FULL_SCRIPT_NAME"))
56
- for (const referencePath of getReferencePathsToGlobal("_FULL_SCRIPT_NAME", program))
57
- 1 == scriptUser || 1 == scriptName ?
58
- referencePath.replaceWith(t.stringLiteral(`$${uniqueID}$FULL_SCRIPT_NAME$`))
59
- : referencePath.replaceWith(t.stringLiteral(`${scriptUser}.${scriptName}`))
60
- let functionDotPrototypeIsReferencedMultipleTimes = !1
61
- const createGetFunctionPrototypeNode = () => {
62
- for (const globalFunction of globalFunctionsUnder7Characters)
63
- if (!program.scope.hasOwnBinding(globalFunction))
64
- return t.memberExpression(
65
- t.memberExpression(t.identifier(globalFunction), t.identifier("constructor")),
66
- t.identifier("prototype")
23
+ ]
24
+ function transform(file, sourceCode, { uniqueID = "00000000000", scriptUser, scriptName, seclevel = 4 }) {
25
+ const topFunctionName = `_${uniqueID}_SCRIPT_`,
26
+ exports = new Map(),
27
+ liveExports = new Map()
28
+ let program
29
+ traverse(file, {
30
+ Program(path) {
31
+ program = path
32
+ path.skip()
33
+ }
34
+ })
35
+ if (program.scope.hasGlobal("_SOURCE"))
36
+ for (const referencePath of getReferencePathsToGlobal("_SOURCE", program))
37
+ referencePath.replaceWith(t.stringLiteral(sourceCode))
38
+ if (program.scope.hasGlobal("_BUILD_DATE"))
39
+ for (const referencePath of getReferencePathsToGlobal("_BUILD_DATE", program))
40
+ referencePath.replaceWith(t.numericLiteral(Date.now()))
41
+ let uniqueIdScriptUserNeeded = !1
42
+ if (program.scope.hasGlobal("_SCRIPT_USER"))
43
+ for (const referencePath of getReferencePathsToGlobal("_SCRIPT_USER", program))
44
+ if (null == scriptUser) {
45
+ uniqueIdScriptUserNeeded = !0
46
+ referencePath.replaceWith(t.identifier(`_${uniqueID}_SCRIPT_USER_`))
47
+ } else
48
+ referencePath.replaceWith(t.stringLiteral(1 == scriptUser ? `$${uniqueID}$SCRIPT_USER$` : scriptUser))
49
+ if (program.scope.hasGlobal("_SCRIPT_NAME"))
50
+ for (const referencePath of getReferencePathsToGlobal("_SCRIPT_NAME", program))
51
+ referencePath.replaceWith(t.stringLiteral(1 == scriptName ? `$${uniqueID}$SCRIPT_NAME$` : scriptName))
52
+ if (program.scope.hasGlobal("_FULL_SCRIPT_NAME"))
53
+ for (const referencePath of getReferencePathsToGlobal("_FULL_SCRIPT_NAME", program))
54
+ if (1 == scriptUser || 1 == scriptName)
55
+ referencePath.replaceWith(t.stringLiteral(`$${uniqueID}$FULL_SCRIPT_NAME$`))
56
+ else if (null == scriptUser) {
57
+ uniqueIdScriptUserNeeded = !0
58
+ referencePath.replaceWith(
59
+ t.binaryExpression(
60
+ "+",
61
+ t.identifier(`_${uniqueID}_SCRIPT_USER_`),
62
+ t.stringLiteral("." + scriptName)
67
63
  )
68
- return t.memberExpression(
69
- t.memberExpression(
70
- t.arrowFunctionExpression([t.identifier("_")], t.identifier("_")),
71
- t.identifier("constructor")
72
- ),
73
- t.identifier("prototype")
64
+ )
65
+ } else referencePath.replaceWith(t.stringLiteral(`${scriptUser}.${scriptName}`))
66
+ let functionDotPrototypeIsReferencedMultipleTimes = !1
67
+ if (program.scope.hasGlobal("Function")) {
68
+ const FunctionReferencePaths = getReferencePathsToGlobal("Function", program)
69
+ if (1 == FunctionReferencePaths.length) {
70
+ const referencePath = FunctionReferencePaths[0]
71
+ assert(
72
+ "MemberExpression" == referencePath.parent.type,
73
+ "src/processScript/transform.ts:105:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
74
74
  )
75
- }
76
- if (program.scope.hasGlobal("Function")) {
77
- const FunctionReferencePaths = getReferencePathsToGlobal("Function", program)
78
- if (1 == FunctionReferencePaths.length) {
79
- const referencePath = FunctionReferencePaths[0]
75
+ assert(
76
+ "Identifier" == referencePath.parent.property.type,
77
+ "src/processScript/transform.ts:110:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
78
+ )
79
+ assert(
80
+ "prototype" == referencePath.parent.property.name,
81
+ "src/processScript/transform.ts:115:8 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
82
+ )
83
+ referencePath.parentPath.replaceWith(createGetFunctionPrototypeNode())
84
+ } else {
85
+ for (const referencePath of FunctionReferencePaths) {
80
86
  assert(
81
87
  "MemberExpression" == referencePath.parent.type,
82
- "`Function` isn't available in hackmud, only `Function.prototype` is accessible"
88
+ "src/processScript/transform.ts:123:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
83
89
  )
84
90
  assert(
85
91
  "Identifier" == referencePath.parent.property.type,
86
- "`Function` isn't available in hackmud, only `Function.prototype` is accessible"
92
+ "src/processScript/transform.ts:128:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
87
93
  )
88
94
  assert(
89
95
  "prototype" == referencePath.parent.property.name,
90
- "`Function` isn't available in hackmud, only `Function.prototype` is accessible"
96
+ "src/processScript/transform.ts:133:9 `Function` isn't available in hackmud, only `Function.prototype` is accessible"
91
97
  )
92
- referencePath.parentPath.replaceWith(createGetFunctionPrototypeNode())
93
- } else {
94
- for (const referencePath of FunctionReferencePaths) {
95
- assert(
96
- "MemberExpression" == referencePath.parent.type,
97
- "`Function` isn't available in hackmud, only `Function.prototype` is accessible"
98
- )
99
- assert(
100
- "Identifier" == referencePath.parent.property.type,
101
- "`Function` isn't available in hackmud, only `Function.prototype` is accessible"
102
- )
103
- assert(
104
- "prototype" == referencePath.parent.property.name,
105
- "`Function` isn't available in hackmud, only `Function.prototype` is accessible"
106
- )
107
- functionDotPrototypeIsReferencedMultipleTimes = !0
108
- referencePath.parentPath.replaceWith(t.identifier(`_${uniqueID}_FUNCTION_DOT_PROTOTYPE_`))
109
- }
110
98
  functionDotPrototypeIsReferencedMultipleTimes = !0
99
+ referencePath.parentPath.replaceWith(t.identifier(`_${uniqueID}_FUNCTION_DOT_PROTOTYPE_`))
111
100
  }
101
+ functionDotPrototypeIsReferencedMultipleTimes = !0
112
102
  }
113
- let detectedSeclevel = 4
114
- const processFakeSubscriptObject = fakeSubscriptObjectName => {
115
- for (const referencePath of getReferencePathsToGlobal(fakeSubscriptObjectName, program)) {
116
- assert("MemberExpression" == referencePath.parent.type)
117
- assert("Identifier" == referencePath.parent.property.type)
118
- assert("MemberExpression" == referencePath.parentPath.parentPath?.node.type)
119
- assert("Identifier" == referencePath.parentPath.parentPath.node.property.type)
120
- assert(
121
- /^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parent.property.name),
122
- `invalid user "${referencePath.parent.property.name}" in subscript`
123
- )
124
- assert(
125
- /^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parentPath.parentPath.node.property.name),
126
- `invalid script name "${referencePath.parentPath.parentPath.node.property.name}" in subscript`
103
+ }
104
+ const neededSubscriptLets = new Set()
105
+ let detectedSeclevel = 4
106
+ for (const fakeSubscriptObjectName of ["$fs", "$4s", "$s"])
107
+ program.scope.hasGlobal(fakeSubscriptObjectName) && processFakeSubscriptObject(fakeSubscriptObjectName)
108
+ for (const fakeSubscriptObjectName of ["$hs", "$3s"])
109
+ if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
110
+ detectedSeclevel = 3
111
+ processFakeSubscriptObject(fakeSubscriptObjectName)
112
+ }
113
+ for (const fakeSubscriptObjectName of ["$ms", "$2s"])
114
+ if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
115
+ detectedSeclevel = 2
116
+ processFakeSubscriptObject(fakeSubscriptObjectName)
117
+ }
118
+ for (const fakeSubscriptObjectName of ["$ls", "$1s"])
119
+ if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
120
+ detectedSeclevel = 1
121
+ processFakeSubscriptObject(fakeSubscriptObjectName)
122
+ }
123
+ for (const fakeSubscriptObjectName of ["$ns", "$0s"])
124
+ if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
125
+ detectedSeclevel = 0
126
+ processFakeSubscriptObject(fakeSubscriptObjectName)
127
+ }
128
+ seclevel = Math.min(seclevel, detectedSeclevel)
129
+ const neededDbMethodLets = new Set()
130
+ if (program.scope.hasGlobal("$db"))
131
+ for (const referencePath of getReferencePathsToGlobal("$db", program)) {
132
+ assert("MemberExpression" == referencePath.parentPath.node.type, "src/processScript/transform.ts:187:69")
133
+ assert("Identifier" == referencePath.parentPath.node.property.type, "src/processScript/transform.ts:188:72")
134
+ const databaseOpMethodName = referencePath.parentPath.node.property.name
135
+ assert(
136
+ validDBMethods.includes(databaseOpMethodName),
137
+ `src/processScript/transform.ts:194:8 invalid db method "${databaseOpMethodName}", valid db methods are "${validDBMethods.join('", "')}"`
138
+ )
139
+ if ("CallExpression" == referencePath.parentPath.parentPath?.type)
140
+ referencePath.parentPath.replaceWith(t.identifier(`$${uniqueID}$DB$${databaseOpMethodName}$`))
141
+ else {
142
+ referencePath.parentPath.replaceWith(
143
+ t.identifier(`_${uniqueID}_CONSOLE_METHOD_${databaseOpMethodName}_`)
127
144
  )
128
- "CallExpression" == referencePath.parentPath.parentPath.parentPath?.type ?
129
- referencePath.parentPath.parentPath.replaceWith(
130
- t.identifier(
131
- `$${uniqueID}$SUBSCRIPT$${referencePath.parent.property.name}$${referencePath.parentPath.parentPath.node.property.name}$`
132
- )
133
- )
134
- : referencePath.parentPath.parentPath.replaceWith(
135
- t.arrowFunctionExpression(
136
- [t.restElement(t.identifier("args"))],
137
- t.callExpression(
138
- t.identifier(
139
- `$${uniqueID}$SUBSCRIPT$${referencePath.parent.property.name}$${referencePath.parentPath.parentPath.node.property.name}$`
140
- ),
141
- [t.spreadElement(t.identifier("args"))]
142
- )
143
- )
144
- )
145
+ neededDbMethodLets.add(databaseOpMethodName)
145
146
  }
146
147
  }
147
- for (const fakeSubscriptObjectName of ["$fs", "$4s", "$s"])
148
- program.scope.hasGlobal(fakeSubscriptObjectName) && processFakeSubscriptObject(fakeSubscriptObjectName)
149
- for (const fakeSubscriptObjectName of ["$hs", "$3s"])
150
- if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
151
- detectedSeclevel = 3
152
- processFakeSubscriptObject(fakeSubscriptObjectName)
153
- }
154
- for (const fakeSubscriptObjectName of ["$ms", "$2s"])
155
- if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
156
- detectedSeclevel = 2
157
- processFakeSubscriptObject(fakeSubscriptObjectName)
158
- }
159
- for (const fakeSubscriptObjectName of ["$ls", "$1s"])
160
- if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
161
- detectedSeclevel = 1
162
- processFakeSubscriptObject(fakeSubscriptObjectName)
148
+ let needDebugLet = !1
149
+ if (program.scope.hasGlobal("$D"))
150
+ for (const referencePath of getReferencePathsToGlobal("$D", program))
151
+ if ("CallExpression" == referencePath.parentPath.type)
152
+ referencePath.replaceWith(t.identifier(`$${uniqueID}$DEBUG$`))
153
+ else {
154
+ referencePath.replaceWith(t.identifier(`_${uniqueID}_DEBUG_`))
155
+ needDebugLet = !0
163
156
  }
164
- for (const fakeSubscriptObjectName of ["$ns", "$0s"])
165
- if (program.scope.hasGlobal(fakeSubscriptObjectName)) {
166
- detectedSeclevel = 0
167
- processFakeSubscriptObject(fakeSubscriptObjectName)
157
+ if (program.scope.hasGlobal("$FMCL"))
158
+ for (const referencePath of getReferencePathsToGlobal("$FMCL", program))
159
+ referencePath.replaceWith(t.identifier(`$${uniqueID}$FMCL$`))
160
+ if (program.scope.hasGlobal("$G"))
161
+ for (const referencePath of getReferencePathsToGlobal("$G", program))
162
+ referencePath.replaceWith(t.identifier(`$${uniqueID}$GLOBAL$`))
163
+ if (program.scope.hasGlobal("_SECLEVEL"))
164
+ for (const referencePath of getReferencePathsToGlobal("_SECLEVEL", program))
165
+ referencePath.replaceWith(t.numericLiteral(seclevel))
166
+ let needGetPrototypeOf = !1
167
+ if (program.scope.hasGlobal("Object"))
168
+ for (const referencePath of getReferencePathsToGlobal("Object", program))
169
+ if ("MemberExpression" == referencePath.parent.type && !referencePath.parent.computed) {
170
+ assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:241:64")
171
+ if ("getPrototypeOf" == referencePath.parent.property.name) {
172
+ referencePath.parentPath.replaceWith(t.identifier(`_${uniqueID}_GET_PROTOTYPE_OF_`))
173
+ needGetPrototypeOf = !0
174
+ }
168
175
  }
169
- seclevel = Math.min(seclevel, detectedSeclevel)
170
- if (program.scope.hasGlobal("$db"))
171
- for (const referencePath of getReferencePathsToGlobal("$db", program)) {
172
- assert("MemberExpression" == referencePath.parentPath.node.type)
173
- assert("Identifier" == referencePath.parentPath.node.property.type)
174
- const databaseOpMethodName = referencePath.parentPath.node.property.name
175
- assert(
176
- validDBMethods.includes(databaseOpMethodName),
177
- `invalid db method "${databaseOpMethodName}", valid db methods are "${validDBMethods.join('", "')}"`
176
+ const consoleMethodsReferenced = new Set()
177
+ if (program.scope.hasGlobal("console"))
178
+ for (const referencePath of getReferencePathsToGlobal("console", program))
179
+ if ("MemberExpression" == referencePath.parent.type && !referencePath.parent.computed) {
180
+ assert("Identifier" == referencePath.parent.property.type, "src/processScript/transform.ts:256:64")
181
+ referencePath.parentPath.replaceWith(
182
+ t.identifier(`_${uniqueID}_CONSOLE_METHOD_${referencePath.parent.property.name}_`)
178
183
  )
179
- "CallExpression" == referencePath.parentPath.parentPath?.type ?
180
- referencePath.parentPath.replaceWith(t.identifier(`$${uniqueID}$DB$${databaseOpMethodName}$`))
181
- : "ObjectId" == databaseOpMethodName ?
182
- referencePath.parentPath.replaceWith(
183
- t.arrowFunctionExpression(
184
- [],
185
- t.callExpression(t.identifier(`$${uniqueID}$DB$${databaseOpMethodName}$`), [])
186
- )
187
- )
188
- : "i" == databaseOpMethodName || "r" == databaseOpMethodName ?
189
- referencePath.parentPath.replaceWith(
190
- t.arrowFunctionExpression(
191
- [t.identifier("a")],
192
- t.callExpression(t.identifier(`$${uniqueID}$DB$${databaseOpMethodName}$`), [
193
- t.identifier("a")
194
- ])
195
- )
196
- )
197
- : referencePath.parentPath.replaceWith(
198
- t.arrowFunctionExpression(
199
- [t.identifier("a"), t.identifier("b")],
200
- t.callExpression(t.identifier(`$${uniqueID}$DB$${databaseOpMethodName}$`), [
201
- t.identifier("a"),
202
- t.identifier("b")
203
- ])
204
- )
205
- )
206
- }
207
- if (program.scope.hasGlobal("$D"))
208
- for (const referencePath of getReferencePathsToGlobal("$D", program))
209
- "CallExpression" == referencePath.parentPath.type ?
210
- referencePath.replaceWith(t.identifier(`$${uniqueID}$DEBUG$`))
211
- : referencePath.replaceWith(
212
- t.arrowFunctionExpression(
213
- [t.identifier("a")],
214
- t.callExpression(t.identifier(`$${uniqueID}$DEBUG$`), [t.identifier("a")])
215
- )
216
- )
217
- if (program.scope.hasGlobal("$FMCL"))
218
- for (const referencePath of getReferencePathsToGlobal("$FMCL", program))
219
- referencePath.replaceWith(t.identifier(`$${uniqueID}$FMCL$`))
220
- if (program.scope.hasGlobal("$G"))
221
- for (const referencePath of getReferencePathsToGlobal("$G", program))
222
- referencePath.replaceWith(t.identifier(`$${uniqueID}$GLOBAL$`))
223
- if (program.scope.hasGlobal("_SECLEVEL"))
224
- for (const referencePath of getReferencePathsToGlobal("_SECLEVEL", program))
225
- referencePath.replaceWith(t.numericLiteral(seclevel))
226
- let needGetPrototypeOf = !1,
227
- needSetPrototypeOf = !1
228
- if (program.scope.hasGlobal("Object"))
229
- for (const referencePath of getReferencePathsToGlobal("Object", program))
230
- if ("MemberExpression" == referencePath.parent.type && !referencePath.parent.computed) {
231
- assert("Identifier" == referencePath.parent.property.type)
232
- if ("getPrototypeOf" == referencePath.parent.property.name) {
233
- referencePath.parentPath.replaceWith(t.identifier(`_${uniqueID}_GET_PROTOTYPE_OF_`))
234
- needGetPrototypeOf = !0
235
- } else if ("setPrototypeOf" == referencePath.parent.property.name) {
236
- referencePath.parentPath.replaceWith(t.identifier(`_${uniqueID}_SET_PROTOTYPE_OF_`))
237
- needSetPrototypeOf = !0
238
- }
239
- }
240
- const lastStatement = program.node.body.at(-1)
241
- let exportDefaultName
242
- assert(lastStatement, "program is empty")
243
- if ("ExportNamedDeclaration" == lastStatement.type) {
244
- program.node.body.pop()
245
- for (const specifier of lastStatement.specifiers) {
246
- assert("ExportSpecifier" == specifier.type, specifier.type + " is currently unsupported")
247
- const exportedName =
248
- "Identifier" == specifier.exported.type ? specifier.exported.name : specifier.exported.value
249
- "default" == exportedName ?
250
- (exportDefaultName = specifier.local.name)
251
- : exports.set(specifier.local.name, exportedName)
184
+ consoleMethodsReferenced.add(referencePath.parent.property.name)
252
185
  }
186
+ const lastStatement = program.node.body.at(-1)
187
+ let exportDefaultName
188
+ assert(lastStatement, "src/processScript/transform.ts:270:27 program is empty")
189
+ if ("ExportNamedDeclaration" == lastStatement.type) {
190
+ program.node.body.pop()
191
+ for (const specifier of lastStatement.specifiers) {
192
+ assert(
193
+ "ExportSpecifier" == specifier.type,
194
+ `src/processScript/transform.ts:276:51 ${specifier.type} is currently unsupported`
195
+ )
196
+ const exportedName =
197
+ "Identifier" == specifier.exported.type ? specifier.exported.name : specifier.exported.value
198
+ "default" == exportedName ?
199
+ (exportDefaultName = specifier.local.name)
200
+ : exports.set(specifier.local.name, exportedName)
253
201
  }
254
- const globalBlock = t.blockStatement([])
255
- let mainFunction
256
- for (const statement of program.node.body)
257
- if ("VariableDeclaration" == statement.type)
258
- for (const declarator of statement.declarations)
259
- if (
260
- "Identifier" != declarator.id.type ||
261
- declarator.id.name != exportDefaultName ||
262
- !declarator.init ||
263
- ("FunctionExpression" != declarator.init.type &&
264
- "ArrowFunctionExpression" != declarator.init.type) ||
265
- declarator.init.async ||
266
- declarator.init.generator
267
- ) {
268
- for (const identifierName in t.getBindingIdentifiers(declarator.id)) {
269
- identifierName == exportDefaultName &&
270
- (mainFunction = t.functionDeclaration(
271
- t.identifier(topFunctionName),
272
- [t.identifier("context"), t.identifier("args")],
273
- t.blockStatement([
274
- t.returnStatement(t.callExpression(t.identifier(exportDefaultName), []))
275
- ])
276
- ))
277
- if ("const" != statement.kind && exports.has(identifierName)) {
278
- liveExports.set(identifierName, exports.get(identifierName))
279
- exports.delete(identifierName)
280
- }
281
- globalBlock.body.push(
282
- t.variableDeclaration("let", [t.variableDeclarator(t.identifier(identifierName))])
283
- )
202
+ }
203
+ const globalBlock = t.blockStatement([])
204
+ let mainFunction
205
+ for (const statement of program.node.body)
206
+ if ("VariableDeclaration" == statement.type)
207
+ for (const declarator of statement.declarations)
208
+ if (
209
+ "Identifier" != declarator.id.type ||
210
+ declarator.id.name != exportDefaultName ||
211
+ !declarator.init ||
212
+ ("FunctionExpression" != declarator.init.type &&
213
+ "ArrowFunctionExpression" != declarator.init.type) ||
214
+ declarator.init.async ||
215
+ declarator.init.generator
216
+ ) {
217
+ for (const identifierName in t.getBindingIdentifiers(declarator.id)) {
218
+ identifierName == exportDefaultName &&
219
+ (mainFunction = t.functionDeclaration(
220
+ t.identifier(topFunctionName),
221
+ [t.identifier("context"), t.identifier("args")],
222
+ t.blockStatement([
223
+ t.returnStatement(t.callExpression(t.identifier(exportDefaultName), []))
224
+ ])
225
+ ))
226
+ if ("const" != statement.kind && exports.has(identifierName)) {
227
+ liveExports.set(identifierName, exports.get(identifierName))
228
+ exports.delete(identifierName)
284
229
  }
285
- declarator.init &&
286
- globalBlock.body.push(
287
- t.expressionStatement(t.assignmentExpression("=", declarator.id, declarator.init))
288
- )
289
- } else
290
- mainFunction = t.functionDeclaration(
291
- t.identifier(topFunctionName),
292
- declarator.init.params,
293
- "BlockStatement" == declarator.init.body.type ?
294
- declarator.init.body
295
- : t.blockStatement([t.returnStatement(declarator.init.body)])
230
+ globalBlock.body.push(
231
+ t.variableDeclaration("let", [t.variableDeclarator(t.identifier(identifierName))])
296
232
  )
297
- else
298
- "FunctionDeclaration" == statement.type ?
299
- statement.id.name == exportDefaultName ?
300
- (mainFunction = statement)
301
- : globalBlock.body.push(
302
- t.variableDeclaration("let", [
303
- t.variableDeclarator(
304
- statement.id,
305
- t.functionExpression(
306
- void 0,
307
- statement.params,
308
- statement.body,
309
- statement.generator,
310
- statement.async
311
- )
312
- )
313
- ])
233
+ }
234
+ declarator.init &&
235
+ globalBlock.body.push(
236
+ t.expressionStatement(t.assignmentExpression("=", declarator.id, declarator.init))
314
237
  )
315
- : globalBlock.body.push(statement)
316
- mainFunction ||= t.functionDeclaration(
317
- t.identifier(topFunctionName),
318
- [t.identifier("context"), t.identifier("args")],
319
- t.blockStatement([])
320
- )
321
- program.node.body = [mainFunction]
322
- if (globalBlock.body.length) {
323
- ;(exports.size || liveExports.size) &&
324
- mainFunction.body.body.push(
325
- t.returnStatement(
326
- t.objectExpression([
327
- ...[...exports].map(([local, exported]) =>
328
- t.objectProperty(t.identifier(exported), t.identifier(local))
329
- ),
330
- ...[...liveExports].map(([local, exported]) =>
331
- t.objectMethod(
332
- "get",
333
- t.identifier(exported),
334
- [],
335
- t.blockStatement([t.returnStatement(t.identifier(local))])
238
+ } else
239
+ mainFunction = t.functionDeclaration(
240
+ t.identifier(topFunctionName),
241
+ declarator.init.params,
242
+ "BlockStatement" == declarator.init.body.type ?
243
+ declarator.init.body
244
+ : t.blockStatement([t.returnStatement(declarator.init.body)])
245
+ )
246
+ else
247
+ "FunctionDeclaration" == statement.type ?
248
+ statement.id.name == exportDefaultName ?
249
+ (mainFunction = statement)
250
+ : globalBlock.body.push(
251
+ t.variableDeclaration("let", [
252
+ t.variableDeclarator(
253
+ statement.id,
254
+ t.functionExpression(
255
+ void 0,
256
+ statement.params,
257
+ statement.body,
258
+ statement.generator,
259
+ statement.async
336
260
  )
337
261
  )
338
262
  ])
339
263
  )
264
+ : globalBlock.body.push(statement)
265
+ mainFunction ||= t.functionDeclaration(
266
+ t.identifier(topFunctionName),
267
+ [t.identifier("context"), t.identifier("args")],
268
+ t.blockStatement([])
269
+ )
270
+ if (uniqueIdScriptUserNeeded) {
271
+ const mainFunctionParams = mainFunction.params
272
+ mainFunction.params = [t.restElement(t.identifier(`_${uniqueID}_PARAMS_`))]
273
+ mainFunction.body.body.unshift(
274
+ t.variableDeclaration("let", [
275
+ t.variableDeclarator(t.arrayPattern(mainFunctionParams), t.identifier(`_${uniqueID}_PARAMS_`)),
276
+ t.variableDeclarator(
277
+ t.arrayPattern([t.identifier(`_${uniqueID}_SCRIPT_USER_`)]),
278
+ t.callExpression(
279
+ t.memberExpression(
280
+ t.memberExpression(
281
+ t.memberExpression(t.identifier(`_${uniqueID}_PARAMS_`), t.numericLiteral(0), !0),
282
+ t.identifier("this_script")
283
+ ),
284
+ t.identifier("split")
285
+ ),
286
+ [t.stringLiteral(".")]
287
+ )
340
288
  )
341
- program.scope.crawl()
342
- const globalBlockVariables = new Set()
343
- let hoistedGlobalBlockFunctions = 0
344
- for (const [globalBlockIndex, globalBlockStatement] of [...globalBlock.body.entries()].reverse())
345
- if ("VariableDeclaration" == globalBlockStatement.type) {
346
- assert(1 == globalBlockStatement.declarations.length)
347
- const declarator = globalBlockStatement.declarations[0]
348
- assert("Identifier" == declarator.id.type, `declarator.id.type was "${declarator.id.type}"`)
349
- program.scope.crawl()
350
- if (program.scope.hasGlobal(declarator.id.name)) {
351
- globalBlock.body.splice(globalBlockIndex, 1)
352
- const [globalBlockPath] = program.unshiftContainer("body", globalBlock),
353
- [globalBlockStatementPath] = program.unshiftContainer("body", globalBlockStatement)
354
- program.scope.crawl()
355
- if (
356
- !declarator.init ||
357
- ("FunctionExpression" != declarator.init.type &&
358
- "ArrowFunctionExpression" != declarator.init.type) ||
359
- Object.keys(program.scope.globals).some(global => globalBlockVariables.has(global))
360
- ) {
361
- const binding = program.scope.getBinding(declarator.id.name)
362
- assert(binding)
363
- for (const referencePath of binding.referencePaths) {
364
- assert("Identifier" == referencePath.node.type)
365
- referencePath.replaceWith(
366
- t.memberExpression(
367
- t.identifier(`$${uniqueID}$GLOBAL$`),
368
- t.identifier(referencePath.node.name)
369
- )
370
- )
371
- }
372
- for (const referencePath of binding.constantViolations)
373
- if ("AssignmentExpression" == referencePath.node.type)
374
- for (const [name, node] of Object.entries(
375
- t.getBindingIdentifiers(referencePath.node)
376
- ))
377
- if (name == declarator.id.name) {
378
- clearObject(node)
379
- Object.assign(
380
- node,
381
- t.memberExpression(
382
- t.identifier(`$${uniqueID}$GLOBAL$`),
383
- t.identifier(name)
384
- )
385
- )
386
- }
387
- globalBlockPath.remove()
388
- globalBlockStatementPath.remove()
389
- declarator.init &&
390
- globalBlock.body.splice(
391
- globalBlockIndex,
392
- 0,
393
- t.expressionStatement(
394
- t.assignmentExpression(
395
- "=",
396
- t.memberExpression(
397
- t.identifier(`$${uniqueID}$GLOBAL$`),
398
- t.identifier(declarator.id.name)
399
- ),
400
- declarator.init
401
- )
402
- )
403
- )
404
- } else {
405
- globalBlockPath.remove()
406
- globalBlockStatementPath.remove()
407
- mainFunction.body.body.unshift(globalBlockStatement)
408
- hoistedGlobalBlockFunctions++
409
- }
410
- } else globalBlockVariables.add(declarator.id.name)
411
- } else if ("ClassDeclaration" == globalBlockStatement.type) {
289
+ ])
290
+ )
291
+ }
292
+ program.node.body = [mainFunction]
293
+ if (globalBlock.body.length) {
294
+ ;(exports.size || liveExports.size) &&
295
+ mainFunction.body.body.push(
296
+ t.returnStatement(
297
+ t.objectExpression([
298
+ ...[...exports].map(([local, exported]) =>
299
+ t.objectProperty(t.identifier(exported), t.identifier(local))
300
+ ),
301
+ ...[...liveExports].map(([local, exported]) =>
302
+ t.objectMethod(
303
+ "get",
304
+ t.identifier(exported),
305
+ [],
306
+ t.blockStatement([t.returnStatement(t.identifier(local))])
307
+ )
308
+ )
309
+ ])
310
+ )
311
+ )
312
+ program.scope.crawl()
313
+ const globalBlockVariables = new Set()
314
+ let hoistedGlobalBlockFunctions = 0
315
+ for (const [globalBlockIndex, globalBlockStatement] of [...globalBlock.body.entries()].reverse())
316
+ if ("VariableDeclaration" == globalBlockStatement.type) {
317
+ assert(1 == globalBlockStatement.declarations.length, "src/processScript/transform.ts:410:59")
318
+ const declarator = globalBlockStatement.declarations[0]
319
+ assert(
320
+ "Identifier" == declarator.id.type,
321
+ `src/processScript/transform.ts:414:51 declarator.id.type was "${declarator.id.type}"`
322
+ )
323
+ program.scope.crawl()
324
+ if (program.scope.hasGlobal(declarator.id.name)) {
325
+ globalBlock.body.splice(globalBlockIndex, 1)
326
+ const [globalBlockPath] = program.unshiftContainer("body", globalBlock),
327
+ [globalBlockStatementPath] = program.unshiftContainer("body", globalBlockStatement)
412
328
  program.scope.crawl()
413
- assert(globalBlockStatement.id, "src/processScript/transform.ts:564:37")
414
- if (program.scope.hasGlobal(globalBlockStatement.id.name)) {
415
- globalBlock.body.splice(globalBlockIndex, 1)
416
- const [globalBlockPath] = program.unshiftContainer("body", globalBlock),
417
- [globalBlockStatementPath] = program.unshiftContainer("body", globalBlockStatement)
418
- program.scope.crawl()
419
- const binding = program.scope.getBinding(globalBlockStatement.id.name)
420
- assert(binding)
329
+ if (
330
+ !declarator.init ||
331
+ ("FunctionExpression" != declarator.init.type &&
332
+ "ArrowFunctionExpression" != declarator.init.type) ||
333
+ Object.keys(program.scope.globals).some(global => globalBlockVariables.has(global))
334
+ ) {
335
+ const binding = program.scope.getBinding(declarator.id.name)
336
+ assert(binding, "src/processScript/transform.ts:433:23")
421
337
  for (const referencePath of binding.referencePaths) {
422
- assert("Identifier" == referencePath.node.type)
338
+ assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:436:56")
423
339
  referencePath.replaceWith(
424
340
  t.memberExpression(
425
341
  t.identifier(`$${uniqueID}$GLOBAL$`),
@@ -427,223 +343,354 @@ const { default: traverse } = babelTraverse,
427
343
  )
428
344
  )
429
345
  }
346
+ for (const referencePath of binding.constantViolations)
347
+ if ("AssignmentExpression" == referencePath.node.type)
348
+ for (const [name, node] of Object.entries(t.getBindingIdentifiers(referencePath.node)))
349
+ if (name == declarator.id.name) {
350
+ clearObject(node)
351
+ Object.assign(
352
+ node,
353
+ t.memberExpression(t.identifier(`$${uniqueID}$GLOBAL$`), t.identifier(name))
354
+ )
355
+ }
430
356
  globalBlockPath.remove()
431
357
  globalBlockStatementPath.remove()
432
- globalBlock.body.splice(
433
- globalBlockIndex,
434
- 0,
435
- t.expressionStatement(
436
- t.assignmentExpression(
437
- "=",
438
- t.memberExpression(
439
- t.identifier(`$${uniqueID}$GLOBAL$`),
440
- t.identifier(globalBlockStatement.id.name)
441
- ),
442
- t.classExpression(
443
- void 0,
444
- globalBlockStatement.superClass,
445
- globalBlockStatement.body,
446
- globalBlockStatement.decorators
358
+ declarator.init &&
359
+ globalBlock.body.splice(
360
+ globalBlockIndex,
361
+ 0,
362
+ t.expressionStatement(
363
+ t.assignmentExpression(
364
+ "=",
365
+ t.memberExpression(
366
+ t.identifier(`$${uniqueID}$GLOBAL$`),
367
+ t.identifier(declarator.id.name)
368
+ ),
369
+ declarator.init
447
370
  )
448
371
  )
449
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:487: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:499:22")
390
+ for (const referencePath of binding.referencePaths) {
391
+ assert("Identifier" == referencePath.node.type, "src/processScript/transform.ts:502:55")
392
+ referencePath.replaceWith(
393
+ t.memberExpression(
394
+ t.identifier(`$${uniqueID}$GLOBAL$`),
395
+ t.identifier(referencePath.node.name)
396
+ )
450
397
  )
451
398
  }
452
- }
453
- if (program.scope.hasGlobal("_EXPORTS"))
454
- for (const referencePath of getReferencePathsToGlobal("_EXPORTS", program))
455
- referencePath.replaceWith(
456
- t.arrayExpression([...exports.keys(), ...liveExports.keys()].map(name => t.stringLiteral(name)))
399
+ globalBlockPath.remove()
400
+ globalBlockStatementPath.remove()
401
+ globalBlock.body.splice(
402
+ globalBlockIndex,
403
+ 0,
404
+ t.expressionStatement(
405
+ t.assignmentExpression(
406
+ "=",
407
+ t.memberExpression(
408
+ t.identifier(`$${uniqueID}$GLOBAL$`),
409
+ t.identifier(globalBlockStatement.id.name)
410
+ ),
411
+ t.classExpression(
412
+ void 0,
413
+ globalBlockStatement.superClass,
414
+ globalBlockStatement.body,
415
+ globalBlockStatement.decorators
416
+ )
417
+ )
418
+ )
457
419
  )
458
- globalBlock.body.length &&
459
- mainFunction.body.body.splice(
460
- hoistedGlobalBlockFunctions,
461
- 0,
462
- t.ifStatement(t.unaryExpression("!", t.identifier(`$${uniqueID}$FMCL$`)), globalBlock)
420
+ }
421
+ }
422
+ if (program.scope.hasGlobal("_EXPORTS"))
423
+ for (const referencePath of getReferencePathsToGlobal("_EXPORTS", program))
424
+ referencePath.replaceWith(
425
+ t.arrayExpression([...exports.keys(), ...liveExports.keys()].map(name => t.stringLiteral(name)))
463
426
  )
464
- }
465
- functionDotPrototypeIsReferencedMultipleTimes &&
466
- mainFunction.body.body.unshift(
467
- t.variableDeclaration("let", [
468
- t.variableDeclarator(
469
- t.identifier(`_${uniqueID}_FUNCTION_DOT_PROTOTYPE_`),
470
- createGetFunctionPrototypeNode()
471
- )
472
- ])
427
+ globalBlock.body.length &&
428
+ mainFunction.body.body.splice(
429
+ hoistedGlobalBlockFunctions,
430
+ 0,
431
+ t.ifStatement(t.unaryExpression("!", t.identifier(`$${uniqueID}$FMCL$`)), globalBlock)
473
432
  )
474
- needSetPrototypeOf &&
475
- mainFunction.body.body.unshift(
476
- t.variableDeclaration("let", [
477
- t.variableDeclarator(
478
- t.identifier(`_${uniqueID}_SET_PROTOTYPE_OF_`),
479
- t.callExpression(
433
+ }
434
+ functionDotPrototypeIsReferencedMultipleTimes &&
435
+ mainFunction.body.body.unshift(
436
+ t.variableDeclaration("let", [
437
+ t.variableDeclarator(
438
+ t.identifier(`_${uniqueID}_FUNCTION_DOT_PROTOTYPE_`),
439
+ createGetFunctionPrototypeNode()
440
+ )
441
+ ])
442
+ )
443
+ needGetPrototypeOf &&
444
+ mainFunction.body.body.unshift(
445
+ t.variableDeclaration("let", [
446
+ t.variableDeclarator(
447
+ t.objectPattern([
448
+ t.objectProperty(t.identifier("get"), t.identifier(`_${uniqueID}_DUNDER_PROTO_GETTER_`))
449
+ ]),
450
+ t.callExpression(
451
+ t.memberExpression(t.identifier("Object"), t.identifier("getOwnPropertyDescriptor")),
452
+ [
453
+ t.memberExpression(t.identifier("Object"), t.identifier("prototype")),
454
+ t.stringLiteral("__proto__")
455
+ ]
456
+ )
457
+ ),
458
+ t.variableDeclarator(
459
+ t.identifier(`_${uniqueID}_GET_PROTOTYPE_OF_`),
460
+ t.callExpression(
461
+ t.memberExpression(
480
462
  t.memberExpression(
481
- t.memberExpression(t.identifier("Object"), t.identifier("call")),
482
- t.identifier("bind")
463
+ t.identifier(
464
+ globalFunctionsUnder7Characters.find(name => !program.scope.hasOwnBinding(name))
465
+ ),
466
+ t.identifier("call")
483
467
  ),
484
- [t.identifier(`_${uniqueID}_DUNDER_PROTO_SETTER_`)]
468
+ t.identifier("bind")
469
+ ),
470
+ [t.identifier(`_${uniqueID}_DUNDER_PROTO_GETTER_`)]
471
+ )
472
+ )
473
+ ])
474
+ )
475
+ consoleMethodsReferenced.size &&
476
+ mainFunction.body.body.unshift(
477
+ t.variableDeclaration(
478
+ "let",
479
+ [...consoleMethodsReferenced].map(name =>
480
+ t.variableDeclarator(
481
+ t.identifier(`_${uniqueID}_CONSOLE_METHOD_${name}_`),
482
+ t.arrowFunctionExpression(
483
+ [t.restElement(t.identifier("args"))],
484
+ t.unaryExpression(
485
+ "void",
486
+ t.callExpression(t.identifier(`$${uniqueID}$DEBUG$`), [t.identifier("args")])
487
+ )
485
488
  )
486
489
  )
487
- ])
490
+ )
488
491
  )
489
- needGetPrototypeOf &&
490
- mainFunction.body.body.unshift(
491
- t.variableDeclaration("let", [
492
- t.variableDeclarator(
493
- t.identifier(`_${uniqueID}_GET_PROTOTYPE_OF_`),
494
- t.callExpression(
495
- t.memberExpression(
496
- t.memberExpression(t.identifier("Object"), t.identifier("call")),
497
- t.identifier("bind")
498
- ),
499
- [t.identifier(`_${uniqueID}_DUNDER_PROTO_GETTER_`)]
492
+ )
493
+ neededDbMethodLets.size &&
494
+ mainFunction.body.body.unshift(
495
+ t.variableDeclaration(
496
+ "let",
497
+ [...neededDbMethodLets].map(name => {
498
+ const getArgs = () =>
499
+ "ObjectId" == name ? []
500
+ : "i" == name || "r" == name ? [t.identifier("a")]
501
+ : [t.identifier("a"), t.identifier("b")]
502
+ return t.variableDeclarator(
503
+ t.identifier(`_${uniqueID}_CONSOLE_METHOD_${name}_`),
504
+ t.arrowFunctionExpression(
505
+ getArgs(),
506
+ t.callExpression(t.identifier(`$${uniqueID}$DB$${name}$`), getArgs())
500
507
  )
501
508
  )
502
- ])
509
+ })
503
510
  )
504
- ;(needGetPrototypeOf || needSetPrototypeOf) &&
505
- mainFunction.body.body.unshift(
506
- t.variableDeclaration("let", [
511
+ )
512
+ needDebugLet &&
513
+ mainFunction.body.body.unshift(
514
+ t.variableDeclaration("let", [
515
+ t.variableDeclarator(
516
+ t.identifier(`_${uniqueID}_DEBUG_`),
517
+ t.callExpression(t.identifier(`$${uniqueID}$DEBUG$`), [t.identifier("a")])
518
+ )
519
+ ])
520
+ )
521
+ neededSubscriptLets.size &&
522
+ mainFunction.body.body.unshift(
523
+ t.variableDeclaration(
524
+ "let",
525
+ [...neededSubscriptLets].map(name =>
507
526
  t.variableDeclarator(
508
- t.objectPattern(
509
- needGetPrototypeOf ?
510
- needSetPrototypeOf ?
511
- [
512
- t.objectProperty(
513
- t.identifier("get"),
514
- t.identifier(`_${uniqueID}_DUNDER_PROTO_GETTER_`)
515
- ),
516
- t.objectProperty(
517
- t.identifier("set"),
518
- t.identifier(`_${uniqueID}_DUNDER_PROTO_SETTER_`)
519
- )
520
- ]
521
- : [
522
- t.objectProperty(
523
- t.identifier("get"),
524
- t.identifier(`_${uniqueID}_DUNDER_PROTO_GETTER_`)
525
- )
526
- ]
527
- : [t.objectProperty(t.identifier("set"), t.identifier(`_${uniqueID}_DUNDER_PROTO_SETTER_`))]
528
- ),
529
- t.callExpression(
530
- t.memberExpression(t.identifier("Object"), t.identifier("getOwnPropertyDescriptor")),
531
- [
532
- t.memberExpression(t.identifier("Object"), t.identifier("prototype")),
533
- t.stringLiteral("__proto__")
534
- ]
527
+ t.identifier(`_${uniqueID}_SUBSCRIPT_${name}_`),
528
+ t.arrowFunctionExpression(
529
+ [t.restElement(t.identifier("args"))],
530
+ t.callExpression(t.identifier(`$${uniqueID}$SUBSCRIPT$${name}$`), [
531
+ t.spreadElement(t.identifier("args"))
532
+ ])
535
533
  )
536
534
  )
537
- ])
535
+ )
538
536
  )
539
- traverse(file, {
540
- BlockStatement({ node: blockStatement }) {
541
- for (const [index, functionDeclaration] of blockStatement.body.entries())
542
- if ("FunctionDeclaration" == functionDeclaration.type && !functionDeclaration.generator) {
543
- blockStatement.body.splice(index, 1)
544
- blockStatement.body.unshift(
537
+ )
538
+ traverse(file, {
539
+ BlockStatement({ node: blockStatement }) {
540
+ for (const [index, functionDeclaration] of blockStatement.body.entries())
541
+ if ("FunctionDeclaration" == functionDeclaration.type && !functionDeclaration.generator) {
542
+ blockStatement.body.splice(index, 1)
543
+ blockStatement.body.unshift(
544
+ t.variableDeclaration("let", [
545
+ t.variableDeclarator(
546
+ functionDeclaration.id,
547
+ t.arrowFunctionExpression(
548
+ functionDeclaration.params,
549
+ functionDeclaration.body,
550
+ functionDeclaration.async
551
+ )
552
+ )
553
+ ])
554
+ )
555
+ }
556
+ },
557
+ ClassBody({ node: classBody, scope, parent }) {
558
+ assert(t.isClass(parent), "src/processScript/transform.ts:669:30")
559
+ let thisIsReferenced = !1
560
+ for (const classMethod of classBody.body) {
561
+ if ("ClassMethod" != classMethod.type) continue
562
+ let methodReferencesThis = !1
563
+ traverse(
564
+ classMethod.body,
565
+ {
566
+ ThisExpression(path) {
567
+ methodReferencesThis = !0
568
+ thisIsReferenced = !0
569
+ path.replaceWith(t.identifier(`_${uniqueID}_THIS_`))
570
+ },
571
+ Function: path => path.skip()
572
+ },
573
+ scope
574
+ )
575
+ if (methodReferencesThis)
576
+ if ("constructor" != classMethod.kind)
577
+ classMethod.body.body.unshift(
545
578
  t.variableDeclaration("let", [
546
579
  t.variableDeclarator(
547
- functionDeclaration.id,
548
- t.arrowFunctionExpression(
549
- functionDeclaration.params,
550
- functionDeclaration.body,
551
- functionDeclaration.async
552
- )
580
+ t.identifier(`_${uniqueID}_THIS_`),
581
+ t.callExpression(t.memberExpression(t.super(), t.identifier("valueOf")), [])
553
582
  )
554
583
  ])
555
584
  )
556
- }
557
- },
558
- ClassBody({ node: classBody, scope, parent }) {
559
- assert(t.isClass(parent))
560
- let thisIsReferenced = !1
561
- for (const classMethod of classBody.body) {
562
- if ("ClassMethod" != classMethod.type) continue
563
- let methodReferencesThis = !1
564
- traverse(
565
- classMethod.body,
566
- {
567
- ThisExpression(path) {
568
- methodReferencesThis = !0
569
- thisIsReferenced = !0
570
- path.replaceWith(t.identifier(`_${uniqueID}_THIS_`))
585
+ else {
586
+ const superCalls = []
587
+ traverse(
588
+ classMethod.body,
589
+ {
590
+ CallExpression(path) {
591
+ "Super" == path.node.callee.type && superCalls.push(path)
592
+ }
571
593
  },
572
- Function(path) {
573
- path.skip()
594
+ scope
595
+ )
596
+ if (superCalls.length)
597
+ if (
598
+ 1 == superCalls.length &&
599
+ "ExpressionStatement" == superCalls[0].parent.type &&
600
+ superCalls[0].parentPath.parentPath.parent == classMethod
601
+ )
602
+ superCalls[0].parentPath.replaceWith(
603
+ t.variableDeclaration("let", [
604
+ t.variableDeclarator(t.identifier(`_${uniqueID}_THIS_`), superCalls[0].node)
605
+ ])
606
+ )
607
+ else {
608
+ for (const path of superCalls)
609
+ path.replaceWith(
610
+ t.assignmentExpression("=", t.identifier(`_${uniqueID}_THIS_`), path.node)
611
+ )
612
+ classMethod.body.body.unshift(
613
+ t.variableDeclaration("let", [
614
+ t.variableDeclarator(t.identifier(`_${uniqueID}_THIS_`))
615
+ ])
616
+ )
574
617
  }
575
- },
576
- scope
577
- )
578
- if (methodReferencesThis)
579
- if ("constructor" != classMethod.kind)
618
+ else
580
619
  classMethod.body.body.unshift(
581
620
  t.variableDeclaration("let", [
582
621
  t.variableDeclarator(
583
622
  t.identifier(`_${uniqueID}_THIS_`),
584
- t.callExpression(t.memberExpression(t.super(), t.identifier("valueOf")), [])
623
+ t.callExpression(t.super(), [])
585
624
  )
586
625
  ])
587
626
  )
588
- else {
589
- const superCalls = []
590
- traverse(
591
- classMethod.body,
592
- {
593
- CallExpression(path) {
594
- "Super" == path.node.callee.type && superCalls.push(path)
595
- }
596
- },
597
- scope
598
- )
599
- if (superCalls.length)
600
- if (
601
- 1 == superCalls.length &&
602
- "ExpressionStatement" == superCalls[0].parent.type &&
603
- superCalls[0].parentPath.parentPath.parent == classMethod
604
- )
605
- superCalls[0].parentPath.replaceWith(
606
- t.variableDeclaration("let", [
607
- t.variableDeclarator(t.identifier(`_${uniqueID}_THIS_`), superCalls[0].node)
608
- ])
609
- )
610
- else {
611
- for (const path of superCalls)
612
- path.replaceWith(
613
- t.assignmentExpression("=", t.identifier(`_${uniqueID}_THIS_`), path.node)
614
- )
615
- classMethod.body.body.unshift(
616
- t.variableDeclaration("let", [
617
- t.variableDeclarator(t.identifier(`_${uniqueID}_THIS_`))
618
- ])
619
- )
620
- }
621
- else
622
- classMethod.body.body.unshift(
623
- t.variableDeclaration("let", [
624
- t.variableDeclarator(
625
- t.identifier(`_${uniqueID}_THIS_`),
626
- t.callExpression(t.super(), [])
627
- )
628
- ])
629
- )
630
- }
631
- }
632
- !parent.superClass && thisIsReferenced && (parent.superClass = t.identifier("Object"))
633
- },
634
- VariableDeclaration({ node: variableDeclaration }) {
635
- "const" == variableDeclaration.kind && (variableDeclaration.kind = "let")
636
- },
637
- ThisExpression(path) {
638
- path.replaceWith(t.identifier("undefined"))
639
- },
640
- BigIntLiteral(path) {
641
- const bigIntAsNumber = Number(path.node.value)
642
- BigInt(bigIntAsNumber) == BigInt(path.node.value) ?
643
- path.replaceWith(t.callExpression(t.identifier("BigInt"), [t.numericLiteral(bigIntAsNumber)]))
644
- : path.replaceWith(t.callExpression(t.identifier("BigInt"), [t.stringLiteral(path.node.value)]))
627
+ }
628
+ }
629
+ !parent.superClass && thisIsReferenced && (parent.superClass = t.identifier("Object"))
630
+ },
631
+ VariableDeclaration({ node: variableDeclaration }) {
632
+ "const" == variableDeclaration.kind && (variableDeclaration.kind = "let")
633
+ },
634
+ ThisExpression: path => path.replaceWith(t.identifier("undefined")),
635
+ BigIntLiteral(path) {
636
+ const bigIntAsNumber = Number(path.node.value)
637
+ path.replaceWith(
638
+ t.callExpression(t.identifier("BigInt"), [
639
+ BigInt(bigIntAsNumber) == BigInt(path.node.value) ?
640
+ t.numericLiteral(bigIntAsNumber)
641
+ : t.stringLiteral(path.node.value)
642
+ ])
643
+ )
644
+ }
645
+ })
646
+ return { file, seclevel }
647
+ function createGetFunctionPrototypeNode() {
648
+ for (const globalFunction of globalFunctionsUnder7Characters)
649
+ if (!program.scope.hasOwnBinding(globalFunction))
650
+ return t.memberExpression(
651
+ t.memberExpression(t.identifier(globalFunction), t.identifier("constructor")),
652
+ t.identifier("prototype")
653
+ )
654
+ return t.memberExpression(
655
+ t.memberExpression(
656
+ t.arrowFunctionExpression([t.identifier("_")], t.identifier("_")),
657
+ t.identifier("constructor")
658
+ ),
659
+ t.identifier("prototype")
660
+ )
661
+ }
662
+ function processFakeSubscriptObject(fakeSubscriptObjectName) {
663
+ for (const referencePath of getReferencePathsToGlobal(fakeSubscriptObjectName, program)) {
664
+ assert("MemberExpression" == referencePath.parent.type, "src/processScript/transform.ts:783:60")
665
+ assert("Identifier" == referencePath.parent.property.type)
666
+ assert(
667
+ "MemberExpression" == referencePath.parentPath.parentPath?.node.type,
668
+ "src/processScript/transform.ts:785:81"
669
+ )
670
+ assert(
671
+ "Identifier" == referencePath.parentPath.parentPath.node.property.type,
672
+ "src/processScript/transform.ts:786:83"
673
+ )
674
+ assert(
675
+ /^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parent.property.name),
676
+ `src/processScript/transform.ts:790:8 invalid user "${referencePath.parent.property.name}" in subscript`
677
+ )
678
+ assert(
679
+ /^[_a-z][\d_a-z]{0,24}$/.test(referencePath.parentPath.parentPath.node.property.name),
680
+ `src/processScript/transform.ts:795:8 invalid script name "${referencePath.parentPath.parentPath.node.property.name}" in subscript`
681
+ )
682
+ if ("CallExpression" == referencePath.parentPath.parentPath.parentPath?.type)
683
+ referencePath.parentPath.parentPath.replaceWith(
684
+ t.identifier(
685
+ `$${uniqueID}$SUBSCRIPT$${referencePath.parent.property.name}$${referencePath.parentPath.parentPath.node.property.name}$`
686
+ )
687
+ )
688
+ else {
689
+ const name = `${referencePath.parent.property.name}$${referencePath.parentPath.parentPath.node.property.name}`
690
+ referencePath.parentPath.parentPath.replaceWith(t.identifier(`_${uniqueID}_SUBSCRIPT_${name}_`))
691
+ neededSubscriptLets.add(name)
645
692
  }
646
- })
647
- return { file, seclevel }
693
+ }
648
694
  }
649
- export { transform as default, transform }
695
+ }
696
+ export { transform }