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.

refactor: drop jsep parser (#3029)

Bogdan Chadkin99d0e53035d8
diff --git a/fixtures/webstudio-custom-template/remix.config.js b/fixtures/webstudio-custom-template/remix.config.js
index eef40f6409..906f06d395 100644
--- a/fixtures/webstudio-custom-template/remix.config.js
+++ b/fixtures/webstudio-custom-template/remix.config.js
@@ -5,7 +5,6 @@ module.exports = {
serverDependenciesToBundle: [
/@webstudio-is\//,
"nanoid",
- "@jsep-plugin/assignment",
"change-case",
"title-case",
],
diff --git a/fixtures/webstudio-remix-netlify-edge-functions/remix.config.js b/fixtures/webstudio-remix-netlify-edge-functions/remix.config.js
index 921dd0f726..ea9af0214a 100644
--- a/fixtures/webstudio-remix-netlify-edge-functions/remix.config.js
+++ b/fixtures/webstudio-remix-netlify-edge-functions/remix.config.js
@@ -8,7 +8,6 @@ const baseConfig =
serverDependenciesToBundle: [
/@webstudio-is\//,
"nanoid",
- "@jsep-plugin/assignment",
"change-case",
"title-case",
],
diff --git a/fixtures/webstudio-remix-netlify-functions/remix.config.js b/fixtures/webstudio-remix-netlify-functions/remix.config.js
index 1e9a05497f..e40d426263 100644
--- a/fixtures/webstudio-remix-netlify-functions/remix.config.js
+++ b/fixtures/webstudio-remix-netlify-functions/remix.config.js
@@ -16,7 +16,6 @@ module.exports = {
serverDependenciesToBundle: [
/@webstudio-is\//,
"nanoid",
- "@jsep-plugin/assignment",
"change-case",
"title-case",
],
diff --git a/fixtures/webstudio-remix-vercel/remix.config.js b/fixtures/webstudio-remix-vercel/remix.config.js
index eef40f6409..906f06d395 100644
--- a/fixtures/webstudio-remix-vercel/remix.config.js
+++ b/fixtures/webstudio-remix-vercel/remix.config.js
@@ -5,7 +5,6 @@ module.exports = {
serverDependenciesToBundle: [
/@webstudio-is\//,
"nanoid",
- "@jsep-plugin/assignment",
"change-case",
"title-case",
],
diff --git a/packages/cli/templates/defaults/remix.config.js b/packages/cli/templates/defaults/remix.config.js
index eef40f6409..906f06d395 100644
--- a/packages/cli/templates/defaults/remix.config.js
+++ b/packages/cli/templates/defaults/remix.config.js
@@ -5,7 +5,6 @@ module.exports = {
serverDependenciesToBundle: [
/@webstudio-is\//,
"nanoid",
- "@jsep-plugin/assignment",
"change-case",
"title-case",
],
diff --git a/packages/cli/templates/netlify-edge-functions/remix.config.js b/packages/cli/templates/netlify-edge-functions/remix.config.js
index 921dd0f726..ea9af0214a 100644
--- a/packages/cli/templates/netlify-edge-functions/remix.config.js
+++ b/packages/cli/templates/netlify-edge-functions/remix.config.js
@@ -8,7 +8,6 @@ const baseConfig =
serverDependenciesToBundle: [
/@webstudio-is\//,
"nanoid",
- "@jsep-plugin/assignment",
"change-case",
"title-case",
],
diff --git a/packages/cli/templates/netlify-functions/remix.config.js b/packages/cli/templates/netlify-functions/remix.config.js
index 1e9a05497f..e40d426263 100644
--- a/packages/cli/templates/netlify-functions/remix.config.js
+++ b/packages/cli/templates/netlify-functions/remix.config.js
@@ -16,7 +16,6 @@ module.exports = {
serverDependenciesToBundle: [
/@webstudio-is\//,
"nanoid",
- "@jsep-plugin/assignment",
"change-case",
"title-case",
],
diff --git a/packages/react-sdk/package.json b/packages/react-sdk/package.json
index 751be27272..b22667e03b 100644
--- a/packages/react-sdk/package.json
+++ b/packages/react-sdk/package.json
@@ -34,8 +34,6 @@
"zod": "^3.19.1"
},
"dependencies": {
- "@jsep-plugin/assignment": "^1.2.1",
- "@jsep-plugin/object": "^1.2.1",
"@webstudio-is/css-engine": "workspace:*",
"@webstudio-is/fonts": "workspace:*",
"@webstudio-is/icons": "workspace:^",
@@ -45,7 +43,6 @@
"acorn-walk": "^8.3.2",
"change-case": "^5.0.2",
"html-tags": "^3.3.1",
- "jsep": "^1.3.8",
"nanoid": "^5.0.1",
"title-case": "^4.1.0"
},
diff --git a/packages/react-sdk/src/expression.test.ts b/packages/react-sdk/src/expression.test.ts
index 194a1586c3..bb1cced09c 100644
--- a/packages/react-sdk/src/expression.test.ts
+++ b/packages/react-sdk/src/expression.test.ts
@@ -1,98 +1,15 @@
import { describe, expect, test } from "@jest/globals";
import {
+ type Diagnostic,
decodeDataSourceVariable,
encodeDataSourceVariable,
executeExpression,
isLiteralExpression,
lintExpression,
transpileExpression,
- validateExpression,
- type Diagnostic,
getExpressionIdentifiers,
} from "./expression";
-test("allow literals and array expressions", () => {
- expect(
- validateExpression(`["", '', 0, true, false, null, undefined]`)
- ).toEqual(`["", '', 0, true, false, null, undefined]`);
-});
-
-test("allow object literals", () => {
- const ids: string[] = [];
- expect(
- validateExpression(
- `{param1: "", "param2": 0, ["param3"]: false, [a]: b }`,
- {
- transformIdentifier: (id) => {
- ids.push(id);
- return id;
- },
- }
- )
- ).toEqual(`{param1: "", "param2": 0, ["param3"]: false, [a]: b}`);
- expect(ids).toEqual(["a", "b"]);
-});
-
-test("expand shorthand properties in objects", () => {
- expect(validateExpression(`{a}`)).toEqual(`{a: a}`);
-});
-
-test("supports array of objects", () => {
- expect(
- validateExpression(`[{name: 'John'}, {name: 'Dave'}, {name: 'Oliver'}]`)
- ).toEqual(`[{name: 'John'}, {name: 'Dave'}, {name: 'Oliver'}]`);
-});
-
-test("allow unary and binary expressions", () => {
- expect(validateExpression(`[-1, 1 + 1]`)).toEqual(`[-1, 1 + 1]`);
-});
-
-test("optionally allow assignment expressions", () => {
- expect(() => {
- validateExpression(`a = 2`);
- }).toThrowError(/Cannot use assignment in this expression/);
- expect(validateExpression(`a = 2`, { effectful: true })).toEqual(`a = 2`);
-});
-
-test("allow member expressions", () => {
- const ids: string[] = [];
- expect(
- validateExpression("a.b.c", {
- transformIdentifier: (id) => {
- ids.push(id);
- return id;
- },
- })
- ).toEqual("a.b.c");
- expect(ids).toEqual(["a"]);
-});
-
-test("allow indexed member expressions", () => {
- const ids: string[] = [];
- expect(
- validateExpression(`a["b"][1]`, {
- transformIdentifier: (id) => {
- ids.push(id);
- return id;
- },
- })
- ).toEqual(`a["b"][1]`);
- expect(ids).toEqual(["a"]);
-});
-
-test("allow indexed member expressions with identifiers", () => {
- const ids: string[] = [];
- expect(
- validateExpression(`a[b]`, {
- transformIdentifier: (id) => {
- ids.push(id);
- return id;
- },
- })
- ).toEqual(`a[b]`);
- expect(ids).toEqual(["a", "b"]);
-});
-
describe("lint expression", () => {
const error = (from: number, to: number, message: string): Diagnostic => ({
from,
@@ -299,68 +216,6 @@ test("check complex objects and arrays", () => {
expect(isLiteralExpression(`{ ...variable }`)).toEqual(false);
});
-test("optionally transpile all member expressions into optional chaining", () => {
- expect(validateExpression(`a.b["c"][1]`, { optional: true })).toEqual(
- `a?.b?.["c"]?.[1]`
- );
-});
-
-test("forbid this expressions", () => {
- expect(() => {
- validateExpression("var1 + this");
- }).toThrowError(/"this" is not supported/);
-});
-
-test("forbid call expressions", () => {
- expect(() => {
- validateExpression("var1 + fn1()");
- }).toThrowError(/Cannot call "fn1"/);
- expect(() => {
- validateExpression("var1 + this.fn1()");
- }).toThrowError(/Cannot call "this.fn1"/);
-});
-
-test("forbid ternary", () => {
- expect(() => {
- validateExpression("var1 ? var2 : var3");
- }).toThrowError(/Ternary operator is not supported/);
-});
-
-test("forbid increment and decrement", () => {
- expect(() => {
- validateExpression("++var1");
- }).toThrowError(/"\+\+" operator is not supported/);
- expect(() => {
- validateExpression("var1++");
- }).toThrowError(/"\+\+" operator is not supported/);
- expect(() => {
- validateExpression("--var1");
- }).toThrowError(/"--" operator is not supported/);
- expect(() => {
- validateExpression("var1--");
- }).toThrowError(/"--" operator is not supported/);
-});
-
-test("forbid multiple expressions", () => {
- expect(() => {
- validateExpression("a b");
- }).toThrowError(/Cannot use multiple expressions/);
- expect(() => {
- validateExpression("a, b");
- }).toThrowError(/Cannot use multiple expressions/);
- expect(() => {
- validateExpression("a; b");
- }).toThrowError(/Cannot use multiple expressions/);
-});
-
-test("transform identifiers", () => {
- expect(
- validateExpression(`a + b`, {
- transformIdentifier: (id) => `$ws$${id}`,
- })
- ).toEqual(`$ws$a + $ws$b`);
-});
-
describe("get expression identifiers", () => {
test("find all identifiers", () => {
expect(getExpressionIdentifiers("a = b * c.d")).toEqual(
diff --git a/packages/react-sdk/src/expression.ts b/packages/react-sdk/src/expression.ts
index f1a17c4a97..fac18d51f3 100644
--- a/packages/react-sdk/src/expression.ts
+++ b/packages/react-sdk/src/expression.ts
@@ -1,175 +1,7 @@
-import jsep from "jsep";
-import jsepAssignment from "@jsep-plugin/assignment";
-import jsepObject from "@jsep-plugin/object";
-import type {
- UpdateExpression,
- AssignmentExpression,
-} from "@jsep-plugin/assignment";
-import type { ObjectExpression, Property } from "@jsep-plugin/object";
import { type Expression, type Identifier, parseExpressionAt } from "acorn";
import { simple } from "acorn-walk";
import type { DataSources, Scope } from "@webstudio-is/sdk";
-jsep.literals["undefined"] = "undefined";
-
-jsep.plugins.register(jsepAssignment);
-jsep.plugins.register(jsepObject);
-
-type TransformIdentifier = (id: string, assignee: boolean) => string;
-
-type Node =
- | jsep.CoreExpression
- | UpdateExpression
- | AssignmentExpression
- | ObjectExpression
- | Property;
-
-const generateCode = (
- node: Node,
- failOnForbidden: boolean,
- options: {
- effectful: boolean;
- optional: boolean;
- transformIdentifier: TransformIdentifier;
- }
-): string => {
- if (node.type === "Identifier") {
- return options.transformIdentifier(node.name, false);
- }
- if (node.type === "MemberExpression") {
- const object = generateCode(node.object as Node, failOnForbidden, options);
- const property = node.property as Node;
- let propertyString: string;
- // prevent transforming identifiers from member expressions like "b" in "a.b"
- if (property.type === "Identifier" && node.computed === false) {
- propertyString = property.name;
- } else {
- propertyString = generateCode(property, failOnForbidden, options);
- }
- if (node.computed) {
- if (options.optional) {
- return `${object}?.[${propertyString}]`;
- } else {
- return `${object}[${propertyString}]`;
- }
- }
- if (options.optional) {
- return `${object}?.${propertyString}`;
- } else {
- return `${object}.${propertyString}`;
- }
- }
- if (node.type === "Literal") {
- return node.raw;
- }
- if (node.type === "UnaryExpression") {
- const arg = generateCode(node.argument as Node, failOnForbidden, options);
- return `${node.operator}${arg}`;
- }
- if (node.type === "BinaryExpression") {
- const left = generateCode(node.left as Node, failOnForbidden, options);
- const right = generateCode(node.right as Node, failOnForbidden, options);
- return `${left} ${node.operator} ${right}`;
- }
- if (node.type === "ArrayExpression") {
- const elements = node.elements.map((element) =>
- generateCode(element as Node, failOnForbidden, options)
- );
- return `[${elements.join(", ")}]`;
- }
- if (node.type === "CallExpression") {
- if (failOnForbidden) {
- const callee = generateCode(node.callee as Node, false, options);
- throw Error(`Cannot call "${callee}"`);
- }
- const callee = generateCode(node.callee as Node, failOnForbidden, options);
- const args = node.arguments.map((arg) =>
- generateCode(arg as Node, failOnForbidden, options)
- );
- return `${callee}(${args.join(", ")})`;
- }
- if (node.type === "ThisExpression") {
- if (failOnForbidden) {
- throw Error(`"this" is not supported`);
- }
- return "this";
- }
- if (node.type === "ConditionalExpression") {
- throw Error("Ternary operator is not supported");
- }
- if (node.type === "Compound") {
- throw Error("Cannot use multiple expressions");
- }
- if (node.type === "AssignmentExpression") {
- if (node.operator !== "=") {
- throw Error(`Only "=" assignment operator is supported`);
- }
- if (options.effectful === false) {
- throw Error(`Cannot use assignment in this expression`);
- }
- const left = generateCode(node.left as Node, failOnForbidden, {
- ...options,
- // override and mark all identifiers inside of left expression as assignee
- transformIdentifier: (id) => options.transformIdentifier(id, true),
- });
- const right = generateCode(node.right as Node, failOnForbidden, options);
- return `${left} ${node.operator} ${right}`;
- }
- if (node.type === "UpdateExpression") {
- throw Error(`"${node.operator}" operator is not supported`);
- }
- if (node.type === "ObjectExpression") {
- const properties = node.properties.map((property) =>
- generateCode(property, failOnForbidden, options)
- );
- return `{${properties.join(", ")}}`;
- }
- if (node.type === "Property") {
- const key = node.key as Node;
- let keyString: string;
- if (key.type === "Identifier" && node.computed === false) {
- keyString = key.name;
- } else {
- keyString = generateCode(key, failOnForbidden, options);
- }
- const value = generateCode(node.value as Node, failOnForbidden, options);
- if (node.computed) {
- return `[${keyString}]: ${value}`;
- }
- return `${keyString}: ${value}`;
- }
- node satisfies never;
- return "";
-};
-
-export const validateExpression = (
- code: string,
- options?: {
- /**
- * Enable assignment operator for actions
- */
- effectful?: boolean;
- /**
- * Add optional chaining to access nested properties safely
- * and without checks even when not exist
- */
- optional?: boolean;
- transformIdentifier?: TransformIdentifier;
- }
-) => {
- const {
- effectful = false,
- optional = false,
- transformIdentifier = (id: string) => id,
- } = options ?? {};
- const expression = jsep(code) as Node;
- return generateCode(expression, true, {
- effectful,
- optional,
- transformIdentifier,
- });
-};
-
export type Diagnostic = {
from: number;
to: number;
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index b80eeb13dc..e5af155341 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1604,12 +1604,6 @@ importers:
packages/react-sdk:
dependencies:
- '@jsep-plugin/assignment':
- specifier: ^1.2.1
- version: 1.2.1([email protected])
- '@jsep-plugin/object':
- specifier: ^1.2.1
- version: 1.2.1([email protected])
'@webstudio-is/css-engine':
specifier: workspace:*
version: link:../css-engine
@@ -1637,9 +1631,6 @@ importers:
html-tags:
specifier: ^3.3.1
version: 3.3.1
- jsep:
- specifier: ^1.3.8
- version: 1.3.8
nanoid:
specifier: ^5.0.1
version: 5.0.1
@@ -4635,24 +4626,6 @@ packages:
'@jridgewell/resolve-uri': 3.1.0
'@jridgewell/sourcemap-codec': 1.4.14
- resolution: {integrity: sha512-gaHqbubTi29aZpVbBlECRpmdia+L5/lh2BwtIJTmtxdbecEyyX/ejAOg7eQDGNvGOUmPY7Z2Yxdy9ioyH/VJeA==}
- engines: {node: '>= 10.16.0'}
- peerDependencies:
- jsep: ^0.4.0||^1.0.0
- dependencies:
- jsep: 1.3.8
- dev: false
-
- resolution: {integrity: sha512-6YoZP80h2QFCuxyqj+OvoqEnTu2r5cSRpgpvGauWlvnevFP/F/dibpvXDpnHeqwT2FIzzvg47YOe3QD/UT8vJw==}
- engines: {node: '>= 10.16.0'}
- peerDependencies:
- jsep: ^0.4.0||^1.0.0
- dependencies:
- jsep: 1.3.8
- dev: false
-
resolution: {integrity: sha512-Lg3PnLp0QXpxwLIAuuJboLeRaIhrgJjeuh797QADg3xz8wGLugQOS5DpsE8A6i6Adgzf+bacllkKZG3J0tGfDw==}
dev: true
@@ -12846,11 +12819,6 @@ packages:
transitivePeerDependencies:
- supports-color
- resolution: {integrity: sha512-qofGylTGgYj9gZFsHuyWAN4jr35eJ66qJCK4eKDnldohuUoQFbU3iZn2zjvEbd9wOAhP9Wx5DsAAduTyE1PSWQ==}
- engines: {node: '>= 10.16.0'}
- dev: false
-
resolution: {integrity: sha512-uZz5UnB7u4T9LvwmFqXii7pZSouaRPorGs5who1Ip7VO0wxanFvBL7GkM6dTHlgX+jhBApRetaWpnDabOeTcnA==}
hasBin: true