react-docgen
Advanced tools
Comparing version 3.0.0 to 4.0.0
@@ -45,10 +45,2 @@ #!/usr/bin/env node | ||
.option( | ||
'--legacy-decorators', | ||
'Enable parsing of legacy decorators proposal. By default only the new decorators syntax will be parsable.' | ||
) | ||
.option( | ||
'--decorators-before-export', | ||
'Switches the decorators proposal to allow decorators before the export statement. By default this is false.' | ||
) | ||
.option( | ||
'-i, --ignore <path>', | ||
@@ -116,6 +108,5 @@ 'Folders to ignore. Default: ' + JSON.stringify(defaultIgnore), | ||
function parse(source) { | ||
function parse(source, filename) { | ||
return parser.parse(source, resolver, null, { | ||
legacyDecorators: argv.legacyDecorators, | ||
decoratorsBeforeExport: argv.decoratorsBeforeExport, | ||
filename, | ||
}); | ||
@@ -158,3 +149,3 @@ } | ||
try { | ||
result[filename] = parse(content); | ||
result[filename] = parse(content, path.join(filePath, filename)); | ||
} catch (parseError) { | ||
@@ -161,0 +152,0 @@ writeError(parseError, filename); |
@@ -23,34 +23,52 @@ "use strict"; | ||
*/ | ||
const parser = require('@babel/parser'); | ||
const babel = require('@babel/core'); | ||
const babelParserOptions = { | ||
sourceType: 'module', | ||
strictMode: false, | ||
tokens: true, | ||
plugins: ['jsx', 'flow', 'estree', 'doExpressions', 'objectRestSpread', 'classProperties', 'classPrivateProperties', 'classPrivateMethods', 'exportDefaultFrom', 'exportNamespaceFrom', 'asyncGenerators', 'functionBind', 'functionSent', 'dynamicImport', 'numericSeparator', 'optionalChaining', 'importMeta', 'bigInt', 'optionalCatchBinding', 'throwExpressions', ['pipelineOperator', { | ||
proposal: 'minimal' | ||
}], 'nullishCoalescingOperator'] | ||
}; | ||
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(options = {}) { | ||
const parserOptions = (0, _objectSpread2.default)({}, babelParserOptions, { | ||
plugins: [...babelParserOptions.plugins] | ||
}); | ||
function buildOptions({ | ||
cwd, | ||
filename, | ||
parserOptions | ||
}) { | ||
let options = { | ||
plugins: [] | ||
}; | ||
if (options.legacyDecorators) { | ||
parserOptions.plugins.push('decorators-legacy'); | ||
} else { | ||
parserOptions.plugins.push(['decorators', { | ||
decoratorsBeforeExport: options.decoratorsBeforeExport || false | ||
}]); | ||
if (parserOptions) { | ||
options = (0, _objectSpread2.default)({}, parserOptions, { | ||
plugins: parserOptions.plugins ? [...parserOptions.plugins] : [] | ||
}); | ||
} | ||
return parserOptions; | ||
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 parserOptions = buildOptions(options); | ||
function buildParse(options = {}) { | ||
const parserOpts = buildOptions(options); | ||
return { | ||
parse(src) { | ||
return parser.parse(src, parserOptions); | ||
return babel.parseSync(src, { | ||
parserOpts, | ||
cwd: options.cwd, | ||
filename: options.filename | ||
}); | ||
} | ||
@@ -57,0 +75,0 @@ |
@@ -14,2 +14,4 @@ "use strict"; | ||
var _isReactForwardRefCall = _interopRequireDefault(require("../utils/isReactForwardRefCall")); | ||
var _isStatelessComponent = _interopRequireDefault(require("../utils/isStatelessComponent")); | ||
@@ -39,3 +41,3 @@ | ||
const types = recast.types.namedTypes; | ||
const definitions = []; | ||
const definitions = new Set(); | ||
@@ -45,3 +47,3 @@ function classVisitor(path) { | ||
(0, _normalizeClassDefinition.default)(path); | ||
definitions.push(path); | ||
definitions.add(path); | ||
} | ||
@@ -54,3 +56,3 @@ | ||
if ((0, _isStatelessComponent.default)(path)) { | ||
definitions.push(path); | ||
definitions.add(path); | ||
} | ||
@@ -68,10 +70,14 @@ | ||
visitCallExpression: function visitCallExpression(path) { | ||
if (!(0, _isReactCreateClassCall.default)(path)) { | ||
return false; | ||
} | ||
if ((0, _isReactForwardRefCall.default)(path)) { | ||
// If the the inner function was previously identified as a component | ||
// replace it with the parent node | ||
const inner = (0, _resolveToValue.default)(path.get('arguments', 0)); | ||
definitions.delete(inner); | ||
definitions.add(path); | ||
} else if ((0, _isReactCreateClassCall.default)(path)) { | ||
const resolvedPath = (0, _resolveToValue.default)(path.get('arguments', 0)); | ||
const resolvedPath = (0, _resolveToValue.default)(path.get('arguments', 0)); | ||
if (types.ObjectExpression.check(resolvedPath.node)) { | ||
definitions.push(resolvedPath); | ||
if (types.ObjectExpression.check(resolvedPath.node)) { | ||
definitions.add(resolvedPath); | ||
} | ||
} | ||
@@ -82,3 +88,3 @@ | ||
}); | ||
return definitions; | ||
return Array.from(definitions); | ||
} |
@@ -16,2 +16,4 @@ "use strict"; | ||
var _isReactForwardRefCall = _interopRequireDefault(require("../utils/isReactForwardRefCall")); | ||
var _isStatelessComponent = _interopRequireDefault(require("../utils/isStatelessComponent")); | ||
@@ -42,3 +44,3 @@ | ||
function isComponentDefinition(path) { | ||
return (0, _isReactCreateClassCall.default)(path) || (0, _isReactComponentClass.default)(path) || (0, _isStatelessComponent.default)(path); | ||
return (0, _isReactCreateClassCall.default)(path) || (0, _isReactComponentClass.default)(path) || (0, _isStatelessComponent.default)(path) || (0, _isReactForwardRefCall.default)(path); | ||
} | ||
@@ -57,3 +59,3 @@ | ||
return definition; | ||
} else if ((0, _isStatelessComponent.default)(definition)) { | ||
} else if ((0, _isStatelessComponent.default)(definition) || (0, _isReactForwardRefCall.default)(definition)) { | ||
return definition; | ||
@@ -60,0 +62,0 @@ } |
@@ -12,4 +12,2 @@ "use strict"; | ||
var _isReactForwardRefCall = _interopRequireDefault(require("../utils/isReactForwardRefCall")); | ||
var _isReactComponentClass = _interopRequireDefault(require("../utils/isReactComponentClass")); | ||
@@ -19,2 +17,4 @@ | ||
var _isReactForwardRefCall = _interopRequireDefault(require("../utils/isReactForwardRefCall")); | ||
var _isStatelessComponent = _interopRequireDefault(require("../utils/isStatelessComponent")); | ||
@@ -61,6 +61,4 @@ | ||
return definition; | ||
} else if ((0, _isStatelessComponent.default)(definition)) { | ||
} else if ((0, _isStatelessComponent.default)(definition) || (0, _isReactForwardRefCall.default)(definition)) { | ||
return definition; | ||
} else if ((0, _isReactForwardRefCall.default)(definition)) { | ||
return definition; | ||
} | ||
@@ -67,0 +65,0 @@ |
@@ -14,2 +14,4 @@ "use strict"; | ||
var _isReactForwardRefCall = _interopRequireDefault(require("./isReactForwardRefCall")); | ||
/* | ||
@@ -39,3 +41,3 @@ * Copyright (c) 2015, Facebook, Inc. | ||
if (types.CallExpression.check(node) && !(0, _isReactCreateClassCall.default)(path)) { | ||
if (types.CallExpression.check(node) && !(0, _isReactCreateClassCall.default)(path) && !(0, _isReactForwardRefCall.default)(path)) { | ||
if (node.arguments.length) { | ||
@@ -42,0 +44,0 @@ return resolveHOC(path.get('arguments', node.arguments.length - 1)); |
{ | ||
"name": "react-docgen", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "A CLI and toolkit to extract information from React components for documentation generation.", | ||
@@ -24,2 +24,3 @@ "repository": { | ||
"build": "rimraf dist/ && babel src/ --out-dir dist/ --ignore **/__tests__,**/__mocks__,**/src/types.js", | ||
"build:website": "cd website/ && yarn && yarn build", | ||
"lint": "eslint . --report-unused-disable-directives", | ||
@@ -29,2 +30,3 @@ "fix": "eslint . --fix --report-unused-disable-directives", | ||
"preversion": "yarn lint", | ||
"start": "cd website && yarn && yarn start", | ||
"test": "jest", | ||
@@ -41,9 +43,9 @@ "test:ci": "yarn lint && yarn flow && yarn test --runInBand", | ||
"dependencies": { | ||
"@babel/parser": "^7.1.3", | ||
"@babel/core": "^7.0.0", | ||
"@babel/runtime": "^7.0.0", | ||
"async": "^2.1.4", | ||
"commander": "^2.19.0", | ||
"doctrine": "^2.0.0", | ||
"doctrine": "^3.0.0", | ||
"node-dir": "^0.1.10", | ||
"recast": "^0.16.0" | ||
"recast": "^0.17.3" | ||
}, | ||
@@ -57,19 +59,20 @@ "devDependencies": { | ||
"@babel/preset-flow": "^7.0.0", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-eslint": "^10.0.1", | ||
"babel-jest": "^23.6.0", | ||
"babel-jest": "^24.1.0", | ||
"cross-spawn": "^6.0.4", | ||
"eslint": "^5.7.0", | ||
"eslint-config-prettier": "^3.1.0", | ||
"eslint-config-prettier": "^4.0.0", | ||
"eslint-plugin-prettier": "^3.0.0", | ||
"flow-bin": "^0.87.0", | ||
"jest": "^23.6.0", | ||
"jest-diff": "^23.6.0", | ||
"jest-matcher-utils": "^23.6.0", | ||
"flow-bin": "^0.93.0", | ||
"jest": "^24.1.0", | ||
"jest-diff": "^24.0.0", | ||
"jest-matcher-utils": "^24.0.0", | ||
"prettier": "^1.14.3", | ||
"rimraf": "^2.3.2", | ||
"temp": "^0.8.1" | ||
"temp": "^0.9.0" | ||
}, | ||
"jest": { | ||
"setupTestFrameworkScriptFile": "<rootDir>/tests/setupTestFramework.js", | ||
"setupFilesAfterEnv": [ | ||
"<rootDir>/tests/setupTestFramework.js" | ||
], | ||
"roots": [ | ||
@@ -76,0 +79,0 @@ "bin", |
@@ -47,3 +47,2 @@ # react-docgen [![Build Status](https://travis-ci.org/reactjs/react-docgen.svg?branch=master)](https://travis-ci.org/reactjs/react-docgen) | ||
path to a module that exports a resolver. [findExportedComponentDefinition] | ||
--legacy-decorators Switch parsing to support only the legacy decorators syntax | ||
@@ -64,2 +63,5 @@ Extract meta information from React components. | ||
`react-docgen` will look for a babel configuration and use it if available. If no config file is found | ||
it will fallback to a default configuration, enabling all [syntax extension](https://babeljs.io/docs/en/babel-parser#plugins) of the babel-parser. | ||
## API | ||
@@ -88,4 +90,28 @@ | ||
| handlers | Array\<function\> | An array of functions of the form `(documentation: Documentation, definition: NodePath) => void`. Each function is called with a `Documentation` object and a reference to the component definition as returned by `resolver`. Handlers extract relevant information from the definition and augment `documentation`. | | ||
| options | Object | Pass options to react-docgen. Supported option is `legacyDecorators` which is a boolean | | ||
| options | Object | Pass options to react-docgen, see below. | | ||
#### options | ||
##### ∙ filename | ||
Type: `string` | ||
The absolute path to the file associated with the code currently being parsed, if there is one. This is used to search for the correct babel config. | ||
This option is optional, but it is highly recommended to set it when integrating `react-docgen`. | ||
##### ∙ cwd | ||
Type: `string` | ||
Default: `process.cwd()` | ||
The working directory that babel configurations will be searched in. | ||
##### ∙ parserOptions | ||
Type: `BabelParserOptions` | ||
This options will be directly supplied to `@babel/parser`. To see a list of | ||
supported options head over to the [babel website](https://babeljs.io/docs/en/babel-parser#options) and have a look. | ||
#### resolver | ||
@@ -143,3 +169,3 @@ | ||
class MyComponent extends Component { | ||
render: function() { | ||
render() { | ||
// ... | ||
@@ -153,3 +179,3 @@ } | ||
*/ | ||
foo: PropTypes.number, | ||
foo: PropTypes.number.isRequired, | ||
/** | ||
@@ -168,3 +194,2 @@ * Description of prop "bar" (a custom validation function). | ||
MyComponent.defaultProps = { | ||
foo: 42, | ||
bar: 21 | ||
@@ -185,8 +210,4 @@ }; | ||
}, | ||
"required": false, | ||
"description": "Description of prop \"foo\".", | ||
"defaultValue": { | ||
"value": "42", | ||
"computed": false | ||
} | ||
"required": true, | ||
"description": "Description of prop \"foo\"." | ||
}, | ||
@@ -193,0 +214,0 @@ "bar": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
198012
19
4854
418
+ Added@babel/core@^7.0.0
+ Added@ampproject/remapping@2.3.0(transitive)
+ Added@babel/code-frame@7.25.9(transitive)
+ Added@babel/compat-data@7.25.9(transitive)
+ Added@babel/core@7.25.9(transitive)
+ Added@babel/generator@7.25.9(transitive)
+ Added@babel/helper-compilation-targets@7.25.9(transitive)
+ Added@babel/helper-module-imports@7.25.9(transitive)
+ Added@babel/helper-module-transforms@7.25.9(transitive)
+ Added@babel/helper-simple-access@7.25.9(transitive)
+ Added@babel/helper-validator-option@7.25.9(transitive)
+ Added@babel/helpers@7.25.9(transitive)
+ Added@babel/highlight@7.25.9(transitive)
+ Added@babel/template@7.25.9(transitive)
+ Added@babel/traverse@7.25.9(transitive)
+ Added@jridgewell/gen-mapping@0.3.5(transitive)
+ Added@jridgewell/resolve-uri@3.1.2(transitive)
+ Added@jridgewell/set-array@1.2.1(transitive)
+ Added@jridgewell/sourcemap-codec@1.5.0(transitive)
+ Added@jridgewell/trace-mapping@0.3.25(transitive)
+ Addedansi-styles@3.2.1(transitive)
+ Addedast-types@0.12.4(transitive)
+ Addedbrowserslist@4.24.2(transitive)
+ Addedcaniuse-lite@1.0.30001669(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedconvert-source-map@2.0.0(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addeddoctrine@3.0.0(transitive)
+ Addedelectron-to-chromium@1.5.45(transitive)
+ Addedescalade@3.2.0(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedgensync@1.0.0-beta.2(transitive)
+ Addedglobals@11.12.0(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedjsesc@3.0.2(transitive)
+ Addedjson5@2.2.3(transitive)
+ Addedlru-cache@5.1.1(transitive)
+ Addedms@2.1.3(transitive)
+ Addednode-releases@2.0.18(transitive)
+ Addedpicocolors@1.1.1(transitive)
+ Addedrecast@0.17.6(transitive)
+ Addedsemver@6.3.1(transitive)
+ Addedsupports-color@5.5.0(transitive)
+ Addedupdate-browserslist-db@1.1.1(transitive)
+ Addedyallist@3.1.1(transitive)
- Removed@babel/parser@^7.1.3
- Removedast-types@0.11.7(transitive)
- Removeddoctrine@2.1.0(transitive)
- Removedrecast@0.16.2(transitive)
Updateddoctrine@^3.0.0
Updatedrecast@^0.17.3