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

Skip to content

Commit c44a3b4

Browse files
committed
JS: Add ClassDefinition.getInferredName
1 parent eead67a commit c44a3b4

5 files changed

Lines changed: 46 additions & 0 deletions

File tree

javascript/ql/src/semmle/javascript/Classes.qll

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ class ClassOrInterface extends @classorinterface, TypeParameterized {
1717
/** Gets the name of the defined class or interface, if any. */
1818
string getName() { result = getIdentifier().getName() }
1919

20+
/**
21+
* Gets the name of the defined class or interface, possibly inferred
22+
* from the context if this is an anonymous class expression.
23+
*
24+
* Has no result if no name could be determined.
25+
*/
26+
string getInferredName() {
27+
result = getName() // Overridden in ClassExpr
28+
}
29+
2030
/** Gets the nearest enclosing function or toplevel in which this class or interface occurs. */
2131
StmtContainer getContainer() { result = this.(ExprOrStmt).getContainer() }
2232

@@ -210,6 +220,25 @@ class ClassDeclStmt extends @classdeclstmt, ClassDefinition, Stmt {
210220
* A class expression.
211221
*/
212222
class ClassExpr extends @classexpr, ClassDefinition, Expr {
223+
override string getInferredName() {
224+
result = getName()
225+
or
226+
exists(VarDef vd | this = vd.getSource() |
227+
result = vd.getTarget().(VarRef).getName()
228+
)
229+
or
230+
exists(Property p |
231+
this = p.getInit() and
232+
result = p.getName()
233+
)
234+
or
235+
exists(AssignExpr assign, DotExpr prop |
236+
this = assign.getRhs().getUnderlyingValue() and
237+
prop = assign.getLhs() and
238+
result = prop.getPropertyName()
239+
)
240+
}
241+
213242
override predicate isImpure() { none() }
214243

215244
/** Gets the nearest enclosing function or toplevel in which this class expression occurs. */
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
getInferredName
2+
| tst.js:16:1:19:1 | class C ... ss C"\\n} | C |
3+
| tst.js:21:2:23:1 | class D ... lass"\\n} | D |
4+
| tst.js:22:12:22:18 | class{} | |
5+
| tst.js:25:11:25:18 | class {} | E |
6+
| tst.js:26:11:29:1 | class G ... ss G"\\n} | G |
7+
| tst.js:34:9:34:16 | class {} | Foo |
8+
#select
19
| tst.js:16:1:19:1 | class C ... ss C"\\n} | class C |
210
| tst.js:21:2:23:1 | class D ... lass"\\n} | class D |
311
| tst.js:22:12:22:18 | class{} | anonymous class |
412
| tst.js:25:11:25:18 | class {} | class E |
513
| tst.js:26:11:29:1 | class G ... ss G"\\n} | class G |
14+
| tst.js:34:9:34:16 | class {} | class Foo |
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(ClassDefinition c) {
4+
result = c.getInferredName()
5+
}
6+
37
from ClassDefinition c
48
select c, c.describe()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ getInferredName
1818
| tst.js:27:8:27:12 | () {} | y |
1919
| tst.js:28:8:28:13 | (v) {} | y |
2020
| tst.js:31:9:31:21 | function() {} | foo |
21+
| tst.js:34:15:34:14 | () {} | constructor |
2122
#select
2223
| tst.js:1:1:1:15 | function f() {} | function f |
2324
| tst.js:2:2:2:16 | function g() {} | function g |
@@ -40,3 +41,4 @@ getInferredName
4041
| tst.js:28:8:28:13 | (v) {} | setter method for property y of class G |
4142
| tst.js:31:9:31:21 | function() {} | method foo |
4243
| tst.js:32:12:32:24 | function() {} | anonymous function |
44+
| tst.js:34:15:34:14 | () {} | default constructor of class Foo |

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ const E = class {}, // "class E", "default constructor of class E"
3030

3131
o.foo = function() {}; // "method foo"
3232
o["Hey"] = function() {}; // "anonymous function"
33+
34+
o.Foo = class {}; // "class Foo"

0 commit comments

Comments
 (0)