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
fix: resolve conflicts in variabld and class names (#3839)
Bogdan Chadkin19a80cc0f33f
diff --git a/packages/sdk/src/scope.test.ts b/packages/sdk/src/scope.test.tsindex 6ddf8f4809..cf97745da6 100644--- a/packages/sdk/src/scope.test.ts+++ b/packages/sdk/src/scope.test.ts@@ -30,3 +30,14 @@ test("prefix name starting with digit", () => {const scope = createScope(["_"]);expect(scope.getName("1", "123")).toEqual("_123");});++test("resolve conflict between name with number and name with index", () => {+ const scope = createScope();+ expect([+ scope.getName("1", "image"),+ scope.getName("2", "image"),+ scope.getName("3", "image"),+ scope.getName("4", "image"),+ scope.getName("5", "image_3"),+ ]).toEqual(["image", "image_1", "image_2", "image_3", "image_3_1"]);+});diff --git a/packages/sdk/src/scope.ts b/packages/sdk/src/scope.tsindex 20e10ec5c7..62d65681ec 100644--- a/packages/sdk/src/scope.ts+++ b/packages/sdk/src/scope.ts@@ -35,25 +35,26 @@ export const createScope = (normalizeName = normalizeJsName,separator = "_"): Scope => {- const freeIndexByPreferredName = new Map<string, number>();- const scopedNameByIdMap = new Map<string, string>();+ const nameById = new Map<string, string>();+ const usedNames = new Set<string>();for (const identifier of occupiedIdentifiers) {- freeIndexByPreferredName.set(identifier, 1);+ usedNames.add(identifier);}const getName = (id: string, preferredName: string) => {- const cachedName = scopedNameByIdMap.get(id);+ const cachedName = nameById.get(id);if (cachedName !== undefined) {return cachedName;}preferredName = normalizeName(preferredName);- const index = freeIndexByPreferredName.get(preferredName);- freeIndexByPreferredName.set(preferredName, (index ?? 0) + 1);+ let index = 0;let scopedName = preferredName;- if (index !== undefined) {+ while (usedNames.has(scopedName)) {+ index += 1;scopedName = `${preferredName}${separator}${index}`;}- scopedNameByIdMap.set(id, scopedName);+ nameById.set(id, scopedName);+ usedNames.add(scopedName);return scopedName;};