Skip to content

Commit

Permalink
fix: handle "new" expressions in MDX
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jul 4, 2020
1 parent be140e0 commit 8ebf546
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 45 deletions.
12 changes: 12 additions & 0 deletions core/instrument/src/babel/extract-attributes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { stringifyObject } from '../misc/stringify-object';

interface StoryAttribute {
name: string;
value: any;
Expand Down Expand Up @@ -28,6 +30,16 @@ const nodeToValue = (node: any): any => {
: 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':
Expand Down
46 changes: 1 addition & 45 deletions core/instrument/src/misc/mdx-exports.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,5 @@
import jsStringEscape from 'js-string-escape';
import { MDXExportType, MDXExportTypes } from '../types';

const stringifyObject = (
val: any,
sep: string = ' ',
depth: number = 1,
): string => {
const t = typeof val;
switch (t) {
case 'string':
return `"${jsStringEscape(val)}"`;
case 'function':
return val.name || val.toString();
case 'object':
if (val instanceof Date) {
return '"' + val.toISOString() + '"';
}
if (val instanceof String) {
return val.toString();
}
if (Array.isArray(val)) {
return (
'[' +
val.map(v => stringifyObject(v, sep, depth + 1)).join(', ') +
']'
);
}
if (val === null) {
return 'null';
}
return `
{
${Object.keys(val)
.map(key => {
return typeof val[key] === 'function'
? null
: `${key}: ${stringifyObject(val[key], sep, depth + 1)}`;
})
.filter(v => v)}
}
`;
default:
return val.toString();
}
};
import { stringifyObject } from './stringify-object';

const mdxPropertiesExport = (exportType: MDXExportType): string | undefined => {
return exportType ? stringifyObject(exportType.story) : undefined;
Expand Down
44 changes: 44 additions & 0 deletions core/instrument/src/misc/stringify-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import jsStringEscape from 'js-string-escape';

export const stringifyObject = (
val: any,
sep: string = ' ',
depth: number = 1,
): string => {
switch (typeof val) {
case 'string':
return `"${jsStringEscape(val)}"`;
case 'function':
return val.name || val.toString();
case 'object':
if (val instanceof Date) {
return '"' + val.toISOString() + '"';
}
if (val instanceof String) {
return val.toString();
}
if (Array.isArray(val)) {
return (
'[' +
val.map(v => stringifyObject(v, sep, depth + 1)).join(', ') +
']'
);
}
if (val === null) {
return 'null';
}
return `
{
${Object.keys(val)
.map(key => {
return typeof val[key] === 'function'
? null
: `${key}: ${stringifyObject(val[key], sep, depth + 1)}`;
})
.filter(v => v)}
}
`;
default:
return val.toString();
}
};

0 comments on commit 8ebf546

Please sign in to comment.