There should be a runtime check for Array-ness
This plugin would be great to use in our configs, except for the issue that it blindly translates any invocation of an includes method into an indexOf and -1 comparison
Here's a test case for where this might fail:
function foo(arr) {
return arr.includes('foo');
}
let x = {
includes: function(x) {
console.log('hi');
}
};
x.includes(123);
will get translated into the following js:
'use strict';
function foo(arr) {
return arr.indexOf('foo') !== -1;
}
var x = {
includes: function includes(x) {
console.log('hi');
}
};
x.indexOf(123) !== -1;
//# sourceMappingURL=test.js.map
which is not valid since x is not an array, and in this case does not have an indexOf() method.
There should probably be something that does type checking at runtime to ensure that this transformation happens in a safe way. I'm not an expert JS spec reader or implementer, so this might have its own issues.
I was thinking that if instead of this transformation of all let z = x.includes(y[, w]) to let z = x.indexOf(y[, w]) !== -1, that it could instead transform the code into:
let z;
if (Array.isArray(x)) {
z = x.indexOf(y, w) !== -1;
} else if (x.includes) {
console.error(new Error('not applying Array.prototype.includes behaviour, object already has includes prop').stack);
z = x.includes(y, w);
}
This might still have issues and is itself not valid JS. I don't really know how to implement this as a babel rule, but I might give it a go.
Hey, This plugin once had such a runtime check. But it apparently doesn't work with babel 6 anymore, so it was removed in #2 (in that way). It would be awesome if you would give it a try. Thanks.