Skip to content

Commit 70e8f37

Browse files
committed
JS: Name and type resolution and underlying types
1 parent 4510d48 commit 70e8f37

17 files changed

+746
-64
lines changed

Diff for: javascript/ql/lib/semmle/javascript/TypeScript.qll

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import javascript
2+
private import semmle.javascript.internal.UnderlyingTypes
23

34
/**
45
* A statement that defines a namespace, that is, a namespace declaration or enum declaration.
@@ -577,7 +578,7 @@ class TypeExpr extends ExprOrType, @typeexpr, TypeAnnotation {
577578
override TopLevel getTopLevel() { result = ExprOrType.super.getTopLevel() }
578579

579580
override DataFlow::ClassNode getClass() {
580-
result.getAstNode() = this.getType().(ClassType).getClass()
581+
UnderlyingTypes::nodeHasUnderlyingClassType(this, result.getAstNode())
581582
}
582583
}
583584

Diff for: javascript/ql/lib/semmle/javascript/Variables.qll

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class Scope extends @scope {
2727
result = this.getAVariable() and
2828
result.getName() = name
2929
}
30+
31+
/** Gets the local type name with the given name declared in this scope. */
32+
LocalTypeName getLocalTypeName(string name) {
33+
result.getScope() = this and
34+
result.getName() = name
35+
}
3036
}
3137

3238
/**

Diff for: javascript/ql/lib/semmle/javascript/dataflow/DataFlow.qll

+21-26
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ private import internal.PreCallGraphStep
2727
private import semmle.javascript.internal.CachedStages
2828
private import semmle.javascript.dataflow.internal.DataFlowPrivate as Private
2929
private import semmle.javascript.dataflow.internal.VariableOrThis
30+
private import semmle.javascript.internal.NameResolution
31+
private import semmle.javascript.internal.UnderlyingTypes
32+
private import semmle.javascript.internal.TypeResolution
3033

3134
module DataFlow {
3235
/**
@@ -189,26 +192,6 @@ module DataFlow {
189192
FlowSteps::identityFunctionStep(result, this)
190193
}
191194

192-
/**
193-
* Gets the static type of this node as determined by the TypeScript type system.
194-
*/
195-
private Type getType() {
196-
exists(AST::ValueNode node |
197-
this = TValueNode(node) and
198-
ast_node_type(node, result)
199-
)
200-
or
201-
exists(BindingPattern pattern |
202-
this = lvalueNode(pattern) and
203-
ast_node_type(pattern, result)
204-
)
205-
or
206-
exists(MethodDefinition def |
207-
this = TThisNode(def.getInit()) and
208-
ast_node_type(def.getDeclaringClass(), result)
209-
)
210-
}
211-
212195
/**
213196
* Gets the type annotation describing the type of this node,
214197
* provided that a static type could not be found.
@@ -229,16 +212,26 @@ module DataFlow {
229212
)
230213
}
231214

215+
private NameResolution::Node getNameResolutionNode() {
216+
this = valueNode(result)
217+
or
218+
exists(PropertyPattern pattern |
219+
result = pattern.getValuePattern() and
220+
this = TPropNode(pattern)
221+
)
222+
}
223+
232224
/**
233225
* Holds if this node is annotated with the given named type,
234226
* or is declared as a subtype thereof, or is a union or intersection containing such a type.
235227
*/
236228
cached
237229
predicate hasUnderlyingType(string globalName) {
238230
Stages::TypeTracking::ref() and
239-
this.getType().hasUnderlyingType(globalName)
240-
or
241-
this.getFallbackTypeAnnotation().getAnUnderlyingType().hasQualifiedName(globalName)
231+
exists(NameResolution::Node type |
232+
TypeResolution::valueHasType(this.getNameResolutionNode(), type) and
233+
UnderlyingTypes::nodeHasUnderlyingType(type, "global", globalName)
234+
)
242235
}
243236

244237
/**
@@ -248,9 +241,11 @@ module DataFlow {
248241
cached
249242
predicate hasUnderlyingType(string moduleName, string typeName) {
250243
Stages::TypeTracking::ref() and
251-
this.getType().hasUnderlyingType(moduleName, typeName)
252-
or
253-
this.getFallbackTypeAnnotation().getAnUnderlyingType().hasQualifiedName(moduleName, typeName)
244+
moduleName != "global" and
245+
exists(NameResolution::Node type |
246+
TypeResolution::valueHasType(this.getNameResolutionNode(), type) and
247+
UnderlyingTypes::nodeHasUnderlyingType(type, moduleName, typeName)
248+
)
254249
}
255250

256251
/**

0 commit comments

Comments
 (0)