Skip to content

Commit 87454f7

Browse files
committed
JS: Also check contextual type
1 parent d92247c commit 87454f7

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

javascript/ql/lib/semmle/javascript/dataflow/DataFlow.qll

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,10 @@ module DataFlow {
229229
predicate hasUnderlyingType(string globalName) {
230230
Stages::TypeTracking::ref() and
231231
exists(NameResolution::Node type |
232-
TypeResolution::valueHasType(this.getNameResolutionNode(), type) and
232+
TypeResolution::valueHasType(this.getNameResolutionNode(), type)
233+
or
234+
TypeResolution::contextualType(this.getNameResolutionNode(), type)
235+
|
233236
UnderlyingTypes::nodeHasUnderlyingType(type, globalName)
234237
)
235238
}
@@ -243,7 +246,12 @@ module DataFlow {
243246
Stages::TypeTracking::ref() and
244247
moduleName != "global" and
245248
exists(NameResolution::Node type |
246-
TypeResolution::valueHasType(this.getNameResolutionNode(), type) and
249+
TypeResolution::valueHasType(this.getNameResolutionNode(), type)
250+
or
251+
// Also check contextual type as this helps when tracking facts between SourceNode only.
252+
// For example, for `var x: T = getFoo()` we need `getFoo()` to have the type T.
253+
TypeResolution::contextualType(this.getNameResolutionNode(), type)
254+
|
247255
UnderlyingTypes::nodeHasUnderlyingType(type, moduleName, typeName)
248256
)
249257
}

javascript/ql/lib/semmle/javascript/internal/TypeResolution.qll

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,12 @@ module TypeResolution {
170170
)
171171
}
172172

173-
private predicate contextualType(Node value, Node type) {
173+
predicate contextualType(Node value, Node type) {
174+
exists(LocalVariable v |
175+
type = v.getADeclaration().getTypeAnnotation() and
176+
value = v.getAnAssignedExpr()
177+
)
178+
or
174179
exists(InvokeExpr call, Function target, int i |
175180
callTarget(call, target) and
176181
value = call.getArgument(i) and

0 commit comments

Comments
 (0)