react-docgen
Version:
A CLI and toolkit to extract information from React components for documentation generation.
76 lines (62 loc) • 2.16 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = buildParse;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));
/*
* 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 babel = require('@babel/core');
const defaultPlugins = ['jsx', 'flow', 'asyncGenerators', 'bigInt', 'classProperties', 'classPrivateProperties', 'classPrivateMethods', ['decorators', {
decoratorsBeforeExport: false
}], 'doExpressions', 'dynamicImport', 'exportDefaultFrom', 'exportNamespaceFrom', 'functionBind', 'functionSent', 'importMeta', 'logicalAssignment', 'nullishCoalescingOperator', 'numericSeparator', 'objectRestSpread', 'optionalCatchBinding', 'optionalChaining', ['pipelineOperator', {
proposal: 'minimal'
}], 'throwExpressions'];
function buildOptions({
cwd,
filename,
parserOptions
}) {
let options = {
plugins: []
};
if (parserOptions) {
options = (0, _objectSpread2.default)({}, parserOptions, {
plugins: parserOptions.plugins ? [...parserOptions.plugins] : []
});
}
const partialConfig = babel.loadPartialConfig({
cwd,
filename
});
if (!partialConfig.hasFilesystemConfig() && options.plugins.length === 0) {
options.plugins = [...defaultPlugins];
} // Recast needs tokens to be in the tree
// $FlowIssue tokens is clearly in the Options
options.tokens = true; // Ensure we always have estree plugin enabled, if we add it a second time
// here it does not matter
options.plugins.push('estree');
return options;
}
function buildParse(options = {}) {
const parserOpts = buildOptions(options);
return {
parse(src) {
return babel.parseSync(src, {
parserOpts,
cwd: options.cwd,
filename: options.filename
});
}
};
}