UNPKG

wix-storybook-utils

Version:

Utilities for automated component documentation within Storybook

42 lines 1.44 kB
import * as types from '@babel/types'; import * as parser from '@babel/parser'; import generator from '@babel/generator'; import traverse from '@babel/traverse'; var ensureShorthandProperties = function (_a) { var ast = _a.ast, parentPath = _a.parentPath, scope = _a.scope; return traverse(ast, { Property: function (path) { var _a = path.node, key = _a.key, value = _a.value; if (key.test === value.test) { path.node.shorthand = true; } }, }, scope, parentPath); }; var functionToString = function (prop) { if (typeof prop !== 'function') { return prop; } var asString = prop.toString(); try { var ast = parser.parseExpression(asString); if (types.isArrowFunctionExpression(ast)) { return prop.toString(); } var arrowFunctionBody = ast.body.body.length === 1 && types.isReturnStatement(ast.body.body[0]) ? ast.body.body[0].argument : ast.body; ensureShorthandProperties({ ast: arrowFunctionBody, parentPath: ast, scope: ast, }); var arrowFuncExpr = types.arrowFunctionExpression(ast.params, arrowFunctionBody); return generator(arrowFuncExpr, { tabWidth: 2 }).code; } catch (e) { return asString; } }; export default functionToString; //# sourceMappingURL=function-to-string.js.map