manipulative
Version:
React devtool for modifying Emotion styles in browser
42 lines (32 loc) • 1.49 kB
JavaScript
;
var babelPluginMacros = require('babel-plugin-macros');
var t = require('@babel/types');
function processReferencePaths(referencePaths, state) {
if (referencePaths !== undefined) {
referencePaths.forEach(function (path) {
var callExpressionNode = path.parentPath.node;
if (!t.isCallExpression(callExpressionNode)) {
return;
}
var filename = state.file.opts.filename;
var start = callExpressionNode.start;
if (filename != null && start != null) {
callExpressionNode.arguments = [t.arrayExpression([t.stringLiteral(filename), t.numericLiteral(start)].concat(path.node.loc !== null ? [t.numericLiteral(path.node.loc.start.line), t.stringLiteral(state.file.code.split("\n")[path.node.loc.start.line - 1])] : []))];
}
});
}
}
module.exports = babelPluginMacros.createMacro(function (_ref) {
var references = _ref.references,
state = _ref.state,
babel = _ref.babel;
var t = babel.types;
processReferencePaths(references["useCssPlaceholder"], state);
if (references["useCssPlaceholder"] !== undefined && references["useCssPlaceholder"].length > 0) {
var pathIter = references["useCssPlaceholder"][0];
while (pathIter.parentPath !== null) {
pathIter = pathIter.parentPath;
}
pathIter.unshiftContainer("body", [t.importDeclaration([t.importSpecifier(t.identifier("useCssPlaceholder"), t.identifier("useCssPlaceholder"))], t.stringLiteral("manipulative"))]);
}
});