diff --git a/lerna.json b/lerna.json index 48cb80242..3d8da8848 100644 --- a/lerna.json +++ b/lerna.json @@ -1,5 +1,5 @@ { - "lerna": "2.0.0-beta.34", + "lerna": "2.0.0-beta.37", "version": "independent", "publishConfig": { "ignore": [ @@ -19,5 +19,8 @@ "Tag: Internal": ":house: Internal", "Tag: Chore": "Chore" } - } + }, + "packages": [ + "packages/*" + ] } diff --git a/package.json b/package.json index 7e6213006..2638b3300 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/packages/babel-plugin-transform-remove-console/__tests__/remove-console-test.js b/packages/babel-plugin-transform-remove-console/__tests__/remove-console-test.js index 02337e4db..44fd28f3d 100644 --- a/packages/babel-plugin-transform-remove-console/__tests__/remove-console-test.js +++ b/packages/babel-plugin-transform-remove-console/__tests__/remove-console-test.js @@ -37,7 +37,7 @@ describe("remove-console-plugin", () => { const expected = unpad(` function foo() { - true; + true && void 0; blah(); } `); @@ -51,7 +51,7 @@ describe("remove-console-plugin", () => { `); const expected = unpad(` - true; + true && void 0; blah(); `); expect(transform(source)).toBe(expected); @@ -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); + }); }); diff --git a/packages/babel-plugin-transform-remove-console/package.json b/packages/babel-plugin-transform-remove-console/package.json index 97c52b3f4..ec12fa716 100644 --- a/packages/babel-plugin-transform-remove-console/package.json +++ b/packages/babel-plugin-transform-remove-console/package.json @@ -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", diff --git a/packages/babel-plugin-transform-remove-console/src/index.js b/packages/babel-plugin-transform-remove-console/src/index.js index 3ebb70697..4553e6b28 100644 --- a/packages/babel-plugin-transform-remove-console/src/index.js +++ b/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)); + } }; diff --git a/packages/babel-plugin-transform-remove-debugger/package.json b/packages/babel-plugin-transform-remove-debugger/package.json index e933f3b26..345efd5d2 100644 --- a/packages/babel-plugin-transform-remove-debugger/package.json +++ b/packages/babel-plugin-transform-remove-debugger/package.json @@ -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",