├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── package.json └── src └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | lib 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | src 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 James Kyle 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # babel-utils-react 2 | 3 | Utility functions for building Babel plugins for React code 4 | 5 | ## Example 6 | 7 | ```js 8 | import reactUtilsFactory from 'babel-utils-react'; 9 | 10 | export default function ({Plugin, types: t}) { 11 | const reactUtils = reactUtilsFactory(t); 12 | 13 | return new Plugin('ast-transform', { 14 | visitor: { 15 | ClassDeclaration(node) { 16 | if (reactUtils.isReactComponentClassDeclaration(node)) { 17 | // Yay, a React.Component class! 18 | } 19 | } 20 | } 21 | }); 22 | } 23 | ``` 24 | 25 | ## Installation 26 | 27 | ```sh 28 | $ npm install babel-utils-react 29 | ``` 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-utils-react", 3 | "version": "1.0.0", 4 | "description": "Utility functions for building Babel plugins for React code", 5 | "repository": "thejameskyle/babel-utils-react", 6 | "license": "MIT", 7 | "main": "lib/index.js", 8 | "devDependencies": { 9 | "babel": "^5.8.23" 10 | }, 11 | "scripts": { 12 | "build": "babel-plugin build", 13 | "push": "babel-plugin publish", 14 | "test": "babel-plugin test" 15 | }, 16 | "keywords": [ 17 | "babel-plugin", 18 | "babel-utils" 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | export function reactUtilsFactory(t) { 2 | 3 | // is `React.createClass`? 4 | function isReactCreateClassMemberExpression(node) { 5 | return ( 6 | t.isMemberExpression(node) && 7 | t.isIdentifier(node.object, { name: 'React' }) && 8 | t.isIdentifier(node.property, { name: 'createClass' }) 9 | ); 10 | } 11 | 12 | // is `class extends React.Component`? 13 | function isReactComponentClassDeclaration(node) { 14 | return ( 15 | t.isClassDeclaration(node) && 16 | t.isMemberExpression(node.superClass) && 17 | t.isIdentifier(node.superClass.object, { name: 'React' }) && 18 | t.isIdentifier(node.superClass.property, { name: 'Component' }) 19 | ); 20 | } 21 | 22 | // `class extends React.Component` 23 | function getReactComponentClassName(node) { 24 | return node.id && node.id.name; 25 | } 26 | 27 | return { 28 | isReactCreateClassMemberExpression, 29 | isReactComponentClassDeclaration, 30 | getReactComponentClassName 31 | }; 32 | }; 33 | --------------------------------------------------------------------------------