Skip to content

Commit

Permalink
add more plugins, rename some
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmck committed Sep 15, 2015
1 parent 3e8cbc6 commit 9969224
Show file tree
Hide file tree
Showing 328 changed files with 2,013 additions and 1,543 deletions.
35 changes: 0 additions & 35 deletions packages/babel-plugin-async-to-bluebird-coroutines/README.md

This file was deleted.

22 changes: 22 additions & 0 deletions packages/babel-plugin-builder-react-jsx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# babel-plugin-builder-react-jsx

## Usage

```javascript
type ElementState = {
tagExpr: Object; // tag node
tagName: string; // raw string tag name
args: Array<Object>; // array of call arguments
call?: Object; // optional call property that can be set to override the call expression returned
};

require("babel-plugin-builder-react-jsx")({
pre: function (state: ElementState) {
// called before building the element
},

post: function (state: ElementState) {
// called after building the element
}
});
```
13 changes: 13 additions & 0 deletions packages/babel-plugin-builder-react-jsx/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "babel-plugin-builder-react-jsx",
"version": "1.0.0",
"description": "",
"repository": "babel/babel",
"license": "MIT",
"main": "lib/index.js",
"dependencies": {
"babel-runtime": "^5.8.20",
"esutils": "^2.0.0",
"lodash": "^3.10.0"
}
}
153 changes: 153 additions & 0 deletions packages/babel-plugin-builder-react-jsx/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import isString from "lodash/lang/isString";
import esutils from "esutils";

export default function (t, opts) {
var visitor = {};

visitor.JSXNamespacedName = function (path) {
throw path.buildCodeFrameError("Namespace tags are not supported. ReactJSX is not XML.");
};

visitor.JSXElement = {
exit(path, file) {
var callExpr = buildElementCall(path.get("openingElement"), file);

callExpr.arguments = callExpr.arguments.concat(path.node.children);

if (callExpr.arguments.length >= 3) {
callExpr._prettyCall = true;
}

return t.inherits(callExpr, path.node);
}
};

return visitor;

function convertFunctionName(path) {
var { node } = path;

if (path.isJSXIdentifier()) {
if (node.name === "this" && path.isReferenced()) {
return t.thisExpression();
} else if (esutils.keyword.isIdentifierNameES6(node.name)) {
node.type = "Identifier";
} else {
return t.stringLiteral(node.name);
}
} else if (path.isJSXMemberExpression()) {
node.computed = t.isLiteral(node.property);
node.type = "MemberExpression";
}

return node;
}

function convertAttributeValue(node) {
if (t.isJSXExpressionContainer(node)) {
return node.expression;
} else {
return node;
}
}

function convertAttribute(node) {
var value = convertAttributeValue(node.value || t.booleanLiteral(true));

if (t.isLiteral(value) && isString(value.value)) {
value.value = value.value.replace(/\n\s+/g, " ");
}

node.name.type = "Identifier";

return t.inherits(t.property("init", node.name, value), node);
}

function buildElementCall(path, file) {
path.parent.children = t.react.buildChildren(path.parent);

var tagExpr = convertFunctionName(path.get("name"));
var args = [];

var tagName;
if (t.isIdentifier(tagExpr)) {
tagName = tagExpr.name;
} else if (t.isLiteral(tagExpr)) {
tagName = tagExpr.value;
}

var state = {
tagExpr: tagExpr,
tagName: tagName,
args: args
};

if (opts.pre) {
opts.pre(state, file);
}

var attribs = path.node.attributes;
if (attribs.length) {
attribs = buildOpeningElementAttributes(attribs, file);
} else {
attribs = t.nullLiteral();
}

args.push(attribs);

if (opts.post) {
opts.post(state, file);
}

return state.call || t.callExpression(state.callee, args);
}

/**
* The logic for this is quite terse. It's because we need to
* support spread elements. We loop over all attributes,
* breaking on spreads, we then push a new object containg
* all prior attributes to an array for later processing.
*/

function buildOpeningElementAttributes(attribs, file) {
var _props = [];
var objs = [];

function pushProps() {
if (!_props.length) return;

objs.push(t.objectExpression(_props));
_props = [];
}

while (attribs.length) {
var prop = attribs.shift();
if (t.isJSXSpreadAttribute(prop)) {
pushProps();
objs.push(prop.argument);
} else {
_props.push(convertAttribute(prop));
}
}

pushProps();

if (objs.length === 1) {
// only one object
attribs = objs[0];
} else {
// looks like we have multiple objects
if (!t.isObjectExpression(objs[0])) {
objs.unshift(t.objectExpression([]));
}

// spread it
attribs = t.callExpression(
file.addHelper("extends"),
objs
);
}

return attribs;
}
}
3 changes: 0 additions & 3 deletions packages/babel-plugin-class-properties/.gitignore

This file was deleted.

3 changes: 0 additions & 3 deletions packages/babel-plugin-comprehensions/.gitignore

This file was deleted.

7 changes: 0 additions & 7 deletions packages/babel-plugin-comprehensions/src/index.js

This file was deleted.

35 changes: 0 additions & 35 deletions packages/babel-plugin-dead-code-elimination/README.md

This file was deleted.

3 changes: 0 additions & 3 deletions packages/babel-plugin-decorators/.gitignore

This file was deleted.

7 changes: 0 additions & 7 deletions packages/babel-plugin-decorators/src/index.js

This file was deleted.

3 changes: 0 additions & 3 deletions packages/babel-plugin-do-expressions/.gitignore

This file was deleted.

7 changes: 0 additions & 7 deletions packages/babel-plugin-do-expressions/src/index.js

This file was deleted.

3 changes: 0 additions & 3 deletions packages/babel-plugin-es2015-arrow-functions/.gitignore

This file was deleted.

35 changes: 0 additions & 35 deletions packages/babel-plugin-es2015-arrow-functions/README.md

This file was deleted.

12 changes: 0 additions & 12 deletions packages/babel-plugin-es2015-arrow-functions/src/index.js

This file was deleted.

35 changes: 0 additions & 35 deletions packages/babel-plugin-es2015-block-scoped-functions/README.md

This file was deleted.

3 changes: 0 additions & 3 deletions packages/babel-plugin-es2015-block-scoping/.gitignore

This file was deleted.

Loading

0 comments on commit 9969224

Please sign in to comment.