New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix remove console.* statements #421
Changes from 3 commits
a1c5eb9
add99ad
127be4c
ad95eef
7bb7462
3937db7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,61 @@ | ||
"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)) return; | ||
if (!path.parentPath.isMemberExpression()) { | ||
path.replaceWith(createNoop()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks not work with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting, So, I would assume it to be replaced like this - // in
console.foo = foo;
// out
console.foo = function () {}; which looks like the right thing to do. because it is hard to detect all usages of const x = "foo";
const y = console;
y[x](); and will lead to runtime error. |
||
} | ||
} | ||
}, | ||
}; | ||
|
||
function isConsole(memberExpr) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems this needs to check for console.log.call and console.log.apply. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And the test needs to check both |
||
const object = memberExpr.get("object"); | ||
if (object.isIdentifier({ name: "console" })) return true; | ||
|
||
const property = memberExpr.get("property"); | ||
return object.get("object").isIdentifier({ name: "console" }) | ||
&& ( | ||
property.isIdentifier({ name: "call" }) | ||
|| property.isIdentifier({ name: "apply" }) | ||
); | ||
} | ||
|
||
function isConsoleBind(memberExpr) { | ||
const object = memberExpr.get("object"); | ||
return object.isMemberExpression() | ||
&& object.get("object").isIdentifier({ name: "console" }) | ||
&& memberExpr.get("property").isIdentifier({ name: "bind" }); | ||
} | ||
|
||
function createNoop() { | ||
return t.functionExpression(null, [], t.blockStatement([])); | ||
} | ||
|
||
function createVoid0() { | ||
return t.unaryExpression("void", t.numericLiteral(0)); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this clearer as:
if (isConsole(path) && !path.parentPath.isMemberExpression()) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup!. Thanks.