Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upNew rule: no-prototype-builtins #539
Comments
feross
added
the
enhancement
label
Jun 2, 2016
This comment has been minimized.
This comment has been minimized.
foo.hasOwnProperty("bar")Seems like would be in reasonably widespread use - do we have a % breakage on this? |
This comment has been minimized.
This comment has been minimized.
Perhaps. Just wanted to start a discussion, since I noticed this is in the latest eslint version. Seems like a legit potential bug in lots of programs, since it's an intricacy of the language that few understand. |
This comment has been minimized.
This comment has been minimized.
Yeah, we should def try and catch as many bugs as possible - to clarify: not opposed to enforcing this as it will catch errors (hadn't thought about this particular interaction until this issue). |
This comment has been minimized.
This comment has been minimized.
3 of those failures are false positives due to a bug in the newest eslint. Still, this is a decent-sized breakage. Next step would be to look through the 23 cases where it fails and see which are actual potential sources of crashes. If a decent number of them (half?) are indeed real issues, then it probably makes sense to enable this in the next major version, even though some will need to change their code. |
This comment has been minimized.
This comment has been minimized.
qzb
commented
Jun 3, 2016
|
Honestly I'm surprised that % breakage is that small. Anyway, I'm not a fan of for (var bar in foo) {
if ({}.isPrototypeOf.call(foo, bar)) {
// ...
}
}BTW, if someone needs map, should use |
This comment has been minimized.
This comment has been minimized.
|
How is this for (var bar in foo) {
if ({}.hasOwnProperty.call(foo, bar)) {
// ...
}
}much worse than this? for (var bar in foo) {
if (foo.hasOwnProperty(bar)) {
// ...
}
}I've seen a lot of recommendations for using the former instead of the latter. Also, I totally agree that you should use a |
This comment has been minimized.
This comment has been minimized.
|
Tiny modules to the rescue? :) module.exports = hasOwnProperty
function hasOwnProperty (obj, prop) {
return {}.hasOwnProperty.call(obj, prop)
}
// example
var hasOwnProperty = require('has-own-property')
// ...
for (var bar in foo) {
if (hasOwnProperty(foo, bar)) {
// ...
}
}..this one looks the best |
This comment has been minimized.
This comment has been minimized.
|
Said and done |
This comment has been minimized.
This comment has been minimized.
|
LOL you beat me barely! :)
|
This comment has been minimized.
This comment has been minimized.
|
@Flet Just saw that you created your own repo, sorry I thought that you where handing out an idea I'd be happy to transfer ownership on npm to you |
This comment has been minimized.
This comment has been minimized.
|
No worries @LinusU! Honestly I thought sindre would already have this built |
This comment has been minimized.
This comment has been minimized.
|
Haha, yes, I was quite surprised when I saw that the name was free I'm adding you as a maintainer at least :) that is, if you want to? |
This comment has been minimized.
This comment has been minimized.
|
Cool! :) |
This comment has been minimized.
This comment has been minimized.
|
@LinusU my problem was it took me over 2 minutes to figure out a cute emoji for the title |
This comment has been minimized.
This comment has been minimized.
|
Hehehe, yeah, I saw that you where very fast with the project structure. Do you use any tool for that? I literally typed out the entire module with |
This comment has been minimized.
This comment has been minimized.
|
I use @ungoldman's module-init to get the structure up quickly. |
This comment has been minimized.
This comment has been minimized.
monolithed
commented
Jun 26, 2016
•
What about own properties?let object = {
hasOwnProperty () {
return false;
}
};
for (let key in object) {
console.log(Object.prototype.hasOwnProperty.call(object, key)); //true
}
for (let key in object) {
console.log(object.hasOwnProperty(key)); // false
}It can be fixed: Object.prototype.length = 1;
let object = {};
for (let key in object) {
console.log(key); //length
}
for (let key in Object.entries(object)) {
// nothing
}But what about legacy code? What about type detections?Object.prototype.toString.call({}); // [object Object]Parsing code program (popular usage): let types = {
'[object Object]' () {
// ... do some work
},
'[object Array]' () {
// ... do some work
},
// ...
};
let type = Object.prototype.toString.call(some_entity);
types[type](); //What about polyfills?It seems you need more experience before making an issue like this |
This comment has been minimized.
This comment has been minimized.
|
I think we'll pass on this for now. Upon further investigation, the error is a bit too confusing and yields mostly false positives. We can always re-examine this later. |
feross commentedJun 2, 2016
•
edited
Disallow use of
Object.prototypesbuiltins directlyhttp://eslint.org/docs/rules/no-prototype-builtins
eslint/eslint#2693