Skip to content

Commit

Permalink
Fix remove console.* statements (#421)
Browse files Browse the repository at this point in the history
* Fix remove console.* statements

* ES5ify arrows

* Add call and apply

* Remove early exit that doesn't make much sense

* Handle local declarations of console

* Handle assignment expressions
  • Loading branch information
boopathi committed Feb 20, 2017
1 parent 9ebc3e1 commit 1407fb7
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 11 deletions.
7 changes: 5 additions & 2 deletions lerna.json
@@ -1,5 +1,5 @@
{
"lerna": "2.0.0-beta.34",
"lerna": "2.0.0-beta.37",
"version": "independent",
"publishConfig": {
"ignore": [
Expand All @@ -19,5 +19,8 @@
"Tag: Internal": ":house: Internal",
"Tag: Chore": "Chore"
}
}
},
"packages": [
"packages/*"
]
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -49,7 +49,7 @@
"gulp-newer": "^1.1.0",
"gulp-util": "^3.0.8",
"jest-cli": "^18.0.0",
"lerna": "2.0.0-beta.34",
"lerna": "2.0.0-beta.37",
"lerna-changelog": "^0.3.0",
"through2": "^2.0.1",
"uglify-js": "^2.7.3"
Expand Down
Expand Up @@ -37,7 +37,7 @@ describe("remove-console-plugin", () => {

const expected = unpad(`
function foo() {
true;
true && void 0;
blah();
}
`);
Expand All @@ -51,7 +51,7 @@ describe("remove-console-plugin", () => {
`);

const expected = unpad(`
true;
true && void 0;
blah();
`);
expect(transform(source)).toBe(expected);
Expand Down Expand Up @@ -89,4 +89,69 @@ describe("remove-console-plugin", () => {
`);
expect(transform(source).trim()).toBe(expected);
});

it("should remove console.* assignments to other variables", () => {
const source = unpad(`
const a = console.log;
a();
const b = console.log.bind(console);
b("asdf");
var x = console.log ? console.log('log') : foo();
function foo() {
if (console.error) {
console.error("Errored");
}
}
console.log.call(console, "foo");
console.log.apply(null, {});
`);
const expected = unpad(`
const a = function () {};
a();
const b = function () {};
b("asdf");
var x = function () {} ? void 0 : foo();
function foo() {
if (function () {}) {}
}
`);
expect(transform(source)).toBe(expected);
});

it("should NOT remove local bindings of name console", () => {
const source = unpad(`
function foo(console) {
console.foo("hi");
const bar = console.foo.bind(console);
}
function bar(a) {
const { console } = a;
a.b = console => console.bar("bar");
if (console.foo.call(console, "bar")) {
return;
}
}
`);
expect(transform(source)).toBe(source);
});

it("should convert assigments to no-op", () => {
const source = unpad(`
function foo() {
console.foo = function foo() {
console.log("foo");
};
console.error = myConsoleError;
console.foo();
console.error("asdf");
}
`);
const expected = unpad(`
function foo() {
console.foo = function () {};
console.error = function () {};
}
`);
expect(transform(source)).toBe(expected);
});
});
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-transform-remove-console",
"version": "0.0.1",
"version": "6.8.0",
"description": "Remove all console.* calls.",
"homepage": "https://github.com/babel/babili#readme",
"repository": "https://github.com/babel/babili/tree/master/packages/babel-plugin-transform-remove-console",
Expand Down
65 changes: 61 additions & 4 deletions packages/babel-plugin-transform-remove-console/src/index.js
@@ -1,14 +1,71 @@
"use strict";

module.exports = function() {
module.exports = function({ types: t }) {
return {
name: "transform-remove-console",
visitor: {
CallExpression(path) {
if (path.get("callee").matchesPattern("console", true)) {
path.remove();
const callee = path.get("callee");

if (!callee.isMemberExpression()) return;

if (isConsole(callee)) {
// console.log()
if (path.parentPath.isExpressionStatement()) {
path.remove();
} else {
path.replaceWith(createVoid0());
}
} else if (isConsoleBind(callee)) {
// console.log.bind()
path.replaceWith(createNoop());
}
},
},
MemberExpression: {
exit(path) {
if (isConsole(path) && !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 isConsole(memberExpr) {
const object = memberExpr.get("object");
if (isGlobalConsoleId(object)) return true;

const property = memberExpr.get("property");
return isGlobalConsoleId(object.get("object"))
&& (
property.isIdentifier({ name: "call" })
|| property.isIdentifier({ name: "apply" })
);
}

function isConsoleBind(memberExpr) {
const object = memberExpr.get("object");
return object.isMemberExpression()
&& 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));
}
};
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-transform-remove-debugger",
"version": "0.0.1",
"version": "6.8.0",
"description": "Remove debugger statements",
"homepage": "https://github.com/babel/babili#readme",
"repository": "https://github.com/babel/babili/tree/master/packages/babel-plugin-transform-remove-debugger",
Expand Down

0 comments on commit 1407fb7

Please sign in to comment.