-
-
Notifications
You must be signed in to change notification settings - Fork 35
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
no-mutating-methods sort after concat #19
Comments
Hi @eddiemoore ! Sorry for the late answer, and thanks for the interesting suggestion. There isn't one at the moment, but we could implement it. This rule is actually kind of a hack (or a heuristic of some sort), in the sense that it forbids some methods without actually knowing whether the object on which the method is called is actually an array and whether its In that sense, this rule is "limited" in its usefulness as it will allow code like this: const array = { // array is not a real array
map(fn) {
window.foo++; // :O
}
}
array.map(a => a); // .map() mutates stuff Similarly, you could make this (Yes, they're all purposely trivial examples) const someGlobalArray = [1, 2, 3];
const fakeArray = {
concat(arg) {
if (!arg) {
return someGlobalArray;
}
return someGlobalArray.concat(arg);
}
};
fakeArray.concat().sort(); // Sort will mutate someGlobalArray This is the part where I am a bit torn. Sure, these examples are too simple, but someone might once use a library whose In practice, I think this should be safe for most cases, so I might be willing to have this implemented. Probably behind a flag though, to keep wary users feeling kind of secure. What do you think? |
Yeah I get what you mean. If people are abusing it then that's their own fault I guess 😛. There will always be someone trying to abuse the code. Just thinking that in most cases it would be safe. Besides, I think if someone is including the In your examples with |
Unless there is an option to make sure a function is not accessing things outside of the scope. So everything would need to be passed into the function. |
Found this when coming to submit an issue about Ramda's As for allowing |
@ahstro You could add "fp/no-mutating-methods": ["error", {
"allowedObjects": ['_', 'R', 'fp']
}] Although yes, |
Oh, thanks, didn't know that |
Is there any way to configure the rule to allow this? It felt dirty, but I tried this with no success:
|
ESLint and this plugin specifically are great at catching high level bugs and style issues, but for full accuracy and safety with (im)mutable types I'd recommend using a typechecker, as ESLint has fairly limited type inference. For example, you can use TypeScript's ReadonlyArray type to ensure that your Array constants are immutable, but built in methods like concat and sort will still return normal Arrays that are mutable: const myarray: ReadonlyArray<unknown> = []
// TS: Property 'sort' does not exist on type 'readonly unknown[]'.
myarray.sort()
// Works fine because concat is implemented on ReadonlyArray, and it returns an Array which implements sort
myarray.concat().sort() |
Was just wondering if there would be a way to enable a mutating method like
.sort()
only after a method that returns a new array such as.map()
,.filter()
,.concat()
etc.So you could do
The text was updated successfully, but these errors were encountered: