Change `prefer-const` to permit the author to use `let` in the case of declaring an object that is then mutated.
See original GitHub issueVersion v2.9.0
Problem you want to solve
As a developer, I might want to choose to use let
when declaring an object I later mutate, even though JavaScript will allow me to use const
. This would be to make the status of the variable clearer to future readers and myself. At the moment, it is not possible to do this while using prefer-const
, which is an important rule in other situations.
My take on the solution
The parser should look to see if a variable is mutated in the scope and, if so, permit the use of let
for that variable. prefer-const
should have an option that enables this behaviour.
What you did
With prefer-const
enabled, write
function example(value) {
let object = {};
object.property = value;
return object;
}
What you would like to happen No errors to be triggered
What happened
The second line is highlighted by prefer-const
.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:7
- Comments:9 (4 by maintainers)
Top GitHub Comments
Thank you for this issue.
Hmm, personally I want not add this option.
const
of JavaScript doesn’t mean that object’s properties are not modified. If people rely onconst
keyword to check whether properties are modified or not, it’s wrong. So this option would mislead people, I think.I understand that JavaScript will not stop you from modifying an object’s properties if you use
const
. This is more to support a developer/team’s decision that they might want to indicate (for code readability) that they intend for an object’s properties to be changed by usinglet
instead ofconst
.I definitely don’t think this should be the default behaviour, but it seems like a reasonable case to be controlled by an option.