Skip to content

Commit 5cefe85

Browse files
i582sansx
authored andcommitted
fix(ts-wrappers): fix TypeScript wrappers generation for messages with single quote (tact-lang#1106)
Fixes tact-lang#972
1 parent de9dc83 commit 5cefe85

File tree

5 files changed

+125
-2
lines changed

5 files changed

+125
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4646
- `as coins` map value serialization type is now handled correctly: PR [#987](https://github.com/tact-lang/tact/pull/987)
4747
- Type checking for `foreach` loops in trait methods: PR [#1017](https://github.com/tact-lang/tact/pull/1017)
4848
- The `sha256()` function no longer throws on statically known strings of any length: PR [#907](https://github.com/tact-lang/tact/pull/907)
49+
- TypeScript wrappers generation for messages with single quote: PR [#1106](https://github.com/tact-lang/tact/pull/1106)
4950

5051
### Release contributors
5152

src/bindings/writeTypescript.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ export function writeTypescript(
352352
r.message.text !== null &&
353353
r.message.text !== undefined
354354
) {
355-
receivers.push(`'${r.message.text}'`);
355+
receivers.push(JSON.stringify(r.message.text));
356356
} else {
357357
receivers.push(`string`);
358358
}
@@ -420,7 +420,7 @@ export function writeTypescript(
420420
w.append(`}`);
421421
} else {
422422
w.append(
423-
`if (message === '${msg.text}') {`,
423+
`if (message === ${JSON.stringify(msg.text)}) {`,
424424
);
425425
w.inIndent(() => {
426426
w.append(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import "@stdlib/deploy";
2+
3+
contract TextMessageReceivers with Deployable {
4+
counter: Int = 0;
5+
6+
receive("increment'") {
7+
self.counter += 1;
8+
}
9+
10+
receive("increment-2\"") {
11+
self.counter += 2;
12+
}
13+
14+
receive("increment-3`") {
15+
self.counter += 3;
16+
}
17+
18+
receive("\\increment-4\\") {
19+
self.counter += 4;
20+
}
21+
22+
get fun getCounter(): Int {
23+
return self.counter;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { toNano } from "@ton/core";
2+
import { Blockchain, SandboxContract, TreasuryContract } from "@ton/sandbox";
3+
import { TextMessageReceivers } from "./contracts/output/text-message-receivers_TextMessageReceivers";
4+
import "@ton/test-utils";
5+
6+
describe("text-message-receivers", () => {
7+
let blockchain: Blockchain;
8+
let treasure: SandboxContract<TreasuryContract>;
9+
let contract: SandboxContract<TextMessageReceivers>;
10+
11+
beforeEach(async () => {
12+
blockchain = await Blockchain.create();
13+
blockchain.verbosity.print = false;
14+
treasure = await blockchain.treasury("treasure");
15+
16+
contract = blockchain.openContract(
17+
await TextMessageReceivers.fromInit(),
18+
);
19+
});
20+
21+
it("should deploy", async () => {
22+
// Deploy the contract
23+
const deployResult = await contract.send(
24+
treasure.getSender(),
25+
{ value: toNano("1") },
26+
{ $$type: "Deploy", queryId: 0n },
27+
);
28+
29+
expect(deployResult.transactions).toHaveTransaction({
30+
from: treasure.address,
31+
to: contract.address,
32+
success: true,
33+
deploy: true,
34+
});
35+
36+
// Verify initial state
37+
expect(await contract.getGetCounter()).toBe(0n);
38+
});
39+
40+
it("should increment counter with different text messages", async () => {
41+
// Deploy the contract
42+
const deployResult = await contract.send(
43+
treasure.getSender(),
44+
{ value: toNano("1") },
45+
{ $$type: "Deploy", queryId: 0n },
46+
);
47+
expect(deployResult.transactions).toHaveTransaction({
48+
from: treasure.address,
49+
to: contract.address,
50+
success: true,
51+
deploy: true,
52+
});
53+
54+
// Verify initial state
55+
expect(await contract.getGetCounter()).toBe(0n);
56+
57+
const sendMessage = async (
58+
message:
59+
| "increment'"
60+
| 'increment-2\\"'
61+
| "increment-3`"
62+
| "\\\\increment-4\\\\",
63+
) => {
64+
const incrementResult1 = await contract.send(
65+
treasure.getSender(),
66+
{ value: toNano("1") },
67+
message,
68+
);
69+
expect(incrementResult1.transactions).toHaveTransaction({
70+
from: treasure.address,
71+
to: contract.address,
72+
success: true,
73+
});
74+
};
75+
76+
// Increment counter
77+
await sendMessage("increment'");
78+
expect(await contract.getGetCounter()).toBe(1n);
79+
80+
await sendMessage('increment-2\\"');
81+
expect(await contract.getGetCounter()).toBe(3n);
82+
83+
await sendMessage("increment-3`");
84+
expect(await contract.getGetCounter()).toBe(6n);
85+
86+
await sendMessage("\\\\increment-4\\\\");
87+
expect(await contract.getGetCounter()).toBe(10n);
88+
});
89+
});

tact.config.json

+8
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,14 @@
421421
"path": "./src/test/e2e-emulated/contracts/asm-functions.tact",
422422
"output": "./src/test/e2e-emulated/contracts/output"
423423
},
424+
{
425+
"name": "text-message-receivers",
426+
"path": "./src/test/e2e-emulated/contracts/text-message-receivers.tact",
427+
"output": "./src/test/e2e-emulated/contracts/output",
428+
"options": {
429+
"debug": true
430+
}
431+
},
424432
{
425433
"name": "tact-reserved-contract-errors",
426434
"path": "./src/test/exit-codes/contracts/tact-reserved-contract-errors.tact",

0 commit comments

Comments
 (0)