Skip to content

Commit 28cf270

Browse files
author
Max Schaefer
committed
Add a test showing API graphs for TypeScript types.
1 parent cc72c06 commit 28cf270

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#-----| root
2+
#-----| module mongoose -> use (module mongoose)
3+
#-----| module typescript-test -> def (module typescript-test)
4+
5+
#-----| def (module typescript-test)
6+
#-----| member exports -> def (member exports (module typescript-test))
7+
8+
#-----| use (module mongoose)
9+
#-----| member exports -> use (member exports (module mongoose))
10+
11+
#-----| def (member exports (module typescript-test))
12+
#-----| member Sized -> def (member Sized (member exports (module typescript-test)))
13+
14+
#-----| use (member exports (module mongoose))
15+
#-----| member Model -> use (member Model (member exports (module mongoose)))
16+
17+
#-----| def (member Sized (member exports (module typescript-test)))
18+
19+
#-----| use (member Model (member exports (module mongoose)))
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @name API graph
3+
* @description Shows the API graph of a code base.
4+
* @kind graph
5+
* @id js/api-graph
6+
*/
7+
8+
import javascript
9+
10+
/** Gets a string representation of the location information for `nd`. */
11+
string locationString(API::Node nd) {
12+
exists(string fp, int sl, int sc, int el, int ec | nd.hasLocationInfo(fp, sl, sc, el, ec) |
13+
result = fp + ":" + sl + ":" + sc + ":" + el + ":" + ec
14+
)
15+
}
16+
17+
/**
18+
* Gets the rank of node `nd` among all nodes ordered by depth, string representation,
19+
* and location.
20+
*/
21+
int nodeRank(API::Node nd) {
22+
nd =
23+
rank[result + 1](API::Node nd2 |
24+
|
25+
nd2 order by nd2.getDepth(), nd2.toString(), locationString(nd2)
26+
)
27+
}
28+
29+
/**
30+
* Gets the rank of `lbl` among all labels on outgoing edges of `pred`, ordered alphabetically.
31+
*/
32+
int labelRank(API::Node pred, string lbl, API::Node succ) {
33+
lbl + "->" + nodeRank(succ) =
34+
rank[result + 1](string l, API::Node s |
35+
s = pred.getASuccessor(l)
36+
or
37+
pred.refersTo(s) and l = ""
38+
|
39+
l + "->" + nodeRank(s)
40+
)
41+
}
42+
43+
query predicate nodes(API::Node f, string key, string value) {
44+
key = "semmle.order" and
45+
value = nodeRank(f).toString()
46+
}
47+
48+
query predicate edges(API::Node pred, API::Node succ, string key, string value) {
49+
exists(string lbl |
50+
succ = pred.getASuccessor(lbl)
51+
or
52+
pred.refersTo(succ) and
53+
lbl = ""
54+
|
55+
key = "semmle.label" and
56+
value = lbl
57+
or
58+
key = "semmle.order" and
59+
value = labelRank(pred, lbl, succ).toString()
60+
)
61+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface Sized {
2+
getSize(): number;
3+
}
4+
5+
import * as mongoose from "mongoose";
6+
var myModel : mongoose.Model;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "typescript-test",
3+
"dependencies": {
4+
"mongoose": "*"
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"include": ["."],
3+
"compilerOptions": {
4+
"esModuleInterop": true
5+
}
6+
}

0 commit comments

Comments
 (0)