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: Identifier names must not be reserved words (#4221)

Ivan Starkov67892b069075
diff --git a/packages/react-sdk/src/component-generator.test.tsx b/packages/react-sdk/src/component-generator.test.tsx
index f9ca7975cf..96287bd94d 100644
--- a/packages/react-sdk/src/component-generator.test.tsx
+++ b/packages/react-sdk/src/component-generator.test.tsx
@@ -999,3 +999,50 @@ test("skip unsafe properties", () => {
)
);
});
+
+test("variable names can be js identifiers", () => {
+ expect(
+ generateWebstudioComponent({
+ classesMap: new Map(),
+ scope: createScope(),
+ name: "Page",
+ rootInstanceId: "body",
+ parameters: [],
+ dataSources: toMap([
+ {
+ type: "variable",
+ id: "variableId",
+ name: "switch",
+ value: { type: "string", value: "initial" },
+ },
+ ]),
+ indexesWithinAncestors: new Map(),
+ ...renderJsx(
+ <$.Body ws:id="body">
+ <$.Input
+ value={new ExpressionValue("$ws$dataSource$variableId")}
+ onChange={
+ new ActionValue(["value"], `$ws$dataSource$variableId = value`)
+ }
+ />
+ </$.Body>
+ ),
+ })
+ ).toEqual(
+ validateJSX(
+ clear(`
+ const Page = () => {
+ let [switch_, set$switch] = useVariableState<any>("initial")
+ return <Body>
+ <Input
+ value={switch_}
+ onChange={(value: any) => {
+ switch_ = value
+ set$switch(switch_)
+ }} />
+ </Body>
+ }
+ `)
+ )
+ );
+});
diff --git a/packages/sdk/package.json b/packages/sdk/package.json
index 70fb648425..965c9a8a03 100644
--- a/packages/sdk/package.json
+++ b/packages/sdk/package.json
@@ -38,6 +38,7 @@
"@webstudio-is/fonts": "workspace:*",
"acorn": "^8.11.3",
"acorn-walk": "^8.3.2",
+ "reserved-identifiers": "^1.0.0",
"type-fest": "^4.24.0",
"zod": "^3.22.4"
},
diff --git a/packages/sdk/src/scope.ts b/packages/sdk/src/scope.ts
index 62d65681ec..b22a8a0fea 100644
--- a/packages/sdk/src/scope.ts
+++ b/packages/sdk/src/scope.ts
@@ -1,3 +1,8 @@
+import reservedIdentifiers from "reserved-identifiers";
+
+const identifiers = reservedIdentifiers({ includeGlobalProperties: true });
+const isReserved = (identifier: string) => identifiers.has(identifier);
+
export type Scope = {
/**
* Accepts unique id to identify specific variable
@@ -20,6 +25,11 @@ const normalizeJsName = (name: string) => {
if (/[A-Za-z_$]/.test(name[0]) === false) {
name = `_${name}`;
}
+
+ if (isReserved(name)) {
+ return `${name}_`;
+ }
+
return name;
};
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 95042bbf03..fd3fe1c207 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1875,6 +1875,9 @@ importers:
acorn-walk:
specifier: ^8.3.2
version: 8.3.2
+ reserved-identifiers:
+ specifier: ^1.0.0
+ version: 1.0.0
type-fest:
specifier: ^4.24.0
version: 4.24.0
@@ -8367,6 +8370,10 @@ packages:
resolution: {integrity: sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==}
+ resolution: {integrity: sha512-h0bP2Katmvf3hv4Z3WtDl4+6xt/OglQ2Xa6TnhZ/Rm9/7IH1crXQqMwD4J2ngKBonVv+fB55zfGgNDAmsevLVQ==}
+ engines: {node: '>=18'}
+
resolution: {integrity: sha512-OrZaX2Mb+rJCpH/6CpSqt9xFVpN++x01XnN2ie9g6P5/3xelLAkXWVADpdz1IHD/KFfEXyE6V0U01OQ3UO2rEg==}
engines: {node: '>=8'}
@@ -16650,6 +16657,8 @@ snapshots:
+
dependencies:
resolve-from: 5.0.0