Open ninevra opened 4 years ago
What happens if the following happens?
const mutable = {a: 1};
mutable.a = 2;
mutable;
Will getStaticValue()
return {a: 1}
or {a: 2}
?
@liangyuanruo, in your example, getStaticValue()
returns {value: {a: 1}}
for each of the three mutable
Identifier
nodes. getStaticValue()
doesn't examine anything besides the given node and the declarations of any variables it references, so it doesn't see the mutation on line 2, regardless of what form that mutation takes.
Hi @ninevra!
Since this repo is unmaintained, you might want to re-open this issue in the @eslint-community fork https://github.com/eslint-community/eslint-utils
For more info about why we created this organization, you can read https://eslint.org/blog/2023/03/announcing-eslint-community-org
When
getStaticValue()
is called with a scope and encounters an identifier referring to a variable declared withconst
, it computes that variable's static value based on itsconst
declaration initializer only. If the variable is initialized to a mutable value, it can later be modified, resulting in the return value fromgetStaticValue()
not matching the variable's true value at time of use.Example:
Calling
getStaticValue()
on the Identifier nodemutable
on line 3 returns{value: {a: 1}}
, butmutable
's actual value is{a: 1, b: 2}
.Minimal working example
This can also result in erroneously identifying identifiers as static. For example:
mutable
on line 3 is not static-valued, butgetStaticValue()
returns{value: {a: 1}}
.