Skip to content

Commit e07a036

Browse files
committed
JS: Mark type-annotated nodes as SourceNode
1 parent 6e82b6e commit e07a036

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,13 @@ module SourceNode {
333333
astNode instanceof TaggedTemplateExpr or
334334
astNode instanceof Templating::PipeRefExpr or
335335
astNode instanceof Templating::TemplateVarRefExpr or
336-
astNode instanceof StringLiteral
336+
astNode instanceof StringLiteral or
337+
astNode instanceof TypeAssertion
338+
)
339+
or
340+
exists(VariableDeclarator decl |
341+
exists(decl.getTypeAnnotation()) and
342+
this = DataFlow::valueNode(decl.getBindingPattern())
337343
)
338344
or
339345
DataFlow::parameterNode(this, _)

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,12 @@ module TypeResolution {
189189
)
190190
}
191191

192-
private predicate contextualType(Node value, Node type) {
192+
predicate contextualType(Node value, Node type) {
193+
exists(LocalVariable v |
194+
type = v.getADeclaration().getTypeAnnotation() and
195+
value = v.getAnAssignedExpr()
196+
)
197+
or
193198
exists(InvokeExpr call, Function target, int i |
194199
callTarget(call, target) and
195200
value = call.getArgument(i) and
@@ -228,6 +233,8 @@ module TypeResolution {
228233
predicate valueHasType(Node value, Node type) {
229234
value.(BindingPattern).getTypeAnnotation() = type
230235
or
236+
value.(TypeAssertion).getTypeAnnotation() = type
237+
or
231238
exists(VarDecl decl |
232239
// ValueFlow::step is restricted to variables with at most one assignment. Allow the type annotation
233240
// of a variable to propagate to its uses, even if the variable has multiple assignments.

javascript/ql/test/library-tests/UnderlyingTypes/test.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@
4848
| subtype.ts:7:13:7:15 | req | 'express'.Request |
4949
| subtype.ts:13:13:13:15 | req | 'express'.Request |
5050
| subtype.ts:19:13:19:15 | req | 'express'.Request |
51+
| varAssignment.ts:4:9:4:11 | req | 'express'.Request |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as express from 'express';
22

33
function t1(e) {
4-
var req: express.Request = e; // $ MISSING: hasUnderlyingType='express'.Request
4+
var req: express.Request = e; // $ hasUnderlyingType='express'.Request
55
}

0 commit comments

Comments
 (0)