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