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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
refactor: switch to acorn for expression linting (#3026)
Bogdan Chadkin61f751adaba2
diff --git a/apps/builder/app/builder/shared/binding-popover.tsx b/apps/builder/app/builder/shared/binding-popover.tsxindex a9f2a64405..323d187e59 100644--- a/apps/builder/app/builder/shared/binding-popover.tsx+++ b/apps/builder/app/builder/shared/binding-popover.tsx@@ -37,6 +37,7 @@ import {} from "@webstudio-is/design-system";import {decodeDataSourceVariable,+ lintExpression,validateExpression,} from "@webstudio-is/react-sdk";import { ExpressionEditor, formatValuePreview } from "./expression-editor";@@ -97,30 +98,18 @@ const BindingPanel = ({}) => {const [expression, setExpression] = useState(value);const usedIdentifiers = useMemo(() => getUsedIdentifiers(value), [value]);- const [error, setError] = useState<undefined | string>();+ const [errors, setErrors] = useState<string[]>([]);const [touched, setTouched] = useState(false);const scopeEntries = Object.entries(scope);const updateExpression = (newExpression: string) => {setExpression(newExpression);onChange();- try {- if (newExpression.trim().length === 0) {- throw Error("Cannot use empty expression");- }- // update value only when expression is valid- validateExpression(newExpression, {- transformIdentifier: (identifier) => {- if (aliases.has(identifier) === false) {- throw Error(`Unknown variable "${identifier}"`);- }- return identifier;- },- });- setError(undefined);- } catch (error) {- setError((error as Error).message);- }+ const diagnostics = lintExpression({+ expression: newExpression,+ availableVariables: new Set(aliases.keys()),+ });+ setErrors(diagnostics.map((diagnostic) => diagnostic.message));};return (@@ -195,7 +184,11 @@ const BindingPanel = ({<Box css={{ padding: `0 ${theme.spacing[9]} ${theme.spacing[9]}` }}><InputErrorsTooltiperrors={- touched && error ? [error] : valueError ? [valueError] : undefined+ touched && errors.length > 0+ ? errors+ : valueError+ ? [valueError]+ : undefined}><div>@@ -203,7 +196,7 @@ const BindingPanel = ({scope={scope}aliases={aliases}color={- error !== undefined || valueError !== undefined+ errors.length > 0 || valueError !== undefined? "error": undefined}@@ -214,7 +207,7 @@ const BindingPanel = ({setTouched(false);}}onBlur={() => {- onSave(expression, error !== undefined);+ onSave(expression, errors.length > 0);setTouched(true);}}/>diff --git a/packages/react-sdk/src/expression.test.ts b/packages/react-sdk/src/expression.test.tsindex 5fedbdc5fb..d58430cb5c 100644--- a/packages/react-sdk/src/expression.test.ts+++ b/packages/react-sdk/src/expression.test.ts@@ -4,8 +4,10 @@ import {encodeDataSourceVariable,executeExpression,isLiteralExpression,+ lintExpression,transpileExpression,validateExpression,+ type Diagnostic,} from "./expression";test("allow literals and array expressions", () => {@@ -90,6 +92,188 @@ test("allow indexed member expressions with identifiers", () => {expect(ids).toEqual(["a", "b"]);});+describe("lint expression", () => {+ const error = (from: number, to: number, message: string): Diagnostic => ({+ from,+ to,+ severity: "error",+ message,+ });++ test("supports empty expression", () => {+ expect(lintExpression({ expression: `` })).toEqual([]);+ });++ test("output parse error as diagnostic", () => {+ expect(lintExpression({ expression: `a + ` })).toEqual([+ error(4, 4, "Unexpected token (1:4)"),+ ]);+ });++ test("restrict expression syntax", () => {+ expect(lintExpression({ expression: `var a = 1` })).toEqual([+ error(0, 0, "Unexpected token (1:0)"),+ ]);+ });++ test("supports accessing variable fields", () => {+ expect(+ lintExpression({+ expression: `a.b.c`,+ availableVariables: new Set(["a"]),+ })+ ).toEqual([]);+ });++ test("supports literals", () => {+ expect(+ lintExpression({+ expression: `"" + 0 + true + [] + {}`,+ })+ ).toEqual([]);+ });++ test("supports ternary operator", () => {+ expect(+ lintExpression({+ expression: `true ? false : true`,+ })+ ).toEqual([]);+ });++ test("supports template literals", () => {+ expect(+ lintExpression({+ expression: "`my ${1} template ${'2'}`",+ })+ ).toEqual([]);+ expect(+ lintExpression({+ expression: "`my template",+ })+ ).toEqual([error(1, 1, "Unterminated template (1:1)")]);+ });++ test("supports parentheses", () => {+ expect(+ lintExpression({+ expression: "(1 + 1) * 2",+ })+ ).toEqual([]);+ });++ test("forbid assignment until enabled", () => {+ expect(+ lintExpression({+ expression: ` a = 1`,+ availableVariables: new Set(["a"]),+ })+ ).toEqual([error(1, 6, "Assignment is supported only inside actions")]);+ expect(+ lintExpression({+ expression: ` a = 1`,+ allowAssignment: true,+ availableVariables: new Set(["a"]),+ })+ ).toEqual([]);+ });++ test("forbid unavailable variables", () => {+ expect(+ lintExpression({ expression: ` a = b + 1`, allowAssignment: true })+ ).toEqual([+ error(5, 6, `"b" is not available in the scope`),+ error(1, 2, `"a" is not available in the scope`),+ ]);+ expect(+ lintExpression({+ expression: ` a = b + 1`,+ allowAssignment: true,+ availableVariables: new Set(["a", "b"]),+ })+ ).toEqual([]);+ });++ test(`forbid "this" keyword`, () => {+ expect(lintExpression({ expression: ` this.name` })).toEqual([+ error(1, 5, `"this" keyword is not supported`),+ ]);+ });++ test("forbid functions", () => {+ expect(+ lintExpression({+ expression: ` function(){} + (() => {}) + fn()`,+ availableVariables: new Set(["fn"]),+ })+ ).toEqual([+ error(1, 13, "Functions are not supported"),+ error(17, 25, "Functions are not supported"),+ error(29, 33, "Functions are not supported"),+ ]);+ });++ test("forbid increment and decrement", () => {+ expect(+ lintExpression({+ expression: ` ++i + --j`,+ availableVariables: new Set(["j", "i"]),+ })+ ).toEqual([+ error(1, 4, "Increment and decrement are not supported"),+ error(7, 10, "Increment and decrement are not supported"),+ ]);+ });++ test("forbid sequence expression", () => {+ expect(lintExpression({ expression: ` 1, 2, 3` })).toEqual([+ error(1, 8, "Only single expression is supported"),+ ]);+ });++ test(`forbid "yield" keyword`, () => {+ expect(lintExpression({ expression: ` yield 1` })).toEqual([+ error(1, 1, `The keyword 'yield' is reserved (1:1)`),+ ]);+ });++ test("forbid tagged template", () => {+ expect(+ lintExpression({+ expression: " tag`hello`",+ availableVariables: new Set(["tag"]),+ })+ ).toEqual([error(1, 11, "Tagged template is not supported")]);+ });++ test("forbid classes", () => {+ expect(+ lintExpression({+ expression: ` class {} + new MyClass()`,+ availableVariables: new Set(["MyClass"]),+ })+ ).toEqual([+ error(1, 9, "Classes are not supported"),+ error(12, 25, "Classes are not supported"),+ ]);+ });++ test("forbid imports", () => {+ expect(+ lintExpression({ expression: ` import("") + import.meta.url` })+ ).toEqual([+ error(1, 11, "Imports are not supported"),+ error(14, 25, "Imports are not supported"),+ ]);+ });++ test(`forbid "await" keyword`, () => {+ expect(lintExpression({ expression: ` await 1` })).toEqual([+ error(1, 8, `"await" keyword is not supported`),+ ]);+ });+});+test("check simple literals", () => {expect(isLiteralExpression(`""`)).toEqual(true);expect(isLiteralExpression(`''`)).toEqual(true);@@ -105,11 +289,13 @@ test("check simple literals", () => {test("check complex objects and arrays", () => {expect(isLiteralExpression(`[1, 2, 3]`)).toEqual(true);expect(isLiteralExpression(`[1, 2, variable]`)).toEqual(false);+ expect(isLiteralExpression(`[...variable]`)).toEqual(false);expect(isLiteralExpression(`{ param: 0 }`)).toEqual(true);expect(isLiteralExpression(`{ "param": 0 }`)).toEqual(true);expect(isLiteralExpression(`{ param: variable }`)).toEqual(false);expect(isLiteralExpression(`{ ["param"]: 0 }`)).toEqual(true);expect(isLiteralExpression(`{ [variable]: 0 }`)).toEqual(false);+ expect(isLiteralExpression(`{ ...variable }`)).toEqual(false);});test("optionally transpile all member expressions into optional chaining", () => {diff --git a/packages/react-sdk/src/expression.ts b/packages/react-sdk/src/expression.tsindex d6ecc20eb2..2b6e2c0f40 100644--- a/packages/react-sdk/src/expression.ts+++ b/packages/react-sdk/src/expression.ts@@ -6,7 +6,7 @@ import type {AssignmentExpression,} from "@jsep-plugin/assignment";import type { ObjectExpression, Property } from "@jsep-plugin/object";-import { type Identifier, parseExpressionAt } from "acorn";+import { type Expression, type Identifier, parseExpressionAt } from "acorn";import { simple } from "acorn-walk";import type { DataSources, Scope } from "@webstudio-is/sdk";@@ -170,23 +170,126 @@ export const validateExpression = (});};-const isLiteralNode = (node: Node): boolean => {+export type Diagnostic = {+ from: number;+ to: number;+ severity: "error" | "hint" | "info" | "warning";+ message: string;+};++type ExpressionVisitor = {+ [K in Expression["type"]]: (node: Extract<Expression, { type: K }>) => void;+};++export const lintExpression = ({+ expression,+ availableVariables = new Set(),+ allowAssignment = false,+}: {+ expression: string;+ availableVariables?: Set<Identifier["name"]>;+ allowAssignment?: boolean;+}): Diagnostic[] => {+ const diagnostics: Diagnostic[] = [];+ const addError = (message: string) => {+ return (node: Expression) => {+ diagnostics.push({+ from: node.start,+ to: node.end,+ severity: "error",+ message: message,+ });+ };+ };+ // allow empty expression+ if (expression.trim().length === 0) {+ return diagnostics;+ }+ try {+ const root = parseExpressionAt(expression, 0, {+ ecmaVersion: "latest",+ // support parsing import to forbid explicitly+ sourceType: "module",+ });+ simple(root, {+ Identifier(node) {+ if (availableVariables.has(node.name) === false) {+ addError(`"${node.name}" is not available in the scope`)(node);+ }+ },+ Literal() {},+ ArrayExpression() {},+ ObjectExpression() {},+ UnaryExpression() {},+ BinaryExpression() {},+ LogicalExpression() {},+ MemberExpression() {},+ ConditionalExpression() {},+ TemplateLiteral() {},+ ChainExpression() {},+ ParenthesizedExpression() {},+ AssignmentExpression(node) {+ if (allowAssignment === false) {+ addError("Assignment is supported only inside actions")(node);+ return;+ }+ simple(node.left, {+ Identifier(node) {+ if (availableVariables.has(node.name) === false) {+ addError(`"${node.name}" is not available in the scope`)(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"),+ } satisfies ExpressionVisitor);+ } catch (error) {+ const castedError = error as { message: string; pos: number };+ diagnostics.push({+ from: castedError.pos,+ to: castedError.pos,+ severity: "error",+ message: castedError.message,+ });+ }+ return diagnostics;+};++const isLiteralNode = (node: Expression): boolean => {if (node.type === "Literal") {return true;}if (node.type === "ArrayExpression") {- return (node.elements as Node[]).every(isLiteralNode);+ return node.elements.every((node) => {+ if (node === null || node.type === "SpreadElement") {+ return false;+ }+ return isLiteralNode(node);+ });}if (node.type === "ObjectExpression") {return node.properties.every((property) => {- const key = property.key as Node;+ if (property.type === "SpreadElement") {+ return false;+ }+ const key = property.key;const isIdentifierKey =key.type === "Identifier" && property.computed === false;const isLiteralKey = key.type === "Literal";- return (- (isLiteralKey || isIdentifierKey) &&- isLiteralNode(property.value as Node)- );+ return (isLiteralKey || isIdentifierKey) && isLiteralNode(property.value);});}return false;@@ -199,7 +302,7 @@ const isLiteralNode = (node: Node): boolean => {*/export const isLiteralExpression = (expression: string) => {try {- const node = jsep(expression) as Node;+ const node = parseExpressionAt(expression, 0, { ecmaVersion: "latest" });return isLiteralNode(node);} catch {// treat invalid expression as non-literal