Skip to content

Instantly share code, notes, and snippets.

@minipai
Created October 24, 2016 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minipai/9678a75a8348cfd78f093dab7563b548 to your computer and use it in GitHub Desktop.
Save minipai/9678a75a8348cfd78f093dab7563b548 to your computer and use it in GitHub Desktop.
babe plugin add Component name on div
module.exports = function (babel) {
var t = babel.types;
var visitor = {
ClassDeclaration: function(code) {
var node = code.node;
var className = node.id.name;
var renderMethod;
for (i=0; i<code.node.body.body.length; i++) {
var method = code.node.body.body[i];
if (method.type === 'ClassMethod' && method.key.name === 'render') {
renderMethod = method;
}
}
var returnStatement;
for (i=0; i<renderMethod.body.body.length; i++) {
var statement = renderMethod.body.body[i];
if (statement.type === 'ReturnStatement') {
returnStatement = statement;
}
}
var jsxelement = returnStatement.argument.openingElement
var attrDisplayName = t.jSXIdentifier('data-component');
jsxelement.attributes.push(t.jSXAttribute(attrDisplayName, t.stringLiteral(className)));
},
};
return {
visitor: visitor
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment