Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix remove console.* statements (#421)
* 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
Showing
6 changed files
with
136 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 61 additions & 4 deletions
65
packages/babel-plugin-transform-remove-console/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters