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

Skip to content

Commit eead67a

Browse files
committed
JS: Add Function.getInferredName()
1 parent 66464b5 commit eead67a

4 files changed

Lines changed: 65 additions & 1 deletion

File tree

javascript/ql/src/semmle/javascript/Functions.qll

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,43 @@ class Function extends @function, Parameterized, TypeParameterized, StmtContaine
4747
/** Gets the identifier specifying the name of this function, if any. */
4848
VarDecl getId() { result = getChildExpr(-1) }
4949

50-
/** Gets the name of this function, if any. */
50+
/**
51+
* Gets the name of this function, if it is a function declaration
52+
* or named function expression.
53+
*/
5154
string getName() { result = getId().getName() }
5255

56+
/**
57+
* Gets the name of this function if it has one, or a name inferred from its context.
58+
*
59+
* For named functions such as `function f() { ... }`, this is just the declared
60+
* name. For functions assigned to variables or properties (including class
61+
* members), this is the name of the variable or property. If no meaningful name
62+
* can be inferred, there is no result.
63+
*/
64+
string getInferredName() {
65+
result = getName()
66+
or
67+
exists(VarDef vd | this = vd.getSource() |
68+
result = vd.getTarget().(VarRef).getName()
69+
)
70+
or
71+
exists(Property p |
72+
this = p.getInit() and
73+
result = p.getName()
74+
)
75+
or
76+
exists(AssignExpr assign, DotExpr prop |
77+
this = assign.getRhs().getUnderlyingValue() and
78+
prop = assign.getLhs() and
79+
result = prop.getPropertyName()
80+
)
81+
or
82+
exists(ClassOrInterface c |
83+
this = c.getMember(result).getInit()
84+
)
85+
}
86+
5387
/** Gets the variable holding this function. */
5488
Variable getVariable() { result = getId().getVariable() }
5589

javascript/ql/test/library-tests/ppNames/ppFnName.expected

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
getInferredName
2+
| tst.js:1:1:1:15 | function f() {} | f |
3+
| tst.js:2:2:2:16 | function g() {} | g |
4+
| tst.js:4:9:4:22 | function () {} | h |
5+
| tst.js:5:9:5:14 | x => x | k |
6+
| tst.js:6:9:6:23 | function n() {} | n |
7+
| tst.js:9:6:9:18 | function() {} | p |
8+
| tst.js:10:6:10:20 | function f() {} | f |
9+
| tst.js:11:8:11:12 | () {} | x |
10+
| tst.js:12:8:12:13 | (v) {} | x |
11+
| tst.js:13:4:13:8 | () {} | m |
12+
| tst.js:17:14:17:18 | () {} | constructor |
13+
| tst.js:18:4:18:8 | () {} | n |
14+
| tst.js:22:17:22:16 | () {} | constructor |
15+
| tst.js:22:21:22:20 | (...arg ... rgs); } | constructor |
16+
| tst.js:25:17:25:16 | () {} | constructor |
17+
| tst.js:26:19:26:18 | () {} | constructor |
18+
| tst.js:27:8:27:12 | () {} | y |
19+
| tst.js:28:8:28:13 | (v) {} | y |
20+
| tst.js:31:9:31:21 | function() {} | foo |
21+
#select
122
| tst.js:1:1:1:15 | function f() {} | function f |
223
| tst.js:2:2:2:16 | function g() {} | function g |
324
| tst.js:3:2:3:14 | function() {} | anonymous function |
@@ -17,3 +38,5 @@
1738
| tst.js:26:19:26:18 | () {} | default constructor of class G |
1839
| tst.js:27:8:27:12 | () {} | getter method for property y of class G |
1940
| tst.js:28:8:28:13 | (v) {} | setter method for property y of class G |
41+
| tst.js:31:9:31:21 | function() {} | method foo |
42+
| tst.js:32:12:32:24 | function() {} | anonymous function |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import javascript
22

3+
query string getInferredName(Function f) {
4+
result = f.getInferredName()
5+
}
6+
37
from Function f
48
select f, f.describe()

javascript/ql/test/library-tests/ppNames/tst.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ const E = class {}, // "class E", "default constructor of class E"
2727
get y() {} // "getter method for property y of class G"
2828
set y(v) {} // "setter method for property y of class G"
2929
};
30+
31+
o.foo = function() {}; // "method foo"
32+
o["Hey"] = function() {}; // "anonymous function"

0 commit comments

Comments
 (0)