From 8ebf5463c69d206907bf5dc0161713420dc5b461 Mon Sep 17 00:00:00 2001 From: atanasster Date: Sat, 4 Jul 2020 10:27:01 -0400 Subject: [PATCH] fix: handle "new" expressions in MDX --- .../src/babel/extract-attributes.ts | 12 +++++ core/instrument/src/misc/mdx-exports.ts | 46 +------------------ core/instrument/src/misc/stringify-object.ts | 44 ++++++++++++++++++ 3 files changed, 57 insertions(+), 45 deletions(-) create mode 100644 core/instrument/src/misc/stringify-object.ts diff --git a/core/instrument/src/babel/extract-attributes.ts b/core/instrument/src/babel/extract-attributes.ts index 05c356605..dd679490c 100644 --- a/core/instrument/src/babel/extract-attributes.ts +++ b/core/instrument/src/babel/extract-attributes.ts @@ -1,3 +1,5 @@ +import { stringifyObject } from '../misc/stringify-object'; + interface StoryAttribute { name: string; value: any; @@ -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': diff --git a/core/instrument/src/misc/mdx-exports.ts b/core/instrument/src/misc/mdx-exports.ts index ab0a98130..4466ee10e 100644 --- a/core/instrument/src/misc/mdx-exports.ts +++ b/core/instrument/src/misc/mdx-exports.ts @@ -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; diff --git a/core/instrument/src/misc/stringify-object.ts b/core/instrument/src/misc/stringify-object.ts new file mode 100644 index 000000000..d0378526a --- /dev/null +++ b/core/instrument/src/misc/stringify-object.ts @@ -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(); + } +};