Skip to content

Commit

Permalink
fix(scaffold): render pref error
Browse files Browse the repository at this point in the history
  • Loading branch information
northword committed Mar 10, 2025
1 parent 509ec08 commit c3bd1dd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
53 changes: 52 additions & 1 deletion packages/scaffold/src/utils/prefs-manager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { writeFileSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { parseSync } from "@swc/core";
import { outputFile } from "fs-extra/esm";
Expand Down Expand Up @@ -25,6 +26,7 @@ export class PrefsManager {
parse(content: string) {
const _map: Prefs = {};
const ast = parseSync(content, { syntax: "ecmascript" });
writeFileSync("ast.json", JSON.stringify(ast, null, 2));
for (const node of ast.body) {
if (
node.type !== "ExpressionStatement"
Expand Down Expand Up @@ -122,8 +124,57 @@ export class PrefsManager {
}

render() {
// function getValueNode(value: unknown) {
// if (typeof value === "string") {
// return { type: "StringLiteral", value };
// }
// else if (typeof value === "number") {
// if (value < 0) {
// // 处理负数,生成 UnaryExpression
// return {
// type: "UnaryExpression",
// operator: "-",
// argument: { type: "NumericLiteral", value: Math.abs(value) },
// };
// }
// return { type: "NumericLiteral", value };
// }
// else if (typeof value === "boolean") {
// return { type: "BooleanLiteral", value };
// }
// throw new Error(`Unsupported value type: ${typeof value}`);
// }
// const defaultSpan = { start: 0, end: 0, ctxt: 0 };

// const ast: Program = {
// type: "Module",
// span: defaultSpan,
// // interpreter: null,
// body: Object.entries(this.prefs).map(([key, value]) => ({
// type: "ExpressionStatement",
// span: defaultSpan,
// ctxt: 0,
// expression: {
// type: "CallExpression",
// span: defaultSpan,
// ctxt: 0,
// callee: { type: "Identifier", value: this.namespace, span: defaultSpan, ctxt: 0, optional: false },
// arguments: [
// { type: "StringLiteral", value: key, span: defaultSpan, ctxt: 0 },
// { ...getValueNode(value), span: defaultSpan, ctxt: 0 },
// ],
// },
// })),
// };
// const { code } = printSync(ast);
// return code;

return Object.entries(this.prefs).map(([key, value]) => {
const _v = typeof value === "string" ? `"${value}"` : value;
const _v = typeof value === "string"
? `"${value
.replaceAll("\\", "\\\\")
.replaceAll("\"", "\\\"")}"`
: value;
return `${this.namespace}("${key}", ${_v});`;
}).join("\n");
}
Expand Down
4 changes: 4 additions & 0 deletions packages/scaffold/test/unit/prefs-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,15 @@ pref("test.boolean.true", true);
prefsManager.setPrefs({
"test.string": "hello",
"test.number": 42,
"test.queto": "{\"key\": \"value\"}",
"test.path": "C:\\path\\to\\file",
});

const result = [
"pref(\"test.string\", \"hello\");",
"pref(\"test.number\", 42);",
"pref(\"test.queto\", \"{\\\"key\\\": \\\"value\\\"}\");",
"pref(\"test.path\", \"C:\\\\path\\\\to\\\\file\");",
].join("\n");

expect(prefsManager.render()).toBe(result);
Expand Down

0 comments on commit c3bd1dd

Please sign in to comment.