├── .gitignore ├── .npmignore ├── package.json ├── README.md └── src └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | lib 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | src 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-plugin-react-class-display-name", 3 | "version": "0.1.0", 4 | "description": "Add displayName to React.Component classes", 5 | "repository": "researchgate/babel-plugin-react-class-display-name", 6 | "license": "MIT", 7 | "main": "lib/index.js", 8 | "devDependencies": { 9 | "babel": "^5.6.0" 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 | ] 19 | } 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # babel-plugin-react-class-display-name 2 | 3 | **Unmaintained: This plugin is not maintained** 4 | 5 | Add displayName to ES6 classes that extend React.Component 6 | 7 | ## Installation 8 | 9 | ```sh 10 | $ npm install babel-plugin-react-class-display-name 11 | ``` 12 | 13 | ## Usage 14 | 15 | ### Via `.babelrc` (Recommended) 16 | 17 | **.babelrc** 18 | 19 | ```json 20 | { 21 | "plugins": ["react-class-display-name"] 22 | } 23 | ``` 24 | 25 | ### Via CLI 26 | 27 | ```sh 28 | $ babel --plugins react-class-display-name script.js 29 | ``` 30 | 31 | ### Via Node API 32 | 33 | ```javascript 34 | require("babel-core").transform("code", { 35 | plugins: ["react-class-display-name"] 36 | }); 37 | ``` 38 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | export default function ({ Plugin, types: t }) { 2 | return new Plugin("react-class-display-name", { 3 | visitor: { 4 | ClassDeclaration(node, parent, scope, file) { 5 | if (this.get("superClass").matchesPattern("React.Component")) { 6 | (parent.type === 'ExportDefaultDeclaration' ? this.parentPath : this).insertAfter([ // take the "export default class ..." form into account 7 | t.expressionStatement(t.assignmentExpression( 8 | "=", 9 | t.memberExpression(node.id, t.identifier("displayName")), 10 | t.literal(node.id.name) 11 | )) 12 | ]); 13 | } 14 | } 15 | } 16 | }); 17 | } 18 | --------------------------------------------------------------------------------