react-docgen
Version:
A CLI and toolkit to extract information from React components for documentation generation.
60 lines (46 loc) • 1.74 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = propDocBlockHandler;
var _getMemberValuePath = _interopRequireDefault(require("../utils/getMemberValuePath"));
var _recast = _interopRequireDefault(require("recast"));
var _resolveToValue = _interopRequireDefault(require("../utils/resolveToValue"));
var _setPropDescription = _interopRequireDefault(require("../utils/setPropDescription"));
/*
* 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.
*
*
*
*/
const types = _recast.default.types.namedTypes;
function resolveDocumentation(documentation, path) {
if (!types.ObjectExpression.check(path.node)) {
return;
}
path.get('properties').each(propertyPath => {
if (types.Property.check(propertyPath.node)) {
(0, _setPropDescription.default)(documentation, propertyPath);
} else if (types.SpreadElement.check(propertyPath.node)) {
const resolvedValuePath = (0, _resolveToValue.default)(propertyPath.get('argument'));
resolveDocumentation(documentation, resolvedValuePath);
}
});
}
function propDocBlockHandler(documentation, path) {
let propTypesPath = (0, _getMemberValuePath.default)(path, 'propTypes');
if (!propTypesPath) {
return;
}
propTypesPath = (0, _resolveToValue.default)(propTypesPath);
if (!propTypesPath) {
return;
}
resolveDocumentation(documentation, propTypesPath);
}