Replace x != undefined with x != null#572
Conversation
|
|
||
| const babel = require("babel-core"); | ||
| const plugin = require("../src/index"); | ||
| const unpad = require("../../../utils/unpad"); |
| const op = node.operator; | ||
| const negated = node.operator.startsWith("!"); | ||
|
|
||
| if (["!=", "=="].includes(node.operator)) { |
There was a problem hiding this comment.
use indexOf, we still support node 4
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.
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.
It doesn’t appear to handle this. Are you saying that I should move it there?
There was a problem hiding this comment.
I could add void 0 to isNull.
There was a problem hiding this comment.
I meant this entire replacement shouldn't be a part of this plugin. It should be handled in path.evaluate of babel.
|
I'm wondering if we can do this, isn't it unsafe? |
|
@xtuc |
| const op = node.operator; | ||
|
|
||
| if (["!=", "=="].indexOf(node.operator) !== -1) { | ||
| if (t.isIdentifier(node.right, { name: "undefined" })) { |
There was a problem hiding this comment.
void 0 will not be transformed.
|
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.
is this return true / false required? I don't see it used anywhere for conditional check.
| function isRealUndefined(path) { | ||
| return ( | ||
| path.isIdentifier({ name: "undefined" }) && | ||
| !path.scope.getBinding("undefined") |
There was a problem hiding this comment.
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.
@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.
/cc @boopathi Any reason why the check is needed? Am i missing some edge case?
There was a problem hiding this comment.
function foo(undefined) { return undefined == void 0 }
foo("bar"); // true or false ?There was a problem hiding this comment.
true even if undefined or both are converted to null
There was a problem hiding this comment.
> function foo(undefined) { return undefined == void 0 }
> foo("bar"); // true or false ?
// false|
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 == nullandnull != nullwith!0and!1, respectively.Tests included.