Created
October 24, 2016 23:38
-
-
Save minipai/9678a75a8348cfd78f093dab7563b548 to your computer and use it in GitHub Desktop.
babe plugin add Component name on div
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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