Image with expanded props throws `The prop must be a JSXAttribute collected by the AST parser.`
See original GitHub issueHi, I have this wrapper around an image:
export default class MaybeImage extends React.Component {
state = {
error: false,
}
handleError = e => this.setState({ error: true })
render() {
const { alt, src, ...props } = this.props
return !src || this.state.error ? (
<span {...props}>{alt}</span>
) : (
<img src={src} alt={alt} onError={this.handleError} {...props} />
)
}
}
But it breaks eslint with an error: The prop must be a JSXAttribute collected by the AST parser. It’s not just an eslint error, rather unhandled javascript exception. I think #7 might be related, but even when I disable the rule, it still breaks.
This line in jsx-ast-utils
gets the prop name, but it doesn’t count with spread operator. This is the content of prop
which throws an error:
Node {
type: 'JSXSpreadAttribute',
argument:
Node {
type: 'Identifier',
name: 'props',
_babelType: 'Identifier',
},
_babelType: 'JSXSpreadAttribute',
parent:
Node {
type: 'JSXOpeningElement',
attributes: [Array],
name: [Node],
selfClosing: true,
_babelType: 'JSXOpeningElement',
} }
What’s your suggestion here? Pass props explicitely so it can be statically analyzed? Or is it bug in jsx-ast-utils
? I think it should show an eslint error or warning at best, but not throw an exception.
Cheers 👋
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Image with expanded props throws The prop must be ... - GitHub
But it breaks eslint with an error: The prop must be a JSXAttribute collected by the AST parser. It's not just an eslint...
Read more >node_modules/eslint-plugin-jsx-a11y/CHANGELOG.md · master · Lê ...
[fix] aria-proptypes support for idlist, #454; [fix] Image with expanded props throws 'The prop must be a JSXAttribute collected by the AST parser....
Read more >jsx Ast Utils: AST Utility Module for Statically analyzing JSX - Morioh
Returns boolean indicating whether an prop exists as an attribute on a JSX element node. Props. Object - The attributes on the visited...
Read more >eslint-plugin-jsx-a11y | Yarn - Package Manager
Static AST checker for accessibility rules on JSX elements. eslint, eslintplugin, eslint-plugin, a11y. readme. build status npm version license ...
Read more >node_modules/eslint-plugin-jsx-a11y/CHANGELOG.md ... - GitLab
[fix] aria-proptypes support for idlist, #454; [fix] Image with expanded props throws 'The prop must be a JSXAttribute collected by the AST parser....
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
Ok, looking into it.
@jessebeach Nice! Thank you. Here’s the PR #460 with test case and possible fix.