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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a1c5eb9
Fix remove console.* statements
boopathi add99ad
ES5ify arrows
boopathi 127be4c
Add call and apply
boopathi ad95eef
Remove early exit that doesn't make much sense
boopathi 7bb7462
Handle local declarations of console
boopathi 3937db7
Handle assignment expressions
boopathi File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
59 changes: 56 additions & 3 deletions
59
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,67 @@ | ||
"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()) { | ||
path.replaceWith(createNoop()); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
function isGlobalConsoleId(id) { | ||
const name = "console"; | ||
return id.isIdentifier({ name }) | ||
&& !id.scope.getBinding(name) | ||
&& id.scope.hasGlobal(name); | ||
} | ||
|
||
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 (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)); | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It looks not work with
console.err = xx
(React Native have similar usage), maybe we can justparentPath.remove()
if path key isleft
and parentPath is assignment expression?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.
Interesting, So, I would assume it to be replaced like this -
which looks like the right thing to do. because it is hard to detect all usages of
console.foo
in codeand will lead to runtime error.