Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/codegen/stdlib/json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Expression, MethodCallNode, ObjectNode } from "../../ast/types.js";
import { Expression, MethodCallNode, ObjectNode, TypeAssertionNode } from "../../ast/types.js";

interface ExprBase {
type: string;
Expand Down Expand Up @@ -500,7 +500,17 @@ export class JsonGenerator {
return this.ctx.emitError("JSON.stringify() requires 1 argument", expr.loc);
}

const arg = expr.args[0];
if (expr.args[0].type === "type_assertion") {
return this.generateStringifyArg(
(expr.args[0] as unknown as TypeAssertionNode).expression,
expr,
params,
);
}
return this.generateStringifyArg(expr.args[0], expr, params);
}

private generateStringifyArg(arg: Expression, expr: MethodCallNode, params: string[]): string {
const spaces = this.getSpaces(expr);

if (this.ctx.isStringExpression(arg)) {
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/builtins/json-stringify-mixed-vars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @test-description: json stringify inline object with mixed string and number variable values
const name = "hello";
const count = 42;
const ts = 1234567890;
const result = JSON.stringify({ name: name, count: count, ts: ts });
if (result.includes("hello") && result.includes("42") && result.includes("1234567890")) {
console.log("TEST_PASSED");
}
8 changes: 8 additions & 0 deletions tests/fixtures/builtins/json-stringify-type-assertion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @test-description: json stringify inline object with type assertion does not crash
const name = "hello";
const count = 42;
const ts = 1234567890;
const result = JSON.stringify({ name: name, count: count, ts: ts } as any);
if (result.includes("hello") && result.includes("42") && result.includes("1234567890")) {
console.log("TEST_PASSED");
}
Loading