Skip to content

Commit

Permalink
Merge pull request #24858 from storybookjs/yann/handle-ast-parsing-error
Browse files Browse the repository at this point in the history
Core: Gracefully handle error when parsing preview.js file
  • Loading branch information
yannbf authored Nov 16, 2023
2 parents 9c6cc6d + 534acf4 commit c4eac36
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
49 changes: 46 additions & 3 deletions code/lib/csf-tools/src/ConfigFile.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;
Expand Down Expand Up @@ -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,
})
);
}
},
},
Expand All @@ -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,
})
);
}
},
},
Expand Down Expand Up @@ -223,7 +260,13 @@ export class ConfigFile {
}
});
} else {
logger.warn(`Unexpected ${JSON.stringify(node)}`);
logger.warn(
getCsfParsingErrorMessage({
expectedType: 'ObjectExpression',
foundType: exportObject?.type,
node: exportObject,
})
);
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions code/lib/telemetry/src/storybook-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c4eac36

Please sign in to comment.