@haneullabs/codegen 0.1.0 → 0.8.2
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/CHANGELOG.md +128 -33
- package/README.md +35 -33
- package/dist/bin/bash-complete.d.mts +6 -0
- package/dist/bin/bash-complete.d.mts.map +1 -0
- package/dist/bin/bash-complete.mjs +29 -0
- package/dist/bin/bash-complete.mjs.map +1 -0
- package/dist/bin/cli.d.mts +1 -0
- package/dist/bin/cli.mjs +24 -0
- package/dist/bin/cli.mjs.map +1 -0
- package/dist/cli/cli.mjs +28 -0
- package/dist/cli/cli.mjs.map +1 -0
- package/dist/cli/commands/generate/command.mjs +90 -0
- package/dist/cli/commands/generate/command.mjs.map +1 -0
- package/dist/cli/commands/generate/impl.mjs +72 -0
- package/dist/cli/commands/generate/impl.mjs.map +1 -0
- package/dist/cli/context.mjs +17 -0
- package/dist/cli/context.mjs.map +1 -0
- package/dist/config.d.mts +106 -0
- package/dist/config.d.mts.map +1 -0
- package/dist/config.mjs +70 -0
- package/dist/config.mjs.map +1 -0
- package/dist/file-builder.mjs +65 -0
- package/dist/file-builder.mjs.map +1 -0
- package/dist/{esm/generate-utils.js → generate-utils.mjs} +95 -27
- package/dist/generate-utils.mjs.map +1 -0
- package/dist/index.d.mts +21 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +80 -0
- package/dist/index.mjs.map +1 -0
- package/dist/move-module-builder.mjs +350 -0
- package/dist/move-module-builder.mjs.map +1 -0
- package/dist/render-types.mjs +207 -0
- package/dist/render-types.mjs.map +1 -0
- package/dist/utils.mjs +89 -0
- package/dist/utils.mjs.map +1 -0
- package/package.json +27 -28
- package/src/bin/bash-complete.ts +18 -7
- package/src/bin/cli.ts +3 -9
- package/src/cli/commands/generate/command.ts +32 -0
- package/src/cli/commands/generate/impl.ts +93 -5
- package/src/config.ts +55 -7
- package/src/file-builder.ts +14 -1
- package/src/generate-utils.ts +88 -18
- package/src/index.ts +89 -29
- package/src/move-module-builder.ts +181 -76
- package/src/render-types.ts +82 -29
- package/dist/cjs/bin/bash-complete.d.ts +0 -2
- package/dist/cjs/bin/bash-complete.js +0 -51
- package/dist/cjs/bin/bash-complete.js.map +0 -7
- package/dist/cjs/bin/cli.d.ts +0 -2
- package/dist/cjs/bin/cli.js +0 -34
- package/dist/cjs/bin/cli.js.map +0 -7
- package/dist/cjs/cli/cli.d.ts +0 -1
- package/dist/cjs/cli/cli.js +0 -49
- package/dist/cjs/cli/cli.js.map +0 -7
- package/dist/cjs/cli/commands/generate/command.d.ts +0 -1
- package/dist/cjs/cli/commands/generate/command.js +0 -80
- package/dist/cjs/cli/commands/generate/command.js.map +0 -7
- package/dist/cjs/cli/commands/generate/impl.d.ts +0 -8
- package/dist/cjs/cli/commands/generate/impl.js +0 -66
- package/dist/cjs/cli/commands/generate/impl.js.map +0 -7
- package/dist/cjs/cli/context.d.ts +0 -6
- package/dist/cjs/cli/context.js +0 -45
- package/dist/cjs/cli/context.js.map +0 -7
- package/dist/cjs/config.d.ts +0 -51
- package/dist/cjs/config.js +0 -75
- package/dist/cjs/config.js.map +0 -7
- package/dist/cjs/file-builder.d.ts +0 -13
- package/dist/cjs/file-builder.js +0 -83
- package/dist/cjs/file-builder.js.map +0 -7
- package/dist/cjs/generate-utils.d.ts +0 -1
- package/dist/cjs/generate-utils.js +0 -187
- package/dist/cjs/generate-utils.js.map +0 -7
- package/dist/cjs/index.d.ts +0 -8
- package/dist/cjs/index.js +0 -124
- package/dist/cjs/index.js.map +0 -7
- package/dist/cjs/move-module-builder.d.ts +0 -26
- package/dist/cjs/move-module-builder.js +0 -464
- package/dist/cjs/move-module-builder.js.map +0 -7
- package/dist/cjs/package.json +0 -5
- package/dist/cjs/render-types.d.ts +0 -19
- package/dist/cjs/render-types.js +0 -313
- package/dist/cjs/render-types.js.map +0 -7
- package/dist/cjs/summary.d.ts +0 -3
- package/dist/cjs/summary.js +0 -218
- package/dist/cjs/summary.js.map +0 -7
- package/dist/cjs/types/deserialized.d.ts +0 -89
- package/dist/cjs/types/deserialized.js +0 -17
- package/dist/cjs/types/deserialized.js.map +0 -7
- package/dist/cjs/types/summary.d.ts +0 -105
- package/dist/cjs/types/summary.js +0 -17
- package/dist/cjs/types/summary.js.map +0 -7
- package/dist/cjs/utils.d.ts +0 -22
- package/dist/cjs/utils.js +0 -164
- package/dist/cjs/utils.js.map +0 -7
- package/dist/esm/bin/bash-complete.d.ts +0 -2
- package/dist/esm/bin/bash-complete.js +0 -31
- package/dist/esm/bin/bash-complete.js.map +0 -7
- package/dist/esm/bin/cli.d.ts +0 -2
- package/dist/esm/bin/cli.js +0 -32
- package/dist/esm/bin/cli.js.map +0 -7
- package/dist/esm/cli/cli.d.ts +0 -1
- package/dist/esm/cli/cli.js +0 -29
- package/dist/esm/cli/cli.js.map +0 -7
- package/dist/esm/cli/commands/generate/command.d.ts +0 -1
- package/dist/esm/cli/commands/generate/command.js +0 -50
- package/dist/esm/cli/commands/generate/command.js.map +0 -7
- package/dist/esm/cli/commands/generate/impl.d.ts +0 -8
- package/dist/esm/cli/commands/generate/impl.js +0 -46
- package/dist/esm/cli/commands/generate/impl.js.map +0 -7
- package/dist/esm/cli/context.d.ts +0 -6
- package/dist/esm/cli/context.js +0 -15
- package/dist/esm/cli/context.js.map +0 -7
- package/dist/esm/config.d.ts +0 -51
- package/dist/esm/config.js +0 -45
- package/dist/esm/config.js.map +0 -7
- package/dist/esm/file-builder.d.ts +0 -13
- package/dist/esm/file-builder.js +0 -63
- package/dist/esm/file-builder.js.map +0 -7
- package/dist/esm/generate-utils.d.ts +0 -1
- package/dist/esm/generate-utils.js.map +0 -7
- package/dist/esm/index.d.ts +0 -8
- package/dist/esm/index.js +0 -104
- package/dist/esm/index.js.map +0 -7
- package/dist/esm/move-module-builder.d.ts +0 -26
- package/dist/esm/move-module-builder.js +0 -457
- package/dist/esm/move-module-builder.js.map +0 -7
- package/dist/esm/package.json +0 -5
- package/dist/esm/render-types.d.ts +0 -19
- package/dist/esm/render-types.js +0 -293
- package/dist/esm/render-types.js.map +0 -7
- package/dist/esm/summary.d.ts +0 -3
- package/dist/esm/summary.js +0 -198
- package/dist/esm/summary.js.map +0 -7
- package/dist/esm/types/deserialized.d.ts +0 -89
- package/dist/esm/types/deserialized.js +0 -1
- package/dist/esm/types/deserialized.js.map +0 -7
- package/dist/esm/types/summary.d.ts +0 -105
- package/dist/esm/types/summary.js +0 -1
- package/dist/esm/types/summary.js.map +0 -7
- package/dist/esm/utils.d.ts +0 -22
- package/dist/esm/utils.js +0 -134
- package/dist/esm/utils.js.map +0 -7
- package/dist/tsconfig.esm.tsbuildinfo +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { normalizeHaneulAddress } from "@haneullabs/haneul/utils";
|
|
2
|
+
|
|
3
|
+
//#region src/render-types.ts
|
|
4
|
+
const MOVE_STDLIB_ADDRESS = normalizeHaneulAddress("0x1");
|
|
5
|
+
const HANEUL_FRAMEWORK_ADDRESS = normalizeHaneulAddress("0x2");
|
|
6
|
+
const HANEUL_SYSTEM_ADDRESS = normalizeHaneulAddress("0x3");
|
|
7
|
+
function getFilteredTypeParameterIndex(originalIndex, typeParameters, includePhantom) {
|
|
8
|
+
if (includePhantom || !typeParameters) return originalIndex;
|
|
9
|
+
let filteredIndex = 0;
|
|
10
|
+
for (let i = 0; i < originalIndex; i++) if (!typeParameters[i]?.phantom) filteredIndex++;
|
|
11
|
+
return filteredIndex;
|
|
12
|
+
}
|
|
13
|
+
function renderTypeSignature(type, options) {
|
|
14
|
+
let bcs = "bcs";
|
|
15
|
+
if (options.bcsImport && usesBcs(type, options)) bcs = options.bcsImport();
|
|
16
|
+
switch (type) {
|
|
17
|
+
case "address": switch (options.format) {
|
|
18
|
+
case "typescriptArg": return "string";
|
|
19
|
+
case "typeTag": return `address`;
|
|
20
|
+
case "bcs": return `${bcs}.Address`;
|
|
21
|
+
default: throw new Error(`Unknown format: ${options.format}`);
|
|
22
|
+
}
|
|
23
|
+
case "bool": switch (options.format) {
|
|
24
|
+
case "typescriptArg": return "boolean";
|
|
25
|
+
case "typeTag": return `bool`;
|
|
26
|
+
case "bcs": return `${bcs}.bool()`;
|
|
27
|
+
default: throw new Error(`Unknown format: ${options.format}`);
|
|
28
|
+
}
|
|
29
|
+
case "u8":
|
|
30
|
+
case "u16":
|
|
31
|
+
case "u32": switch (options.format) {
|
|
32
|
+
case "typescriptArg": return "number";
|
|
33
|
+
case "typeTag": return type.toLowerCase();
|
|
34
|
+
case "bcs": return `${bcs}.${type.toLowerCase()}()`;
|
|
35
|
+
default: throw new Error(`Unknown format: ${options.format}`);
|
|
36
|
+
}
|
|
37
|
+
case "u64":
|
|
38
|
+
case "u128":
|
|
39
|
+
case "u256": switch (options.format) {
|
|
40
|
+
case "typescriptArg": return `number | bigint`;
|
|
41
|
+
case "typeTag": return type.toLowerCase();
|
|
42
|
+
case "bcs": return `${bcs}.${type.toLowerCase()}()`;
|
|
43
|
+
default: throw new Error(`Unknown format: ${options.format}`);
|
|
44
|
+
}
|
|
45
|
+
case "signer": throw new Error("Signer is not supported");
|
|
46
|
+
case "_": throw new Error("Macro placeholder is not supported");
|
|
47
|
+
}
|
|
48
|
+
if ("Datatype" in type) return renderDataType(type.Datatype, options);
|
|
49
|
+
if ("Reference" in type) return renderTypeSignature(type.Reference[1], options);
|
|
50
|
+
if ("vector" in type) switch (options.format) {
|
|
51
|
+
case "typescriptArg": return `${renderTypeSignature(type.vector, options)}[]`;
|
|
52
|
+
case "typeTag": return `vector<${renderTypeSignature(type.vector, options)}>`;
|
|
53
|
+
case "bcs": return `${bcs}.vector(${renderTypeSignature(type.vector, options)})`;
|
|
54
|
+
default: throw new Error(`Unknown format: ${options.format}`);
|
|
55
|
+
}
|
|
56
|
+
if ("TypeParameter" in type) {
|
|
57
|
+
options.onTypeParameter?.(type.TypeParameter);
|
|
58
|
+
const originalIndex = type.TypeParameter;
|
|
59
|
+
const filteredIndex = getFilteredTypeParameterIndex(originalIndex, options.typeParameters, options.includePhantomTypeParameters);
|
|
60
|
+
switch (options.format) {
|
|
61
|
+
case "typescriptArg": return options.typeParameters?.[originalIndex]?.name ?? `T${originalIndex}`;
|
|
62
|
+
case "typeTag": return `\${options.typeArguments[${originalIndex}]}`;
|
|
63
|
+
case "bcs": return `typeParameters[${filteredIndex}]`;
|
|
64
|
+
default: throw new Error(`Unknown format: ${options.format}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if ("NamedTypeParameter" in type) {
|
|
68
|
+
options.onTypeParameter?.(type.NamedTypeParameter);
|
|
69
|
+
const originalIndex = options.typeParameters?.findIndex((p) => p.name === type.NamedTypeParameter) ?? -1;
|
|
70
|
+
if (originalIndex === -1) throw new Error(`Named type parameter ${type.NamedTypeParameter} not found`);
|
|
71
|
+
const filteredIndex = getFilteredTypeParameterIndex(originalIndex, options.typeParameters, options.includePhantomTypeParameters);
|
|
72
|
+
switch (options.format) {
|
|
73
|
+
case "typescriptArg": return type.NamedTypeParameter;
|
|
74
|
+
case "typeTag": return `\${options.typeArguments[${originalIndex}]}`;
|
|
75
|
+
case "bcs": return `typeParameters[${filteredIndex}]`;
|
|
76
|
+
default: throw new Error(`Unknown format: ${options.format}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
throw new Error(`Unknown type signature: ${JSON.stringify(type, null, 2)}`);
|
|
80
|
+
}
|
|
81
|
+
function usesBcs(type, options) {
|
|
82
|
+
if (typeof type === "string") return true;
|
|
83
|
+
if ("Reference" in type) return usesBcs(type.Reference[1], options);
|
|
84
|
+
if ("Datatype" in type) return isPureDataType(type.Datatype, options);
|
|
85
|
+
if ("vector" in type) return true;
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
function isPureDataType(type, options) {
|
|
89
|
+
const address = options.resolveAddress(type.module.address);
|
|
90
|
+
if (address === MOVE_STDLIB_ADDRESS) {
|
|
91
|
+
if ((type.module.name === "ascii" || type.module.name === "string") && type.name === "String") return true;
|
|
92
|
+
if (type.module.name === "option" && type.name === "Option") return true;
|
|
93
|
+
}
|
|
94
|
+
if (address === HANEUL_FRAMEWORK_ADDRESS) {
|
|
95
|
+
if (type.module.name === "object" && (type.name === "ID" || type.name === "UID")) return true;
|
|
96
|
+
}
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
function renderDataType(type, options) {
|
|
100
|
+
const address = options.resolveAddress(type.module.address);
|
|
101
|
+
if (options.format === "typeTag") {
|
|
102
|
+
if (address === HANEUL_FRAMEWORK_ADDRESS) {
|
|
103
|
+
if (type.module.name === "clock" && type.name === "Clock") return "0x2::clock::Clock";
|
|
104
|
+
if (type.module.name === "random" && type.name === "Random") return "0x2::random::Random";
|
|
105
|
+
if (type.module.name === "deny_list" && type.name === "DenyList") return "0x2::deny_list::DenyList";
|
|
106
|
+
if (type.module.name === "object" && (type.name === "ID" || type.name === "UID")) return "0x2::object::ID";
|
|
107
|
+
}
|
|
108
|
+
if (address === HANEUL_SYSTEM_ADDRESS) {
|
|
109
|
+
if (type.module.name === "haneul_system" && type.name === "HaneulSystemState") return "0x3::haneul_system::HaneulSystemState";
|
|
110
|
+
}
|
|
111
|
+
if (address === MOVE_STDLIB_ADDRESS) {
|
|
112
|
+
if ((type.module.name === "ascii" || type.module.name === "string") && type.name === "String") return "0x1::string::String";
|
|
113
|
+
if (type.module.name === "option" && type.name === "Option") return `0x1::option::Option<${renderTypeSignature(type.type_arguments[0].argument, options)}>`;
|
|
114
|
+
}
|
|
115
|
+
return "null";
|
|
116
|
+
}
|
|
117
|
+
if (address === MOVE_STDLIB_ADDRESS) {
|
|
118
|
+
if ((type.module.name === "ascii" || type.module.name === "string") && type.name === "String") switch (options.format) {
|
|
119
|
+
case "typescriptArg": return "string";
|
|
120
|
+
case "bcs": return `${options.bcsImport?.() ?? "bcs"}.string()`;
|
|
121
|
+
default: throw new Error(`Unknown format: ${options.format}`);
|
|
122
|
+
}
|
|
123
|
+
if (type.module.name === "option" && type.name === "Option") switch (options.format) {
|
|
124
|
+
case "typescriptArg":
|
|
125
|
+
if (isPureDataType(type, options)) return `${renderTypeSignature(type.type_arguments[0].argument, options)} | null`;
|
|
126
|
+
break;
|
|
127
|
+
case "bcs": return `${options.bcsImport?.() ?? "bcs"}.option(${renderTypeSignature(type.type_arguments[0].argument, options)})`;
|
|
128
|
+
default: throw new Error(`Unknown format: ${options.format}`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (address === HANEUL_FRAMEWORK_ADDRESS) {
|
|
132
|
+
if (type.module.name === "object" && (type.name === "ID" || type.name === "UID")) switch (options.format) {
|
|
133
|
+
case "typescriptArg": return "string";
|
|
134
|
+
case "bcs": return `${options.bcsImport?.() ?? "bcs"}.Address`;
|
|
135
|
+
default: throw new Error(`Unknown format: ${options.format}`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const isCurrentModule = address === options.resolveAddress(options.summary.id.address) && type.module.name === options.summary.id.name;
|
|
139
|
+
const importName = options.onDependency?.(type.module.address, type.module.name, type.name);
|
|
140
|
+
const typeNameRef = isCurrentModule ? type.name : `${importName ?? getSafeName(type.module.name)}.${getSafeName(type.name)}`;
|
|
141
|
+
const filteredTypeArguments = type.type_arguments.filter((arg) => !arg.phantom);
|
|
142
|
+
switch (options.format) {
|
|
143
|
+
case "typescriptArg": return "string";
|
|
144
|
+
case "bcs":
|
|
145
|
+
if (filteredTypeArguments.length === 0) return typeNameRef;
|
|
146
|
+
return `${typeNameRef}(
|
|
147
|
+
${filteredTypeArguments.map((type$1) => renderTypeSignature(type$1.argument, options)).join(", ")})`;
|
|
148
|
+
default: throw new Error(`Unknown format: ${options.format}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const JS_RESERVED_NAMES = [
|
|
152
|
+
"new",
|
|
153
|
+
"delete",
|
|
154
|
+
"class",
|
|
155
|
+
"function",
|
|
156
|
+
"import",
|
|
157
|
+
"export",
|
|
158
|
+
"return",
|
|
159
|
+
"this",
|
|
160
|
+
"super",
|
|
161
|
+
"arguments",
|
|
162
|
+
"eval",
|
|
163
|
+
"void",
|
|
164
|
+
"typeof",
|
|
165
|
+
"instanceof",
|
|
166
|
+
"delete",
|
|
167
|
+
"in",
|
|
168
|
+
"from",
|
|
169
|
+
"of",
|
|
170
|
+
"as",
|
|
171
|
+
"async",
|
|
172
|
+
"await",
|
|
173
|
+
"break",
|
|
174
|
+
"case",
|
|
175
|
+
"catch",
|
|
176
|
+
"continue",
|
|
177
|
+
"debugger",
|
|
178
|
+
"default",
|
|
179
|
+
"do",
|
|
180
|
+
"else",
|
|
181
|
+
"finally",
|
|
182
|
+
"for",
|
|
183
|
+
"function",
|
|
184
|
+
"if",
|
|
185
|
+
"import",
|
|
186
|
+
"in",
|
|
187
|
+
"instanceof",
|
|
188
|
+
"new",
|
|
189
|
+
"return",
|
|
190
|
+
"switch",
|
|
191
|
+
"throw",
|
|
192
|
+
"try",
|
|
193
|
+
"typeof",
|
|
194
|
+
"var",
|
|
195
|
+
"void",
|
|
196
|
+
"while",
|
|
197
|
+
"with",
|
|
198
|
+
"yield",
|
|
199
|
+
"package"
|
|
200
|
+
];
|
|
201
|
+
function getSafeName(name) {
|
|
202
|
+
return JS_RESERVED_NAMES.includes(name) ? `_${name}` : name;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
//#endregion
|
|
206
|
+
export { HANEUL_FRAMEWORK_ADDRESS, HANEUL_SYSTEM_ADDRESS, getSafeName, renderTypeSignature };
|
|
207
|
+
//# sourceMappingURL=render-types.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render-types.mjs","names":["type"],"sources":["../src/render-types.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { normalizeHaneulAddress } from '@haneullabs/haneul/utils';\n\nimport type {\n\tDatatype,\n\tDatatypeParameter,\n\tModuleSummary,\n\tType,\n\tTypeParameter,\n} from './types/summary.js';\n\nexport const MOVE_STDLIB_ADDRESS = normalizeHaneulAddress('0x1');\nexport const HANEUL_FRAMEWORK_ADDRESS = normalizeHaneulAddress('0x2');\nexport const HANEUL_SYSTEM_ADDRESS = normalizeHaneulAddress('0x3');\n\ntype TypeSignatureFormat = 'typescriptArg' | 'bcs' | 'typeTag';\ninterface RenderTypeSignatureOptions {\n\tformat: TypeSignatureFormat;\n\tsummary: ModuleSummary;\n\ttypeParameters?: TypeParameter[];\n\tonDependency?: (address: string, module: string, type: string) => string | undefined;\n\tbcsImport?: () => string;\n\tonTypeParameter?: (typeParameter: number | string) => void;\n\tresolveAddress: (address: string) => string;\n\tincludePhantomTypeParameters: boolean;\n}\n\nfunction getFilteredTypeParameterIndex(\n\toriginalIndex: number,\n\ttypeParameters: TypeParameter[] | undefined,\n\tincludePhantom: boolean,\n): number {\n\tif (includePhantom || !typeParameters) return originalIndex;\n\n\tlet filteredIndex = 0;\n\tfor (let i = 0; i < originalIndex; i++) {\n\t\tconst param = typeParameters[i] as DatatypeParameter | undefined;\n\t\tif (!param?.phantom) {\n\t\t\tfilteredIndex++;\n\t\t}\n\t}\n\treturn filteredIndex;\n}\n\nexport function renderTypeSignature(type: Type, options: RenderTypeSignatureOptions): string {\n\tlet bcs = 'bcs';\n\tif (options.bcsImport && usesBcs(type, options)) {\n\t\tbcs = options.bcsImport();\n\t}\n\n\tswitch (type) {\n\t\tcase 'address':\n\t\t\tswitch (options.format) {\n\t\t\t\tcase 'typescriptArg':\n\t\t\t\t\treturn 'string';\n\t\t\t\tcase 'typeTag':\n\t\t\t\t\treturn `address`;\n\t\t\t\tcase 'bcs':\n\t\t\t\t\treturn `${bcs}.Address`;\n\t\t\t\tdefault:\n\t\t\t\t\tthrow new Error(`Unknown format: ${options.format}`);\n\t\t\t}\n\t\tcase 'bool':\n\t\t\tswitch (options.format) {\n\t\t\t\tcase 'typescriptArg':\n\t\t\t\t\treturn 'boolean';\n\t\t\t\tcase 'typeTag':\n\t\t\t\t\treturn `bool`;\n\t\t\t\tcase 'bcs':\n\t\t\t\t\treturn `${bcs}.bool()`;\n\t\t\t\tdefault:\n\t\t\t\t\tthrow new Error(`Unknown format: ${options.format}`);\n\t\t\t}\n\t\tcase 'u8':\n\t\tcase 'u16':\n\t\tcase 'u32':\n\t\t\tswitch (options.format) {\n\t\t\t\tcase 'typescriptArg':\n\t\t\t\t\treturn 'number';\n\t\t\t\tcase 'typeTag':\n\t\t\t\t\treturn type.toLowerCase();\n\t\t\t\tcase 'bcs':\n\t\t\t\t\treturn `${bcs}.${type.toLowerCase()}()`;\n\t\t\t\tdefault:\n\t\t\t\t\tthrow new Error(`Unknown format: ${options.format}`);\n\t\t\t}\n\t\tcase 'u64':\n\t\tcase 'u128':\n\t\tcase 'u256':\n\t\t\tswitch (options.format) {\n\t\t\t\tcase 'typescriptArg':\n\t\t\t\t\treturn `number | bigint`;\n\t\t\t\tcase 'typeTag':\n\t\t\t\t\treturn type.toLowerCase();\n\t\t\t\tcase 'bcs':\n\t\t\t\t\treturn `${bcs}.${type.toLowerCase()}()`;\n\t\t\t\tdefault:\n\t\t\t\t\tthrow new Error(`Unknown format: ${options.format}`);\n\t\t\t}\n\t\tcase 'signer':\n\t\t\tthrow new Error('Signer is not supported');\n\t\tcase '_':\n\t\t\tthrow new Error('Macro placeholder is not supported');\n\t}\n\n\tif ('Datatype' in type) {\n\t\treturn renderDataType(type.Datatype, options);\n\t}\n\n\tif ('Reference' in type) {\n\t\treturn renderTypeSignature(type.Reference[1], options);\n\t}\n\n\tif ('vector' in type) {\n\t\tswitch (options.format) {\n\t\t\tcase 'typescriptArg':\n\t\t\t\treturn `${renderTypeSignature(type.vector, options)}[]`;\n\t\t\tcase 'typeTag':\n\t\t\t\treturn `vector<${renderTypeSignature(type.vector, options)}>`;\n\t\t\tcase 'bcs':\n\t\t\t\treturn `${bcs}.vector(${renderTypeSignature(type.vector, options)})`;\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Unknown format: ${options.format}`);\n\t\t}\n\t}\n\n\tif ('TypeParameter' in type) {\n\t\toptions.onTypeParameter?.(type.TypeParameter);\n\t\tconst originalIndex = type.TypeParameter;\n\t\tconst filteredIndex = getFilteredTypeParameterIndex(\n\t\t\toriginalIndex,\n\t\t\toptions.typeParameters,\n\t\t\toptions.includePhantomTypeParameters,\n\t\t);\n\t\tswitch (options.format) {\n\t\t\tcase 'typescriptArg':\n\t\t\t\treturn options.typeParameters?.[originalIndex]?.name ?? `T${originalIndex}`;\n\t\t\tcase 'typeTag':\n\t\t\t\treturn `\\${options.typeArguments[${originalIndex}]}`;\n\t\t\tcase 'bcs':\n\t\t\t\treturn `typeParameters[${filteredIndex}]`;\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Unknown format: ${options.format}`);\n\t\t}\n\t}\n\n\tif ('NamedTypeParameter' in type) {\n\t\toptions.onTypeParameter?.(type.NamedTypeParameter);\n\t\tconst originalIndex =\n\t\t\toptions.typeParameters?.findIndex((p) => p.name === type.NamedTypeParameter) ?? -1;\n\n\t\tif (originalIndex === -1) {\n\t\t\tthrow new Error(`Named type parameter ${type.NamedTypeParameter} not found`);\n\t\t}\n\n\t\tconst filteredIndex = getFilteredTypeParameterIndex(\n\t\t\toriginalIndex,\n\t\t\toptions.typeParameters,\n\t\t\toptions.includePhantomTypeParameters,\n\t\t);\n\n\t\tswitch (options.format) {\n\t\t\tcase 'typescriptArg':\n\t\t\t\treturn type.NamedTypeParameter;\n\t\t\tcase 'typeTag':\n\t\t\t\treturn `\\${options.typeArguments[${originalIndex}]}`;\n\t\t\tcase 'bcs':\n\t\t\t\treturn `typeParameters[${filteredIndex}]`;\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Unknown format: ${options.format}`);\n\t\t}\n\t}\n\n\tthrow new Error(`Unknown type signature: ${JSON.stringify(type, null, 2)}`);\n}\n\nexport function usesBcs(type: Type, options: RenderTypeSignatureOptions): boolean {\n\tif (typeof type === 'string') {\n\t\treturn true;\n\t}\n\n\tif ('Reference' in type) {\n\t\treturn usesBcs(type.Reference[1], options);\n\t}\n\n\tif ('Datatype' in type) {\n\t\treturn isPureDataType(type.Datatype, options);\n\t}\n\n\tif ('vector' in type) {\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nexport function isPureSignature(type: Type, options: RenderTypeSignatureOptions): boolean {\n\tif (typeof type === 'string') {\n\t\treturn true;\n\t}\n\n\tif ('Reference' in type) {\n\t\treturn isPureSignature(type.Reference[1], options);\n\t}\n\n\tif ('Datatype' in type) {\n\t\treturn isPureDataType(type.Datatype, options);\n\t}\n\n\tif ('vector' in type) {\n\t\treturn isPureSignature(type.vector, options);\n\t}\n\n\tif ('TypeParameter' in type) {\n\t\treturn false;\n\t}\n\n\tif ('NamedTypeParameter' in type) {\n\t\treturn false;\n\t}\n\n\tthrow new Error(`Unknown type signature: ${JSON.stringify(type, null, 2)}`);\n}\n\nfunction isPureDataType(type: Datatype, options: RenderTypeSignatureOptions) {\n\tconst address = options.resolveAddress(type.module.address);\n\n\tif (address === MOVE_STDLIB_ADDRESS) {\n\t\tif ((type.module.name === 'ascii' || type.module.name === 'string') && type.name === 'String') {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (type.module.name === 'option' && type.name === 'Option') {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\tif (address === HANEUL_FRAMEWORK_ADDRESS) {\n\t\tif (type.module.name === 'object' && (type.name === 'ID' || type.name === 'UID')) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\treturn false;\n}\n\nfunction renderDataType(type: Datatype, options: RenderTypeSignatureOptions): string {\n\tconst address = options.resolveAddress(type.module.address);\n\n\tif (options.format === 'typeTag') {\n\t\tif (address === HANEUL_FRAMEWORK_ADDRESS) {\n\t\t\tif (type.module.name === 'clock' && type.name === 'Clock') return '0x2::clock::Clock';\n\t\t\tif (type.module.name === 'random' && type.name === 'Random') return '0x2::random::Random';\n\t\t\tif (type.module.name === 'deny_list' && type.name === 'DenyList')\n\t\t\t\treturn '0x2::deny_list::DenyList';\n\t\t\tif (type.module.name === 'object' && (type.name === 'ID' || type.name === 'UID'))\n\t\t\t\treturn '0x2::object::ID';\n\t\t}\n\t\tif (address === HANEUL_SYSTEM_ADDRESS) {\n\t\t\tif (type.module.name === 'haneul_system' && type.name === 'HaneulSystemState')\n\t\t\t\treturn '0x3::haneul_system::HaneulSystemState';\n\t\t}\n\n\t\tif (address === MOVE_STDLIB_ADDRESS) {\n\t\t\tif (\n\t\t\t\t(type.module.name === 'ascii' || type.module.name === 'string') &&\n\t\t\t\ttype.name === 'String'\n\t\t\t) {\n\t\t\t\treturn '0x1::string::String';\n\t\t\t}\n\t\t\tif (type.module.name === 'option' && type.name === 'Option') {\n\t\t\t\tconst innerType = renderTypeSignature(type.type_arguments[0].argument, options);\n\t\t\t\treturn `0x1::option::Option<${innerType}>`;\n\t\t\t}\n\t\t}\n\n\t\treturn 'null';\n\t}\n\n\tif (address === MOVE_STDLIB_ADDRESS) {\n\t\tif ((type.module.name === 'ascii' || type.module.name === 'string') && type.name === 'String') {\n\t\t\tswitch (options.format) {\n\t\t\t\tcase 'typescriptArg':\n\t\t\t\t\treturn 'string';\n\t\t\t\tcase 'bcs':\n\t\t\t\t\treturn `${options.bcsImport?.() ?? 'bcs'}.string()`;\n\t\t\t\tdefault:\n\t\t\t\t\tthrow new Error(`Unknown format: ${options.format}`);\n\t\t\t}\n\t\t}\n\n\t\tif (type.module.name === 'option' && type.name === 'Option') {\n\t\t\tswitch (options.format) {\n\t\t\t\tcase 'typescriptArg':\n\t\t\t\t\tif (isPureDataType(type, options)) {\n\t\t\t\t\t\treturn `${renderTypeSignature(type.type_arguments[0].argument, options)} | null`;\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'bcs':\n\t\t\t\t\treturn `${options.bcsImport?.() ?? 'bcs'}.option(${renderTypeSignature(type.type_arguments[0].argument, options)})`;\n\t\t\t\tdefault:\n\t\t\t\t\tthrow new Error(`Unknown format: ${options.format}`);\n\t\t\t}\n\t\t}\n\t}\n\n\tif (address === HANEUL_FRAMEWORK_ADDRESS) {\n\t\tif (type.module.name === 'object' && (type.name === 'ID' || type.name === 'UID')) {\n\t\t\tswitch (options.format) {\n\t\t\t\tcase 'typescriptArg':\n\t\t\t\t\treturn 'string';\n\t\t\t\tcase 'bcs':\n\t\t\t\t\treturn `${options.bcsImport?.() ?? 'bcs'}.Address`;\n\t\t\t\tdefault:\n\t\t\t\t\tthrow new Error(`Unknown format: ${options.format}`);\n\t\t\t}\n\t\t}\n\t}\n\n\tconst isCurrentModule =\n\t\taddress === options.resolveAddress(options.summary.id.address) &&\n\t\ttype.module.name === options.summary.id.name;\n\n\tconst importName = options.onDependency?.(type.module.address, type.module.name, type.name);\n\n\tconst typeNameRef = isCurrentModule\n\t\t? type.name\n\t\t: `${importName ?? getSafeName(type.module.name)}.${getSafeName(type.name)}`;\n\n\tconst filteredTypeArguments = type.type_arguments.filter((arg) => !arg.phantom);\n\n\tswitch (options.format) {\n\t\tcase 'typescriptArg':\n\t\t\treturn 'string';\n\t\tcase 'bcs':\n\t\t\tif (filteredTypeArguments.length === 0) {\n\t\t\t\treturn typeNameRef;\n\t\t\t}\n\n\t\t\treturn `${typeNameRef}(\n ${filteredTypeArguments.map((type) => renderTypeSignature(type.argument, options)).join(', ')})`;\n\t\tdefault:\n\t\t\tthrow new Error(`Unknown format: ${options.format}`);\n\t}\n}\n\nconst JS_RESERVED_NAMES = [\n\t'new',\n\t'delete',\n\t'class',\n\t'function',\n\t'import',\n\t'export',\n\t'return',\n\t'this',\n\t'super',\n\t'arguments',\n\t'eval',\n\t'void',\n\t'typeof',\n\t'instanceof',\n\t'delete',\n\t'in',\n\t'from',\n\t'of',\n\t'as',\n\t'async',\n\t'await',\n\t'break',\n\t'case',\n\t'catch',\n\t'continue',\n\t'debugger',\n\t'default',\n\t'do',\n\t'else',\n\t'finally',\n\t'for',\n\t'function',\n\t'if',\n\t'import',\n\t'in',\n\t'instanceof',\n\t'new',\n\t'return',\n\t'switch',\n\t'throw',\n\t'try',\n\t'typeof',\n\t'var',\n\t'void',\n\t'while',\n\t'with',\n\t'yield',\n\t'package',\n];\n\nexport function getSafeName(name: string) {\n\treturn JS_RESERVED_NAMES.includes(name) ? `_${name}` : name;\n}\n"],"mappings":";;;AAaA,MAAa,sBAAsB,uBAAuB,MAAM;AAChE,MAAa,2BAA2B,uBAAuB,MAAM;AACrE,MAAa,wBAAwB,uBAAuB,MAAM;AAclE,SAAS,8BACR,eACA,gBACA,gBACS;AACT,KAAI,kBAAkB,CAAC,eAAgB,QAAO;CAE9C,IAAI,gBAAgB;AACpB,MAAK,IAAI,IAAI,GAAG,IAAI,eAAe,IAElC,KAAI,CADU,eAAe,IACjB,QACX;AAGF,QAAO;;AAGR,SAAgB,oBAAoB,MAAY,SAA6C;CAC5F,IAAI,MAAM;AACV,KAAI,QAAQ,aAAa,QAAQ,MAAM,QAAQ,CAC9C,OAAM,QAAQ,WAAW;AAG1B,SAAQ,MAAR;EACC,KAAK,UACJ,SAAQ,QAAQ,QAAhB;GACC,KAAK,gBACJ,QAAO;GACR,KAAK,UACJ,QAAO;GACR,KAAK,MACJ,QAAO,GAAG,IAAI;GACf,QACC,OAAM,IAAI,MAAM,mBAAmB,QAAQ,SAAS;;EAEvD,KAAK,OACJ,SAAQ,QAAQ,QAAhB;GACC,KAAK,gBACJ,QAAO;GACR,KAAK,UACJ,QAAO;GACR,KAAK,MACJ,QAAO,GAAG,IAAI;GACf,QACC,OAAM,IAAI,MAAM,mBAAmB,QAAQ,SAAS;;EAEvD,KAAK;EACL,KAAK;EACL,KAAK,MACJ,SAAQ,QAAQ,QAAhB;GACC,KAAK,gBACJ,QAAO;GACR,KAAK,UACJ,QAAO,KAAK,aAAa;GAC1B,KAAK,MACJ,QAAO,GAAG,IAAI,GAAG,KAAK,aAAa,CAAC;GACrC,QACC,OAAM,IAAI,MAAM,mBAAmB,QAAQ,SAAS;;EAEvD,KAAK;EACL,KAAK;EACL,KAAK,OACJ,SAAQ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QAAO,GAAG,QAAQ,aAAa,IAAI,MAAM;GAC1C,QACC,OAAM,IAAI,MAAM,mBAAmB,QAAQ,SAAS;;;CAKxD,MAAM,kBACL,YAAY,QAAQ,eAAe,QAAQ,QAAQ,GAAG,QAAQ,IAC9D,KAAK,OAAO,SAAS,QAAQ,QAAQ,GAAG;CAEzC,MAAM,aAAa,QAAQ,eAAe,KAAK,OAAO,SAAS,KAAK,OAAO,MAAM,KAAK,KAAK;CAE3F,MAAM,cAAc,kBACjB,KAAK,OACL,GAAG,cAAc,YAAY,KAAK,OAAO,KAAK,CAAC,GAAG,YAAY,KAAK,KAAK;CAE3E,MAAM,wBAAwB,KAAK,eAAe,QAAQ,QAAQ,CAAC,IAAI,QAAQ;AAE/E,SAAQ,QAAQ,QAAhB;EACC,KAAK,gBACJ,QAAO;EACR,KAAK;AACJ,OAAI,sBAAsB,WAAW,EACpC,QAAO;AAGR,UAAO,GAAG,YAAY;kBACP,sBAAsB,KAAK,WAAS,oBAAoBA,OAAK,UAAU,QAAQ,CAAC,CAAC,KAAK,KAAK,CAAC;EAC5G,QACC,OAAM,IAAI,MAAM,mBAAmB,QAAQ,SAAS;;;AAIvD,MAAM,oBAAoB;CACzB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AAED,SAAgB,YAAY,MAAc;AACzC,QAAO,kBAAkB,SAAS,KAAK,GAAG,IAAI,SAAS"}
|
package/dist/utils.mjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { HANEUL_FRAMEWORK_ADDRESS, HANEUL_SYSTEM_ADDRESS } from "./render-types.mjs";
|
|
2
|
+
import { format } from "prettier";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
|
|
5
|
+
//#region src/utils.ts
|
|
6
|
+
function printNodes(...nodes) {
|
|
7
|
+
const printer = ts.createPrinter({ removeComments: false });
|
|
8
|
+
return nodes.map((node) => printer.printNode(ts.EmitHint.Unspecified, node, node.getSourceFile())).join("\n");
|
|
9
|
+
}
|
|
10
|
+
function parseTS(strings, ...values) {
|
|
11
|
+
const lines = strings.reduce((acc, str, i) => {
|
|
12
|
+
if (typeof values[i] === "object") {
|
|
13
|
+
if (Array.isArray(values[i])) return `${acc}${str}${printNodes(...values[i])}`;
|
|
14
|
+
return `${acc}${str}${printNodes(values[i])}`;
|
|
15
|
+
}
|
|
16
|
+
return `${acc}${str}${values[i] ?? ""}`;
|
|
17
|
+
}, "").replace(/^\s/m, "").split("\n");
|
|
18
|
+
const indent = lines[0].match(/^\s*/)?.[0] ?? "";
|
|
19
|
+
const unIndented = lines.map((line) => line.replace(indent, "")).join("\n");
|
|
20
|
+
return [...ts.createSourceFile("file.ts", unIndented, ts.ScriptTarget.Latest, true).statements.values()];
|
|
21
|
+
}
|
|
22
|
+
async function mapToObject({ items, mapper, getComment }) {
|
|
23
|
+
const fieldProps = await Promise.all((await Promise.all([...items].map(async (item) => {
|
|
24
|
+
const mapped = await mapper(item);
|
|
25
|
+
if (!mapped) return null;
|
|
26
|
+
const [key, value] = mapped;
|
|
27
|
+
return [
|
|
28
|
+
item,
|
|
29
|
+
key,
|
|
30
|
+
value
|
|
31
|
+
];
|
|
32
|
+
}))).filter((value) => value !== null).map(([item, key, value]) => {
|
|
33
|
+
const [node] = parseTS`({${key}: ${value}})`;
|
|
34
|
+
if (!ts.isExpressionStatement(node)) throw new Error("Expected Expression statement");
|
|
35
|
+
if (!ts.isParenthesizedExpression(node.expression)) throw new Error("Expected Parenthesized Expression");
|
|
36
|
+
if (!ts.isObjectLiteralExpression(node.expression.expression)) throw new Error("Expected Object Literal Expression");
|
|
37
|
+
const comment = getComment?.(item);
|
|
38
|
+
return withComment({ comment }, node.expression.expression.properties[0]);
|
|
39
|
+
}));
|
|
40
|
+
return ts.factory.createObjectLiteralExpression(fieldProps, true);
|
|
41
|
+
}
|
|
42
|
+
async function withComment(options, nodes) {
|
|
43
|
+
const comment = options.comment ?? options.doc;
|
|
44
|
+
if (comment) {
|
|
45
|
+
const firstNode = Array.isArray(nodes) ? nodes[0] : nodes;
|
|
46
|
+
ts.addSyntheticLeadingComment(firstNode, ts.SyntaxKind.MultiLineCommentTrivia, await formatComment(comment), true);
|
|
47
|
+
}
|
|
48
|
+
return nodes;
|
|
49
|
+
}
|
|
50
|
+
async function formatComment(text) {
|
|
51
|
+
const lines = (await format(text, {
|
|
52
|
+
printWidth: 80,
|
|
53
|
+
semi: true,
|
|
54
|
+
singleQuote: true,
|
|
55
|
+
tabWidth: 2,
|
|
56
|
+
trailingComma: "all",
|
|
57
|
+
useTabs: true,
|
|
58
|
+
proseWrap: "always",
|
|
59
|
+
parser: "markdown"
|
|
60
|
+
})).trim().replaceAll("*/", "*\\/").split("\n");
|
|
61
|
+
if (lines.length === 1) return `* ${lines[0]} `;
|
|
62
|
+
return `*\n${lines.map((line) => ` * ${line}`).join("\n")}\n `;
|
|
63
|
+
}
|
|
64
|
+
function camelCase(str) {
|
|
65
|
+
return str.replaceAll(/(?:_)([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
66
|
+
}
|
|
67
|
+
function capitalize(str) {
|
|
68
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
69
|
+
}
|
|
70
|
+
function isWellKnownObjectParameter(type, resolveAddress) {
|
|
71
|
+
if (typeof type === "string") return false;
|
|
72
|
+
if ("Reference" in type) return isWellKnownObjectParameter(type.Reference[1], resolveAddress);
|
|
73
|
+
if (!("Datatype" in type)) return false;
|
|
74
|
+
const { Datatype } = type;
|
|
75
|
+
const address = resolveAddress(Datatype.module.address);
|
|
76
|
+
if (address === HANEUL_FRAMEWORK_ADDRESS) {
|
|
77
|
+
if (Datatype.module.name === "deny_list") return Datatype.name === "DenyList";
|
|
78
|
+
if (Datatype.module.name === "random") return Datatype.name === "Random";
|
|
79
|
+
if (Datatype.module.name === "clock") return Datatype.name === "Clock";
|
|
80
|
+
}
|
|
81
|
+
if (address === HANEUL_SYSTEM_ADDRESS) {
|
|
82
|
+
if (Datatype.module.name === "haneul_system") return Datatype.name === "HaneulSystemState";
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
export { camelCase, capitalize, formatComment, isWellKnownObjectParameter, mapToObject, parseTS, printNodes, withComment };
|
|
89
|
+
//# sourceMappingURL=utils.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.mjs","names":[],"sources":["../src/utils.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\nimport { format } from 'prettier';\nimport ts from 'typescript';\nimport type { Type } from './types/summary.js';\nimport { HANEUL_FRAMEWORK_ADDRESS, HANEUL_SYSTEM_ADDRESS } from './render-types.js';\n\nexport function printNodes(...nodes: ts.Node[]) {\n\tconst printer = ts.createPrinter({\n\t\tremoveComments: false,\n\t});\n\n\treturn nodes\n\t\t.map((node) => printer.printNode(ts.EmitHint.Unspecified, node, node.getSourceFile()))\n\t\t.join('\\n');\n}\n\ntype TSTemplateValue = string | number | boolean | ts.Statement[] | ts.Expression;\n\nexport function parseTS(strings: TemplateStringsArray, ...values: TSTemplateValue[]) {\n\tconst source = strings.reduce((acc, str, i) => {\n\t\tif (typeof values[i] === 'object') {\n\t\t\tif (Array.isArray(values[i])) {\n\t\t\t\treturn `${acc}${str}${printNodes(...values[i])}`;\n\t\t\t}\n\n\t\t\treturn `${acc}${str}${printNodes(values[i])}`;\n\t\t}\n\n\t\treturn `${acc}${str}${values[i] ?? ''}`;\n\t}, '');\n\n\tconst lines = source.replace(/^\\s/m, '').split('\\n');\n\tconst firstLine = lines[0];\n\tconst indent = firstLine.match(/^\\s*/)?.[0] ?? '';\n\tconst unIndented = lines.map((line) => line.replace(indent, '')).join('\\n');\n\n\tconst sourceFile = ts.createSourceFile('file.ts', unIndented, ts.ScriptTarget.Latest, true);\n\treturn [...sourceFile.statements.values()];\n}\n\nexport async function mapToObject<T>({\n\titems,\n\tmapper,\n\tgetComment,\n}: {\n\titems: Iterable<T>;\n\tmapper: (item: T) => Promise<null | [string, TSTemplateValue]> | null | [string, TSTemplateValue];\n\tgetComment?: (item: T) => string | null | undefined;\n}) {\n\tconst fieldProps = await Promise.all(\n\t\t(\n\t\t\tawait Promise.all(\n\t\t\t\t[...items].map(async (item) => {\n\t\t\t\t\tconst mapped = await mapper(item);\n\t\t\t\t\tif (!mapped) {\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\n\t\t\t\t\tconst [key, value] = mapped;\n\t\t\t\t\treturn [item, key, value] as const;\n\t\t\t\t}),\n\t\t\t)\n\t\t)\n\t\t\t.filter((value) => value !== null)\n\t\t\t.map(([item, key, value]) => {\n\t\t\t\tconst [node] = parseTS /* ts */ `({${key}: ${value}})`;\n\n\t\t\t\tif (!ts.isExpressionStatement(node)) {\n\t\t\t\t\tthrow new Error('Expected Expression statement');\n\t\t\t\t}\n\n\t\t\t\tif (!ts.isParenthesizedExpression(node.expression)) {\n\t\t\t\t\tthrow new Error('Expected Parenthesized Expression');\n\t\t\t\t}\n\n\t\t\t\tif (!ts.isObjectLiteralExpression(node.expression.expression)) {\n\t\t\t\t\tthrow new Error('Expected Object Literal Expression');\n\t\t\t\t}\n\n\t\t\t\tconst comment = getComment?.(item);\n\t\t\t\treturn withComment({ comment }, node.expression.expression.properties[0]);\n\t\t\t}),\n\t);\n\n\treturn ts.factory.createObjectLiteralExpression(fieldProps, true);\n}\n\nexport async function withComment<T extends ts.Node | ts.Node[]>(\n\toptions:\n\t\t| { comment?: string | null; doc?: never }\n\t\t| { doc?: string | null | undefined; comment?: never },\n\tnodes: T,\n): Promise<T> {\n\tconst comment = options.comment ?? options.doc;\n\n\tif (comment) {\n\t\tconst firstNode = Array.isArray(nodes) ? nodes[0] : (nodes as ts.Node);\n\t\tts.addSyntheticLeadingComment(\n\t\t\tfirstNode,\n\t\t\tts.SyntaxKind.MultiLineCommentTrivia,\n\t\t\tawait formatComment(comment),\n\t\t\ttrue,\n\t\t);\n\t}\n\n\treturn nodes;\n}\n\nexport async function formatComment(text: string) {\n\tconst lines = (\n\t\tawait format(text, {\n\t\t\tprintWidth: 80,\n\t\t\tsemi: true,\n\t\t\tsingleQuote: true,\n\t\t\ttabWidth: 2,\n\t\t\ttrailingComma: 'all',\n\t\t\tuseTabs: true,\n\t\t\tproseWrap: 'always',\n\t\t\tparser: 'markdown',\n\t\t})\n\t)\n\t\t.trim()\n\t\t.replaceAll('*/', '*\\\\/')\n\t\t.split('\\n');\n\n\tif (lines.length === 1) return `* ${lines[0]} `;\n\n\treturn `*\\n${lines.map((line) => ` * ${line}`).join('\\n')}\\n `;\n}\n\nexport function camelCase(str: string) {\n\treturn str.replaceAll(/(?:_)([a-z])/g, (_, letter) => letter.toUpperCase());\n}\n\nexport function capitalize(str: string) {\n\treturn str.charAt(0).toUpperCase() + str.slice(1);\n}\n\nexport function isWellKnownObjectParameter(\n\ttype: Type,\n\tresolveAddress: (address: string) => string,\n) {\n\tif (typeof type === 'string') {\n\t\treturn false;\n\t}\n\n\tif ('Reference' in type) {\n\t\treturn isWellKnownObjectParameter(type.Reference[1], resolveAddress);\n\t}\n\n\tif (!('Datatype' in type)) {\n\t\treturn false;\n\t}\n\n\tconst { Datatype } = type;\n\n\tconst address = resolveAddress(Datatype.module.address);\n\tif (address === HANEUL_FRAMEWORK_ADDRESS) {\n\t\tif (Datatype.module.name === 'deny_list') {\n\t\t\treturn Datatype.name === 'DenyList';\n\t\t}\n\n\t\tif (Datatype.module.name === 'random') {\n\t\t\treturn Datatype.name === 'Random';\n\t\t}\n\n\t\tif (Datatype.module.name === 'clock') {\n\t\t\treturn Datatype.name === 'Clock';\n\t\t}\n\t}\n\n\t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AO,EAAE,CAAC,aAAa,GAAG,IAAI,MAAM,EAAE;;AAGlD,SAAgB,2BACf,MACA,gBACC;AACD,KAAI,OAAO,SAAS,SACnB,QAAO;AAGR,KAAI,eAAe,KAClB,QAAO,2BAA2B,KAAK,UAAU,IAAI,eAAe;AAGrE,KAAI,EAAE,cAAc,MACnB,QAAO;CAGR,MAAM,EAAE,aAAa;CAErB,MAAM,UAAU,eAAe,SAAS,OAAO,QAAQ;AACvD,KAAI,YAAY,0BAA0B;AACzC,MAAI,SAAS,OAAO,SAAS,YAC5B,QAAO,SAAS,SAAS;AAG1B,MAAI,SAAS,OAAO,SAAS,SAC5B,QAAO,SAAS,SAAS;AAG1B,MAAI,SAAS,OAAO,SAAS,QAC5B,QAAO,SAAS,SAAS;;AAI3B,KAAI,YAAY,uBACf;MAAI,SAAS,OAAO,SAAS,gBAC5B,QAAO,SAAS,SAAS;;AAI3B,QAAO"}
|
package/package.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@haneullabs/codegen",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "typescript codegen for
|
|
3
|
+
"version": "0.8.2",
|
|
4
|
+
"description": "typescript codegen for haneul move",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
|
-
"author": "Haneul Labs <build@
|
|
6
|
+
"author": "Haneul Labs <build@haneul-labs.com>",
|
|
7
7
|
"sideEffects": false,
|
|
8
|
-
"type": "
|
|
9
|
-
"main": "./dist/
|
|
10
|
-
"
|
|
11
|
-
"types": "./dist/cjs/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "./dist/index.mjs",
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
12
11
|
"exports": {
|
|
13
12
|
".": {
|
|
14
|
-
"
|
|
15
|
-
"
|
|
13
|
+
"types": "./dist/index.d.mts",
|
|
14
|
+
"import": "./dist/index.mjs",
|
|
15
|
+
"default": "./dist/index.mjs"
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
"files": [
|
|
@@ -21,12 +21,12 @@
|
|
|
21
21
|
"src"
|
|
22
22
|
],
|
|
23
23
|
"bin": {
|
|
24
|
-
"haneul-ts-codegen": "dist/
|
|
25
|
-
"
|
|
24
|
+
"haneul-ts-codegen": "dist/bin/cli.mjs",
|
|
25
|
+
"__sui-ts-codegen_bash_complete": "dist/bin/bash-complete.mjs"
|
|
26
26
|
},
|
|
27
27
|
"repository": {
|
|
28
28
|
"type": "git",
|
|
29
|
-
"url": "git+https://github.com/GeunhwaJeong/
|
|
29
|
+
"url": "git+https://github.com/GeunhwaJeong/ts-sdks.git"
|
|
30
30
|
},
|
|
31
31
|
"keywords": [
|
|
32
32
|
"move",
|
|
@@ -34,37 +34,36 @@
|
|
|
34
34
|
"haneul"
|
|
35
35
|
],
|
|
36
36
|
"bugs": {
|
|
37
|
-
"url": "https://github.com/
|
|
37
|
+
"url": "https://github.com/mystenlabs/ts-sdks/issues"
|
|
38
38
|
},
|
|
39
|
-
"homepage": "https://github.com/GeunhwaJeong/
|
|
39
|
+
"homepage": "https://github.com/GeunhwaJeong/ts-sdks/blob/main/packages/codegen#readme",
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"vitest": "^4.0.
|
|
42
|
-
"@haneullabs/build-scripts": "0.1.0"
|
|
41
|
+
"vitest": "^4.0.17"
|
|
43
42
|
},
|
|
44
43
|
"dependencies": {
|
|
45
|
-
"@stricli/auto-complete": "^1.2.
|
|
46
|
-
"@stricli/core": "^1.2.
|
|
47
|
-
"@types/node": "^
|
|
44
|
+
"@stricli/auto-complete": "^1.2.5",
|
|
45
|
+
"@stricli/core": "^1.2.5",
|
|
46
|
+
"@types/node": "^25.0.8",
|
|
48
47
|
"cosmiconfig": "^9.0.0",
|
|
49
|
-
"prettier": "^3.7.
|
|
48
|
+
"prettier": "^3.7.4",
|
|
50
49
|
"toml": "^3.0.0",
|
|
51
50
|
"typescript": "^5.9.3",
|
|
52
|
-
"zod": "^3.
|
|
53
|
-
"@haneullabs/bcs": "0.
|
|
54
|
-
"@haneullabs/haneul": "
|
|
51
|
+
"zod": "^4.3.5",
|
|
52
|
+
"@haneullabs/bcs": "^2.0.2",
|
|
53
|
+
"@haneullabs/haneul": "^2.4.0"
|
|
55
54
|
},
|
|
56
55
|
"scripts": {
|
|
57
56
|
"clean": "rm -rf tsconfig.tsbuildinfo ./dist",
|
|
58
57
|
"dev": "pnpm build --watch",
|
|
59
|
-
"build": "
|
|
58
|
+
"build": "rm -rf dist && tsc --noEmit && tsdown",
|
|
60
59
|
"codegen": "pnpm tsx src/bin/cli.ts generate && pnpm lint:fix",
|
|
61
60
|
"test": "vitest run",
|
|
62
61
|
"test:watch": "vitest",
|
|
63
62
|
"prettier:check": "prettier -c --ignore-unknown .",
|
|
64
63
|
"prettier:fix": "prettier -w --ignore-unknown .",
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"lint": "pnpm run
|
|
68
|
-
"lint:fix": "pnpm run
|
|
64
|
+
"oxlint:check": "oxlint .",
|
|
65
|
+
"oxlint:fix": "oxlint --fix",
|
|
66
|
+
"lint": "pnpm run oxlint:check && pnpm run prettier:check",
|
|
67
|
+
"lint:fix": "pnpm run oxlint:fix && pnpm run prettier:fix"
|
|
69
68
|
}
|
|
70
69
|
}
|
package/src/bin/bash-complete.ts
CHANGED
|
@@ -3,19 +3,30 @@
|
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
|
|
5
5
|
import { proposeCompletions } from '@stricli/core';
|
|
6
|
+
import { readFile } from 'node:fs/promises';
|
|
7
|
+
import { resolve } from 'node:path';
|
|
6
8
|
import { buildContext } from '../cli/context.js';
|
|
7
9
|
import { buildCli } from '../cli/cli.js';
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
async function getVersion() {
|
|
12
|
+
// @ts-ignore
|
|
13
|
+
const dirname = import.meta.dirname;
|
|
14
|
+
const packageJsonPath = resolve(
|
|
15
|
+
dirname,
|
|
16
|
+
dirname.endsWith('src/bin') ? '../../package.json' : '../../package.json',
|
|
17
|
+
);
|
|
18
|
+
const packageJson: { version: string } = JSON.parse(await readFile(packageJsonPath, 'utf-8'));
|
|
19
|
+
return packageJson.version;
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
export async function main() {
|
|
18
|
-
|
|
23
|
+
const version = await getVersion();
|
|
24
|
+
const cli = buildCli(version);
|
|
25
|
+
|
|
26
|
+
const inputs = process.argv.slice(3);
|
|
27
|
+
if (process.env['COMP_LINE']?.endsWith(' ')) {
|
|
28
|
+
inputs.push('');
|
|
29
|
+
}
|
|
19
30
|
|
|
20
31
|
try {
|
|
21
32
|
for (const { completion } of await proposeCompletions(cli, inputs, buildContext(process))) {
|
package/src/bin/cli.ts
CHANGED
|
@@ -9,18 +9,12 @@ import { readFile } from 'node:fs/promises';
|
|
|
9
9
|
import { resolve } from 'node:path';
|
|
10
10
|
|
|
11
11
|
async function getVersion() {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
try {
|
|
15
|
-
dirname = __dirname;
|
|
16
|
-
} catch {
|
|
17
|
-
// @ts-ignore
|
|
18
|
-
dirname = import.meta.dirname;
|
|
19
|
-
}
|
|
12
|
+
// @ts-ignore
|
|
13
|
+
const dirname = import.meta.dirname;
|
|
20
14
|
|
|
21
15
|
const packageJsonPath = resolve(
|
|
22
16
|
dirname,
|
|
23
|
-
dirname.endsWith('src/bin') ? '../../package.json' : '
|
|
17
|
+
dirname.endsWith('src/bin') ? '../../package.json' : '../../package.json',
|
|
24
18
|
);
|
|
25
19
|
const packageJson: { version: string } = JSON.parse(await readFile(packageJsonPath, 'utf-8'));
|
|
26
20
|
return packageJson.version;
|
|
@@ -38,10 +38,42 @@ export const generateCommand = buildCommand({
|
|
|
38
38
|
optional: true,
|
|
39
39
|
brief: 'Network to generate for (default: testnet)',
|
|
40
40
|
},
|
|
41
|
+
importExtension: {
|
|
42
|
+
kind: 'enum',
|
|
43
|
+
values: ['.js', '.ts', 'none'],
|
|
44
|
+
optional: true,
|
|
45
|
+
brief: 'File extension for import statements (default: .js)',
|
|
46
|
+
},
|
|
47
|
+
modules: {
|
|
48
|
+
kind: 'parsed',
|
|
49
|
+
parse: String,
|
|
50
|
+
optional: true,
|
|
51
|
+
variadic: ',',
|
|
52
|
+
brief: 'Only generate from these modules (comma-separated)',
|
|
53
|
+
},
|
|
54
|
+
noTypes: {
|
|
55
|
+
kind: 'parsed',
|
|
56
|
+
parse: Boolean,
|
|
57
|
+
optional: true,
|
|
58
|
+
brief: 'Skip type generation',
|
|
59
|
+
},
|
|
60
|
+
noFunctions: {
|
|
61
|
+
kind: 'parsed',
|
|
62
|
+
parse: Boolean,
|
|
63
|
+
optional: true,
|
|
64
|
+
brief: 'Skip function generation',
|
|
65
|
+
},
|
|
66
|
+
private: {
|
|
67
|
+
kind: 'enum',
|
|
68
|
+
values: ['none', 'entry', 'all'],
|
|
69
|
+
optional: true,
|
|
70
|
+
brief: 'Which private functions to generate (default: entry)',
|
|
71
|
+
},
|
|
41
72
|
},
|
|
42
73
|
aliases: {
|
|
43
74
|
o: 'outputDir',
|
|
44
75
|
n: 'network',
|
|
76
|
+
m: 'modules',
|
|
45
77
|
},
|
|
46
78
|
},
|
|
47
79
|
docs: {
|