Skip to content

Commit

Permalink
chore: refactor instrument options
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 6, 2020
1 parent 5dc5bf6 commit 925baea
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 86 deletions.
1 change: 1 addition & 0 deletions core/instrument/.eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist
test/examples/
18 changes: 6 additions & 12 deletions core/instrument/src/babel/extract-component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as resolve from 'resolve';
import * as parser from '@babel/parser';
import { File } from '@babel/types';
import {
StoriesStore,
Expand All @@ -9,6 +7,7 @@ import {
} from '@component-controls/specification';
import { followImports } from './follow-imports';
import { packageInfo } from '../project/packageInfo';
import { InstrumentOptions } from '../types';

const componentFromParams = (
parameters?: StoryArguments,
Expand Down Expand Up @@ -47,16 +46,14 @@ export const extractComponent = async (
componentName: string,
filePath: string,
source?: string,
parserOptions?: parser.ParserOptions,
resolveOptions?: resolve.SyncOpts,
optiomns?: InstrumentOptions,
initialAST?: File,
): Promise<StoryComponent | undefined> => {
const follow = followImports(
componentName,
filePath,
source,
parserOptions,
resolveOptions,
optiomns,
initialAST,
);
return follow
Expand All @@ -75,8 +72,7 @@ export const extractSotreComponent = async (
store: StoriesStore,
filePath: string,
source: string,
parserOptions?: parser.ParserOptions,
resolveOptions?: resolve.SyncOpts,
optiomns?: InstrumentOptions,
initialAST?: File,
) => {
const kinds = Object.keys(store.kinds);
Expand All @@ -89,8 +85,7 @@ export const extractSotreComponent = async (
componentName,
filePath,
source,
parserOptions,
resolveOptions,
optiomns,
initialAST,
);
if (component) {
Expand All @@ -107,8 +102,7 @@ export const extractSotreComponent = async (
componentName,
filePath,
source,
parserOptions,
resolveOptions,
optiomns,
initialAST,
);
if (component) {
Expand Down
23 changes: 12 additions & 11 deletions core/instrument/src/babel/follow-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ExportType,
EXPORT_ALL,
} from './extract-exports';
import { InstrumentOptions } from '../types';

export interface FollowImportType {
exportedAs: string;
Expand All @@ -25,13 +26,13 @@ export const followImports = (
importName: string,
filePath: string,
fileSource?: string,
parserOptions?: parser.ParserOptions,
resolveOptions?: resolve.SyncOpts,
options?: InstrumentOptions,
initialAST?: File,
): FollowImportType | undefined => {
const { parser: parserOptions, resolve: resolveOptions } = options || {};

const source = fileSource || fs.readFileSync(filePath, 'utf8');
const ast = initialAST || parser.parse(source, parserOptions);

const baseImportedName = importName.split('.')[0];

const exports: ExportTypes = {
Expand Down Expand Up @@ -61,8 +62,7 @@ export const followImports = (
findExport.internalName,
resolvedFilePath,
undefined,
parserOptions,
resolveOptions,
options,
);
return imported ? { ...imported, from: findExport.from } : undefined;
}
Expand All @@ -84,8 +84,7 @@ export const followImports = (
baseImportedName,
resolvedFilePath,
undefined,
parserOptions,
resolveOptions,
options,
);
}
return { ...foundInExportAll, from: e.from };
Expand All @@ -96,6 +95,7 @@ export const followImports = (
const imports: ImportTypes = {};
traverse(ast, traverseImports(imports));
const findImport = imports[baseImportedName];

if (findImport) {
try {
const resolvedFilePath = resolve.sync(findImport.from, {
Expand All @@ -106,12 +106,13 @@ export const followImports = (
findImport.importedName,
resolvedFilePath,
undefined,
parserOptions,
resolveOptions,
options,
);
return imported ? { ...imported, from: findImport.from } : undefined;
return imported
? { ...imported, from: findImport.from }
: { exportedAs: findImport.importedName, from: findImport.from };
} catch (e) {
//non-exxsiting file
//non-existing file
return { exportedAs: findImport.importedName, from: findImport.from };
}
}
Expand Down
80 changes: 35 additions & 45 deletions core/instrument/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const mdx = require('@mdx-js/mdx');
import { toId, storyNameFromExport } from '@storybook/csf';
import traverse from '@babel/traverse';
import generate from '@babel/generator';
import * as resolve from 'resolve';
import prettier, { Options, ResolveConfigOptions } from 'prettier';
import prettier from 'prettier';
import parserBabel from 'prettier/parser-babylon';
import {
StoriesStore,
Expand All @@ -18,30 +17,43 @@ import { extractMDXStories } from './babel/mdx-stories';
import { removeMDXAttributes } from './babel/remove-mdx-attributes';
import { extractSotreComponent } from './babel/extract-component';
import { packageInfo } from './project/packageInfo';
import {
InstrumentOptions,
defaultParserOptions,
defaultResolveOptions,
} from './types';

type TraverseFn = (stories: StoriesStore) => any;

export type PrettierOptions = Options & {
resolveConfigOptions?: ResolveConfigOptions;
};
export * from './types';

export const defaultResolveOptions: resolve.SyncOpts = {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.vue', '.mjs', '.es', '.es6'],
};
type TraverseFn = (stories: StoriesStore) => any;

export const defaultParserOptions: parser.ParserOptions = {
sourceType: 'module',
plugins: ['jsx', 'typescript'],
};
const parseSource = async (
code: string,
traverseFn: TraverseFn,
originalSource: string,
filePath: string,
parserOptions?: parser.ParserOptions,
prettierOptions?: PrettierOptions,
resolveOptions?: resolve.SyncOpts,
options?: InstrumentOptions,
): Promise<StoriesStore> => {
const {
parser: parserOptions,
prettier: prettierOptions,
resolve: resolveOptions,
component: componentOptions,
} = options || {};

const mergedOptions = {
parser: {
...defaultParserOptions,
...parserOptions,
},
resolve: {
...defaultResolveOptions,
...resolveOptions,
},
prettier: prettierOptions,
component: componentOptions,
};

const prettify = async (c: string): Promise<string> => {
if (prettierOptions !== false) {
const { resolveConfigOptions, ...otherOptions } = prettierOptions || {};
Expand All @@ -64,11 +76,7 @@ const parseSource = async (
}
};
const source = await prettify(code);
const mergedParserOptions = {
...defaultParserOptions,
...parserOptions,
};
const ast = parser.parse(source, mergedParserOptions);
const ast = parser.parse(source, mergedOptions.parser);
const store: StoriesStore = {
stories: {},
kinds: {},
Expand All @@ -94,17 +102,7 @@ const parseSource = async (
{},
);
}
await extractSotreComponent(
store,
filePath,
source,
mergedParserOptions,
{
...defaultResolveOptions,
...resolveOptions,
},
ast,
);
await extractSotreComponent(store, filePath, source, mergedOptions, 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 Expand Up @@ -162,27 +160,21 @@ const parseSource = async (
export const parseCSF = async (
source: string,
filePath: string,
parserOptions?: parser.ParserOptions,
prettierOptions?: PrettierOptions,
resolveOptions?: resolve.SyncOpts,
options?: InstrumentOptions,
): Promise<StoriesStore> => {
return await parseSource(
source,
extractCSFStories,
source,
filePath,
parserOptions,
prettierOptions,
resolveOptions,
options,
);
};

export const parseMDX = async (
source: string,
filePath: string,
parserOptions?: parser.ParserOptions,
prettierOptions?: PrettierOptions,
resolveOptions?: resolve.SyncOpts,
options?: InstrumentOptions,
): Promise<StoriesStore> => {
const code = await mdx(source);

Expand All @@ -200,8 +192,6 @@ export const parseMDX = async (
extractMDXStories,
source,
filePath,
parserOptions,
prettierOptions,
resolveOptions,
options,
);
};
34 changes: 34 additions & 0 deletions core/instrument/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as parser from '@babel/parser';
import * as resolve from 'resolve';
import { Options, ResolveConfigOptions } from 'prettier';

export type PrettierOptions = Options & {
resolveConfigOptions?: ResolveConfigOptions;
};

export const defaultResolveOptions: resolve.SyncOpts = {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.vue', '.mjs', '.es', '.es6'],
};

export const defaultParserOptions: parser.ParserOptions = {
sourceType: 'module',
plugins: ['jsx', 'typescript'],
};

export interface ComponentOptions {
resolveComponent?: (
componentName: string,
FilePath: string,
) => string | undefined;
}

export type ParserOptions = parser.ParserOptions;

export type ResolveOptions = resolve.SyncOpts;

export interface InstrumentOptions {
parser?: parser.ParserOptions;
prettier?: PrettierOptions;
resolve?: ResolveOptions;
component?: ComponentOptions;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`extract-component non-existing-file.js 1`] = `
exports[`extract-component node-modules.js 1`] = `
Object {
"from": "./Button",
"from": "theme-ui",
"loc": undefined,
"name": "Button",
"repository": undefined,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { Button } from 'theme-ui';
15 changes: 7 additions & 8 deletions core/instrument/test/extract-component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import { extractComponent } from '../src/babel/extract-component';

describe('extract-component', () => {
const folderName = path.join(__dirname, 'examples', 'extract-component');
const fileNames = fs.readdirSync(folderName);
const fileNames = fs
.readdirSync(folderName)
.filter(fn => fn === 'node-modules.js');
fileNames.forEach(file => {
const fileName = path.join(folderName, file);
it(file, async () => {
expect(
await extractComponent(
'Button',
fileName,
undefined,
defaultParserOptions,
defaultResolveOptions,
),
await extractComponent('Button', fileName, undefined, {
parser: defaultParserOptions,
resolve: defaultResolveOptions,
}),
).toMatchSnapshot();
});
});
Expand Down
2 changes: 1 addition & 1 deletion core/instrument/test/extract-exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { defaultParserOptions } from '../src/index';
import { extractExports } from '../src/babel/extract-exports';

describe('extract-exports', () => {
const extractExportsForFile = fileName => {
const extractExportsForFile = (fileName: string) => {
const content = fs.readFileSync(path.join(__dirname, fileName), 'utf8');
return extractExports(content, defaultParserOptions);
};
Expand Down
11 changes: 4 additions & 7 deletions core/instrument/test/follow-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ describe('follow-imports', () => {
importName: string,
fileName: string,
) => {
return followImports(
importName,
require.resolve(fileName),
undefined,
defaultParserOptions,
defaultResolveOptions,
);
return followImports(importName, require.resolve(fileName), undefined, {
parser: defaultParserOptions,
resolve: defaultResolveOptions,
});
};
it('simple import', () => {
expect(
Expand Down

0 comments on commit 925baea

Please sign in to comment.