Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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