move some babel-plugins into the main repo
This commit is contained in:
3
packages/babel-plugin-react-display-name/.npmignore
Normal file
3
packages/babel-plugin-react-display-name/.npmignore
Normal file
@@ -0,0 +1,3 @@
|
||||
node_modules
|
||||
*.log
|
||||
src
|
||||
35
packages/babel-plugin-react-display-name/README.md
Normal file
35
packages/babel-plugin-react-display-name/README.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# babel-plugin-react-display-name
|
||||
|
||||
Add displayName to React.createClass calls
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
$ npm install babel-plugin-react-display-name
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Via `.babelrc` (Recommended)
|
||||
|
||||
**.babelrc**
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": ["react-display-name"]
|
||||
}
|
||||
```
|
||||
|
||||
### Via CLI
|
||||
|
||||
```sh
|
||||
$ babel --plugins react-display-name script.js
|
||||
```
|
||||
|
||||
### Via Node API
|
||||
|
||||
```javascript
|
||||
require("babel-core").transform("code", {
|
||||
plugins: ["react-display-name"]
|
||||
});
|
||||
```
|
||||
11
packages/babel-plugin-react-display-name/package.json
Normal file
11
packages/babel-plugin-react-display-name/package.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "babel-plugin-react-display-name",
|
||||
"version": "2.0.0",
|
||||
"description": "Add displayName to React.createClass calls",
|
||||
"repository": "babel-plugins/babel-plugin-react-display-name",
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
]
|
||||
}
|
||||
84
packages/babel-plugin-react-display-name/src/index.js
Normal file
84
packages/babel-plugin-react-display-name/src/index.js
Normal file
@@ -0,0 +1,84 @@
|
||||
import path from "path";
|
||||
|
||||
export default function ({ Plugin, types: t }) {
|
||||
function addDisplayName(id, call) {
|
||||
var props = call.arguments[0].properties;
|
||||
var safe = true;
|
||||
|
||||
for (var i = 0; i < props.length; i++) {
|
||||
var prop = props[i];
|
||||
var key = t.toComputedKey(prop);
|
||||
if (t.isLiteral(key, { value: "displayName" })) {
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (safe) {
|
||||
props.unshift(t.property("init", t.identifier("displayName"), t.literal(id)));
|
||||
}
|
||||
}
|
||||
|
||||
var isCreateClassCallExpression = t.buildMatchMemberExpression("React.createClass");
|
||||
|
||||
function isCreateClass(node) {
|
||||
if (!node || !t.isCallExpression(node)) return false;
|
||||
|
||||
// not React.createClass call member object
|
||||
if (!isCreateClassCallExpression(node.callee)) return false;
|
||||
|
||||
// no call arguments
|
||||
var args = node.arguments;
|
||||
if (args.length !== 1) return false;
|
||||
|
||||
// first node arg is not an object
|
||||
var first = args[0];
|
||||
if (!t.isObjectExpression(first)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return new Plugin("react-display-name", {
|
||||
metadata: {
|
||||
group: "builtin-pre"
|
||||
},
|
||||
|
||||
visitor: {
|
||||
ExportDefaultDeclaration(node, parent, scope, file) {
|
||||
if (isCreateClass(node.declaration)) {
|
||||
var displayName = file.opts.basename;
|
||||
|
||||
// ./{module name}/index.js
|
||||
if (displayName === "index") {
|
||||
displayName = path.basename(path.dirname(file.opts.filename));
|
||||
}
|
||||
|
||||
addDisplayName(displayName, node.declaration);
|
||||
}
|
||||
},
|
||||
|
||||
"AssignmentExpression|Property|VariableDeclarator"(node) {
|
||||
var left, right;
|
||||
|
||||
if (t.isAssignmentExpression(node)) {
|
||||
left = node.left;
|
||||
right = node.right;
|
||||
} else if (t.isProperty(node)) {
|
||||
left = node.key;
|
||||
right = node.value;
|
||||
} else if (t.isVariableDeclarator(node)) {
|
||||
left = node.id;
|
||||
right = node.init;
|
||||
}
|
||||
|
||||
if (t.isMemberExpression(left)) {
|
||||
left = left.property;
|
||||
}
|
||||
|
||||
if (t.isIdentifier(left) && isCreateClass(right)) {
|
||||
addDisplayName(left.name, right);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user