Added support for ES6 imports and exports (and PureComponent)
Fixes #23 & #8 by checking imports for import React, { Component } from 'react', and also adds support for the new PureComponent.
It possibly also fixes #22 due to the fix for export declarations (which are mostly only used in the tests, since Babel already converts the ES6 exports down to ES5 syntax).
- Updated
babel-helper-is-react-class- Version bump to
2.0.0because of the API change (requirespathinstead ofpath.node) - Added a traversal for named imports (e.g.
import React, { Component } from 'react') - Added checks for
PureComponent
- Version bump to
- Updated
babel-plugin-transform-react-pure-class-to-function- Refactored replacement logic to read more clearly now that there's 3 code paths
- Added export declaration check, to avoid "We don't know what to do with this node type" errors
- Added extra tests for new functionality
@LeoIannacone Well, I've made a temporary fork on NPM that I've been using: babel-plugin-transform-react-pure-class-to-function-fork
I was planning on deprecating it once this PR got merged.
@thedillonb Wouldn't a shouldComponentUpdate inherently contain references to this, and thus would skip the transformation?
@thedillonb Actually, after a cursory glance over the plugin, it looks like anything that contains more than just render, static propTypes, or static defaultProps will not be transformed.
@vdh, you're right. If there are methods other than render then it will not be transformed. However, in your test case for test/fixtures/pure-component/actual.js and test/fixtures/pure-component/expected.js, the expected is incorrect. Converting a PureComponent into a stateless functional component (SFC) drops the functionality. React.PureComponent implements componentShouldUpdate for you as a shallowCompare a SFC does not do any comparisons on the prop types and with thus always re-render even if the props are the same (see https://github.com/facebook/react/issues/5677, and more specifically https://github.com/facebook/react/issues/5677#issuecomment-241190513 and https://github.com/facebook/react/issues/5677#issuecomment-289324043).
@thedillonb Hmm, that's sort of a limitation in how React currently implements the rendering of stateless functional components, but I get what you're saying.
Perhaps if I added some sort of optional configuration toggle to skip PureComponent? Something like ignorePureComponent to mirror the related config in the prefer-stateless-function rule from eslint-plugin-react.
I have forked and revived the original plugin (here, don't like the idea of having presets and plugins mixed) and made it up to date with some of the pull requests in here.
I have added the option (disabled by default) to convert PureComponents or not, and added some additional features.
You know, maybe if a single person in any of these PRs offered to help out maintaining this project, then it would be more active. But everyone just wants to complain. So you all get nothing
@thejameskyle I can try at least to go though the opened issues. I use the plugin in production, so I'm interested in it. Would you mind to add some permissions to me?
Added