Skip to content

Commit c8c6744

Browse files
committed
JS: Also use contextual types directly for underlying types
1 parent aff5952 commit c8c6744

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

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

Lines changed: 8 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::valueHasContextualType(this.getNameResolutionNode(), type)
235+
|
233236
UnderlyingTypes::nodeHasUnderlyingType(type, "global", globalName)
234237
)
235238
}
@@ -243,7 +246,10 @@ 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+
TypeResolution::valueHasContextualType(this.getNameResolutionNode(), type)
252+
|
247253
UnderlyingTypes::nodeHasUnderlyingType(type, moduleName, typeName)
248254
)
249255
}

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,22 @@ module TypeResolution {
114114
)
115115
}
116116

117-
private predicate contextualType(Node value, Node contextualType) {
117+
predicate valueHasContextualType(Node value, Node type) {
118118
exists(InvokeExpr call, Function target, int i |
119119
callTarget(call, target) and
120120
value = call.getArgument(i) and
121-
contextualType = target.getParameter(i).getTypeAnnotation()
121+
type = target.getParameter(i).getTypeAnnotation()
122+
)
123+
or
124+
exists(VariableDeclarator decl |
125+
value = decl.getInit() and
126+
type = decl.getTypeAnnotation()
127+
)
128+
or
129+
exists(Function functionValue, Function functionType |
130+
valueHasContextualType(functionValue, trackFunctionType(functionType)) and
131+
value = functionValue.getAReturnedExpr() and
132+
type = functionType.getReturnTypeAnnotation()
122133
)
123134
}
124135

@@ -154,10 +165,10 @@ module TypeResolution {
154165
or
155166
// Contextual typing for parameters
156167
exists(Function lambda, Function functionType, int i |
157-
contextualType(lambda, trackFunctionType(functionType))
168+
valueHasContextualType(lambda, trackFunctionType(functionType))
158169
or
159170
exists(InterfaceDefinition interface |
160-
contextualType(lambda, trackType(interface)) and
171+
valueHasContextualType(lambda, trackType(interface)) and
161172
functionType = interface.getACallSignature().getBody()
162173
)
163174
|

0 commit comments

Comments
 (0)