UNPKG

react-docgen

Version:

A CLI and toolkit to extract information from React components for documentation generation.

91 lines (76 loc) 2.46 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.String = toString; exports.Array = toArray; var _resolveToValue = _interopRequireDefault(require("./resolveToValue")); var _recast = _interopRequireDefault(require("recast")); /* * Copyright (c) 2015, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. * * * */ /*eslint no-loop-func: 0, no-use-before-define: 0*/ const types = _recast.default.types.namedTypes; /** * Splits a MemberExpression or CallExpression into parts. * E.g. foo.bar.baz becomes ['foo', 'bar', 'baz'] */ function toArray(path) { const parts = [path]; let result = []; while (parts.length > 0) { path = parts.shift(); const node = path.node; if (types.CallExpression.check(node)) { parts.push(path.get('callee')); continue; } else if (types.MemberExpression.check(node)) { parts.push(path.get('object')); if (node.computed) { const resolvedPath = (0, _resolveToValue.default)(path.get('property')); if (resolvedPath !== undefined) { result = result.concat(toArray(resolvedPath)); } else { result.push('<computed>'); } } else { result.push(node.property.name); } continue; } else if (types.Identifier.check(node)) { result.push(node.name); continue; } else if (types.Literal.check(node)) { result.push(node.raw); continue; } else if (types.ThisExpression.check(node)) { result.push('this'); continue; } else if (types.ObjectExpression.check(node)) { const properties = path.get('properties').map(function (property) { return toString(property.get('key')) + ': ' + toString(property.get('value')); }); result.push('{' + properties.join(', ') + '}'); continue; } else if (types.ArrayExpression.check(node)) { result.push('[' + path.get('elements').map(toString).join(', ') + ']'); continue; } } return result.reverse(); } /** * Creates a string representation of a member expression. */ function toString(path) { return toArray(path).join('.'); }