2018-05-24 21:30:45 +03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
//
|
|
|
|
// BEFORE:
|
2018-06-28 21:56:25 +03:00
|
|
|
// eval("require")("./path/to/file")
|
|
|
|
// eval("require")(identifier)
|
2018-05-24 21:30:45 +03:00
|
|
|
//
|
|
|
|
// AFTER:
|
|
|
|
// require("./file")
|
2018-06-28 21:56:25 +03:00
|
|
|
// require(identifier)
|
2018-05-24 21:30:45 +03:00
|
|
|
//
|
|
|
|
|
|
|
|
module.exports = function(babel) {
|
|
|
|
const t = babel.types;
|
2018-06-28 21:56:25 +03:00
|
|
|
|
2018-05-24 21:30:45 +03:00
|
|
|
return {
|
|
|
|
visitor: {
|
2018-06-28 21:56:25 +03:00
|
|
|
CallExpression(path) {
|
2018-05-24 21:30:45 +03:00
|
|
|
const node = path.node;
|
2018-06-28 21:56:25 +03:00
|
|
|
if (isEvalRequire(node)) {
|
|
|
|
let arg = node.arguments[0];
|
|
|
|
if (t.isLiteral(arg) && arg.value.startsWith(".")) {
|
|
|
|
const value = "." + arg.value.substring(arg.value.lastIndexOf("/"));
|
|
|
|
arg = t.stringLiteral(value);
|
|
|
|
}
|
|
|
|
path.replaceWith(t.callExpression(t.identifier("require"), [arg]));
|
2018-05-24 21:30:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-06-28 21:56:25 +03:00
|
|
|
|
|
|
|
function isEvalRequire(node) {
|
|
|
|
return (
|
|
|
|
t.isCallExpression(node.callee) &&
|
|
|
|
node.arguments.length === 1 &&
|
|
|
|
t.isIdentifier(node.callee.callee, { name: "eval" }) &&
|
|
|
|
node.callee.arguments.length === 1 &&
|
|
|
|
t.isLiteral(node.callee.arguments[0], { value: "require" })
|
|
|
|
);
|
|
|
|
}
|
2018-05-24 21:30:45 +03:00
|
|
|
};
|