-
-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
import { findVariable } from "./find-variable.mjs" | ||
/** @typedef {import("./types.mjs").StaticValue} StaticValue */ | ||
/** @typedef {import("eslint").Scope.Scope} Scope */ | ||
/** @typedef {import("eslint").Scope.Variable} Variable */ | ||
/** @typedef {import("estree").Node} Node */ | ||
/** @typedef {import("@typescript-eslint/types").TSESTree.Node} TSESTreeNode */ | ||
/** @typedef {import("@typescript-eslint/types").TSESTree.AST_NODE_TYPES} TSESTreeNodeTypes */ | ||
|
@@ -23,8 +24,8 @@ | |
typeof window !== "undefined" | ||
? // @ts-ignore | ||
window | ||
: typeof global !== "undefined" | ||
Check failure on line 27 in src/get-static-value.mjs
|
||
? global | ||
Check failure on line 28 in src/get-static-value.mjs
|
||
: {} | ||
|
||
const builtinNames = Object.freeze( | ||
|
@@ -266,9 +267,40 @@ | |
return valueList | ||
} | ||
|
||
/** | ||
* Checks if a variable is a built-in global. | ||
* @param {Variable|null} variable The variable to check. | ||
* @returns {variable is Variable & {defs:[]}} | ||
*/ | ||
function isBuiltinGlobal(variable) { | ||
return ( | ||
variable != null && | ||
variable.defs.length === 0 && | ||
builtinNames.has(variable.name) && | ||
variable.name in globalObject | ||
) | ||
} | ||
|
||
/** | ||
* Checks if a variable can be considered as a constant. | ||
* @param {Variable} variable | ||
* @returns {variable is Variable & {defs: [import("eslint").Scope.Definition & { type: "Variable" }]}} True if the variable can be considered as a constant. | ||
*/ | ||
function canBeConsideredConst(variable) { | ||
if (variable.defs.length !== 1) { | ||
return false | ||
} | ||
const def = variable.defs[0] | ||
return Boolean( | ||
def.parent && | ||
def.type === "Variable" && | ||
(def.parent.kind === "const" || isEffectivelyConst(variable)), | ||
) | ||
} | ||
|
||
/** | ||
* Returns whether the given variable is never written to after initialization. | ||
* @param {import("eslint").Scope.Variable} variable | ||
* @param {Variable} variable | ||
* @returns {boolean} | ||
*/ | ||
function isEffectivelyConst(variable) { | ||
|
@@ -283,6 +315,68 @@ | |
return false | ||
} | ||
|
||
/** | ||
* Checks if a variable has mutation in its property. | ||
* @param {Variable} variable The variable to check. | ||
* @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) { | ||
for (const ref of variable.references) { | ||
let node = /** @type {TSESTreeNode} */ (ref.identifier) | ||
while (node && node.parent && node.parent.type === "MemberExpression") { | ||
node = node.parent | ||
} | ||
if (!node || !node.parent) { | ||
continue | ||
} | ||
if ( | ||
(node.parent.type === "AssignmentExpression" && | ||
node.parent.left === node) || | ||
(node.parent.type === "UpdateExpression" && | ||
node.parent.argument === node) | ||
) { | ||
// This is a mutation. | ||
return true | ||
} | ||
if ( | ||
node.parent.type === "CallExpression" && | ||
node.parent.callee === node && | ||
node.type === "MemberExpression" | ||
) { | ||
const methodName = getStaticPropertyNameValue(node, initialScope) | ||
if (isNameOfMutationArrayMethod(methodName)) { | ||
// This is a mutation. | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
|
||
/** | ||
* Checks if a method name is one of the mutation array methods. | ||
* @param {StaticValue|null} methodName The method name to check. | ||
* @returns {boolean} True if the method name is a mutation array method. | ||
*/ | ||
function isNameOfMutationArrayMethod(methodName) { | ||
if (methodName == null || methodName.value == null) { | ||
return false | ||
} | ||
const name = methodName.value | ||
return ( | ||
name === "copyWithin" || | ||
name === "fill" || | ||
name === "pop" || | ||
name === "push" || | ||
name === "reverse" || | ||
name === "shift" || | ||
name === "sort" || | ||
name === "splice" || | ||
name === "unshift" | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* @template {TSESTreeNodeTypes} T | ||
* @callback VisitorCallback | ||
|
@@ -512,28 +606,35 @@ | |
if (initialScope != null) { | ||
const variable = findVariable(initialScope, node) | ||
|
||
// Built-in globals. | ||
if ( | ||
variable != null && | ||
variable.defs.length === 0 && | ||
builtinNames.has(variable.name) && | ||
variable.name in globalObject | ||
) { | ||
return { value: globalObject[variable.name] } | ||
} | ||
if (variable != null) { | ||
// Built-in globals. | ||
if (isBuiltinGlobal(variable)) { | ||
return { value: globalObject[variable.name] } | ||
} | ||
|
||
// Constants. | ||
if (variable != null && variable.defs.length === 1) { | ||
const def = variable.defs[0] | ||
if ( | ||
def.parent && | ||
def.type === "Variable" && | ||
(def.parent.kind === "const" || | ||
isEffectivelyConst(variable)) && | ||
// TODO(mysticatea): don't support destructuring here. | ||
def.node.id.type === "Identifier" | ||
) { | ||
return getStaticValueR(def.node.init, initialScope) | ||
// Constants. | ||
if (canBeConsideredConst(variable)) { | ||
const def = variable.defs[0] | ||
if ( | ||
// TODO(mysticatea): don't support destructuring here. | ||
def.node.id.type === "Identifier" | ||
) { | ||
const init = getStaticValueR( | ||
def.node.init, | ||
initialScope, | ||
) | ||
if ( | ||
init && | ||
typeof init.value === "object" && | ||
init.value !== null | ||
) { | ||
if (hasMutationInProperty(variable, initialScope)) { | ||
// This variable has mutation in its property. | ||
return null | ||
} | ||
} | ||
return init | ||
} | ||
} | ||
} | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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?
Uh oh!
There was an error while loading. Please reload this page.
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!
I agree with it, but I think it's at least a step forward from where we are now 😅
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.Uh oh!
There was an error while loading. Please reload this page.
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.
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).
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.