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.

feat: allow saving expressions with unset variables (#4792)

Bogdan Chadkin6b04c9762da4
diff --git a/apps/builder/app/builder/features/pages/custom-metadata.tsx b/apps/builder/app/builder/features/pages/custom-metadata.tsx
index f411a2660f..275b025afa 100644
--- a/apps/builder/app/builder/features/pages/custom-metadata.tsx
+++ b/apps/builder/app/builder/features/pages/custom-metadata.tsx
@@ -13,11 +13,11 @@ import {
import { TrashIcon, PlusIcon } from "@webstudio-is/icons";
import { isFeatureEnabled } from "@webstudio-is/feature-flags";
import { isLiteralExpression } from "@webstudio-is/sdk";
+import { computeExpression } from "~/shared/data-variables";
import {
BindingControl,
BindingPopover,
} from "~/builder/shared/binding-popover";
-import { computeExpression } from "~/shared/nano-states";
import { $pageRootScope } from "./page-utils";
type Meta = {
diff --git a/apps/builder/app/builder/features/pages/page-settings.tsx b/apps/builder/app/builder/features/pages/page-settings.tsx
index 33e9c3cad0..573e52d956 100644
--- a/apps/builder/app/builder/features/pages/page-settings.tsx
+++ b/apps/builder/app/builder/features/pages/page-settings.tsx
@@ -74,7 +74,6 @@ import {
$instances,
$pages,
$dataSources,
- computeExpression,
$dataSourceVariables,
$publishedOrigin,
$project,
@@ -112,6 +111,7 @@ import type { UserPlanFeatures } from "~/shared/db/user-plan-features.server";
import { useUnmount } from "~/shared/hook-utils/use-mount";
import { Card } from "../marketplace/card";
import { selectInstance } from "~/shared/awareness";
+import { computeExpression } from "~/shared/data-variables";
const fieldDefaultValues = {
name: "Untitled",
diff --git a/apps/builder/app/builder/features/settings-panel/variable-popover.tsx b/apps/builder/app/builder/features/settings-panel/variable-popover.tsx
index 62d0398ee4..4b69a90bb0 100644
--- a/apps/builder/app/builder/features/settings-panel/variable-popover.tsx
+++ b/apps/builder/app/builder/features/settings-panel/variable-popover.tsx
@@ -442,6 +442,7 @@ BooleanForm.displayName = "BooleanForm";
const validateJsonValue = (expression: string) => {
const diagnostics = lintExpression({ expression });
+ // prevent saving with any message including unset variable
return diagnostics.length > 0 ? "error" : "";
};
diff --git a/apps/builder/app/builder/features/settings-panel/variables-section.tsx b/apps/builder/app/builder/features/settings-panel/variables-section.tsx
index 16066841be..b6f995c98b 100644
--- a/apps/builder/app/builder/features/settings-panel/variables-section.tsx
+++ b/apps/builder/app/builder/features/settings-panel/variables-section.tsx
@@ -220,8 +220,10 @@ const VariablesItem = ({
id={variable.id}
index={index}
label={
- <Flex align="center" css={{}}>
- <Label color={source}>{variable.name}</Label>
+ <Flex align="center">
+ <Label tag="label" color={source}>
+ {variable.name}
+ </Label>
{value !== undefined && (
<span className={variableLabelStyle.toString()}>
&nbsp;
diff --git a/apps/builder/app/builder/shared/binding-popover.tsx b/apps/builder/app/builder/shared/binding-popover.tsx
index c68b5868f0..f99df7eff3 100644
--- a/apps/builder/app/builder/shared/binding-popover.tsx
+++ b/apps/builder/app/builder/shared/binding-popover.tsx
@@ -38,16 +38,13 @@ import {
getExpressionIdentifiers,
lintExpression,
} from "@webstudio-is/sdk";
+import { $dataSourceVariables, $isDesignMode } from "~/shared/nano-states";
+import { computeExpression } from "~/shared/data-variables";
import {
ExpressionEditor,
formatValuePreview,
type EditorApi,
} from "./expression-editor";
-import {
- $dataSourceVariables,
- $isDesignMode,
- computeExpression,
-} from "~/shared/nano-states";
export const evaluateExpressionWithinScope = (
expression: string,
@@ -94,7 +91,9 @@ const BindingPanel = ({
expression,
availableVariables: new Set(aliases.keys()),
});
- setErrorsCount(diagnostics.length);
+ // prevent saving expression only with syntax error
+ const errors = diagnostics.filter((item) => item.severity === "error");
+ setErrorsCount(errors.length);
};
const updateExpression = (newExpression: string) => {
diff --git a/apps/builder/app/builder/shared/code-editor-base.tsx b/apps/builder/app/builder/shared/code-editor-base.tsx
index 8cad9adf57..07c4485270 100644
--- a/apps/builder/app/builder/shared/code-editor-base.tsx
+++ b/apps/builder/app/builder/shared/code-editor-base.tsx
@@ -122,6 +122,10 @@ const editorContentStyle = css({
textDecoration: "underline wavy red",
backgroundColor: "rgba(255, 0, 0, 0.1)",
},
+ ".cm-lintRange-warning": {
+ textDecoration: "underline wavy orange",
+ backgroundColor: "rgba(255, 0, 0, 0.1)",
+ },
".cm-gutters": {
backgroundColor: "transparent",
border: 0,
diff --git a/apps/builder/app/shared/data-variables.test.ts b/apps/builder/app/shared/data-variables.test.ts
index 68269106ff..111b1679e3 100644
--- a/apps/builder/app/shared/data-variables.test.ts
+++ b/apps/builder/app/shared/data-variables.test.ts
@@ -1,5 +1,6 @@
-import { expect, test } from "vitest";
+import { expect, test, vi } from "vitest";
import {
+ computeExpression,
decodeDataVariableName,
encodeDataVariableName,
restoreExpressionVariables,
@@ -56,3 +57,59 @@ test("restore expression variables", () => {
})
).toEqual("$ws$dataSource$myId + missingVariable");
});
+
+test("compute expression with decoded ids", () => {
+ expect(
+ computeExpression("$ws$dataSource$myId", new Map([["myId", "value"]]))
+ ).toEqual("value");
+});
+
+test("compute expression with decoded names", () => {
+ expect(
+ computeExpression("My$32$Name", new Map([["My Name", "value"]]))
+ ).toEqual("value");
+});
+
+test("compute expression when invalid syntax", () => {
+ // prevent error message in test report
+ const spy = vi.spyOn(console, "error");
+ spy.mockImplementationOnce(() => {});
+ expect(computeExpression("https://github.com", new Map())).toEqual(undefined);
+ expect(spy).toHaveBeenCalledOnce();
+ spy.mockRestore();
+});
+
+test("compute expression with nested field of undefined without error", () => {
+ const spy = vi.spyOn(console, "error");
+ const variables = new Map([["myVariable", undefined]]);
+ expect(computeExpression("myVariable.field", variables)).toEqual(undefined);
+ expect(spy).not.toHaveBeenCalled();
+ spy.mockRestore();
+});
+
+test("compute literal expression when variable is json object", () => {
+ const jsonObject = { hello: "world", subObject: { world: "hello" } };
+ const variables = new Map([["jsonVariable", jsonObject]]);
+ expect(computeExpression("`${jsonVariable}`", variables)).toEqual(
+ `{"hello":"world","subObject":{"world":"hello"}}`
+ );
+ expect(computeExpression("`${jsonVariable.subObject}`", variables)).toEqual(
+ `{"world":"hello"}`
+ );
+});
+
+test("compute literal expression when object is frozen", () => {
+ const jsonObject = Object.freeze({
+ hello: "world",
+ subObject: { world: "hello" },
+ });
+ const variables = new Map([["jsonVariable", jsonObject]]);
+ expect(computeExpression("`${jsonVariable.subObject}`", variables)).toEqual(
+ `{"world":"hello"}`
+ );
+});
+
+test("compute unset variables as undefined", () => {
+ expect(computeExpression(`a`, new Map())).toEqual(undefined);
+ expect(computeExpression("`${a}`", new Map())).toEqual("undefined");
+});
diff --git a/apps/builder/app/shared/data-variables.ts b/apps/builder/app/shared/data-variables.ts
index 1c17c8d6b1..e0705c1c8a 100644
--- a/apps/builder/app/shared/data-variables.ts
+++ b/apps/builder/app/shared/data-variables.ts
@@ -4,6 +4,10 @@ import {
encodeDataVariableId,
transpileExpression,
} from "@webstudio-is/sdk";
+import {
+ createJsonStringifyProxy,
+ isPlainObject,
+} from "@webstudio-is/sdk/runtime";
const allowedJsChars = /[A-Za-z_]/;
@@ -92,3 +96,80 @@ export const restoreExpressionVariables = ({
return expression;
}
};
+
+export const computeExpression = (
+ expression: string,
+ variables: Map<DataSource["name"], unknown>
+) => {
+ try {
+ const usedVariables = new Map();
+ const transpiled = transpileExpression({
+ expression,
+ executable: true,
+ replaceVariable: (identifier) => {
+ const id = decodeDataVariableId(identifier);
+ if (id) {
+ usedVariables.set(identifier, id);
+ } else {
+ // access all variable values from specified map
+ const name = decodeDataVariableName(identifier);
+ usedVariables.set(identifier, name);
+ }
+ },
+ });
+ let code = "";
+ // add only used variables in expression and get values
+ // from variables map without additional serializing of these values
+ for (const [identifier, name] of usedVariables) {
+ code += `let ${identifier} = _variables.get(${JSON.stringify(name)});\n`;
+ }
+ code += `return (${transpiled})`;
+
+ /**
+ *
+ * We are using structuredClone on frozen values because, for some reason,
+ * the Proxy example below throws a cryptic error:
+ * TypeError: 'get' on proxy: property 'data' is a read-only and non-configurable
+ * data property on the proxy target, but the proxy did not return its actual value
+ * (expected '[object Array]' but got '[object Array]').
+ *
+ * ```
+ * const createJsonStringifyProxy = (target) => {
+ * return new Proxy(target, {
+ * get(target, prop, receiver) {
+ *
+ * console.log((prop in target), prop)
+ *
+ * const value = Reflect.get(target, prop, receiver);
+ *
+ * if (typeof value === "object" && value !== null) {
+ * return createJsonStringifyProxy(value);
+ * }
+ *
+ * return value;
+ * },
+ * });
+ * };
+ * const obj = Object.freeze({ data: [1, 2, 3, 4] });
+ * const proxy = createJsonStringifyProxy(obj)
+ * proxy.data
+ *
+ * ```
+ */
+ const proxiedVariables = new Map(
+ [...variables.entries()].map(([key, value]) => [
+ key,
+ isPlainObject(value)
+ ? createJsonStringifyProxy(
+ Object.isFrozen(value) ? structuredClone(value) : value
+ )
+ : value,
+ ])
+ );
+
+ const result = new Function("_variables", code)(proxiedVariables);
+ return result;
+ } catch (error) {
+ console.error(error);
+ }
+};
diff --git a/apps/builder/app/shared/nano-states/props.test.ts b/apps/builder/app/shared/nano-states/props.test.ts
index debc382a3d..7dfed126ff 100644
--- a/apps/builder/app/shared/nano-states/props.test.ts
+++ b/apps/builder/app/shared/nano-states/props.test.ts
@@ -2,17 +2,12 @@ import { beforeEach, expect, test } from "vitest";
import { cleanStores } from "nanostores";
import { createDefaultPages } from "@webstudio-is/project-build";
import { setEnv } from "@webstudio-is/feature-flags";
-import {
- type Instance,
- encodeDataSourceVariable,
- collectionComponent,
-} from "@webstudio-is/sdk";
+import { type Instance, collectionComponent } from "@webstudio-is/sdk";
import { textContentAttribute } from "@webstudio-is/react-sdk";
import { $instances } from "./instances";
import {
$propValuesByInstanceSelector,
$variableValuesByInstanceSelector,
- computeExpression,
} from "./props";
import { $pages } from "./pages";
import { $assets, $dataSources, $props, $resources } from "./misc";
@@ -978,24 +973,3 @@ test("prefill default system variable value", () => {
cleanStores($variableValuesByInstanceSelector);
});
-
-test("compute expression when invalid syntax", () => {
- expect(computeExpression("https://github.com", new Map())).toEqual(undefined);
-});
-
-test("compute literal expression when variable is json object", () => {
- const variableName = "jsonVariable";
- const encVariableName = encodeDataSourceVariable(variableName);
- const jsonObject = { hello: "world", subObject: { world: "hello" } };
- const variables = new Map([[variableName, jsonObject]]);
- const expression = "`${" + encVariableName + "}`";
-
- expect(computeExpression(expression, variables)).toEqual(
- JSON.stringify(jsonObject)
- );
-
- const subObjectExpression = "`${" + encVariableName + ".subObject}`";
- expect(computeExpression(subObjectExpression, variables)).toEqual(
- JSON.stringify(jsonObject.subObject)
- );
-});
diff --git a/apps/builder/app/shared/nano-states/props.ts b/apps/builder/app/shared/nano-states/props.ts
index 94c6cf61b3..cfbaecdd27 100644
--- a/apps/builder/app/shared/nano-states/props.ts
+++ b/apps/builder/app/shared/nano-states/props.ts
@@ -15,10 +15,6 @@ import {
collectionComponent,
portalComponent,
} from "@webstudio-is/sdk";
-import {
- createJsonStringifyProxy,
- isPlainObject,
-} from "@webstudio-is/sdk/runtime";
import { normalizeProps, textContentAttribute } from "@webstudio-is/react-sdk";
import { isFeatureEnabled } from "@webstudio-is/feature-flags";
import { mapGroupBy } from "~/shared/shim";
@@ -44,6 +40,7 @@ import {
import { uploadingFileDataToAsset } from "~/builder/shared/assets/asset-utils";
import { fetch } from "~/shared/fetch.client";
import { $selectedPage, getInstanceKey } from "../awareness";
+import { computeExpression } from "../data-variables";
export const assetBaseUrl = "/cgi/asset/";
@@ -204,79 +201,6 @@ const $loaderVariableValues = computed(
}
);
-export const computeExpression = (
- expression: string,
- variables: Map<DataSource["id"], unknown>
-) => {
- try {
- const usedVariables = new Map();
- const transpiled = transpileExpression({
- expression,
- executable: true,
- replaceVariable: (identifier) => {
- const id = decodeDataSourceVariable(identifier);
- if (id) {
- usedVariables.set(identifier, id);
- }
- },
- });
- let code = "";
- // add only used variables in expression and get values
- // from variables map without additional serializing of these values
- for (const [identifier, id] of usedVariables) {
- code += `let ${identifier} = _variables.get("${id}");\n`;
- }
- code += `return (${transpiled})`;
-
- /**
- *
- * We are using structuredClone on frozen values because, for some reason,
- * the Proxy example below throws a cryptic error:
- * TypeError: 'get' on proxy: property 'data' is a read-only and non-configurable
- * data property on the proxy target, but the proxy did not return its actual value
- * (expected '[object Array]' but got '[object Array]').
- *
- * ```
- * const createJsonStringifyProxy = (target) => {
- * return new Proxy(target, {
- * get(target, prop, receiver) {
- *
- * console.log((prop in target), prop)
- *
- * const value = Reflect.get(target, prop, receiver);
- *
- * if (typeof value === "object" && value !== null) {
- * return createJsonStringifyProxy(value);
- * }
- *
- * return value;
- * },
- * });
- * };
- * const obj = Object.freeze({ data: [1, 2, 3, 4] });
- * const proxy = createJsonStringifyProxy(obj)
- * proxy.data
- *
- * ```
- */
- const proxiedVariables = new Map(
- [...variables.entries()].map(([key, value]) => [
- key,
- isPlainObject(value)
- ? createJsonStringifyProxy(
- Object.isFrozen(value) ? structuredClone(value) : value
- )
- : value,
- ])
- );
-
- const result = new Function("_variables", code)(proxiedVariables);
- return result;
- } catch (error) {
- console.error(error);
- }
-};
-
/**
* compute prop values within context of instance ancestors
* like a dry-run of rendering and accessing react contexts deep in the tree
diff --git a/packages/design-system/src/components/floating-panel.tsx b/packages/design-system/src/components/floating-panel.tsx
index f9f94d6ac9..fab19cebb0 100644
--- a/packages/design-system/src/components/floating-panel.tsx
+++ b/packages/design-system/src/components/floating-panel.tsx
@@ -127,8 +127,8 @@ export const FloatingPanel = ({
placement === "bottom" && flip(),
offset(offsetProp),
],
- }).then((position) => {
- setPosition(position);
+ }).then(({ x, y }) => {
+ setPosition({ x, y });
positionIsSetRef.current = true;
});
}, [
diff --git a/packages/design-system/src/components/label.tsx b/packages/design-system/src/components/label.tsx
index 8d275a6d5e..21042d02ab 100644
--- a/packages/design-system/src/components/label.tsx
+++ b/packages/design-system/src/components/label.tsx
@@ -127,6 +127,7 @@ const StyledLabel = styled(RadixLabel, {
});
type Props = {
+ tag?: "button" | "label";
color?: (typeof labelColors)[number];
text?: "title" | "sentence" | "mono";
disabled?: boolean;
@@ -137,13 +138,17 @@ type Props = {
export const isLabelButton = (color: Props["color"]) => color !== undefined;
export const Label = forwardRef((props: Props, ref: Ref<HTMLLabelElement>) => {
- const { disabled, children, ...rest } = props;
+ const { tag, disabled, children, ...rest } = props;
// To enable keyboard accessibility for users who rely on the spacebar to activate the radix
// when using a preset, locala, overwritten or remote color, we need to wrap the label with
// a button that has a "label" role.
// (Radix adds role="button" to the label)
- const isButton = isLabelButton(props.color);
+ let isButton = isLabelButton(props.color) || tag === "button";
+ // when explicit label
+ if (tag === "label") {
+ isButton = false;
+ }
return (
<StyledLabel
diff --git a/packages/react-sdk/src/component-generator.test.tsx b/packages/react-sdk/src/component-generator.test.tsx
index bb1351854f..90adf5bd97 100644
--- a/packages/react-sdk/src/component-generator.test.tsx
+++ b/packages/react-sdk/src/component-generator.test.tsx
@@ -997,7 +997,7 @@ test("variable names can be js identifiers", () => {
);
});
-test("Renders nothing if only templates are present in block", () => {
+test("renders nothing if only templates are present in block", () => {
const BlockTemplate = ws["block-template"];
expect(
generateWebstudioComponent({
@@ -1029,8 +1029,8 @@ test("Renders nothing if only templates are present in block", () => {
);
});
-test("Renders only block children", () => {
- const Bt = ws["block-template"];
+test("renders only block children", () => {
+ const BlockTemplate = ws["block-template"];
expect(
generateWebstudioComponent({
@@ -1043,9 +1043,9 @@ test("Renders only block children", () => {
...renderData(
<$.Body ws:id="body">
<ws.block ws:id="block">
- <Bt>
+ <BlockTemplate>
<$.Box>Test</$.Box>
- </Bt>
+ </BlockTemplate>
<$.Box>Child0</$.Box>
</ws.block>
</$.Body>
@@ -1065,3 +1065,33 @@ test("Renders only block children", () => {
)
);
});
+
+test("generate unset variables as undefined", () => {
+ expect(
+ generateWebstudioComponent({
+ classesMap: new Map(),
+ scope: createScope(),
+ name: "Page",
+ rootInstanceId: "body",
+ parameters: [],
+ indexesWithinAncestors: new Map(),
+ ...renderData(
+ <$.Body ws:id="body">
+ <$.Box>{expression`a + b`}</$.Box>
+ </$.Body>
+ ),
+ })
+ ).toEqual(
+ validateJSX(
+ clear(`
+ const Page = () => {
+ return <Body>
+ <Box>
+ {undefined + undefined}
+ </Box>
+ </Body>
+ }
+ `)
+ )
+ );
+});
diff --git a/packages/sdk/src/expression.test.ts b/packages/sdk/src/expression.test.ts
index b7ef4e4810..97e2710d01 100644
--- a/packages/sdk/src/expression.test.ts
+++ b/packages/sdk/src/expression.test.ts
@@ -20,6 +20,13 @@ describe("lint expression", () => {
message,
});
+ const warn = (from: number, to: number, message: string): Diagnostic => ({
+ from,
+ to,
+ severity: "warning",
+ message,
+ });
+
test("forbid empty expression", () => {
expect(lintExpression({ expression: `` })).toEqual([
error(0, 0, `Expression cannot be empty`),
@@ -119,8 +126,8 @@ describe("lint expression", () => {
expect(
lintExpression({ expression: ` a = b + 1`, allowAssignment: true })
).toEqual([
- error(5, 6, `"b" is not defined in the scope`),
- error(1, 2, `"a" is not defined in the scope`),
+ warn(5, 6, `"b" is not defined in the scope`),
+ warn(1, 2, `"a" is not defined in the scope`),
]);
expect(
lintExpression({
diff --git a/packages/sdk/src/expression.ts b/packages/sdk/src/expression.ts
index db8b256b34..f2e09d951c 100644
--- a/packages/sdk/src/expression.ts
+++ b/packages/sdk/src/expression.ts
@@ -24,13 +24,16 @@ export const lintExpression = ({
allowAssignment?: boolean;
}): Diagnostic[] => {
const diagnostics: Diagnostic[] = [];
- const addError = (message: string) => {
+ const addMessage = (
+ message: string,
+ severity: "error" | "warning" = "error"
+ ) => {
return (node: Expression) => {
diagnostics.push({
// tune error position after wrapping expression with parentheses
from: node.start - 1,
to: node.end - 1,
- severity: "error",
+ severity,
message: message,
});
};
@@ -57,7 +60,10 @@ export const lintExpression = ({
simple(root, {
Identifier(node) {
if (availableVariables.has(node.name) === false) {
- addError(`"${node.name}" is not defined in the scope`)(node);
+ addMessage(
+ `"${node.name}" is not defined in the scope`,
+ "warning"
+ )(node);
}
},
Literal() {},
@@ -73,31 +79,34 @@ export const lintExpression = ({
ParenthesizedExpression() {},
AssignmentExpression(node) {
if (allowAssignment === false) {
- addError("Assignment is supported only inside actions")(node);
+ addMessage("Assignment is supported only inside actions")(node);
return;
}
simple(node.left, {
Identifier(node) {
if (availableVariables.has(node.name) === false) {
- addError(`"${node.name}" is not defined in the scope`)(node);
+ addMessage(
+ `"${node.name}" is not defined in the scope`,
+ "warning"
+ )(node);
}
},
});
},
// parser forbids to yield inside module
YieldExpression() {},
- ThisExpression: addError(`"this" keyword is not supported`),
- FunctionExpression: addError("Functions are not supported"),
- UpdateExpression: addError("Increment and decrement are not supported"),
- CallExpression: addError("Functions are not supported"),
- NewExpression: addError("Classes are not supported"),
- SequenceExpression: addError(`Only single expression is supported`),
- ArrowFunctionExpression: addError("Functions are not supported"),
- TaggedTemplateExpression: addError("Tagged template is not supported"),
- ClassExpression: addError("Classes are not supported"),
- MetaProperty: addError("Imports are not supported"),
- AwaitExpression: addError(`"await" keyword is not supported`),
- ImportExpression: addError("Imports are not supported"),
+ ThisExpression: addMessage(`"this" keyword is not supported`),
+ FunctionExpression: addMessage("Functions are not supported"),
+ UpdateExpression: addMessage("Increment and decrement are not supported"),
+ CallExpression: addMessage("Functions are not supported"),
+ NewExpression: addMessage("Classes are not supported"),
+ SequenceExpression: addMessage(`Only single expression is supported`),
+ ArrowFunctionExpression: addMessage("Functions are not supported"),
+ TaggedTemplateExpression: addMessage("Tagged template is not supported"),
+ ClassExpression: addMessage("Classes are not supported"),
+ MetaProperty: addMessage("Imports are not supported"),
+ AwaitExpression: addMessage(`"await" keyword is not supported`),
+ ImportExpression: addMessage("Imports are not supported"),
} satisfies ExpressionVisitor);
} catch (error) {
const castedError = error as { message: string; pos: number };
@@ -340,10 +349,14 @@ export const generateExpression = ({
usedDataSources?.set(dep.id, dep);
return scope.getName(dep.id, dep.name);
}
+ return "undefined";
},
});
};
+/**
+ * edge case utility for "statoc" expression without variables
+ */
export const executeExpression = (expression: undefined | string) => {
try {
const fn = new Function(`return (${expression})`);