Skip to content

Commit

Permalink
fix: fix for component extraction from parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 16, 2020
1 parent ff394b9 commit b266ff6
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 37 deletions.
34 changes: 27 additions & 7 deletions core/instrument/src/babel/csf-stories.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
StoriesStore,
Story,
StoryKind,
Stories,
} from '@component-controls/specification';
import { File } from '@babel/types';
Expand Down Expand Up @@ -33,6 +34,7 @@ export const extractCSFStories = (ast: File): StoriesStore => {
}
return undefined;
};
let components: { [key: string]: string | undefined } = {};
const store: StoriesStore = {
stories: {},
kinds: {},
Expand All @@ -45,14 +47,19 @@ export const extractCSFStories = (ast: File): StoriesStore => {

const { title } = attributes || {};
if (typeof title === 'string') {
const components = componentsFromParams(attributes);
store.kinds[title] = {
const attrComponents = componentsFromParams(attributes);
components = attrComponents.reduce(
(acc, componentName) => ({ ...acc, [componentName]: undefined }),
components,
);
const kind: StoryKind = {
...attributes,
components: components.reduce(
(acc, componentName) => ({ ...acc, [componentName]: undefined }),
{},
),
components: {},
};
if (attrComponents.length > 0) {
kind.component = attrComponents[0];
}
store.kinds[title] = kind;
}
},
AssignmentExpression: (path: any) => {
Expand All @@ -72,11 +79,20 @@ export const extractCSFStories = (ast: File): StoriesStore => {
};

if (store.stories[storyName]) {
store.stories[storyName] = {
const attrComponents = componentsFromParams(attributes);
components = attrComponents.reduce(
(acc, componentName) => ({ ...acc, [componentName]: undefined }),
components,
);
const story: Story = {
...attributes,
name,
...store.stories[storyName],
};
if (attrComponents.length > 0) {
story.component = attrComponents[0];
}
store.stories[storyName] = story;
}
}
},
Expand Down Expand Up @@ -129,5 +145,9 @@ export const extractCSFStories = (ast: File): StoriesStore => {
}
},
});
if (Object.keys(store.kinds).length === 1) {
//@ts-ignore
store.kinds[Object.keys(store.kinds)[0]].components = components;
}
return store;
};
26 changes: 1 addition & 25 deletions core/instrument/src/babel/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { followImports } from './follow-imports';
import { packageInfo } from '../misc/packageInfo';
import { propsInfo } from '../misc/propsInfo';
import { InstrumentOptions } from '../types';
import { componentsFromParams } from '../misc/componentAttributes';

const globalCache: {
[filePath: string]: StoryComponent;
Expand Down Expand Up @@ -73,9 +72,7 @@ export const extractStoreComponent = async (
const kinds = Object.keys(store.kinds);
if (kinds.length > 0) {
const kind: StoriesKind = store.kinds[kinds[0]];
kind.components = {};
const componentNames = componentsFromParams(kind);

const componentNames = Object.keys(kind.components);
if (componentNames) {
for (const componentName of componentNames) {
const component = await extractComponent(
Expand All @@ -88,29 +85,8 @@ export const extractStoreComponent = async (
if (component) {
store.components[filePath] = component;
kind.components[componentName] = filePath;
kind.component = componentName;
}
}
}
Object.keys(store.stories).forEach(async (name: string) => {
const story = store.stories[name];
const componentNames = componentsFromParams(story);
if (componentNames) {
for (const componentName of componentNames) {
const component = await extractComponent(
componentName,
filePath,
source,
options,
initialAST,
);
if (component) {
store.components[filePath] = component;
kind.components[componentName] = filePath;
story.component = componentName;
}
}
}
});
}
};
27 changes: 24 additions & 3 deletions core/instrument/src/babel/mdx-stories.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import {
StoriesStore,
Story,
StoriesKind,
StoryParameters,
} from '@component-controls/specification';
import { File } from '@babel/types';
import traverse from '@babel/traverse';
import { extractFunctionParameters } from './get-function-parameters';
import { extractAttributes } from './extract-attributes';
import { sourceLocation } from './utils';
import { componentsFromParams } from '../misc/componentAttributes';

export const extractMDXStories = (ast: File): StoriesStore => {
let components: { [key: string]: string | undefined } = {};
const store: StoriesStore = {
stories: {},
kinds: {},
Expand Down Expand Up @@ -39,7 +42,13 @@ export const extractMDXStories = (ast: File): StoriesStore => {
},
{},
);

const attrComponents = componentsFromParams(attributes);
components = attrComponents.reduce(
(acc, componentName) => ({ ...acc, [componentName]: undefined }),
components,
);
const component =
attrComponents.length > 0 ? attrComponents[0] : undefined;
switch (node.name.name) {
case 'Story': {
const { name } = attributes;
Expand All @@ -49,6 +58,9 @@ export const extractMDXStories = (ast: File): StoriesStore => {
name,
loc: sourceLocation(path.node.loc),
};
if (component) {
story.component = component;
}
traverse(
path.node,
extractFunctionParameters(story),
Expand All @@ -62,11 +74,15 @@ export const extractMDXStories = (ast: File): StoriesStore => {
case 'Meta': {
const { title } = attributes;
if (title) {
store.kinds[title] = {
const kind: StoriesKind = {
components: {},
...attributes,
title,
components: {},
};
if (component !== undefined) {
kind.component = component;
}
store.kinds[title] = kind;
}
break;
}
Expand All @@ -77,5 +93,10 @@ export const extractMDXStories = (ast: File): StoriesStore => {
}
},
});

if (Object.keys(store.kinds).length === 1) {
//@ts-ignore
store.kinds[Object.keys(store.kinds)[0]].components = components;
}
return store;
};
8 changes: 6 additions & 2 deletions core/instrument/src/misc/componentAttributes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Story, StoriesKind } from '@component-controls/specification';
import {
Story,
StoriesKind,
StoryParameters,
} from '@component-controls/specification';

export const componentsFromParams = (
element: StoriesKind | Story,
element: StoriesKind | Story | StoryParameters,
): string[] => {
const result = [];
let { component } = element;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Object {
"stories": Object {
"myStory": Object {
"arguments": Array [],
"component": "ControlsTable",
"loc": Object {
"end": Object {
"column": 31,
Expand Down

0 comments on commit b266ff6

Please sign in to comment.