Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix: compile expressions when compute properties (#2831)

Bogdan Chadkin06aa515fdd3c
diff --git a/apps/builder/app/shared/nano-states/props.test.ts b/apps/builder/app/shared/nano-states/props.test.ts
index e2f21df7a4..af62526a7c 100644
--- a/apps/builder/app/shared/nano-states/props.test.ts
+++ b/apps/builder/app/shared/nano-states/props.test.ts
@@ -120,6 +120,14 @@ test("compute expression prop values", () => {
type: "expression",
value: `$ws$dataSource$var2 + ' World!'`,
},
+ {
+ id: "prop3",
+ name: "third",
+ instanceId: "box",
+ type: "expression",
+ // do not fail when access fields of undefined
+ value: `$ws$dataSource$var1.second.third || "something"`,
+ },
])
);
expect(
@@ -128,6 +136,7 @@ test("compute expression prop values", () => {
new Map<string, unknown>([
["first", 3],
["second", "Hello World!"],
+ ["third", "something"],
])
);
@@ -138,6 +147,7 @@ test("compute expression prop values", () => {
new Map<string, unknown>([
["first", 6],
["second", "Hello World!"],
+ ["third", "something"],
])
);
diff --git a/apps/builder/app/shared/nano-states/props.ts b/apps/builder/app/shared/nano-states/props.ts
index 8fe22db7cb..47d6a4de12 100644
--- a/apps/builder/app/shared/nano-states/props.ts
+++ b/apps/builder/app/shared/nano-states/props.ts
@@ -141,7 +141,11 @@ const computeExpression = (
const identifier = encodeDataSourceVariable(id);
code += `const ${identifier} = ${JSON.stringify(value)};\n`;
}
- code += `return (${expression})`;
+ const transpiled = validateExpression(expression, {
+ effectful: true,
+ optional: true,
+ });
+ code += `return (${transpiled})`;
try {
const result = new Function(code)();
return result;