diff --git a/code/lib/csf-tools/src/ConfigFile.ts b/code/lib/csf-tools/src/ConfigFile.ts index e31750a2fed6..eb2921b4e06d 100644 --- a/code/lib/csf-tools/src/ConfigFile.ts +++ b/code/lib/csf-tools/src/ConfigFile.ts @@ -1,5 +1,6 @@ /* eslint-disable no-underscore-dangle */ import fs from 'fs-extra'; +import dedent from 'ts-dedent'; import * as t from '@babel/types'; @@ -12,6 +13,30 @@ import { babelParse } from './babelParse'; const logger = console; +const getCsfParsingErrorMessage = ({ + expectedType, + foundType, + node, +}: { + expectedType: string; + foundType: string | undefined; + node: any | undefined; +}) => { + let nodeInfo = ''; + if (node) { + try { + nodeInfo = JSON.stringify(node); + } catch (e) { + // + } + } + + return dedent` + CSF Parsing error: Expected '${expectedType}' but found '${foundType}' instead in '${node?.type}'. + ${nodeInfo} + `; +}; + const propKey = (p: t.ObjectProperty) => { if (t.isIdentifier(p.key)) return p.key.name; if (t.isStringLiteral(p.key)) return p.key.value; @@ -163,7 +188,13 @@ export class ConfigFile { } }); } else { - logger.warn(`Unexpected ${JSON.stringify(node)}`); + logger.warn( + getCsfParsingErrorMessage({ + expectedType: 'ObjectExpression', + foundType: decl?.type, + node: decl || node.declaration, + }) + ); } }, }, @@ -183,7 +214,13 @@ export class ConfigFile { } }); } else { - logger.warn(`Unexpected ${JSON.stringify(node)}`); + logger.warn( + getCsfParsingErrorMessage({ + expectedType: 'VariableDeclaration', + foundType: node.declaration?.type, + node: node.declaration, + }) + ); } }, }, @@ -223,7 +260,13 @@ export class ConfigFile { } }); } else { - logger.warn(`Unexpected ${JSON.stringify(node)}`); + logger.warn( + getCsfParsingErrorMessage({ + expectedType: 'ObjectExpression', + foundType: exportObject?.type, + node: exportObject, + }) + ); } } } diff --git a/code/lib/telemetry/src/storybook-metadata.ts b/code/lib/telemetry/src/storybook-metadata.ts index 2c70b566f97d..782c4d4a4b78 100644 --- a/code/lib/telemetry/src/storybook-metadata.ts +++ b/code/lib/telemetry/src/storybook-metadata.ts @@ -164,13 +164,17 @@ export const computeStorybookMetadata = async ({ const storybookInfo = getStorybookInfo(packageJson); - const { previewConfig } = storybookInfo; - if (previewConfig) { - const config = await readConfig(previewConfig); - const usesGlobals = !!( - config.getFieldNode(['globals']) || config.getFieldNode(['globalTypes']) - ); - metadata.preview = { ...metadata.preview, usesGlobals }; + try { + const { previewConfig } = storybookInfo; + if (previewConfig) { + const config = await readConfig(previewConfig); + const usesGlobals = !!( + config.getFieldNode(['globals']) || config.getFieldNode(['globalTypes']) + ); + metadata.preview = { ...metadata.preview, usesGlobals }; + } + } catch (e) { + // gracefully handle error, as it's not critical information and AST parsing can cause trouble } const storybookVersion = storybookPackages[storybookInfo.frameworkPackage]?.version;