-
-
Notifications
You must be signed in to change notification settings - Fork 225
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
Replace x != undefined
with x != null
#572
Conversation
|
||
const babel = require("babel-core"); | ||
const plugin = require("../src/index"); | ||
const unpad = require("../../../utils/unpad"); |
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.
can be removed
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.
Done.
@@ -9,6 +14,17 @@ module.exports = function() { | |||
BinaryExpression(path) { | |||
const { node } = path; | |||
const op = node.operator; | |||
const negated = node.operator.startsWith("!"); | |||
|
|||
if (["!=", "=="].includes(node.operator)) { |
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.
use indexOf, we still support node 4
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.
Fixed.
Codecov Report
@@ Coverage Diff @@
## master #572 +/- ##
==========================================
+ Coverage 83.02% 83.46% +0.44%
==========================================
Files 41 42 +1
Lines 2763 2867 +104
Branches 967 1008 +41
==========================================
+ Hits 2294 2393 +99
- Misses 282 283 +1
- Partials 187 191 +4
Continue to review full report at Codecov.
|
}); | ||
|
||
it("should shorten `undefined == null`", () => { | ||
const source = "undefined == null"; |
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.
this should be already taken care of by constant-folding plugin. This plugin should not worry about these folding constants.
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 doesn’t appear to handle this. Are you saying that I should move it there?
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.
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.
I could add void 0
to isNull
.
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.
How does that sound @boopathi?
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.
I meant this entire replacement shouldn't be a part of this plugin. It should be handled in path.evaluate
of babel.
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.
@boopathi done.
I'm wondering if we can do this, isn't it unsafe? |
@xtuc |
@@ -9,6 +9,13 @@ module.exports = function() { | |||
BinaryExpression(path) { | |||
const { node } = path; | |||
const op = node.operator; | |||
|
|||
if (["!=", "=="].indexOf(node.operator) !== -1) { | |||
if (t.isIdentifier(node.right, { name: "undefined" })) { |
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.
void 0
will not be transformed.
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.
Fixed.
Can you add some tests for this ? |
@boopathi Are you assuming in babili that people won’t do |
@boopathi Tests restored. |
Constant-folding plugin should take care of folding constants. |
Just a few thoughts on this -
|
|
|
|
My question is whether it should go into comparison operators, or should it go into simplify. IMO, Comparison operators simplifies the operator used without changing the operands. |
function undefinedToNull(path) { | ||
if (isRealUndefined(path) || isPureVoid(path)) { | ||
path.replaceWith(t.nullLiteral()); | ||
return true; |
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 return true / false
required? I don't see it used anywhere for conditional check.
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.
Removed 🔥
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.
👍
function isRealUndefined(path) { | ||
return ( | ||
path.isIdentifier({ name: "undefined" }) && | ||
!path.scope.getBinding("undefined") |
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.
why is this check required?. Even if someone does
var undefined = 'blah';
Its a read only property and does not impact our null conversion.
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.
@boopathi said:
t.isIdentifier("undefined")
check should also check for bindings in the scopepath.scope.getBinding("undefined")
there is a definedundefined
.
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.
/cc @boopathi Any reason why the check is needed? Am i missing some edge case?
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.
function foo(undefined) { return undefined == void 0 }
foo("bar"); // true or false ?
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.
true
even if undefined or both are converted to null
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.
> function foo(undefined) { return undefined == void 0 }
> foo("bar"); // true or false ?
// false
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.
okay got it 👍
It's not immediately obvious that this goes into
|
@boopathi If you’d like me to move it to |
Sorry for the delay on this @boopathi 😢 I’ve moved the changes to |
Looks like the Travis failure is unrelated. Closing and reopening to restart the build. |
Ping @boopathi and @vigneshshanmugam for 👀 |
@j-f1 Hi, Thanks for the PR. Right now let's concentrate on fixing the existing bugs, babel-7, improving the speed of transformations and fixing the memory issue to get it to 1.0. New set of features may delay that. I'll leave it open and postpone it to sometime later. |
Bonus: replaceundefined == null
andnull != null
with!0
and!1
, respectively.Tests included.