Rule proposal: no-valueof-field / no-valueof
jfmengels opened this issue · 3 comments
Rule proposal: Forbid specifying a valueOf
field in an object.
const object = {
valueOf: () => 2 // or do something odd
};
object+''
// => '2'
Having valueOf
overriden for an object, or any value, can cause some unexpected, or simply implicit behavior.
Invalid
const object1 = {
value: 15,
valueOf: function() { return this.value; }
};
const object2 = {
value: 25,
valueOf: function() { return this.value; }
};
object1 + object2
// => 40
Valid
const addValueOf = (a, b) => a.value + b.value;
const object1 = {
value: 15
};
const object2 = {
value: 25
};
addValueOf(object1, object2)
// => 40
I'm wondering whether this should also check for X.prototype.valueOf = ...
, X.valueOf = ...
. I'm not sure as it's already kind of covered by no-mutation
, and I'd prefer not to create 2 errors for it. I still think it should (and then the name should be changed, probably to no-valueof
) as people can turn on this rule and not no-mutation
.
And that brings another question, should no-valueof
then also forbid calling valueOf
explicitly?
If people are watching, would gladly get some feedback on this :)
Decided not to forbid calling valueOf
explicitly in this rule. Let me know if someone thinks that should be a new rule.
No, I like it. :)