Last active
September 23, 2015 17:43
-
-
Save drd/ac470e06099a41c4961d to your computer and use it in GitHub Desktop.
Fun with builder
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
const babel = require('babel'); | |
const ast = require('./ast'); | |
const extraction = require('./extraction'); | |
const freeVariables = require('./free-variables'); | |
module.exports = function({Plugin, types: t}) { | |
return new Plugin('transformation', { | |
visitor: { | |
JSXElement: function(node, parent) { | |
if (ast.isElementMarker(node)) { | |
const vars = freeVariables.freeVariablesInMessage(node); | |
const message = extraction.extractElementMessage(node); | |
return t.callExpression( | |
t.memberExpression( | |
t.identifier('React'), | |
t.identifier('createElement') | |
), | |
[ | |
t.literal('I18N'), | |
t.objectExpression([ | |
t.property('init', t.identifier('message'), t.literal(message)), | |
t.property('init', t.identifier('context'), t.arrayExpression( | |
vars.map(v => t.identifier(v)) | |
)), | |
t.property('init', t.identifier('fallback'), t.functionExpression( | |
null, | |
[], | |
t.blockStatement([ | |
t.returnStatement( | |
t.callExpression( | |
t.memberExpression( | |
t.identifier('React'), | |
t.identifier('createElement') | |
), | |
[ | |
t.literal('span'), | |
t.identifier('null'), | |
t.arrayExpression(node.children) | |
] | |
) | |
) | |
]) | |
)) | |
]), | |
t.identifier('null') | |
] | |
) | |
} | |
} | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment