Files
add-and-commit/node_modules/babel-plugin-transform-remove-console/lib/index.js
Federico Grandi f118062594 Update with working working version (#12)
* Fix workflow

* Trigger

* [auto] Update compiled version

* [auto] Commit modules

* Push Windows changes

* Fix

* [auto] Update compiled version

* Try removing cwd

* [auto] Update compiled version

* Try with path module

* [auto] Update compiled version

* Fix path

* [auto] Update compiled version

* Use raw path

* [auto] Update compiled version

* Other path

* [auto] Update compiled version

* Avoid @action/exec

* [auto] Update compiled version

* test

* [auto] Update compiled version

* test

* [auto] Update compiled version

* test

* [auto] Update compiled version

* test

* [auto] Update compiled version

* Try with shelljs

* [auto] Update compiled version

* Fix my stupidity

* Copy scripts to local dir

* [auto] Update compiled version

* Still use path

* [auto] Update compiled version

* Delete entrypoint.sh

* [auto] Update compiled version

* Make file executable

* [auto] Update compiled version

* Try using bash

* [auto] Update compiled version
2019-12-14 21:47:13 +01:00

82 lines
2.3 KiB
JavaScript

"use strict";
module.exports = function ({
types: t
}) {
return {
name: "transform-remove-console",
visitor: {
CallExpression(path, state) {
const callee = path.get("callee");
if (!callee.isMemberExpression()) return;
if (isIncludedConsole(callee, state.opts.exclude)) {
// console.log()
if (path.parentPath.isExpressionStatement()) {
path.remove();
} else {
path.replaceWith(createVoid0());
}
} else if (isIncludedConsoleBind(callee, state.opts.exclude)) {
// console.log.bind()
path.replaceWith(createNoop());
}
},
MemberExpression: {
exit(path, state) {
if (isIncludedConsole(path, state.opts.exclude) && !path.parentPath.isMemberExpression()) {
if (path.parentPath.isAssignmentExpression() && path.parentKey === "left") {
path.parentPath.get("right").replaceWith(createNoop());
} else {
path.replaceWith(createNoop());
}
}
}
}
}
};
function isGlobalConsoleId(id) {
const name = "console";
return id.isIdentifier({
name
}) && !id.scope.getBinding(name) && id.scope.hasGlobal(name);
}
function isExcluded(property, excludeArray) {
return excludeArray && excludeArray.some(name => property.isIdentifier({
name
}));
}
function isIncludedConsole(memberExpr, excludeArray) {
const object = memberExpr.get("object");
const property = memberExpr.get("property");
if (isExcluded(property, excludeArray)) return false;
if (isGlobalConsoleId(object)) return true;
return isGlobalConsoleId(object.get("object")) && (property.isIdentifier({
name: "call"
}) || property.isIdentifier({
name: "apply"
}));
}
function isIncludedConsoleBind(memberExpr, excludeArray) {
const object = memberExpr.get("object");
if (!object.isMemberExpression()) return false;
if (isExcluded(object.get("property"), excludeArray)) return false;
return isGlobalConsoleId(object.get("object")) && memberExpr.get("property").isIdentifier({
name: "bind"
});
}
function createNoop() {
return t.functionExpression(null, [], t.blockStatement([]));
}
function createVoid0() {
return t.unaryExpression("void", t.numericLiteral(0));
}
};