Add react helpers to babel-types

This commit is contained in:
Marvin Hagemeister
2017-07-18 11:13:56 +02:00
parent 7d57d41e6b
commit 352aa44c0a
2 changed files with 38 additions and 2 deletions
+27
View File
@@ -29,3 +29,30 @@ t.assertBinaryExpression(ast);
t.assertBinaryExpression(ast, { operator: "*" });
var exp: t.Expression = t.nullLiteral();
// React examples:
// https://github.com/babel/babel/blob/4e50b2d9d9c376cee7a2cbf56553fe5b982ea53c/packages/babel-plugin-transform-react-inline-elements/src/index.js#L61
traverse(ast, {
JSXElement(path, file) {
const { node } = path;
const open = node.openingElement;
// init
const type = open.name;
let newType: t.StringLiteral;
if (t.isJSXIdentifier(type) && t.react.isCompatTag(type.name)) {
newType = t.stringLiteral(type.name);
}
const args: any[] = [];
if (node.children.length) {
const children = t.react.buildChildren(node);
args.push(
t.unaryExpression("void", t.numericLiteral(0), true),
...children,
);
}
}
});
+11 -2
View File
@@ -1,6 +1,8 @@
// Type definitions for babel-types v6.7
// Type definitions for babel-types v6.25
// Project: https://github.com/babel/babel/tree/master/packages/babel-types
// Definitions by: Troy Gerwien <https://github.com/yortus>, Sam Baxter <https://github.com/baxtersa>
// Definitions by: Troy Gerwien <https://github.com/yortus>
// Sam Baxter <https://github.com/baxtersa>
// Marvin Hagemeister <https://github.com/marvinhagemeister>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export interface Comment {
@@ -1226,6 +1228,13 @@ export function isUser(node: Object, opts?: Object): boolean;
export function isGenerated(node: Object, opts?: Object): boolean;
export function isPure(node: Object, opts?: Object): boolean;
// React specific
interface ReactHelpers {
isCompatTag(tagName?: string): boolean;
buildChildren(node: Object): Object[];
}
export const react: ReactHelpers;
export function assertArrayExpression(node: Object, opts?: Object): void;
export function assertAssignmentExpression(node: Object, opts?: Object): void;
export function assertBinaryExpression(node: Object, opts?: Object): void;