Skip to content

Commit

Permalink
fix: use const instead of let in TS wrappers (#1292)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich authored Jan 10, 2025
1 parent 408995b commit 951aa91
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
84 changes: 42 additions & 42 deletions src/bindings/typescript/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) : ""};`,
);
}
},
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -927,12 +927,12 @@ const map: Serializer<MapSerializerDescr> = {
},
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) {
Expand Down
12 changes: 6 additions & 6 deletions src/bindings/typescript/writeStruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'); }`,
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);`);
}
Expand All @@ -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(`};`);
Expand All @@ -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());`);
}
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/bindings/writeTypescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}');`);
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit 951aa91

Please sign in to comment.