From dbecb2097658bb6c0ab6743be93070701d885f46 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 11 Aug 2025 15:05:37 +0000 Subject: [PATCH] Fix JSON parsing and file reading with default values and safe parsing Co-authored-by: jordi.err --- packages/ui-patterns/src/ComplexTabs/withQueryParams.tsx | 4 ++-- packages/ui/internals/tokens/extract-design-tokens.js | 2 +- packages/ui/src/lib/utils/mergeDeep.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/ui-patterns/src/ComplexTabs/withQueryParams.tsx b/packages/ui-patterns/src/ComplexTabs/withQueryParams.tsx index f2e0ce89c4b..929c5a6120e 100644 --- a/packages/ui-patterns/src/ComplexTabs/withQueryParams.tsx +++ b/packages/ui-patterns/src/ComplexTabs/withQueryParams.tsx @@ -54,7 +54,7 @@ const withQueryParams = !new URLSearchParams(window.location.search).has(queryGroupRef.current) ) { try { - const storedValues = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY) ?? '') + const storedValues = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY) ?? 'null') if (storedValues === null || typeof storedValues !== 'object') return let storedValue: any = null @@ -83,7 +83,7 @@ const withQueryParams = if (queryGroupRef.current && queryTab) { let updatedValues: Record = {} try { - const oldValues = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY) ?? '') + const oldValues = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY) ?? '{}') if (oldValues && typeof oldValues === 'object') { updatedValues = oldValues } diff --git a/packages/ui/internals/tokens/extract-design-tokens.js b/packages/ui/internals/tokens/extract-design-tokens.js index 716a2604f69..7d9c062819c 100644 --- a/packages/ui/internals/tokens/extract-design-tokens.js +++ b/packages/ui/internals/tokens/extract-design-tokens.js @@ -3,7 +3,7 @@ const path = require('path') // Read the JSON file try { - const rawData = fs.readFileSync('./tokens/design-tokens.tokens.json') + const rawData = fs.readFileSync('./tokens/design-tokens.tokens.json', 'utf8') const jsonData = JSON.parse(rawData) // Extract "core" diff --git a/packages/ui/src/lib/utils/mergeDeep.ts b/packages/ui/src/lib/utils/mergeDeep.ts index 3731a7ade4f..41b6b200220 100644 --- a/packages/ui/src/lib/utils/mergeDeep.ts +++ b/packages/ui/src/lib/utils/mergeDeep.ts @@ -14,7 +14,7 @@ export function mergeDeep(target: any, ...sources: any[]): any { const source = sources.shift() if (isObject(target) && isObject(source)) { - for (const key in source) { + for (const key of Object.keys(source)) { if (isObject(source[key])) { if (!target[key]) Object.assign(target, { [key]: {} }) mergeDeep(target[key], source[key])