Skip to content

fix(get-static-value): incorrect return when variable has object mutation #257

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ota-meshi
Copy link
Member

This PR fixes getStaticValue to not retrieve the value if the variable has an object mutation.

Previously, for example, the value retrieved by getStaticValue for the a identifier in the following script returned {foo: 'a'}, which is incorrect.

const a = {foo: 'a'};
a.bar = 'b';
a; // <-- ?

Ideally, it would return {foo: 'a', bar: 'b'}, but since the result differs depending on when the value is retrieved and the a identifier is not static, I have fixed it so that getStaticValue returns null, assuming the value cannot be retrieved.

@ota-meshi ota-meshi closed this Jul 29, 2025
@ota-meshi ota-meshi reopened this Jul 29, 2025
* @param {Scope|null} initialScope The scope to start finding variable. Optional. If the node is a computed property node and this scope was given, this checks the computed property name by the `getStringIfConstant` function with the scope, and returns the value of it.
* @returns {boolean} True if the variable has mutation in its property.
*/
function hasMutationInProperty(variable, initialScope) {
Copy link

@fisker fisker Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this, there are too many ways to mutate an object, make an exception for property assignment not really helping IMHO. Maybe we should add extra property the result to indicate if the value is absolutely safe to use?

Copy link
Member Author

@ota-meshi ota-meshi Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your comment!

there are too many ways to mutate an object

I agree with it, but I think it's at least a step forward from where we are now 😅

Maybe we should add extra property the result to indicate if the value is absolutely safe to use?

I think we could add a property, but under what specific conditions would that be absolutely safe?
Either way, I think that can be handled in a separate PR. This PR would allow us to return null for non-static values.

Copy link

@fisker fisker Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but under what specific conditions would that be absolutely safe?

Only defined, no other reference, except property access(read primitive value).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the a variable in the last statement of the script below won't get that mark, is that correct?
So, I think the user should consider that if the value is an object, it is potentially dangerous (not absolutely safe).

const a = {foo: 'a'};

// ... 

a; // <--

This PR finds mutations of a in the following script, so I think it would be better to make adding new properties a separate task.

const a = {foo: 'a'};
a.bar = 'b';
a; // <--

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants