React-docgen fails to parse components with optional chaining.
See original GitHub issueIf a component contains optional chaining it will now fail to parse. This used to work.
I’m on version 4.1.1.
Now you get this error:
Error: did not recognize object of type "ChainExpression"
at Object.getFieldNames (/proj/node_modules/ast-types/lib/types.js:661:19)
at visitChildren (/proj/node_modules/ast-types/lib/path-visitor.js:186:36)
at Visitor.PVp.visitWithoutReset (/proj/node_modules/ast-types/lib/path-visitor.js:168:20)
at visitChildren (/proj/node_modules/ast-types/lib/path-visitor.js:205:25)
at Visitor.PVp.visitWithoutReset (/proj/node_modules/ast-types/lib/path-visitor.js:168:20)
at NodePath.each (/proj/node_modules/ast-types/lib/path.js:89:26)
at visitChildren (/proj/node_modules/ast-types/lib/path-visitor.js:180:18)
at Visitor.PVp.visitWithoutReset (/proj/node_modules/ast-types/lib/path-visitor.js:168:20)
at visitChildren (/proj/node_modules/ast-types/lib/path-visitor.js:205:25)
at Visitor.PVp.visitWithoutReset (/proj/node_modules/ast-types/lib/path-visitor.js:168:20)
I kind of suspect this has to do with one of the transitive dependencies more that react-docgen but I’m logging this here first.
How to recreate, run the following test against the below jsx file.
test.js
const reactDocs = require('react-docgen');
const fs = require('fs');
const filePath = './file.jsx';
const source = fs.readFileSync(filePath, 'utf8');
const parsedProps = reactDocs.parse(source);
console.log('parsed', parsedProps);
file.jsx
import React from 'react';
const ApplicationBase = () => {
const thing = {
stuff: 'stuff',
};
// Parse works when the next line is commented out.
const string = thing?.stuff;
// Works when un-commented
// const string = 'stuff';
return (
<div>
{string}
</div>
);
};
export default ApplicationBase;
Also an issue template would help me remember all the things you’d like to see on an issue 😄
Issue Analytics
- State:
- Created 3 years ago
- Reactions:17
- Comments:21 (8 by maintainers)
Top Results From Across the Web
Why is react-scripts having a compile error for optional ...
It sounds like you're using the latest version of react-scripts which means it's likely to support optional chaining. Is there a sample repo ......
Read more >ast-types - Bountysource
I stumbled upon this while trying to use react-doc-gen against a component with optional chanining and the ast-types library that it uses fails...
Read more >Developer guide | React Styleguidist
Styleguidist uses react-docgen to parse source files (not transpiled). react-docgen finds exported React components and generates documentation based on ...
Read more >Attribution - Sandtime.io
... @babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining ... @babel/plugin-syntax-optional-chaining ... @babel/plugin-transform-react-display-name ...
Read more >@alfalab/core-components - npm
Start using @alfalab/core-components in your project by running `npm i ... @babel/plugin-proposal-optional-chaining · @babel/preset- ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You can work around this problem by locking in your babel dependencies in your project’s package.json
For example:
Here’s the change we made to our project: https://github.com/cerner/terra-ui/pull/290/files
In case anyone is interested in what this means, in your package.json just add a resolutions key like so