From 177786943ca35fba3de280e05b8a7d9871b18c3e Mon Sep 17 00:00:00 2001 From: Yossi Saadi Date: Mon, 1 Apr 2024 11:47:00 +0300 Subject: [PATCH] docs(withLiveEdit): do not fail when prettier fails to format prettier fails because the parsing method at withLiveEdit is currently not generic enough --- .../storybook/decorators/withLiveEdit/prettier-utils.ts | 2 +- .../storybook/decorators/withLiveEdit/withLiveEdit.tsx | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/core/src/storybook/decorators/withLiveEdit/prettier-utils.ts b/packages/core/src/storybook/decorators/withLiveEdit/prettier-utils.ts index 68c99cac9a..4d45b279aa 100644 --- a/packages/core/src/storybook/decorators/withLiveEdit/prettier-utils.ts +++ b/packages/core/src/storybook/decorators/withLiveEdit/prettier-utils.ts @@ -12,6 +12,6 @@ export function formatCode(code: string): string { try { return format(code, options).replace(/;\s*$/, ""); } catch (e) { - throw new Error(`[Playground Error]: Error formatting code: ${e}`); + throw new Error(`[LiveEdit Error]: Error formatting code: ${e}`); } } diff --git a/packages/core/src/storybook/decorators/withLiveEdit/withLiveEdit.tsx b/packages/core/src/storybook/decorators/withLiveEdit/withLiveEdit.tsx index fd8efe98a5..08b0d5cdb9 100644 --- a/packages/core/src/storybook/decorators/withLiveEdit/withLiveEdit.tsx +++ b/packages/core/src/storybook/decorators/withLiveEdit/withLiveEdit.tsx @@ -33,7 +33,14 @@ const withLiveEdit: Decorator = (Story, { id, parameters, viewMode }: Parameters }; useEffect(() => { - setCode(code => formatCode(code)); + setCode(code => { + try { + return formatCode(code); + } catch (e) { + console.error(e); + return originalCode.current; + } + }); }, []); const shouldAllowLiveEdit = viewMode === "docs" && parameters.docs?.liveEdit?.enableLiveEdit;