Skip to content

Commit

Permalink
fix: generate code for complex attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jul 4, 2020
1 parent 8ebf546 commit 17f5b09
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
3 changes: 1 addition & 2 deletions core/instrument/src/babel/csf-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,10 @@ export const extractCSFStories = (
},
});
if (store.doc) {
debugger;
//@ts-ignore
store.doc.componentsLookup = components;
} else {
throw new Error(`stories should have one default export`);
throw new Error(`esm files should have one default export`);
}
return store;
};
27 changes: 13 additions & 14 deletions core/instrument/src/babel/extract-attributes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import generate from '@babel/generator';
import { stringifyObject } from '../misc/stringify-object';

interface StoryAttribute {
Expand All @@ -24,32 +25,30 @@ const nodeToValue = (node: any): any => {
return node.raw;
case 'RegExpLiteral':
const value = node.raw ?? node.extra ? node.extra.raw : undefined;
// remove leading trailing slashes for string to reg xonversion
// remove leading trailing slashes for string to reg conversion
return typeof value === 'string'
? value.replace(/^\/|\/$/g, '')
: value;
case 'MemberExpression':
return new String(`${node.object.name}.${node.property.name}`);
case 'NewExpression':
return new String(
`new ${node.callee.name}(${
node.arguments
? node.arguments
.map((arg: any) => stringifyObject(nodeToValue(arg)))
.join(', ')
: ''
})`,
);
case 'ObjectExpression':
return extractAttributes(node);
case 'ArrayExpression':
if (Array.isArray(node.elements)) {
return node.elements.map((v: any) => nodeToValue(v));
}
break;
default:
// console.log(property.value);
return undefined;
default: {
if (node.type) {
const { code } = generate(node, {
retainFunctionParens: true,
retainLines: true,
});
return code ? new String(code) : undefined;
} else {
return node;
}
}
}
}
return undefined;
Expand Down

0 comments on commit 17f5b09

Please sign in to comment.