react-docgen: React-docgen fails to parse components with optional chaining.
If 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 😄
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 17
- Comments: 21 (8 by maintainers)
Commits related to this issue
- Workaround for https://github.com/reactjs/react-docgen/issues/463 — committed to nafg/scalajs-facades by nafg 4 years ago
- Removing optional chaining, to fix docs https://github.com/reactjs/react-docgen/issues/463 — committed to GetStream/stream-chat-react-native by vishalnarkhede 4 years ago
- Smaller fix for reactjs/react-docgen#463 — committed to eps1lon/material-ui by eps1lon 4 years ago
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
As I can see ast-types released a version to support the new ChainExpression. Should react-docgen update his internal dependency to close the issue?
Hi!
When can we expect a new release from
react-docgen
with these changes?Sure.Done.bump, @danez when we can expect a release containing this update?
Update: Babel says it’s an intentional change. I’ve logged an issue to ast-types to add support for the chainExpression type.
Note that you can’t fix this in your project without using e.g.
resolutions
in yarn. Package managers treat 0.x minor bumps as major and won’t useast-types@0.14.x
forreact-docgen
even on fresh installations. So even if this is just a minor version bump it still requires a new release.There isn’t any need to use AST-types any more I don’t think
Looks like babel parser estree plugin made a change that is incompatible with ast-types I’ve logged this issue https://github.com/babel/babel/issues/11908 to babel to check if this considered a non passive change or if it’s a bug that ast-types doesn’t support the
chainedExpression
type.