hackmud-script-manager-ng 0.0.0-stage → 0.26.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/.envrc +1 -0
  2. package/.github/FUNDING.yml +14 -0
  3. package/.github/workflows/publish.yml +27 -0
  4. package/.github/workflows/test.yml +18 -0
  5. package/.vscode/settings.json +55 -0
  6. package/CONTRIBUTING.md +68 -0
  7. package/LICENSE +21 -0
  8. package/README.md +86 -2
  9. package/env.d.ts +1305 -0
  10. package/eslint.config.js +81 -0
  11. package/game-scripts-tests/array-rest-works.ts +6 -0
  12. package/game-scripts-tests/this_array_in_object.ts +16 -0
  13. package/game-scripts-tests/this_function_array.ts +9 -0
  14. package/game-scripts-tests/this_in_default_argument.ts +35 -0
  15. package/game-scripts-tests/this_in_lambda_in_object.ts +26 -0
  16. package/package.json +113 -3
  17. package/pnpm-workspace.yaml +2 -0
  18. package/rollup.config.js +69 -0
  19. package/scripts/dev.sh +3 -0
  20. package/scripts/emit-declarations.sh +3 -0
  21. package/scripts/emit-package-json.js +9 -0
  22. package/scripts/lint.sh +5 -0
  23. package/scripts/package.sh +7 -0
  24. package/scripts/update.sh +4 -0
  25. package/scripts/version-dev.js +14 -0
  26. package/src/bin/hsm.ts +754 -0
  27. package/src/constants.ts +2 -0
  28. package/src/env.d.ts +13 -0
  29. package/src/generateTypeDeclaration.ts +119 -0
  30. package/src/index.ts +13 -0
  31. package/src/processScript/__snapshots__/index.ts.snap +8 -0
  32. package/src/processScript/index.ts +474 -0
  33. package/src/processScript/minify.ts +594 -0
  34. package/src/processScript/postprocess.ts +19 -0
  35. package/src/processScript/preprocess.ts +130 -0
  36. package/src/processScript/shared.ts +33 -0
  37. package/src/processScript/transform.ts +978 -0
  38. package/src/pull.ts +20 -0
  39. package/src/push.ts +200 -0
  40. package/src/syncMacros.ts +53 -0
  41. package/src/tsconfig.json +14 -0
  42. package/src/watch.ts +258 -0
  43. package/tsconfig.json +23 -0
  44. package/vitest.config.ts +19 -0
