Comments (5)
This is how I would implement isComponentClass
:
import _ from 'lodash';
let isComponentClass,
isComponentIdentifier,
isReactComponentMemberExpression,
isReactIdentifier,
isRenderMethod;
/**
* @param {Object} node
* @return {Boolean}
*/
isRenderMethod = (node) => {
return _.every([
node.kind === 'method',
node.key.name === 'render'
]);
};
/**
* @param {Object} node
* @return {Boolean}
*/
isReactIdentifier = (node) => {
return _.every([
node.type === 'Identifier',
node.name === 'React'
]);
};
/**
* @param {Object} node
* @return {Boolean}
*/
isComponentIdentifier = (node) => {
return _.every([
node.type === 'Identifier',
node.name === 'Component'
]);
}
/**
* Checks if class extends React.Component.
*
* @param {Object} node
* @return {Boolean}
*/
isReactComponentMemberExpression = (node) => {
return _.every([
node.type === 'MemberExpression',
isReactIdentifier(node.object),
isComponentIdentifier(node.property)
]);
};
/**
* Checks if class has render method and class extends React.Component.
*
* @param {Object} node
* @return {Boolean}
*/
isComponentClass = (node) => {
let hasRenderMethod,
extendsReactComponent;
hasRenderMethod = Boolean(_.find(node.body.body, isRenderMethod));
extendsReactComponent = isReactComponentMemberExpression(node.superClass);
return _.every([
hasRenderMethod,
extendsReactComponent
]);
};
However, this assumes that class extends React.Component
. I know that some people use Component
alias (which I think is too generic for checking). However, it is easy to extend this implementation to detect if class extends React.Component
or class extends Component
.
from babel-plugin-react-transform.
Check if class extends Component.
Check if class extends React.Component.
React classes don't have to extend Component. You can check: it works just fine without it. Moreover, people might have custom base classes. (Yes it's a bad idea, but people do that occasionally.)
from babel-plugin-react-transform.
On the other hand, people often add extends Component
to appease Flow, so we may as well demand the same. This will also discourage people from inheritance which I guess is a net win. Finally, we can make this configurable like #3 describes, with baseClasses: ['React.Component', 'Component']
being default.
from babel-plugin-react-transform.
Sounds like a good idea. I am still getting my head around how to write Babel plugins. I am in the midst of writing one myself babel-react-plugin-css-modules
, which is an experiment to automate react-css-modules
even further.
I am happy to own this issue and send a PR in a window of a few days.
from babel-plugin-react-transform.
Let's discuss in #8.
from babel-plugin-react-transform.
Related Issues (20)
- Local path as an argument to "transform" does not resolve the path.
- Modify the contents of the function will not be updated.
- Doesn't work with React.PureComponent HOT 1
- Template is not a function
- Deprecated with no alternative? HOT 1
- no release info or changelog for 3.0.0
- `addImport()` is deprecated in Babel 7 HOT 2
- Use without .babelrc HOT 4
- jspm with plugin-babel and react preset: "cannot read property 'transform' of undefined" HOT 1
- React transforms being run on Backbone views HOT 6
- Question: Is it a bad practice to use react-transform for production code? HOT 1
- Transform(s) to strip propTypes and displayName HOT 4
- Ember objects detected as react components HOT 5
- Appears to wrap other functions than createClass() HOT 10
- error when passing properties to createClass() HOT 4
- Future of React Transform HOT 2
- Can we have superClass Regexp matching option , for inheritance ? HOT 2
- undefined value static properties outside class HOT 3
- displayName equals 'Constructor' HOT 8
- static properties are undefined HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from babel-plugin-react-transform.