Skip to content
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

Misleading result from getStaticValue() when called on mutable const-declared variables #10

Open
ninevra opened this issue Jul 19, 2020 · 3 comments
Labels
bug Something isn't working

Comments

@ninevra
Copy link
Contributor

ninevra commented Jul 19, 2020

When getStaticValue() is called with a scope and encounters an identifier referring to a variable declared with const, it computes that variable's static value based on its const declaration initializer only. If the variable is initialized to a mutable value, it can later be modified, resulting in the return value from getStaticValue() not matching the variable's true value at time of use.

Example:

const mutable = {a: 1};
mutable.b = 2;
mutable;

Calling getStaticValue() on the Identifier node mutable on line 3 returns {value: {a: 1}}, but mutable's actual value is {a: 1, b: 2}.

Minimal working example

This can also result in erroneously identifying identifiers as static. For example:

const mutable = {a: 1};
mutable.b = foo();
mutable;

mutable on line 3 is not static-valued, but getStaticValue() returns {value: {a: 1}}.

@liangyuanruo
Copy link

What happens if the following happens?

const mutable = {a: 1};
mutable.a = 2;
mutable;

Will getStaticValue() return {a: 1} or {a: 2}?

@ninevra
Copy link
Contributor Author

ninevra commented Jan 21, 2021

@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.

@mysticatea mysticatea added the bug Something isn't working label May 14, 2021
fisker pushed a commit to fisker/eslint-utils that referenced this issue Nov 28, 2022
* feat: support ESLint 8.x

* chore: use `@babel/register` instead of `esm` (mysticatea#16)

* chore: use esbuild-register instead of esm.

* chore: add esbuild to deps

* chore: use @babel/register instead of esbuild-register

Co-authored-by: Yosuke Ota <[email protected]>
@MichaelDeBoey
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants