hackmud-script-manager-ng 0.26.4 → 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.
- 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/generateTypeDeclaration.d.ts +1 -0
- package/generateTypeDeclaration.js +70 -0
- package/index.d.ts +16 -0
- package/index.js +52 -0
- package/package.json +9 -25
- 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,594 +0,0 @@
|
|
|
1
|
-
/* eslint-disable jsdoc/check-param-names */
|
|
2
|
-
import type { NodePath } from "@babel/traverse"
|
|
3
|
-
import type { Expression, File, FunctionDeclaration, Program } from "@babel/types"
|
|
4
|
-
import type { LaxPartial } from "@samual/lib"
|
|
5
|
-
import generate from "@babel/generator"
|
|
6
|
-
import traverse from "@babel/traverse"
|
|
7
|
-
import t from "@babel/types"
|
|
8
|
-
import { assert } from "@samual/lib/assert"
|
|
9
|
-
import { countHackmudCharacters } from "@samual/lib/countHackmudCharacters"
|
|
10
|
-
import { spliceString } from "@samual/lib/spliceString"
|
|
11
|
-
import { tokenizer as tokenize, tokTypes as tokenTypes } from "acorn"
|
|
12
|
-
import * as terser from "terser"
|
|
13
|
-
import { getReferencePathsToGlobal, includesIllegalString, replaceUnsafeStrings } from "./shared"
|
|
14
|
-
|
|
15
|
-
type MinifyOptions = LaxPartial<{
|
|
16
|
-
/** 11 a-z 0-9 characters */ uniqueId: string
|
|
17
|
-
/** whether to mangle function and class names (defaults to `false`) */ mangleNames: boolean
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* when set to `true` forces use of quine cheats
|
|
21
|
-
*
|
|
22
|
-
* when set to `false` forces quine cheats not to be used
|
|
23
|
-
*
|
|
24
|
-
* when left unset or set to `undefined`, automatically uses or doesn't use quine cheats based on character count
|
|
25
|
-
*/
|
|
26
|
-
forceQuineCheats: boolean
|
|
27
|
-
|
|
28
|
-
// /** the comment inserted after the function signature */ autocomplete: string
|
|
29
|
-
}>
|
|
30
|
-
|
|
31
|
-
const minifyNumber = async (number: number) => /\$\((?<number>.+)\)/
|
|
32
|
-
.exec(((await terser.minify(`$(${number})`, { ecma: 2015 })).code!))!.groups!.number!
|
|
33
|
-
|
|
34
|
-
// TODO move autocomplete code outside this function
|
|
35
|
-
// TODO replace references to `arguments`
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* @param file babel ast node representing a file containing transformed code
|
|
39
|
-
* @param options {@link MinifyOptions details}
|
|
40
|
-
*/
|
|
41
|
-
export async function minify(
|
|
42
|
-
file: File,
|
|
43
|
-
// { uniqueId = `00000000000`, mangleNames = false, forceQuineCheats, autocomplete }: MinifyOptions = {}
|
|
44
|
-
{ uniqueId = `00000000000`, mangleNames = false, forceQuineCheats }: MinifyOptions = {}
|
|
45
|
-
): Promise<string> {
|
|
46
|
-
assert(/^\w{11}$/.exec(uniqueId), HERE)
|
|
47
|
-
|
|
48
|
-
let program!: NodePath<Program>
|
|
49
|
-
|
|
50
|
-
traverse(file, {
|
|
51
|
-
Program(path) {
|
|
52
|
-
program = path
|
|
53
|
-
path.skip()
|
|
54
|
-
}
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
if (program.scope.hasGlobal(`_START`)) {
|
|
58
|
-
for (const referencePath of getReferencePathsToGlobal(`_START`, program))
|
|
59
|
-
referencePath.replaceWith(t.identifier(`_ST`))
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (program.scope.hasGlobal(`_TIMEOUT`)) {
|
|
63
|
-
for (const referencePath of getReferencePathsToGlobal(`_TIMEOUT`, program))
|
|
64
|
-
referencePath.replaceWith(t.identifier(`_TO`))
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// typescript does not like NodePath#get() and becomes slow so I have to dance around it
|
|
68
|
-
const mainFunctionPath = program.get(`body.0` as string) as NodePath<FunctionDeclaration>
|
|
69
|
-
|
|
70
|
-
for (const parameter of [ ...mainFunctionPath.node.params ].reverse()) {
|
|
71
|
-
if (parameter.type == `Identifier`) {
|
|
72
|
-
const binding = mainFunctionPath.scope.getBinding(parameter.name)!
|
|
73
|
-
|
|
74
|
-
if (!binding.referenced) {
|
|
75
|
-
mainFunctionPath.node.params.pop()
|
|
76
|
-
continue
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
break
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
for (const global in (program.scope as any).globals as Record<string, any>) {
|
|
84
|
-
if (global == `arguments` || global.startsWith(`$${uniqueId}$`))
|
|
85
|
-
continue
|
|
86
|
-
|
|
87
|
-
const referencePaths = getReferencePathsToGlobal(global, program)
|
|
88
|
-
|
|
89
|
-
if (5 + global.length + referencePaths.length >= global.length * referencePaths.length)
|
|
90
|
-
continue
|
|
91
|
-
|
|
92
|
-
for (const path of referencePaths)
|
|
93
|
-
path.replaceWith(t.identifier(`_${uniqueId}_GLOBAL_${global}_`))
|
|
94
|
-
|
|
95
|
-
mainFunctionPath.node.body.body.unshift(t.variableDeclaration(
|
|
96
|
-
`let`,
|
|
97
|
-
[ t.variableDeclarator(t.identifier(`_${uniqueId}_GLOBAL_${global}_`), t.identifier(global)) ]
|
|
98
|
-
))
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const jsonValues: any[] = []
|
|
102
|
-
// this needs `as boolean` because typescript is dumb
|
|
103
|
-
let undefinedIsReferenced = false as boolean
|
|
104
|
-
let scriptBeforeJSONValueReplacement
|
|
105
|
-
|
|
106
|
-
if (forceQuineCheats != true) {
|
|
107
|
-
const fileBeforeJSONValueReplacement = t.cloneNode(file)
|
|
108
|
-
|
|
109
|
-
traverse(fileBeforeJSONValueReplacement, {
|
|
110
|
-
MemberExpression({ node: memberExpression }) {
|
|
111
|
-
if (memberExpression.computed)
|
|
112
|
-
return
|
|
113
|
-
|
|
114
|
-
assert(memberExpression.property.type == `Identifier`, HERE)
|
|
115
|
-
|
|
116
|
-
if (memberExpression.property.name == `prototype`) {
|
|
117
|
-
memberExpression.computed = true
|
|
118
|
-
memberExpression.property = t.identifier(`_${uniqueId}_PROTOTYPE_PROPERTY_`)
|
|
119
|
-
} else if (memberExpression.property.name == `__proto__`) {
|
|
120
|
-
memberExpression.computed = true
|
|
121
|
-
memberExpression.property = t.identifier(`_${uniqueId}_PROTO_PROPERTY_`)
|
|
122
|
-
} else if (includesIllegalString(memberExpression.property.name)) {
|
|
123
|
-
memberExpression.computed = true
|
|
124
|
-
|
|
125
|
-
memberExpression.property = t.stringLiteral(
|
|
126
|
-
replaceUnsafeStrings(uniqueId, memberExpression.property.name)
|
|
127
|
-
)
|
|
128
|
-
}
|
|
129
|
-
},
|
|
130
|
-
ObjectProperty({ node: objectProperty }) {
|
|
131
|
-
if (objectProperty.key.type == `Identifier` && includesIllegalString(objectProperty.key.name)) {
|
|
132
|
-
objectProperty.key = t.stringLiteral(replaceUnsafeStrings(uniqueId, objectProperty.key.name))
|
|
133
|
-
objectProperty.shorthand = false
|
|
134
|
-
}
|
|
135
|
-
},
|
|
136
|
-
StringLiteral({ node }) {
|
|
137
|
-
node.value = replaceUnsafeStrings(uniqueId, node.value)
|
|
138
|
-
},
|
|
139
|
-
TemplateLiteral({ node }) {
|
|
140
|
-
for (const templateElement of node.quasis) {
|
|
141
|
-
if (templateElement.value.cooked) {
|
|
142
|
-
templateElement.value.cooked = replaceUnsafeStrings(uniqueId, templateElement.value.cooked)
|
|
143
|
-
|
|
144
|
-
templateElement.value.raw = templateElement.value.cooked
|
|
145
|
-
.replaceAll(`\\`, `\\\\`)
|
|
146
|
-
.replaceAll(`\``, `\\\``)
|
|
147
|
-
.replaceAll(`\${`, `$\\{`)
|
|
148
|
-
} else
|
|
149
|
-
templateElement.value.raw = replaceUnsafeStrings(uniqueId, templateElement.value.raw)
|
|
150
|
-
}
|
|
151
|
-
},
|
|
152
|
-
RegExpLiteral(path) {
|
|
153
|
-
path.node.pattern = replaceUnsafeStrings(uniqueId, path.node.pattern)
|
|
154
|
-
delete path.node.extra
|
|
155
|
-
}
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
scriptBeforeJSONValueReplacement = (await terser.minify(generate(fileBeforeJSONValueReplacement!).code, {
|
|
159
|
-
ecma: 2015,
|
|
160
|
-
compress: {
|
|
161
|
-
passes: Infinity,
|
|
162
|
-
unsafe: true,
|
|
163
|
-
unsafe_arrows: true,
|
|
164
|
-
// breaks NaN comparisons: !(a < b) is not a >= b
|
|
165
|
-
unsafe_comps: false,
|
|
166
|
-
unsafe_symbols: true,
|
|
167
|
-
unsafe_methods: true,
|
|
168
|
-
unsafe_proto: true,
|
|
169
|
-
unsafe_regexp: true,
|
|
170
|
-
unsafe_undefined: true,
|
|
171
|
-
sequences: false
|
|
172
|
-
},
|
|
173
|
-
format: { semicolons: false },
|
|
174
|
-
keep_classnames: !mangleNames,
|
|
175
|
-
keep_fnames: !mangleNames
|
|
176
|
-
})).code!
|
|
177
|
-
.replace(new RegExp(`_${uniqueId}_PROTOTYPE_PROPERTY_`, `g`), `"prototype"`)
|
|
178
|
-
.replace(new RegExp(`_${uniqueId}_PROTO_PROPERTY_`, `g`), `"__proto__"`)
|
|
179
|
-
|
|
180
|
-
// if (autocomplete) {
|
|
181
|
-
// scriptBeforeJSONValueReplacement = spliceString(
|
|
182
|
-
// scriptBeforeJSONValueReplacement,
|
|
183
|
-
// `//${autocomplete}\n`,
|
|
184
|
-
// getFunctionBodyStart(scriptBeforeJSONValueReplacement) + 1
|
|
185
|
-
// )
|
|
186
|
-
// }
|
|
187
|
-
|
|
188
|
-
if (forceQuineCheats == false)
|
|
189
|
-
return scriptBeforeJSONValueReplacement
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
let comment: string | undefined
|
|
193
|
-
let hasComment = false
|
|
194
|
-
let code
|
|
195
|
-
|
|
196
|
-
{
|
|
197
|
-
const promises: Promise<any>[] = []
|
|
198
|
-
|
|
199
|
-
traverse(file, {
|
|
200
|
-
FunctionDeclaration(path) {
|
|
201
|
-
const body = path.get(`body`)
|
|
202
|
-
|
|
203
|
-
body.traverse({
|
|
204
|
-
Function(path) {
|
|
205
|
-
if (path.parent.type != `CallExpression` && path.parentKey != `callee`)
|
|
206
|
-
path.skip()
|
|
207
|
-
},
|
|
208
|
-
Loop: path => path.skip(),
|
|
209
|
-
ObjectExpression(path) {
|
|
210
|
-
const o: Record<string, unknown> = {}
|
|
211
|
-
|
|
212
|
-
if (parseObjectExpression(path.node, o))
|
|
213
|
-
path.replaceWith(t.identifier(`_${uniqueId}_JSON_VALUE_${jsonValues.push(o) - 1}_`))
|
|
214
|
-
},
|
|
215
|
-
ArrayExpression(path) {
|
|
216
|
-
const o: unknown[] = []
|
|
217
|
-
|
|
218
|
-
if (parseArrayExpression(path.node, o))
|
|
219
|
-
path.replaceWith(t.identifier(`_${uniqueId}_JSON_VALUE_${jsonValues.push(o) - 1}_`))
|
|
220
|
-
}
|
|
221
|
-
})
|
|
222
|
-
|
|
223
|
-
body.traverse({
|
|
224
|
-
TemplateLiteral(path) {
|
|
225
|
-
if (path.parent.type == `TaggedTemplateExpression`)
|
|
226
|
-
return
|
|
227
|
-
|
|
228
|
-
const templateLiteral = path.node
|
|
229
|
-
let replacement: babel.Node = t.stringLiteral(templateLiteral.quasis[0]!.value.cooked!)
|
|
230
|
-
|
|
231
|
-
for (let index = 0; index < templateLiteral.expressions.length; index++) {
|
|
232
|
-
const expression = templateLiteral.expressions[index] as Expression
|
|
233
|
-
const templateElement = templateLiteral.quasis[index + 1]!
|
|
234
|
-
|
|
235
|
-
replacement = t.binaryExpression(`+`, replacement, expression)
|
|
236
|
-
|
|
237
|
-
if (!templateElement.value.cooked)
|
|
238
|
-
continue
|
|
239
|
-
|
|
240
|
-
replacement = t
|
|
241
|
-
.binaryExpression(`+`, replacement, t.stringLiteral(templateElement.value.cooked!))
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
path.replaceWith(replacement)
|
|
245
|
-
},
|
|
246
|
-
MemberExpression({ node: memberExpression }) {
|
|
247
|
-
if (memberExpression.computed)
|
|
248
|
-
return
|
|
249
|
-
|
|
250
|
-
assert(memberExpression.property.type == `Identifier`, HERE)
|
|
251
|
-
|
|
252
|
-
if (memberExpression.property.name.length < 3)
|
|
253
|
-
return
|
|
254
|
-
|
|
255
|
-
memberExpression.computed = true
|
|
256
|
-
memberExpression.property = t.stringLiteral(memberExpression.property.name)
|
|
257
|
-
},
|
|
258
|
-
UnaryExpression(path) {
|
|
259
|
-
if (path.node.operator == `void`) {
|
|
260
|
-
if (path.node.argument.type == `NumericLiteral` && !path.node.argument.value) {
|
|
261
|
-
path.replaceWith(t.identifier(`_${uniqueId}_UNDEFINED_`))
|
|
262
|
-
undefinedIsReferenced = true
|
|
263
|
-
}
|
|
264
|
-
} else if (path.node.operator == `-` && path.node.argument.type == `NumericLiteral`) {
|
|
265
|
-
const value = -path.node.argument.value
|
|
266
|
-
|
|
267
|
-
promises.push((async () => {
|
|
268
|
-
if ((await minifyNumber(value)).length <= 3)
|
|
269
|
-
return
|
|
270
|
-
|
|
271
|
-
if (path.parentKey == `key` && path.parent.type == `ObjectProperty`)
|
|
272
|
-
path.parent.computed = true
|
|
273
|
-
|
|
274
|
-
let jsonValueIndex = jsonValues.indexOf(value)
|
|
275
|
-
|
|
276
|
-
if (jsonValueIndex == -1)
|
|
277
|
-
jsonValueIndex += jsonValues.push(value)
|
|
278
|
-
|
|
279
|
-
path.replaceWith(t.identifier(`_${uniqueId}_JSON_VALUE_${jsonValueIndex}_`))
|
|
280
|
-
})())
|
|
281
|
-
|
|
282
|
-
path.skip()
|
|
283
|
-
}
|
|
284
|
-
},
|
|
285
|
-
NullLiteral(path) {
|
|
286
|
-
let jsonValueIndex = jsonValues.indexOf(null)
|
|
287
|
-
|
|
288
|
-
if (jsonValueIndex == -1)
|
|
289
|
-
jsonValueIndex += jsonValues.push(null)
|
|
290
|
-
|
|
291
|
-
path.replaceWith(t.identifier(`_${uniqueId}_JSON_VALUE_${jsonValueIndex}_`))
|
|
292
|
-
},
|
|
293
|
-
NumericLiteral(path) {
|
|
294
|
-
promises.push((async () => {
|
|
295
|
-
if ((await minifyNumber(path.node.value)).length <= 3)
|
|
296
|
-
return
|
|
297
|
-
|
|
298
|
-
if (path.parentKey == `key` && path.parent.type == `ObjectProperty`)
|
|
299
|
-
path.parent.computed = true
|
|
300
|
-
|
|
301
|
-
let jsonValueIndex = jsonValues.indexOf(path.node.value)
|
|
302
|
-
|
|
303
|
-
if (jsonValueIndex == -1)
|
|
304
|
-
jsonValueIndex += jsonValues.push(path.node.value)
|
|
305
|
-
|
|
306
|
-
path.replaceWith(t.identifier(`_${uniqueId}_JSON_VALUE_${jsonValueIndex}_`))
|
|
307
|
-
})())
|
|
308
|
-
},
|
|
309
|
-
StringLiteral(path) {
|
|
310
|
-
if (JSON.stringify(path.node.value).includes(`\\u00`) || path.toString().length < 4) {
|
|
311
|
-
path.node.value = replaceUnsafeStrings(uniqueId, path.node.value)
|
|
312
|
-
|
|
313
|
-
return
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
if (path.parentKey == `key` && path.parent.type == `ObjectProperty`)
|
|
317
|
-
path.parent.computed = true
|
|
318
|
-
|
|
319
|
-
let jsonValueIndex = jsonValues.indexOf(path.node.value)
|
|
320
|
-
|
|
321
|
-
if (jsonValueIndex == -1)
|
|
322
|
-
jsonValueIndex += jsonValues.push(path.node.value)
|
|
323
|
-
|
|
324
|
-
path.replaceWith(t.identifier(`_${uniqueId}_JSON_VALUE_${jsonValueIndex}_`))
|
|
325
|
-
},
|
|
326
|
-
ObjectProperty({ node }) {
|
|
327
|
-
if (node.computed || node.key.type != `Identifier` || node.key.name.length < 4)
|
|
328
|
-
return
|
|
329
|
-
|
|
330
|
-
let jsonValueIndex = jsonValues.indexOf(node.key.name)
|
|
331
|
-
|
|
332
|
-
if (jsonValueIndex == -1)
|
|
333
|
-
jsonValueIndex += jsonValues.push(node.key.name)
|
|
334
|
-
|
|
335
|
-
node.computed = true
|
|
336
|
-
node.key = t.identifier(`_${uniqueId}_JSON_VALUE_${jsonValueIndex}_`)
|
|
337
|
-
},
|
|
338
|
-
RegExpLiteral(path) {
|
|
339
|
-
path.node.pattern = replaceUnsafeStrings(uniqueId, path.node.pattern)
|
|
340
|
-
delete path.node.extra
|
|
341
|
-
}
|
|
342
|
-
})
|
|
343
|
-
|
|
344
|
-
path.skip()
|
|
345
|
-
}
|
|
346
|
-
})
|
|
347
|
-
|
|
348
|
-
await Promise.all(promises)
|
|
349
|
-
|
|
350
|
-
const functionDeclaration = file.program.body[0]!
|
|
351
|
-
|
|
352
|
-
assert(functionDeclaration.type == `FunctionDeclaration`, HERE)
|
|
353
|
-
|
|
354
|
-
if (jsonValues.length) {
|
|
355
|
-
hasComment = true
|
|
356
|
-
|
|
357
|
-
if (jsonValues.length == 1) {
|
|
358
|
-
if (typeof jsonValues[0] == `string` && !jsonValues[0].includes(`\n`) && !jsonValues[0].includes(`\t`)
|
|
359
|
-
) {
|
|
360
|
-
const variableDeclaration = t.variableDeclaration(`let`, [
|
|
361
|
-
t.variableDeclarator(
|
|
362
|
-
t.identifier(`_${uniqueId}_JSON_VALUE_0_`),
|
|
363
|
-
t.memberExpression(
|
|
364
|
-
t.taggedTemplateExpression(
|
|
365
|
-
t.memberExpression(
|
|
366
|
-
t.callExpression(t.identifier(`$${uniqueId}$4$SUBSCRIPT$scripts$quine$`), []),
|
|
367
|
-
t.identifier(`split`)
|
|
368
|
-
),
|
|
369
|
-
t.templateLiteral([ t.templateElement({ raw: `\t`, cooked: `\t` }, true) ], [])
|
|
370
|
-
),
|
|
371
|
-
t.identifier(`$${uniqueId}$SPLIT_INDEX$`),
|
|
372
|
-
true
|
|
373
|
-
)
|
|
374
|
-
)
|
|
375
|
-
])
|
|
376
|
-
|
|
377
|
-
if (undefinedIsReferenced) {
|
|
378
|
-
variableDeclaration.declarations
|
|
379
|
-
.push(t.variableDeclarator(t.identifier(`_${uniqueId}_UNDEFINED_`)))
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
functionDeclaration.body.body.unshift(variableDeclaration)
|
|
383
|
-
comment = jsonValues[0]
|
|
384
|
-
} else {
|
|
385
|
-
const variableDeclaration = t.variableDeclaration(`let`, [
|
|
386
|
-
t.variableDeclarator(
|
|
387
|
-
t.identifier(`_${uniqueId}_JSON_VALUE_0_`),
|
|
388
|
-
t.callExpression(t.memberExpression(t.identifier(`JSON`), t.identifier(`parse`)), [
|
|
389
|
-
t.memberExpression(
|
|
390
|
-
t.taggedTemplateExpression(
|
|
391
|
-
t.memberExpression(
|
|
392
|
-
t.callExpression(t.identifier(`$${uniqueId}$4$SUBSCRIPT$scripts$quine$`), []),
|
|
393
|
-
t.identifier(`split`)
|
|
394
|
-
),
|
|
395
|
-
t.templateLiteral([ t.templateElement({ raw: `\t`, cooked: `\t` }, true) ], [])
|
|
396
|
-
),
|
|
397
|
-
t.identifier(`$${uniqueId}$SPLIT_INDEX$`),
|
|
398
|
-
true
|
|
399
|
-
)
|
|
400
|
-
])
|
|
401
|
-
)
|
|
402
|
-
])
|
|
403
|
-
|
|
404
|
-
if (undefinedIsReferenced) {
|
|
405
|
-
variableDeclaration.declarations
|
|
406
|
-
.push(t.variableDeclarator(t.identifier(`_${uniqueId}_UNDEFINED_`)))
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
functionDeclaration.body.body.unshift(variableDeclaration)
|
|
410
|
-
comment = JSON.stringify(jsonValues[0])
|
|
411
|
-
}
|
|
412
|
-
} else {
|
|
413
|
-
const variableDeclaration = t.variableDeclaration(`let`, [
|
|
414
|
-
t.variableDeclarator(
|
|
415
|
-
t.arrayPattern(jsonValues.map((_, index) => t.identifier(`_${uniqueId}_JSON_VALUE_${index}_`))),
|
|
416
|
-
t.callExpression(t.memberExpression(t.identifier(`JSON`), t.identifier(`parse`)), [
|
|
417
|
-
t.memberExpression(
|
|
418
|
-
t.taggedTemplateExpression(
|
|
419
|
-
t.memberExpression(
|
|
420
|
-
t.callExpression(t.identifier(`$${uniqueId}$4$SUBSCRIPT$scripts$quine$`), []),
|
|
421
|
-
t.identifier(`split`)
|
|
422
|
-
),
|
|
423
|
-
t.templateLiteral([ t.templateElement({ raw: `\t`, cooked: `\t` }, true) ], [])
|
|
424
|
-
),
|
|
425
|
-
t.identifier(`$${uniqueId}$SPLIT_INDEX$`),
|
|
426
|
-
true
|
|
427
|
-
)
|
|
428
|
-
])
|
|
429
|
-
)
|
|
430
|
-
])
|
|
431
|
-
|
|
432
|
-
if (undefinedIsReferenced)
|
|
433
|
-
variableDeclaration.declarations.push(t.variableDeclarator(t.identifier(`_${uniqueId}_UNDEFINED_`)))
|
|
434
|
-
|
|
435
|
-
functionDeclaration.body.body.unshift(variableDeclaration)
|
|
436
|
-
comment = JSON.stringify(jsonValues)
|
|
437
|
-
}
|
|
438
|
-
} else if (undefinedIsReferenced) {
|
|
439
|
-
functionDeclaration.body.body.unshift(t.variableDeclaration(`let`, [
|
|
440
|
-
t.variableDeclarator(t.identifier(`_${uniqueId}_UNDEFINED_`)) ]
|
|
441
|
-
))
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
code = generate(file!).code
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
code = (await terser.minify(code, {
|
|
448
|
-
ecma: 2015,
|
|
449
|
-
compress: {
|
|
450
|
-
passes: Infinity,
|
|
451
|
-
unsafe: true,
|
|
452
|
-
unsafe_arrows: true,
|
|
453
|
-
// breaks NaN comparisons: !(a < b) is not a >= b
|
|
454
|
-
unsafe_comps: false,
|
|
455
|
-
unsafe_symbols: true,
|
|
456
|
-
unsafe_methods: true,
|
|
457
|
-
unsafe_proto: true,
|
|
458
|
-
unsafe_regexp: true,
|
|
459
|
-
unsafe_undefined: true,
|
|
460
|
-
sequences: false
|
|
461
|
-
},
|
|
462
|
-
format: { semicolons: false, wrap_func_args: false },
|
|
463
|
-
keep_classnames: !mangleNames,
|
|
464
|
-
keep_fnames: !mangleNames
|
|
465
|
-
})).code || ``
|
|
466
|
-
|
|
467
|
-
// this step affects the character count and can't happen after the count comparison
|
|
468
|
-
if (comment != undefined) {
|
|
469
|
-
code = spliceString(
|
|
470
|
-
code,
|
|
471
|
-
// `${autocomplete ? `//${autocomplete}\n` : ``}\n//\t${comment}\t\n`,
|
|
472
|
-
`\n//\t${comment}\t\n`,
|
|
473
|
-
getFunctionBodyStart(code) + 1
|
|
474
|
-
)
|
|
475
|
-
|
|
476
|
-
code = code.replace(
|
|
477
|
-
`$${uniqueId}$SPLIT_INDEX$`,
|
|
478
|
-
await minifyNumber(code.split(`\t`).findIndex(part => part == comment))
|
|
479
|
-
)
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
if (forceQuineCheats == true)
|
|
483
|
-
return code
|
|
484
|
-
|
|
485
|
-
assert(scriptBeforeJSONValueReplacement, HERE)
|
|
486
|
-
|
|
487
|
-
// if the script has a comment, it's also gonna contain `SC$scripts$quine()`
|
|
488
|
-
// which is gonna compile to `#fs.scripts.quine()` which contains
|
|
489
|
-
// an extra character so we have to account for that
|
|
490
|
-
if (countHackmudCharacters(scriptBeforeJSONValueReplacement) <= (countHackmudCharacters(code) + Number(hasComment)))
|
|
491
|
-
return scriptBeforeJSONValueReplacement
|
|
492
|
-
|
|
493
|
-
return code
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
function parseObjectExpression(node: babel.types.ObjectExpression, o: Record<string, unknown>) {
|
|
497
|
-
if (!node.properties.length)
|
|
498
|
-
return false
|
|
499
|
-
|
|
500
|
-
for (const property of node.properties) {
|
|
501
|
-
if (property.type != `ObjectProperty` || property.computed)
|
|
502
|
-
return false
|
|
503
|
-
|
|
504
|
-
assert(
|
|
505
|
-
property.key.type == `Identifier` || property.key.type == `NumericLiteral` ||
|
|
506
|
-
property.key.type == `StringLiteral`,
|
|
507
|
-
HERE
|
|
508
|
-
)
|
|
509
|
-
|
|
510
|
-
if (property.value.type == `ArrayExpression`) {
|
|
511
|
-
const childArray: unknown[] = []
|
|
512
|
-
|
|
513
|
-
if (property.value.elements.length && !parseArrayExpression(property.value, childArray))
|
|
514
|
-
return false
|
|
515
|
-
|
|
516
|
-
o[property.key.type == `Identifier` ? property.key.name : property.key.value] = childArray
|
|
517
|
-
} else if (property.value.type == `ObjectExpression`) {
|
|
518
|
-
const childObject: Record<string, unknown> = {}
|
|
519
|
-
|
|
520
|
-
if (property.value.properties.length && !parseObjectExpression(property.value, childObject))
|
|
521
|
-
return false
|
|
522
|
-
|
|
523
|
-
o[property.key.type == `Identifier` ? property.key.name : property.key.value] = childObject
|
|
524
|
-
} else if (property.value.type == `NullLiteral`)
|
|
525
|
-
o[property.key.type == `Identifier` ? property.key.name : property.key.value] = null
|
|
526
|
-
else if (property.value.type == `BooleanLiteral` || property.value.type == `NumericLiteral` ||
|
|
527
|
-
property.value.type == `StringLiteral`
|
|
528
|
-
)
|
|
529
|
-
o[property.key.type == `Identifier` ? property.key.name : property.key.value] = property.value.value
|
|
530
|
-
else if (property.value.type == `TemplateLiteral` && !property.value.expressions.length) {
|
|
531
|
-
o[property.key.type == `Identifier` ? property.key.name : property.key.value] =
|
|
532
|
-
property.value.quasis[0]!.value.cooked
|
|
533
|
-
} else
|
|
534
|
-
return false
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
return true
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
function parseArrayExpression(node: babel.types.ArrayExpression, o: unknown[]) {
|
|
541
|
-
if (!node.elements.length)
|
|
542
|
-
return false
|
|
543
|
-
|
|
544
|
-
for (const element of node.elements) {
|
|
545
|
-
if (!element)
|
|
546
|
-
return false
|
|
547
|
-
|
|
548
|
-
if (element.type == `ArrayExpression`) {
|
|
549
|
-
const childArray: unknown[] = []
|
|
550
|
-
|
|
551
|
-
if (element.elements.length && !parseArrayExpression(element, childArray))
|
|
552
|
-
return false
|
|
553
|
-
|
|
554
|
-
o.push(childArray)
|
|
555
|
-
} else if (element.type == `ObjectExpression`) {
|
|
556
|
-
const childObject: Record<string, unknown> = {}
|
|
557
|
-
|
|
558
|
-
if (element.properties.length && !parseObjectExpression(element, childObject))
|
|
559
|
-
return false
|
|
560
|
-
|
|
561
|
-
o.push(childObject)
|
|
562
|
-
} else if (element.type == `NullLiteral`)
|
|
563
|
-
o.push(null)
|
|
564
|
-
else if (element.type == `BooleanLiteral` || element.type == `NumericLiteral` || element.type == `StringLiteral`)
|
|
565
|
-
o.push(element.value)
|
|
566
|
-
else if (element.type == `TemplateLiteral` && !element.expressions.length)
|
|
567
|
-
o.push(element.quasis[0]!.value.cooked)
|
|
568
|
-
else
|
|
569
|
-
return false
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
return true
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
function getFunctionBodyStart(code: string) {
|
|
576
|
-
const tokens = tokenize(code, { ecmaVersion: 2015 })
|
|
577
|
-
|
|
578
|
-
tokens.getToken() // function
|
|
579
|
-
tokens.getToken() // name
|
|
580
|
-
tokens.getToken() // (
|
|
581
|
-
|
|
582
|
-
let nests = 1
|
|
583
|
-
|
|
584
|
-
while (nests) {
|
|
585
|
-
const token = tokens.getToken()
|
|
586
|
-
|
|
587
|
-
if (token.type == tokenTypes.parenL)
|
|
588
|
-
nests++
|
|
589
|
-
else if (token.type == tokenTypes.parenR)
|
|
590
|
-
nests--
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
return tokens.getToken().start // {
|
|
594
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export const postprocess = (code: string, uniqueId: string, autocomplete: string | undefined): string => code
|
|
2
|
-
.replace(/^function\s*[\w$]+\(/, `function(`)
|
|
3
|
-
.replace(/(^.+\{)/, `$1${autocomplete ? `//${autocomplete}` : ``}`)
|
|
4
|
-
.replace(new RegExp(`\\$${uniqueId}\\$\\\\(?:\\\\)?\\$SC_DOLLAR\\$`, `g`), `S\\C$`)
|
|
5
|
-
.replace(new RegExp(`\\$${uniqueId}\\$\\\\(?:\\\\)?\\$DB_DOLLAR\\$`, `g`), `D\\B$`)
|
|
6
|
-
.replace(new RegExp(`\\$${uniqueId}\\$\\\\(?:\\\\)?\\$D\\$`, `g`), `_\\_D_S`)
|
|
7
|
-
.replace(new RegExp(`\\$${uniqueId}\\$\\\\(?:\\\\)?\\$FMCL\\$`, `g`), `_\\_FMCL_`)
|
|
8
|
-
.replace(new RegExp(`\\$${uniqueId}\\$\\\\(?:\\\\)?\\$G\\$`, `g`), `_\\_G_`)
|
|
9
|
-
.replace(new RegExp(`\\$${uniqueId}\\$(\\d)\\$SUBSCRIPT\\$(\\w+)\\$(\\w+)\\$`, `g`), `#$1s.$2.$3`)
|
|
10
|
-
.replace(new RegExp(`\\$${uniqueId}\\$DEBUG\\$`, `g`), `#D`)
|
|
11
|
-
.replace(new RegExp(`\\$${uniqueId}\\$FMCL\\$`, `g`), `#FMCL`)
|
|
12
|
-
.replace(new RegExp(`\\$${uniqueId}\\$GLOBAL\\$`, `g`), `#G`)
|
|
13
|
-
.replace(new RegExp(`\\$${uniqueId}\\$DB\\$(\\w+)\\$`, `g`), `#db.$1`)
|
|
14
|
-
.replace(new RegExp(`\\$${uniqueId}\\$SLASH_SLASH\\$`, `g`), `/\\/`)
|
|
15
|
-
.replace(new RegExp(`\\$${uniqueId}\\$NOT_A_SUBSCRIPT\\$(#[\\w\\.]+)\\(\\$`, `g`), `$1\\(`)
|
|
16
|
-
.replace(new RegExp(`\\$${uniqueId}\\$NOT_A_DB_CALL\\$(\\w+)\\$`, `g`), `#db.$1\\(`)
|
|
17
|
-
.replace(new RegExp(`\\$${uniqueId}\\$NOT_A_DEBUG_CALL\\$`, `g`), `#D\\(`)
|
|
18
|
-
.replace(new RegExp(`\\$${uniqueId}\\$NOT_FMCL\\$`, `g`), `#\\FMCL`)
|
|
19
|
-
.replace(new RegExp(`\\$${uniqueId}\\$NOT_G\\$`, `g`), `#\\G`)
|