Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit efa7e11

Browse files
committed
JS: Add Node.hasUnderlyingType
1 parent c3e1fb4 commit efa7e11

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,62 @@ module DataFlow {
193193
not fun.getExit().isJoin() // can only reach exit by the return statement
194194
)
195195
}
196+
197+
/**
198+
* Gets the static type of this node as determined by the TypeScript type system.
199+
*/
200+
private Type getType() {
201+
exists(AST::ValueNode node |
202+
this = TValueNode(node) and
203+
ast_node_type(node, result)
204+
)
205+
or
206+
exists(BindingPattern pattern |
207+
this = lvalueNode(pattern) and
208+
ast_node_type(pattern, result)
209+
)
210+
or
211+
exists(MethodDefinition def |
212+
this = TThisNode(def.getInit()) and
213+
ast_node_type(def.getDeclaringClass(), result)
214+
)
215+
}
216+
217+
/**
218+
* Gets the type annotation describing the type of this node,
219+
* provided that a static type could not be found.
220+
*
221+
* Doesn't take field types and function return types into account.
222+
*/
223+
private JSDocTypeExpr getFallbackTypeAnnotation() {
224+
exists(BindingPattern pattern |
225+
this = lvalueNode(pattern) and
226+
not ast_node_type(pattern, _) and
227+
result = pattern.getTypeAnnotation()
228+
)
229+
or
230+
result = getAPredecessor().getFallbackTypeAnnotation()
231+
}
232+
233+
/**
234+
* Holds if this node is annotated with the given named type,
235+
* or is declared as a subtype thereof, or is a union or intersection containing such a type.
236+
*/
237+
predicate hasUnderlyingType(string globalName) {
238+
getType().hasUnderlyingType(globalName)
239+
or
240+
getFallbackTypeAnnotation().getAnUnderlyingType().hasQualifiedName(globalName)
241+
}
242+
243+
/**
244+
* Holds if this node is annotated with the given named type,
245+
* or is declared as a subtype thereof, or is a union or intersection containing such a type.
246+
*/
247+
predicate hasUnderlyingType(string moduleName, string typeName) {
248+
getType().hasUnderlyingType(moduleName, typeName)
249+
or
250+
getFallbackTypeAnnotation().getAnUnderlyingType().hasQualifiedName(moduleName, typeName)
251+
}
196252
}
197253

198254
/**

0 commit comments

Comments
 (0)