Skip to content

Commit

Permalink
feat: enhance object controls handling, merging
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed May 4, 2020
1 parent 04c9838 commit 62366e4
Show file tree
Hide file tree
Showing 26 changed files with 667 additions and 462 deletions.
11 changes: 6 additions & 5 deletions core/instrument/src/babel/extract-arguments-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const addArgumentUsage = (
story: Story,
args: StoryArguments,
node: any,
shorthand: boolean,
): ArgUsageLocation | undefined => {
const param = findArguments(args, node.name);

Expand All @@ -46,7 +47,7 @@ export const addArgumentUsage = (
p.loc.end.line === loc.end.line &&
p.loc.end.column === loc.end.column,
);
const usage: ArgUsageLocation = { loc };
const usage: ArgUsageLocation = { loc, shorthand };
if (!existing) {
param.usage.push(usage);
return usage;
Expand All @@ -59,22 +60,22 @@ export const extractArgumentsUsage = (story: Story, args: StoryArguments) => {
TemplateLiteral: (path: any) => {
path.node.expressions.forEach((expression: any) => {
if (expression.type === 'Identifier') {
addArgumentUsage(story, args, expression);
addArgumentUsage(story, args, expression, expression.shorthand);
}
});
},
JSXSpreadAttribute: (path: any) => {
if (path.node.argument.type === 'Identifier') {
addArgumentUsage(story, args, path.node.argument);
addArgumentUsage(story, args, path.node.argument, path.node.shorthand);
}
},
Identifier: (path: any) => {
addArgumentUsage(story, args, path.node);
addArgumentUsage(story, args, path.node, path.node.shorthand);
},
Property: (path: any) => {
const node = path.node;
if (node.value.type === 'Identifier' && node.key.type === 'Identifier') {
const usage = addArgumentUsage(story, args, node.value);
const usage = addArgumentUsage(story, args, node.value, node.shorthand);
if (usage) {
usage.name = {
loc: adjustSourceLocation(story, node.key.loc),
Expand Down
2 changes: 1 addition & 1 deletion core/instrument/src/babel/extract-function-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const extractFunctionParameters = (
if (story.arguments.length) {
const params = story.arguments[0];
if (node.body.type === 'Identifier') {
addArgumentUsage(story, [params], node.body);
addArgumentUsage(story, [params], node.body, node.shorthand);
} else if (node.body.type === 'TemplateLiteral') {
traverse(
node,
Expand Down
2 changes: 1 addition & 1 deletion core/instrument/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const parseSource = async (
}
for (const key of Object.keys(store.stories)) {
const story: Story = store.stories[key];
story.source = await prettify(getASTSource(source, story.loc));
story.source = getASTSource(source, story.loc);
}
return store;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ Object {
},
},
"name": "story",
"source": "() => 'hello';
",
"source": "() => 'hello'",
"subcomponents": Object {
"My Button Tab": "Button",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ Object {
},
},
"name": "story3",
"source": "() => {};
",
"source": "() => {}",
},
},
"transformed": "export default {
Expand Down Expand Up @@ -81,8 +80,7 @@ Object {
},
},
"name": "story1",
"source": "() => {};
",
"source": "() => {}",
},
},
"transformed": "export default {
Expand Down Expand Up @@ -131,8 +129,7 @@ Object {
},
},
"name": "story1",
"source": "() => {};
",
"source": "() => {}",
},
"story2": Object {
"arguments": Array [],
Expand All @@ -148,8 +145,7 @@ Object {
},
},
"name": "story2",
"source": "() => {};
",
"source": "() => {}",
},
},
"transformed": "export default {
Expand Down Expand Up @@ -195,8 +191,7 @@ Object {
},
},
"name": "story2",
"source": "() => {};
",
"source": "() => {}",
},
"story3": Object {
"arguments": Array [],
Expand All @@ -212,8 +207,7 @@ Object {
},
},
"name": "story3",
"source": "() => {};
",
"source": "() => {}",
},
},
"transformed": "export default {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ Object {
},
},
"name": "myStory",
"source": "() => {};
",
"source": "() => {}",
},
},
"transformed": "export default { title: 'Story' };
Expand Down Expand Up @@ -75,8 +74,7 @@ Object {
},
},
"name": "myStory",
"source": "() => {};
",
"source": "() => {}",
},
},
"transformed": "export default { title: 'Story' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ Object {
},
"component": "ControlsTable",
},
"source": "() => {};
",
"source": "() => {}",
},
},
"transformed": "export default { title: 'Story' };
Expand Down Expand Up @@ -105,8 +104,7 @@ Object {
},
},
},
"source": "() => {};
",
"source": "() => {}",
},
},
"transformed": "export default { title: 'Story' };
Expand Down Expand Up @@ -161,8 +159,7 @@ Object {
},
},
"name": "myStory",
"source": "() => {};
",
"source": "() => {}",
},
},
"transformed": "export default { title: 'Story' };
Expand Down
29 changes: 16 additions & 13 deletions core/instrument/test/__snapshots__/csf-props-info.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,51 @@ Can adapt to multiple groups of controls, displaying them in their own tabs.",
"description": "if false, will nothave a collapsible frame.",
"parentName": "BlockContainerProps",
"type": Object {
"name": "any",
"raw": "any",
"name": "boolean",
"raw": "boolean",
},
},
"description": Object {
"description": "optional markdown description.",
"parentName": "BlockContainerProps",
"type": Object {
"name": "any",
"raw": "any",
"name": "string",
"raw": "string",
},
},
"id": Object {
"description": "id of the story",
"description": "id of the story
optional id to be used for the block
if no id is provided, one will be calculated automatically
from the title.",
"parentName": "StoryInputProps",
"type": Object {
"name": "any",
"raw": "any",
"name": "string",
"raw": "string",
},
},
"name": Object {
"description": "alternatively you can use the name of a story to load from an external file",
"parentName": "StoryInputProps",
"type": Object {
"name": "any",
"raw": "any",
"name": "string",
"raw": "string",
},
},
"sxStyle": Object {
"description": "theme-ui styling object for Block Box",
"parentName": "BlockContainerProps",
"type": Object {
"name": "any",
"raw": "any",
"name": "SystemStyleObject",
"raw": "SystemStyleObject",
},
},
"title": Object {
"description": "optional section title for the block.",
"parentName": "BlockContainerProps",
"type": Object {
"name": "any",
"raw": "any",
"name": "string",
"raw": "string",
},
},
},
Expand Down
Loading

0 comments on commit 62366e4

Please sign in to comment.