question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Image with expanded props throws `The prop must be a JSXAttribute collected by the AST parser.`

See original GitHub issue

Hi, 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:closed
  • Created 5 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jessebeachcommented, Jul 8, 2018

Ok, looking into it.

0reactions
tricoder42commented, Jul 9, 2018

@jessebeach Nice! Thank you. Here’s the PR #460 with test case and possible fix.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found