-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
base: main
Are you sure you want to change the base?
Conversation
* @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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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; // <--
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 thea
identifier in the following script returned{foo: 'a'}
, which is incorrect.Ideally, it would return
{foo: 'a', bar: 'b'}
, but since the result differs depending on when the value is retrieved and thea
identifier is not static, I have fixed it so thatgetStaticValue
returns null, assuming the value cannot be retrieved.