Skip to content

Commit

Permalink
feat: improve ts.ValueExpression conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach committed Dec 22, 2024
1 parent e471bb9 commit 0a5a638
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
7 changes: 2 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
"console": "internalConsole",
"outputCapture": "std",
"autoAttachChildProcesses": true,
"outFiles": [
"${workspaceFolder}/**/*.(m|c|)js",
"!**/node_modules/**"
],
"outFiles": ["${workspaceFolder}/**/*.(m|c|)js", "!**/node_modules/**"],
"smartStep": true,
"skipFiles": ["<node_internals>/**", "**/node_modules/**"],
"cwd": "${workspaceFolder}"
Expand All @@ -35,7 +32,7 @@
"autoAttachChildProcesses": true,
"env": {
"FORCE_COLOR": "3"
},
}
}
]
}
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,
);
});

0 comments on commit 0a5a638

Please sign in to comment.