Anton Rusinov fcdfc61bdb Move plugin processing to top of plugins (#6381)
* centralize plugin options

* Centralize plugins options

- move more options to the top
- move validations that depend on options to the top

* use isLoose option

* Move more validations to the top

* Move ref parameter for rewriteModuleStatementsAndPrepareHeader() to the top

* fix eslint errors

* remove unused parameter

* set default systemGlobal value

* Revert "Move ref parameter for rewriteModuleStatementsAndPrepareHeader() to the top"

This reverts commit b3855302d17fa19d8acb4c8accab3680c8d2710e.

* Revert "Move more validations to the top"

This reverts commit e5861d8a034ff8f553391f55654f753bcf428a5d.

* fix allowMutablePropsOnTags option usage

* improve naming

* change Contructor definition for sake of consistency

* move allowMutablePropsOnTags validation to the top

* add missing !
2017-10-10 00:51:34 -04:00

56 lines
1.3 KiB
JavaScript

import jsx from "babel-plugin-syntax-jsx";
import helper from "babel-helper-builder-react-jsx";
export default function({ types: t }, options) {
const { pragma } = options;
let id = pragma || "React.createElement";
const JSX_ANNOTATION_REGEX = /\*?\s*@jsx\s+([^\s]+)/;
const visitor = helper({
pre(state) {
const tagName = state.tagName;
const args = state.args;
if (t.react.isCompatTag(tagName)) {
args.push(t.stringLiteral(tagName));
} else {
args.push(state.tagExpr);
}
},
post(state, pass) {
state.callee = pass.get("jsxIdentifier")();
},
});
visitor.Program = function(path, state) {
const { file } = state;
for (const comment of (file.ast.comments: Array<Object>)) {
const matches = JSX_ANNOTATION_REGEX.exec(comment.value);
if (matches) {
id = matches[1];
break;
}
}
state.set("jsxIdentifier", () =>
id
.split(".")
.map(name => t.identifier(name))
.reduce((object, property) => t.memberExpression(object, property)),
);
};
visitor.JSXAttribute = function(path) {
if (t.isJSXElement(path.node.value)) {
path.node.value = t.jSXExpressionContainer(path.node.value);
}
};
return {
inherits: jsx,
visitor,
};
}