@@ -0,0 +1,130 @@
1
+ /* eslint-disable jsdoc/check-param-names */
2
+ import type { NodePath } from "@babel/traverse"
3
+ import type { Program } from "@babel/types"
4
+ import type { LaxPartial } from "@samual/lib"
5
+ import generate from "@babel/generator"
6
+ import { parse } from "@babel/parser"
7
+ import traverse from "@babel/traverse"
8
+ import t from "@babel/types"
9
+ import { assert } from "@samual/lib/assert"
10
+ import { spliceString } from "@samual/lib/spliceString"
11
+ import { resolve as resolveModule } from "import-meta-resolve"
12
+
13
+ export type PreprocessOptions = LaxPartial<{ /** 11 a-z 0-9 characters */ uniqueId: string }>
14
+
15
+ /**
16
+ * @param code source code for preprocessing
17
+ * @param options {@link PreprocessOptions details}
18
+ */
19
+ export async function preprocess(code: string, { uniqueId = `00000000000` }: PreprocessOptions = {}): Promise<{ code: string }> {
20
+ assert(/^\w{11}$/.test(uniqueId), HERE)
21
+
22
+ const sourceCode = code
23
+ let lengthBefore
24
+
25
+ do {
26
+ lengthBefore = code.length
27
+ code = code.replace(/^\s+/, ``).replace(/^\/\/.*/, ``).replace(/^\/\*[\s\S]*?\*\//, ``)
28
+ } while (code.length != lengthBefore)
29
+
30
+ code = code.replace(/^function\s*\(/, `export default function (`)
31
+
32
+ let file
33
+
34
+ while (true) {
35
+ let error
36
+
37
+ try {
38
+ file = parse(code, {
39
+ plugins: [
40
+ `typescript`,
41
+ [ `decorators`, { decoratorsBeforeExport: true } ],
42
+ `doExpressions`,
43
+ `functionBind`,
44
+ `functionSent`,
45
+ `partialApplication`,
46
+ [ `pipelineOperator`, { proposal: `hack`, topicToken: `%` } ],
47
+ `throwExpressions`,
48
+ [ `recordAndTuple`, { syntaxType: `hash` } ],
49
+ `classProperties`,
50
+ `classPrivateProperties`,
51
+ `classPrivateMethods`,
52
+ `logicalAssignment`,
53
+ `numericSeparator`,
54
+ `nullishCoalescingOperator`,
55
+ `optionalChaining`,
56
+ `optionalCatchBinding`,
57
+ `objectRestSpread`
58
+ ],
59
+ sourceType: `module`
60
+ })
61
+
62
+ break
63
+ } catch (error_) {
64
+ assert(error_ instanceof SyntaxError, HERE)
65
+ error = error_ as SyntaxError & { pos: number, code: string, reasonCode: string }
66
+ }
67
+
68
+ if (error.code != `BABEL_PARSER_SYNTAX_ERROR` || error.reasonCode != `PrivateInExpectedIn`) {
69
+ console.error((/.+/.exec(code.slice(error.pos)))?.[0])
70
+
71
+ throw error
72
+ }
73
+
74
+ const codeSlice = code.slice(error.pos)
75
+ let match
76
+
77
+ if ((match = /^#[0-4fhmln]s\.scripts\.quine\(\)/.exec(codeSlice)))
78
+ code = spliceString(code, JSON.stringify(sourceCode), error.pos, match[0]!.length)
79
+ else if ((match = /^#[0-4fhmln]?s\./.exec(codeSlice)))
80
+ code = spliceString(code, `$`, error.pos, 1)
81
+ else if ((match = /^#D[^\w$]/.exec(codeSlice)))
82
+ code = spliceString(code, `$`, error.pos, 1)
83
+ else if ((match = /^#FMCL/.exec(codeSlice)))
84
+ code = spliceString(code, `$${uniqueId}$FMCL$`, error.pos, match[0]!.length)
85
+ else if ((match = /^#G/.exec(codeSlice)))
86
+ code = spliceString(code, `$${uniqueId}$GLOBAL$`, error.pos, match[0]!.length)
87
+ else if ((match = /^#db\./.exec(codeSlice)))
88
+ code = spliceString(code, `$`, error.pos, 1)
89
+ else
90
+ throw error
91
+ }
92
+
93
+ let program!: NodePath<Program>
94
+
95
+ traverse(file, {
96
+ Program(path) {
97
+ program = path
98
+ path.skip()
99
+ }
100
+ })
101
+
102
+ const needRecord = program.scope.hasGlobal(`Record`)
103
+ const needTuple = program.scope.hasGlobal(`Tuple`)
104
+
105
+ if (needRecord || needTuple) {
106
+ file.program.body.unshift(t.importDeclaration(
107
+ needRecord ?
108
+ needTuple ?
109
+ [
110
+ t.importSpecifier(t.identifier(`Record`), t.identifier(`Record`)),
111
+ t.importSpecifier(t.identifier(`Tuple`), t.identifier(`Tuple`))
112
+ ]
113
+ : [ t.importSpecifier(t.identifier(`Record`), t.identifier(`Record`)) ]
114
+ : [ t.importSpecifier(t.identifier(`Tuple`), t.identifier(`Tuple`)) ],
115
+ t.stringLiteral(`@bloomberg/record-tuple-polyfill`)
116
+ ))
117
+ }
118
+
119
+ if (program.scope.hasGlobal(`Proxy`)) {
120
+ file.program.body.unshift(t.importDeclaration([
121
+ t.importDefaultSpecifier(t.identifier(`Proxy`))
122
+ ], t.stringLiteral(resolveModule(`proxy-polyfill/src/proxy.js`, import.meta.url).slice(7))))
123
+ }
124
+
125
+ if (program.node.body.length == 1 && program.node.body[0]!.type == `FunctionDeclaration`) {
126
+ throw Error(`Scripts that only contain a single function declaration are no longer supported.\nPrefix the function declaration with \`export default\`.`)
127
+ }
128
+
129
+ return { code: generate(file).code }
130
+ }
@@ -0,0 +1,33 @@
1
+ import type { NodePath } from "@babel/traverse"
2
+ import type { Identifier, Program } from "@babel/types"
3
+ import t from "@babel/types"
4
+ import { ensure } from "@samual/lib/assert"
5
+
6
+ export function getReferencePathsToGlobal(name: string, program: NodePath<Program>) {
7
+ const [ variableDeclaration ] = program
8
+ .unshiftContainer(`body`, t.variableDeclaration(`let`, [ t.variableDeclarator(t.identifier(name)) ]))
9
+
10
+ program.scope.crawl()
11
+
12
+ const binding = ensure(program.scope.getBinding(name), HERE)
13
+
14
+ variableDeclaration.remove()
15
+
16
+ return binding.referencePaths as NodePath<Identifier>[]
17
+ }
18
+
19
+ export const includesIllegalString = (toCheck: string): boolean => toCheck.includes(`SC$`) || toCheck.includes(`DB$`) ||
20
+ toCheck.includes(`__D_S`) || toCheck.includes(`__FMCL_`) || toCheck.includes(`__G_`)
21
+
22
+ export const replaceUnsafeStrings = (uniqueId: string, toReplace: string): string => toReplace
23
+ .replaceAll(`SC$`, `$${uniqueId}$\\$SC_DOLLAR$`)
24
+ .replaceAll(`DB$`, `$${uniqueId}$\\$DB_DOLLAR$`)
25
+ .replaceAll(`__D_S`, `$${uniqueId}$\\$D$`)
26
+ .replaceAll(`__FMCL_`, `$${uniqueId}$\\$FMCL$`)
27
+ .replaceAll(`__G_`, `$${uniqueId}$\\$G$`)
28
+ .replaceAll(`//`, `$${uniqueId}$SLASH_SLASH$`)
29
+ .replaceAll(/#[0-4fhmln]?s(?:\.[_a-z][\d_a-z]{0,24}){2}\(/g, `$${uniqueId}$NOT_A_SUBSCRIPT$$$&$`)
30
+ .replaceAll(/#db\.(?<methodName>[irfu]|u1|us|ObjectId)\(/g, `$${uniqueId}$NOT_A_DB_CALL$$$1$`)
31
+ .replaceAll(`#D(`, `$${uniqueId}$NOT_A_DEBUG_CALL$`)
32
+ .replaceAll(`#FMCL`, `$${uniqueId}$NOT_FMCL$`)
33
+ .replaceAll(`#G`, `$${uniqueId}$NOT_G$`)