JS: Fix regression from global declare vars

This commit is contained in:
Asger F
2025-05-13 16:16:33 +02:00
parent 27979c6a2f
commit 9bcc62002d
7 changed files with 48 additions and 8 deletions

View File

@@ -28,7 +28,7 @@ module NameResolution {
Location getLocation() {
result = this.(AstNode).getLocation()
or
result = this.(LocalVariable).getLocation()
result = this.(LocalVariableLike).getLocation()
or
result = this.(JSDocTypeExpr).getLocation()
}
@@ -47,6 +47,22 @@ module NameResolution {
}
}
/**
* A local variable, or a top-level variable that acts as a global variable due to an ambient declaration.
*/
class LocalVariableLike extends Variable {
LocalVariableLike() { this.isLocal() or this.isTopLevelWithAmbientDeclaration() }
Location getLocation() {
result =
min(Location loc |
loc = this.getADeclaration().getLocation()
|
loc order by loc.getStartLine(), loc.getStartColumn()
)
}
}
/**
* Holds if values/namespaces/types in `node1` can flow to values/namespaces/types in `node2`.
*/
@@ -224,7 +240,7 @@ module NameResolution {
/**
* A local variable with exactly one definition, not counting implicit initialization.
*/
private class EffectivelyConstantVariable extends LocalVariable {
private class EffectivelyConstantVariable extends LocalVariableLike {
EffectivelyConstantVariable() {
count(SsaExplicitDefinition ssa | ssa.getSourceVariable() = this) <= 1 // count may be zero if ambient
}
@@ -294,7 +310,7 @@ module NameResolution {
* Holds if `value` is stored in `target.prop`. Only needs to recognise assignments
* that are also recognised by JSDoc tooling such as the Closure compiler.
*/
private predicate storeToVariable(Expr value, string prop, LocalVariable target) {
private predicate storeToVariable(Expr value, string prop, LocalVariableLike target) {
exists(AssignExpr assign |
// exports.name = value
assign.getLhs().(PropAccess).accesses(target.getAnAccess(), prop) and

View File

@@ -190,7 +190,7 @@ module TypeResolution {
}
predicate contextualType(Node value, Node type) {
exists(LocalVariable v |
exists(LocalVariableLike v |
type = v.getADeclaration().getTypeAnnotation() and
value = v.getAnAssignedExpr()
)
@@ -239,7 +239,7 @@ module TypeResolution {
// ValueFlow::step is restricted to variables with at most one assignment. Allow the type annotation
// of a variable to propagate to its uses, even if the variable has multiple assignments.
type = decl.getTypeAnnotation() and
value = decl.getVariable().(LocalVariable).getAnAccess()
value = decl.getVariable().(LocalVariableLike).getAnAccess()
)
or
exists(MemberDeclaration member |