Skip to content

Commit ac4b390

Browse files
committed
JS: Make jump-to-def behave nicer
1 parent fbebf72 commit ac4b390

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

javascript/ql/lib/definitions.qll

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,39 @@ private predicate typedInvokeLookup(AstNode ref, AstNode decl, string kind) {
140140
exists(InvokeExpr invoke, Expr callee |
141141
callee = invoke.getCallee().getUnderlyingReference() and
142142
(ref = callee.(Identifier) or ref = callee.(DotExpr).getPropertyNameExpr()) and
143-
decl = invoke.getResolvedCallee() and
143+
decl = redirectCallee(invoke.getResolvedCallee()) and
144144
kind = "M"
145145
)
146146
}
147147

148+
/** Redirects calls to `f` to a more appropriate AST node for jump-to-def. */
149+
private AstNode getCalleeRedirect(Function f) {
150+
exists(ConstructorDeclaration ctor, ClassDefinition cls |
151+
ctor.isSynthetic() and
152+
f = ctor.getBody() and
153+
cls = ctor.getDeclaringClass()
154+
|
155+
result = cls.getIdentifier()
156+
or
157+
not exists(cls.getIdentifier()) and
158+
result = cls
159+
)
160+
or
161+
exists(MethodDeclaration member |
162+
not member instanceof ConstructorDeclaration and
163+
f = member.getBody() and
164+
result = member.getNameExpr()
165+
)
166+
}
167+
168+
bindingset[f]
169+
private AstNode redirectCallee(Function f) {
170+
result = getCalleeRedirect(f)
171+
or
172+
not exists(getCalleeRedirect(f)) and
173+
result = f
174+
}
175+
148176
/**
149177
* Holds if `ref` is a JSDoc type annotation referring to a class defined at `decl`.
150178
*/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,7 @@ sources
15141514
| tst2.ts:7:1:9:1 | return of function setX |
15151515
| tst2.ts:8:3:8:5 | A.x |
15161516
| tst2.ts:11:11:11:13 | A.x |
1517+
| tst2.ts:11:11:11:23 | A.x as number |
15171518
| tst2.ts:13:1:13:40 | class S ... ing> {} |
15181519
| tst2.ts:13:26:13:29 | List |
15191520
| tst2.ts:13:39:13:38 | (...arg ... rgs); } |

javascript/ql/test/query-tests/definitions/definitions.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
| client.ts:13:35:13:35 | C | tslib.ts:1:8:3:1 | class C {\\n m() {}\\n} | T |
1111
| client.ts:13:47:13:47 | C | tslib.ts:6:10:8:3 | class C ... {}\\n } | T |
1212
| client.ts:14:3:14:3 | x | client.ts:13:22:13:22 | x | V |
13-
| client.ts:14:5:14:5 | m | client.ts:4:3:4:8 | m() {} | M |
13+
| client.ts:14:5:14:5 | m | client.ts:4:3:4:3 | m | M |
1414
| client.ts:15:3:15:3 | y | client.ts:13:28:13:28 | y | V |
15-
| client.ts:15:5:15:5 | m | tslib.ts:2:3:2:8 | m() {} | M |
15+
| client.ts:15:5:15:5 | m | tslib.ts:2:3:2:3 | m | M |
1616
| client.ts:16:3:16:3 | z | client.ts:13:38:13:38 | z | V |
17-
| client.ts:16:5:16:5 | m | tslib.ts:7:5:7:10 | m() {} | M |
17+
| client.ts:16:5:16:5 | m | tslib.ts:7:5:7:5 | m | M |
1818
| d.js:1:17:1:21 | './c' | c.js:1:1:1:20 | <toplevel> | I |
1919
| d.js:10:1:10:1 | A | d.js:7:1:9:1 | functio ... = 42;\\n} | V |
2020
| d.js:16:19:16:23 | Super | d.js:12:1:14:1 | class S ... () {}\\n} | V |

0 commit comments

Comments
 (0)