From 38d447b2bfdcd508694ba6c072603b80a1cddf81 Mon Sep 17 00:00:00 2001 From: Gusarich Date: Fri, 10 Jan 2025 03:04:43 +0300 Subject: [PATCH 1/2] feat: use `const` instead of `let` in ts wrappers --- src/bindings/typescript/serializers.ts | 84 +++++++++++++------------- src/bindings/typescript/writeStruct.ts | 12 ++-- src/bindings/writeTypescript.ts | 8 +-- 3 files changed, 52 insertions(+), 52 deletions(-) diff --git a/src/bindings/typescript/serializers.ts b/src/bindings/typescript/serializers.ts index 268ae6474..7e45c326f 100644 --- a/src/bindings/typescript/serializers.ts +++ b/src/bindings/typescript/serializers.ts @@ -43,17 +43,17 @@ const intSerializer: Serializer<{ bits: number; optional: boolean }> = { tsLoad(v, slice, field, w) { if (v.optional) { w.append( - `let ${field} = ${slice}.loadBit() ? ${slice}.loadIntBig(${v.bits}) : null;`, + `const ${field} = ${slice}.loadBit() ? ${slice}.loadIntBig(${v.bits}) : null;`, ); } else { - w.append(`let ${field} = ${slice}.loadIntBig(${v.bits});`); + w.append(`const ${field} = ${slice}.loadIntBig(${v.bits});`); } }, tsLoadTuple(v, reader, field, w) { if (v.optional) { - w.append(`let ${field} = ${reader}.readBigNumberOpt();`); + w.append(`const ${field} = ${reader}.readBigNumberOpt();`); } else { - w.append(`let ${field} = ${reader}.readBigNumber();`); + w.append(`const ${field} = ${reader}.readBigNumber();`); } }, tsStoreTuple(v, to, field, w) { @@ -99,17 +99,17 @@ const uintSerializer: Serializer<{ bits: number; optional: boolean }> = { tsLoad(v, slice, field, w) { if (v.optional) { w.append( - `let ${field} = ${slice}.loadBit() ? ${slice}.loadUintBig(${v.bits}) : null;`, + `const ${field} = ${slice}.loadBit() ? ${slice}.loadUintBig(${v.bits}) : null;`, ); } else { - w.append(`let ${field} = ${slice}.loadUintBig(${v.bits});`); + w.append(`const ${field} = ${slice}.loadUintBig(${v.bits});`); } }, tsLoadTuple(v, reader, field, w) { if (v.optional) { - w.append(`let ${field} = ${reader}.readBigNumberOpt();`); + w.append(`const ${field} = ${reader}.readBigNumberOpt();`); } else { - w.append(`let ${field} = ${reader}.readBigNumber();`); + w.append(`const ${field} = ${reader}.readBigNumber();`); } }, tsStore(v, builder, field, w) { @@ -155,17 +155,17 @@ const coinsSerializer: Serializer<{ optional: boolean }> = { tsLoad(v, slice, field, w) { if (v.optional) { w.append( - `let ${field} = ${slice}.loadBit() ? ${slice}.loadCoins() : null;`, + `const ${field} = ${slice}.loadBit() ? ${slice}.loadCoins() : null;`, ); } else { - w.append(`let ${field} = ${slice}.loadCoins();`); + w.append(`const ${field} = ${slice}.loadCoins();`); } }, tsLoadTuple(v, reader, field, w) { if (v.optional) { - w.append(`let ${field} = ${reader}.readBigNumberOpt();`); + w.append(`const ${field} = ${reader}.readBigNumberOpt();`); } else { - w.append(`let ${field} = ${reader}.readBigNumber();`); + w.append(`const ${field} = ${reader}.readBigNumber();`); } }, tsStore(v, builder, field, w) { @@ -212,17 +212,17 @@ const varIntSerializer: Serializer<{ v.format === "varint16" || v.format === "varuint16" ? 2 : 4; if (v.optional) { w.append( - `let ${field} = ${slice}.loadBit() ? ${slice}.${loader}(${length}) : null;`, + `const ${field} = ${slice}.loadBit() ? ${slice}.${loader}(${length}) : null;`, ); } else { - w.append(`let ${field} = ${slice}.${loader}(${length});`); + w.append(`const ${field} = ${slice}.${loader}(${length});`); } }, tsLoadTuple(v, reader, field, w) { if (v.optional) { - w.append(`let ${field} = ${reader}.readBigNumberOpt();`); + w.append(`const ${field} = ${reader}.readBigNumberOpt();`); } else { - w.append(`let ${field} = ${reader}.readBigNumber();`); + w.append(`const ${field} = ${reader}.readBigNumber();`); } }, tsStore(v, builder, field, w) { @@ -274,17 +274,17 @@ const boolSerializer: Serializer<{ optional: boolean }> = { tsLoad(v, slice, field, w) { if (v.optional) { w.append( - `let ${field} = ${slice}.loadBit() ? ${slice}.loadBit() : null;`, + `const ${field} = ${slice}.loadBit() ? ${slice}.loadBit() : null;`, ); } else { - w.append(`let ${field} = ${slice}.loadBit();`); + w.append(`const ${field} = ${slice}.loadBit();`); } }, tsLoadTuple(v, reader, field, w) { if (v.optional) { - w.append(`let ${field} = ${reader}.readBooleanOpt();`); + w.append(`const ${field} = ${reader}.readBooleanOpt();`); } else { - w.append(`let ${field} = ${reader}.readBoolean();`); + w.append(`const ${field} = ${reader}.readBoolean();`); } }, tsStore(v, builder, field, w) { @@ -321,16 +321,16 @@ const addressSerializer: Serializer<{ optional: boolean }> = { }, tsLoad(v, slice, field, w) { if (v.optional) { - w.append(`let ${field} = ${slice}.loadMaybeAddress();`); + w.append(`const ${field} = ${slice}.loadMaybeAddress();`); } else { - w.append(`let ${field} = ${slice}.loadAddress();`); + w.append(`const ${field} = ${slice}.loadAddress();`); } }, tsLoadTuple(v, reader, field, w) { if (v.optional) { - w.append(`let ${field} = ${reader}.readAddressOpt();`); + w.append(`const ${field} = ${reader}.readAddressOpt();`); } else { - w.append(`let ${field} = ${reader}.readAddress();`); + w.append(`const ${field} = ${reader}.readAddress();`); } }, tsStore(v, builder, field, w) { @@ -383,22 +383,22 @@ const cellSerializer: Serializer<{ tsLoad(v, slice, field, w) { if (v.optional) { w.append( - `let ${field} = ${slice}.loadBit() ? ${slice}.loadRef()${v.kind !== "cell" ? getCellLikeTsAsMethod(v) : ""} : null;`, + `const ${field} = ${slice}.loadBit() ? ${slice}.loadRef()${v.kind !== "cell" ? getCellLikeTsAsMethod(v) : ""} : null;`, ); } else { w.append( - `let ${field} = ${slice}.loadRef()${v.kind !== "cell" ? getCellLikeTsAsMethod(v) : ""};`, + `const ${field} = ${slice}.loadRef()${v.kind !== "cell" ? getCellLikeTsAsMethod(v) : ""};`, ); } }, tsLoadTuple(v, reader, field, w) { if (v.optional) { w.append( - `let ${field} = ${reader}.readCellOpt()${v.kind !== "cell" ? getCellLikeTsAsMethod(v) : ""};`, + `const ${field} = ${reader}.readCellOpt()${v.kind !== "cell" ? getCellLikeTsAsMethod(v) : ""};`, ); } else { w.append( - `let ${field} = ${reader}.readCell()${v.kind !== "cell" ? getCellLikeTsAsMethod(v) : ""};`, + `const ${field} = ${reader}.readCell()${v.kind !== "cell" ? getCellLikeTsAsMethod(v) : ""};`, ); } }, @@ -454,12 +454,12 @@ const remainderSerializer: Serializer<{ kind: "cell" | "slice" | "builder" }> = }, tsLoad(v, slice, field, w) { w.append( - `let ${field} = ${slice}${v.kind !== "slice" ? getCellLikeTsAsMethod(v) : ""};`, + `const ${field} = ${slice}${v.kind !== "slice" ? getCellLikeTsAsMethod(v) : ""};`, ); }, tsLoadTuple(v, reader, field, w) { w.append( - `let ${field} = ${reader}.readCell()${v.kind !== "cell" ? getCellLikeTsAsMethod(v) : ""};`, + `const ${field} = ${reader}.readCell()${v.kind !== "cell" ? getCellLikeTsAsMethod(v) : ""};`, ); }, tsStore(v, builder, field, w) { @@ -499,17 +499,17 @@ const fixedBytesSerializer: Serializer<{ bytes: number; optional: boolean }> = { tsLoad(v, slice, field, w) { if (v.optional) { w.append( - `let ${field} = ${slice}.loadBit() ? ${slice}.loadBuffer(${v.bytes}) : null;`, + `const ${field} = ${slice}.loadBit() ? ${slice}.loadBuffer(${v.bytes}) : null;`, ); } else { - w.append(`let ${field} = ${slice}.loadBuffer(${v.bytes});`); + w.append(`const ${field} = ${slice}.loadBuffer(${v.bytes});`); } }, tsLoadTuple(v, reader, field, w) { if (v.optional) { - w.append(`let ${field} = ${reader}.readBufferOpt();`); + w.append(`const ${field} = ${reader}.readBufferOpt();`); } else { - w.append(`let ${field} = ${reader}.readBuffer();`); + w.append(`const ${field} = ${reader}.readBuffer();`); } }, tsStore(v, builder, field, w) { @@ -550,17 +550,17 @@ const stringSerializer: Serializer<{ optional: boolean }> = { tsLoad(v, slice, field, w) { if (v.optional) { w.append( - `let ${field} = ${slice}.loadBit() ? ${slice}.loadStringRefTail() : null;`, + `const ${field} = ${slice}.loadBit() ? ${slice}.loadStringRefTail() : null;`, ); } else { - w.append(`let ${field} = ${slice}.loadStringRefTail();`); + w.append(`const ${field} = ${slice}.loadStringRefTail();`); } }, tsLoadTuple(v, reader, field, w) { if (v.optional) { - w.append(`let ${field} = ${reader}.readStringOpt();`); + w.append(`const ${field} = ${reader}.readStringOpt();`); } else { - w.append(`let ${field} = ${reader}.readString();`); + w.append(`const ${field} = ${reader}.readString();`); } }, tsStore(v, builder, field, w) { @@ -638,10 +638,10 @@ const struct: Serializer<{ name: string; optional: boolean }> = { tsLoad(v, slice, field, w) { if (v.optional) { w.append( - `let ${field} = ${slice}.loadBit() ? load${v.name}(${slice}) : null;`, + `const ${field} = ${slice}.loadBit() ? load${v.name}(${slice}) : null;`, ); } else { - w.append(`let ${field} = load${v.name}(${slice});`); + w.append(`const ${field} = load${v.name}(${slice});`); } }, tsLoadTuple(v, reader, field, w, fromGet: boolean) { @@ -927,12 +927,12 @@ const map: Serializer = { }, tsLoad(v, slice, field, w) { w.append( - `let ${field} = Dictionary.load(${getKeyParser(v.key)}, ${getValueParser(v.value)}, ${slice});`, + `const ${field} = Dictionary.load(${getKeyParser(v.key)}, ${getValueParser(v.value)}, ${slice});`, ); }, tsLoadTuple(v, reader, field, w) { w.append( - `let ${field} = Dictionary.loadDirect(${getKeyParser(v.key)}, ${getValueParser(v.value)}, ${reader}.readCellOpt());`, + `const ${field} = Dictionary.loadDirect(${getKeyParser(v.key)}, ${getValueParser(v.value)}, ${reader}.readCellOpt());`, ); }, tsStore(v, builder, field, w) { diff --git a/src/bindings/typescript/writeStruct.ts b/src/bindings/typescript/writeStruct.ts index 477b534e7..2235e0efe 100644 --- a/src/bindings/typescript/writeStruct.ts +++ b/src/bindings/typescript/writeStruct.ts @@ -37,7 +37,7 @@ export function writeStruct( export function writeParser(s: ABIType, allocation: AllocationCell, w: Writer) { w.append(`export function load${s.name}(slice: Slice) {`); w.inIndent(() => { - w.append(`let sc_0 = slice;`); + w.append(`const sc_0 = slice;`); if (s.header) { w.append( `if (sc_0.loadUint(32) !== ${s.header}) { throw Error('Invalid prefix'); }`, @@ -62,7 +62,7 @@ function writeParserCell( writeParserField(gen, f, s, w); } if (src.next) { - w.append(`let sc_${gen + 1} = sc_${gen}.loadRef().beginParse();`); + w.append(`const sc_${gen + 1} = sc_${gen}.loadRef().beginParse();`); writeParserCell(gen + 1, src.next, s, w); } } @@ -94,7 +94,7 @@ export function writeSerializer( w.inIndent(() => { w.append(`return (builder: Builder) => {`); w.inIndent(() => { - w.append(`let b_0 = builder;`); + w.append(`const b_0 = builder;`); if (s.header) { w.append(`b_0.storeUint(${s.header}, 32);`); } @@ -115,7 +115,7 @@ export function writeInitSerializer( w.inIndent(() => { w.append(`return (builder: Builder) => {`); w.inIndent(() => { - w.append(`let b_0 = builder;`); + w.append(`const b_0 = builder;`); writeSerializerCell(0, allocation, w); }); w.append(`};`); @@ -129,7 +129,7 @@ function writeSerializerCell(gen: number, src: AllocationCell, w: Writer) { writeSerializerField(gen, f, w); } if (src.next) { - w.append(`let b_${gen + 1} = new Builder();`); + w.append(`const b_${gen + 1} = new Builder();`); writeSerializerCell(gen + 1, src.next, w); w.append(`b_${gen}.storeRef(b_${gen + 1}.endCell());`); } @@ -213,7 +213,7 @@ function writeTupleFieldParser( export function writeTupleSerializer(s: ABIType, w: Writer) { w.append(`function storeTuple${s.name}(source: ${s.name}) {`); w.inIndent(() => { - w.append(`let builder = new TupleBuilder();`); + w.append(`const builder = new TupleBuilder();`); for (const f of s.fields) { writeVariableToStack(`source.${f.name}`, f.type, w); } diff --git a/src/bindings/writeTypescript.ts b/src/bindings/writeTypescript.ts index b179f9bf0..c534ff71e 100644 --- a/src/bindings/writeTypescript.ts +++ b/src/bindings/writeTypescript.ts @@ -171,7 +171,7 @@ export function writeTypescript( w.inIndent(() => { // Code references w.append(`const __code = Cell.fromBase64('${init.code}');`); - w.append("let builder = beginCell();"); + w.append("const builder = beginCell();"); if (init.system !== null) { w.append(`const __system = Cell.fromBase64('${init.system}');`); @@ -596,7 +596,7 @@ export function writeTypescript( `async get${getterNames.get(g.name)}(${["provider: ContractProvider", ...writeArguments(g.arguments ? g.arguments : [])].join(", ")}) {`, ); w.inIndent(() => { - w.append(`let builder = new TupleBuilder();`); + w.append(`const builder = new TupleBuilder();`); if (g.arguments) { for (const a of g.arguments) { writeArgumentToStack(a.name, a.type, w); @@ -608,11 +608,11 @@ export function writeTypescript( // but the ContractProvider's interface get methods can only // take strings (function names) w.append( - `let source = (await provider.get(${g.methodId} as any, builder.build())).stack;`, + `const source = (await provider.get(${g.methodId} as any, builder.build())).stack;`, ); } else { w.append( - `let source = (await provider.get('${g.name}', builder.build())).stack;`, + `const source = (await provider.get('${g.name}', builder.build())).stack;`, ); } if (g.returnType) { From 519d40b58262b01f3386a3dc92358ada67917196 Mon Sep 17 00:00:00 2001 From: Gusarich Date: Fri, 10 Jan 2025 03:07:46 +0300 Subject: [PATCH 2/2] feat: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c8708e0b..efb802825 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Remove `enabledMasterchain` compiler config option from `tact.config.json`: PR [#1207](https://github.com/tact-lang/tact/pull/1207) - Remove `org.ton.chain.any.v0` interface: PR [#1207](https://github.com/tact-lang/tact/pull/1207) - To reduce fees, Tact no longer stores the parent contract code in the system cell that holds all the child contract codes used in `initOf`. Instead, the `MYCODE` instruction is used: PR [#1213](https://github.com/tact-lang/tact/pull/1213) +- Generated TS wrappers now use `const` where possible for variable declarations: PR [#1292](https://github.com/tact-lang/tact/pull/1292) ### Fixed