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: parse jsx ai output with acorn (#3392)

Bogdan Chadkinf889adb98a7d
diff --git a/packages/jsx-utils/package.json b/packages/jsx-utils/package.json
index f684e7869f..675ab4e8d9 100644
--- a/packages/jsx-utils/package.json
+++ b/packages/jsx-utils/package.json
@@ -30,11 +30,11 @@
"license": "AGPL-3.0-or-later",
"sideEffects": false,
"dependencies": {
- "@babel/parser": "^7.23.0",
- "@babel/types": "7.24.0",
"@webstudio-is/css-data": "workspace:*",
"@webstudio-is/react-sdk": "workspace:*",
"@webstudio-is/sdk": "workspace:*",
+ "acorn": "^8.11.3",
+ "acorn-jsx": "^5.3.2",
"json5": "^2.2.3",
"zod": "^3.22.4"
}
diff --git a/packages/jsx-utils/src/jsx.test.ts b/packages/jsx-utils/src/jsx.test.ts
index bad5cf736b..8124a56060 100644
--- a/packages/jsx-utils/src/jsx.test.ts
+++ b/packages/jsx-utils/src/jsx.test.ts
@@ -129,3 +129,40 @@ const jsx = `
Hello from Webstudio
</Box>
`;
+
+test("automatically wrap with fragment unwrapped jsx elements", async () => {
+ const jsx = `
+ <Box>Box 1</Box>
+ <Box>Box 2</Box>
+ `;
+ const parsed = await jsxToWSEmbedTemplate(jsx.trim());
+ expect(() => WsEmbedTemplate.parse(parsed)).not.toThrow();
+ expect(parsed).toMatchInlineSnapshot(`
+[
+ {
+ "children": [
+ {
+ "type": "text",
+ "value": "Box 1",
+ },
+ ],
+ "component": "Box",
+ "props": [],
+ "styles": [],
+ "type": "instance",
+ },
+ {
+ "children": [
+ {
+ "type": "text",
+ "value": "Box 2",
+ },
+ ],
+ "component": "Box",
+ "props": [],
+ "styles": [],
+ "type": "instance",
+ },
+]
+`);
+});
diff --git a/packages/jsx-utils/src/jsx.ts b/packages/jsx-utils/src/jsx.ts
index ec75a08a0c..a7fd5b5a0f 100644
--- a/packages/jsx-utils/src/jsx.ts
+++ b/packages/jsx-utils/src/jsx.ts
@@ -1,16 +1,11 @@
-import * as parser from "@babel/parser";
-import type { ParseError } from "@babel/parser";
-import {
- type JSXElement,
- type JSXOpeningElement,
- type JSXText,
-} from "@babel/types";
import {
WsEmbedTemplate,
type EmbedTemplateProp,
} from "@webstudio-is/react-sdk";
import JSON5 from "json5";
import type { JsonObject } from "type-fest";
+import type { JSXElement, JSXOpeningElement, JSXText } from "./parser";
+import { parseExpression } from "./parser";
export const jsxToWSEmbedTemplate = async (
code: string
@@ -20,29 +15,42 @@ export const jsxToWSEmbedTemplate = async (
let ast;
try {
- ast = parser.parseExpression(code, {
- plugins: ["jsx"],
- });
+ ast = parseExpression(code);
} catch (error) {
if (
- error &&
- (error as ParseError).reasonCode === "UnwrappedAdjacentJSXElements"
+ error instanceof Error &&
+ error.message.includes(
+ "Adjacent JSX elements must be wrapped in an enclosing tag"
+ )
) {
- code = `<Fragment>${code}</Fragment>`;
- ast = parser.parseExpression(code, {
- plugins: ["jsx"],
- });
+ code = `<>${code}</>`;
+ ast = parseExpression(code);
+ }
+ }
+
+ const template: WsEmbedTemplate = [];
+ if (ast?.type === "JSXFragment") {
+ for (const child of ast.children) {
+ if (child.type === "JSXElement") {
+ const instance = await transform(child, code);
+ if (instance) {
+ template.push(instance);
+ }
+ }
}
}
if (ast && ast.type === "JSXElement") {
- const template = await transform(ast, code);
- if (template !== null) {
- // Validate template
- WsEmbedTemplate.parse([template]);
- return [template];
+ const instance = await transform(ast, code);
+ if (instance !== null) {
+ template.push(instance);
}
}
+ if (template.length > 0) {
+ // Validate template
+ WsEmbedTemplate.parse(template);
+ return template;
+ }
throw new Error("JSX to Webstudio Embed Template produced an empty result");
};
@@ -144,7 +152,7 @@ const getProps = function getProps(
value = parsed;
}
} catch (error) {
- /**/
+ // empty block
}
return {
diff --git a/packages/jsx-utils/src/parser.ts b/packages/jsx-utils/src/parser.ts
new file mode 100644
index 0000000000..0074431ccc
--- /dev/null
+++ b/packages/jsx-utils/src/parser.ts
@@ -0,0 +1,107 @@
+import type {
+ Literal,
+ Node,
+ Expression,
+ Identifier,
+ SpreadElement,
+} from "acorn";
+import { Parser } from "acorn";
+import jsx from "acorn-jsx";
+
+// https://github.com/facebook/jsx/blob/main/AST.md
+// JSXSpreadChild is not implemented in acorn-jsx
+
+// JSX Names
+
+export type JSXIdentifier = Omit<Identifier, "type"> & {
+ type: "JSXIdentifier";
+};
+
+export type JSXMemberExpression = Omit<Node, "type"> & {
+ type: "JSXMemberExpression";
+ object: JSXMemberExpression | JSXIdentifier;
+ property: JSXIdentifier;
+};
+
+export type JSXNamespacedName = Omit<Node, "type"> & {
+ type: "JSXNamespacedName";
+ namespace: JSXIdentifier;
+ name: JSXIdentifier;
+};
+
+// JSX Expression Container
+
+export type JSXEmptyExpression = Omit<Node, "type"> & {
+ type: "JSXEmptyExpression";
+};
+
+export type JSXExpressionContainer = Omit<Node, "type"> & {
+ type: "JSXExpressionContainer";
+ expression: Expression | JSXEmptyExpression;
+};
+
+// JSX Attributes
+
+export type JSXAttribute = Omit<Node, "type"> & {
+ type: "JSXAttribute";
+ name: JSXIdentifier | JSXNamespacedName;
+ value: Literal | JSXExpressionContainer | JSXElement | JSXFragment | null;
+};
+
+export type JSXSpreadAttribute = Omit<SpreadElement, "type"> & {
+ type: "JSXSpreadAttribute";
+};
+
+// JSX Text
+
+export type JSXText = Omit<Node, "type"> & {
+ type: "JSXText";
+ value: string;
+ raw: string;
+};
+
+// JSX Element
+
+export type JSXOpeningElement = Omit<Node, "type"> & {
+ type: "JSXOpeningElement";
+ name: JSXIdentifier | JSXMemberExpression | JSXNamespacedName;
+ attributes: Array<JSXAttribute | JSXSpreadAttribute>;
+ selfClosing: boolean;
+};
+
+export type JSXClosingElement = Omit<Node, "type"> & {
+ type: "JSXClosingElement";
+ name: JSXIdentifier | JSXMemberExpression | JSXNamespacedName;
+};
+
+export type JSXElement = Omit<Node, "type"> & {
+ type: "JSXElement";
+ openingElement: JSXOpeningElement;
+ closingElement: JSXClosingElement | null;
+ children: [JSXText | JSXExpressionContainer | JSXElement | JSXFragment];
+};
+
+// JSX Fragment
+
+export type JSXOpeningFragment = Omit<Node, "type"> & {
+ type: "JSXOpeningFragment";
+};
+
+export type JSXClosingFragment = Omit<Node, "type"> & {
+ type: "JSXClosingFragment";
+};
+
+export type JSXFragment = Omit<Node, "type"> & {
+ type: "JSXFragment";
+ openingFragment: JSXOpeningFragment;
+ closingFragment: JSXClosingFragment;
+ children: Array<JSXText | JSXExpressionContainer | JSXElement | JSXFragment>;
+};
+
+const JsxParser = Parser.extend(jsx());
+
+export const parseExpression = (
+ code: string
+): Expression | JSXElement | JSXFragment => {
+ return JsxParser.parseExpressionAt(code, 0, { ecmaVersion: "latest" });
+};
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 1e3f789c08..8f0b5448d4 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1611,12 +1611,6 @@ importers:
packages/jsx-utils:
dependencies:
- '@babel/parser':
- specifier: ^7.23.0
- version: 7.23.0
- '@babel/types':
- specifier: 7.24.0
- version: 7.24.0
'@webstudio-is/css-data':
specifier: workspace:*
version: link:../css-data
@@ -1626,6 +1620,12 @@ importers:
'@webstudio-is/sdk':
specifier: workspace:*
version: link:../sdk
+ acorn:
+ specifier: ^8.11.3
+ version: 8.11.3
+ acorn-jsx:
+ specifier: ^5.3.2
+ version: 5.3.2([email protected])
json5:
specifier: ^2.2.3
version: 2.2.3