In our angular project the ESLint shout:
"Detects variable[key] as a left- or right-hand assignment operand."
It is detect-object-injection
rule. And says that:
//Bad:
variable[key] = value;
And as a proof provide the following link with description.
I'm not going to start to argue that this rule is sucks.
But I'm interested how to fix it. The link about has the solution:
The most direct fix here is going to be to avoid the use of user input in property name fields. This isn't reasonable in all circumstances, however, and there should be a way to safely use core language features.
Another option is to create a whitelist of allowed property names, and filter each user input through a helper function to check before allowing it to be used. This is a great option in situations where you know specifically what property names to allow.
In cases where you don't have a strictly defined data model ( which isn't ideal, but there are cases where it has to be so ) then using the same method as above, but with a blacklist of disallowed properties instead is a valid choice.
So the first option "do not use it" is not acceptable as my key is dynamic and the bracket notation is the only way to access the property.
The second solution is also is not suite for me as I don't have and cannot create a whitelist of allowed property names.
So the last option which I have is to use a blacklist of disallowed properties. But the question is: where to take this blacklist and how to apply it? It doesn't have an example and I'm interested how to properly implement it in Angular.
Thanks in advance!
UPDATE: As my question is not totally clear, I'll explain one more time.
The code I have is:
someObject[someDynamicKey] = 'someValue';
The problem that the eslint treat this line as bad. Because of setting the object property by Square Bracket Notation. EsLint says that it's dangerous. So I ask how should we do if the is no possibility to access the object property with dot notation? Examples? Thanks!
Explanation why Square Bracket Notation is Dangers according to ESLint.