Skip to content

Commit

Permalink
fix: prevent parsing MDX files from filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 5, 2020
1 parent 3cf5964 commit 5fb3c47
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
5 changes: 5 additions & 0 deletions core/instrument/src/babel/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ const componentFromParams = (
export const extractComponent = async (
componentName: string,
filePath: string,
source?: string,
parserOptions?: parser.ParserOptions,
resolveOptions?: resolve.SyncOpts,
initialAST?: File,
): Promise<StoryComponent | undefined> => {
const follow = followImports(
componentName,
filePath,
source,
parserOptions,
resolveOptions,
initialAST,
Expand All @@ -72,6 +74,7 @@ export const extractComponent = async (
export const extractSotreComponent = async (
store: StoriesStore,
filePath: string,
source: string,
parserOptions?: parser.ParserOptions,
resolveOptions?: resolve.SyncOpts,
initialAST?: File,
Expand All @@ -85,6 +88,7 @@ export const extractSotreComponent = async (
const component = await extractComponent(
componentName,
filePath,
source,
parserOptions,
resolveOptions,
initialAST,
Expand All @@ -102,6 +106,7 @@ export const extractSotreComponent = async (
const component = await extractComponent(
componentName,
filePath,
source,
parserOptions,
resolveOptions,
initialAST,
Expand Down
6 changes: 5 additions & 1 deletion core/instrument/src/babel/follow-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ export interface FollowImportType {
export const followImports = (
importName: string,
filePath: string,
fileSource?: string,
parserOptions?: parser.ParserOptions,
resolveOptions?: resolve.SyncOpts,
initialAST?: File,
): FollowImportType | undefined => {
const source = fs.readFileSync(filePath, 'utf8');
const source = fileSource || fs.readFileSync(filePath, 'utf8');
const ast = initialAST || parser.parse(source, parserOptions);

const baseImportedName = importName.split('.')[0];
Expand Down Expand Up @@ -59,6 +60,7 @@ export const followImports = (
const imported = followImports(
findExport.internalName,
resolvedFilePath,
undefined,
parserOptions,
resolveOptions,
);
Expand All @@ -81,6 +83,7 @@ export const followImports = (
foundInExportAll = followImports(
baseImportedName,
resolvedFilePath,
undefined,
parserOptions,
resolveOptions,
);
Expand All @@ -102,6 +105,7 @@ export const followImports = (
const imported = followImports(
findImport.importedName,
resolvedFilePath,
undefined,
parserOptions,
resolveOptions,
);
Expand Down
15 changes: 11 additions & 4 deletions core/instrument/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,17 @@ const parseSource = async (
{},
);
}
await extractSotreComponent(store, filePath, mergedParserOptions, {
...defaultResolveOptions,
...resolveOptions,
});
await extractSotreComponent(
store,
filePath,
source,
mergedParserOptions,
{
...defaultResolveOptions,
...resolveOptions,
},
ast,
);
const kindsNames = Object.keys(store.kinds);
for (let i = 0; i < kindsNames.length; i += 1) {
const kind: StoriesKind = store.kinds[kindsNames[i]];
Expand Down

0 comments on commit 5fb3c47

Please sign in to comment.