Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve ts.ValueExpression conversion #46

Merged
merged 2 commits into from
Jan 15, 2025
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
4 changes: 3 additions & 1 deletion packages/typescript/src/components/ValueExpression.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export function ValueExpression(props: ValueExpressionProps) {
return "undefined";
} else if (typeof jsValue === "number" || typeof jsValue === "boolean") {
return String(jsValue);
} else if (typeof jsValue === "bigint") {
return `${jsValue}n`;
} else if (typeof jsValue === "string") {
return `"${jsValue}"`;
return JSON.stringify(jsValue);
} else if (typeof jsValue === "object") {
if (jsValue === null) {
return "null";
Expand Down
39 changes: 39 additions & 0 deletions packages/typescript/test/value-expression.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { d } from "@alloy-js/core/testing";
import { expect, it } from "vitest";
import { ValueExpression } from "../src/index.js";
import { toSourceText } from "./utils.jsx";

it.each([
[undefined, "undefined"],
[null, "null"],
[true, "true"],
[false, "false"],
[42, "42"],
[42n, "42n"],
["abc", `"abc"`],
["a\nb\rc\\", `"a\\nb\\rc\\\\"`],
[
[1, 2, 3],
d`
[
1,
2,
3
]
`,
],
[
{ a: 1, b: 2, c: 3 },
d`
{
a: 1,
b: 2,
c: 3
}
`,
],
])("works - %o => %s", (jsValue, expectedSource) => {
expect(toSourceText(<ValueExpression jsValue={jsValue} />)).toBe(
expectedSource,
);
});
Loading