Skip to content

Commit

Permalink
feat: doc default export as const
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed May 20, 2021
1 parent 52d6a9a commit 05348ee
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 30 deletions.
50 changes: 31 additions & 19 deletions core/instrument/src/babel/esm-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,24 @@ export const extractCSFStories = (
traverse(ast, {
ExportDefaultDeclaration: (path: any) => {
const { declaration } = path.node;
const attributes = extractAttributes(declaration);
const template = declaration.expression?.properties.find(
(prop: any) =>
prop.key?.name === 'template' &&
prop.value?.type === 'ArrowFunctionExpression',
);
let attributes: ReturnType<typeof extractAttributes>;
let template: any;
if (declaration.type === 'Identifier') {
attributes = { ...locals[declaration.name] };
delete attributes['id'];
delete attributes['name'];
} else {
attributes = extractAttributes(declaration);
template = declaration.expression?.properties.find(
(prop: any) =>
prop.key?.name === 'template' &&
prop.value?.type === 'ArrowFunctionExpression',
);
}
const { title: docTitle, name } = attributes || {};
if (template) {
delete attributes.template;
}

const title = docTitle || name;
if (typeof title === 'string') {
const attrComponents = componentsFromParams(attributes);
Expand Down Expand Up @@ -116,7 +123,6 @@ export const extractCSFStories = (
},
VariableDeclaration: (path: any) => {
const story = extractVarFunction(
ast,
_options,
{ source, filePath },
path,
Expand All @@ -142,17 +148,23 @@ export const extractCSFStories = (
const { declarations } = declaration;
if (declarations) {
if (Array.isArray(declarations) && declarations.length > 0) {
const story = extractFunction(
path as NodePath,
declarations[0],
declarations[0].id.name,
);
if (story) {
const name = story.name;
store.stories[name] = {
...story,
...globals[name],
};
const declaration = declarations[0];
if (
declaration.init.type !== 'ObjectExpression' ||
store.doc?.template
) {
const story = extractFunction(
path as NodePath,
declaration,
declaration.id.name,
);
if (story) {
const name = story.name;
store.stories[name] = {
...story,
...globals[name],
};
}
}
}
} else {
Expand Down
21 changes: 11 additions & 10 deletions core/instrument/src/babel/extract-function.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Story, Example } from '@component-controls/core';
import traverse, { NodePath } from '@babel/traverse';
import { File, FunctionDeclaration, VariableDeclarator } from '@babel/types';
import { FunctionDeclaration, VariableDeclarator } from '@babel/types';
import { extractAttributes } from './extract-attributes';
import { extractFunctionParameters } from './extract-function-parameters';
import { followStoryImport } from './follow-imports';
import { sourceLocation } from '../misc/source-location';
Expand Down Expand Up @@ -37,16 +38,17 @@ export const extractFunction = (
);
}
case 'ObjectExpression': {
const attributes = extractAttributes(declaration.init);
const story: Story = {
name,
id: name,
...attributes,
};
if (template) {
const story: Story = {
name,
id: name,
loc: template.loc,
arguments: template.arguments,
};
return story;
story.loc = template.loc;
story.arguments = template.arguments;
}
break;
return story;
}
case 'CallExpression': {
//template.bind
Expand Down Expand Up @@ -81,7 +83,6 @@ export const extractFunction = (
};

export const extractVarFunction = (
ast: File,
_options: InstrumentOptions,
{ source, filePath }: { source: string; filePath: string },
path: any,
Expand Down
1 change: 0 additions & 1 deletion core/instrument/src/babel/mdx-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ export const extractMDXStories: (
},
VariableDeclaration: (path: any) => {
const story = extractVarFunction(
ast,
_options,
{ source, filePath },
path,
Expand Down
9 changes: 9 additions & 0 deletions core/instrument/test/esm-doc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ const createTest = (fileName: string, callback: TestCallback) =>
fixtureToTest(['esm', 'doc'], fileName, callback);

describe('esm-doc', () => {
createTest('default-export-const.ts', parsed => {
expect(parsed).toMatchObject({
doc: {
title: 'Story',
component: 'ControlsTable',
},
});
});

createTest('title-and-parameters.js', parsed => {
expect(parsed).toMatchObject({
doc: {
Expand Down
8 changes: 8 additions & 0 deletions core/instrument/test/fixtures/esm/doc/default-export-const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Document } from '@component-controls/core';
const { ControlsTable } = require('@component-controls/core');
const doc: Document = {
title: 'Story',
component: ControlsTable,
};

export default doc;

0 comments on commit 05348ee

Please sign in to comment.