Skip to content

Commit 8cb80d6

Browse files
committed
JS: Switch from hasLocationInfo to Location
1 parent 81b96a8 commit 8cb80d6

File tree

6 files changed

+37
-19
lines changed

6 files changed

+37
-19
lines changed

javascript/ql/lib/semmle/javascript/ApiGraphs.qll

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,16 +501,25 @@ module API {
501501
}
502502

503503
/**
504+
* Gets the location of this API node, if it corresponds to a program element with a source location.
505+
*/
506+
final Location getLocation() { result = this.getInducingNode().getLocation() }
507+
508+
/**
509+
* DEPRECATED: Use `getLocation().hasLocationInfo()` instead.
510+
*
504511
* Holds if this node is located in file `path` between line `startline`, column `startcol`,
505512
* and line `endline`, column `endcol`.
506513
*
507514
* For nodes that do not have a meaningful location, `path` is the empty string and all other
508515
* parameters are zero.
509516
*/
510-
predicate hasLocationInfo(string path, int startline, int startcol, int endline, int endcol) {
511-
this.getInducingNode().hasLocationInfo(path, startline, startcol, endline, endcol)
517+
deprecated predicate hasLocationInfo(
518+
string path, int startline, int startcol, int endline, int endcol
519+
) {
520+
this.getLocation().hasLocationInfo(path, startline, startcol, endline, endcol)
512521
or
513-
not exists(this.getInducingNode()) and
522+
not exists(this.getLocation()) and
514523
path = "" and
515524
startline = 0 and
516525
startcol = 0 and

javascript/ql/lib/semmle/javascript/frameworks/data/ModelsAsData.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ module ModelExport<ModelExportSig S> {
8888
private import codeql.mad.dynamic.GraphExport
8989
private import internal.ApiGraphModelsExport
9090

91-
private module GraphExportConfig implements GraphExportSig<API::Node> {
91+
private module GraphExportConfig implements GraphExportSig<Location, API::Node> {
9292
predicate edge = Specific::apiGraphHasEdge/3;
9393

9494
predicate shouldContain = S::shouldContain/1;

javascript/ql/lib/semmle/javascript/frameworks/data/internal/ApiGraphModelsExport.qll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ signature predicate shouldContainTypeSig(string type);
6565
* Notice that the access path `Member[blah].Member[z]` consists of an access path generated from the API
6666
* graph, with pieces of the access path from the original type model appended to it.
6767
*/
68-
module TypeGraphExport<GraphExportSig<API::Node> S, shouldContainTypeSig/1 shouldContainType> {
68+
module TypeGraphExport<
69+
GraphExportSig<Specific::Location, API::Node> S, shouldContainTypeSig/1 shouldContainType>
70+
{
6971
/** Like `shouldContainType` but includes types that lead to `type` via type models. */
7072
private predicate shouldContainTypeEx(string type) {
7173
shouldContainType(type)
@@ -76,7 +78,7 @@ module TypeGraphExport<GraphExportSig<API::Node> S, shouldContainTypeSig/1 shoul
7678
)
7779
}
7880

79-
private module Config implements GraphExportSig<API::Node> {
81+
private module Config implements GraphExportSig<Specific::Location, API::Node> {
8082
import S
8183

8284
predicate shouldContain(API::Node node) {
@@ -93,7 +95,7 @@ module TypeGraphExport<GraphExportSig<API::Node> S, shouldContainTypeSig/1 shoul
9395
}
9496
}
9597

96-
private module ExportedGraph = GraphExport<API::Node, Config>;
98+
private module ExportedGraph = GraphExport<Specific::Location, API::Node, Config>;
9799

98100
import ExportedGraph
99101

javascript/ql/lib/semmle/javascript/frameworks/data/internal/ApiGraphModelsSpecific.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ module API = JS::API;
2727

2828
import JS::DataFlow as DataFlow
2929

30+
class Location = JS::Location;
31+
3032
/**
3133
* Holds if `rawType` represents the JavaScript type `qualifiedName` from the given NPM `package`.
3234
*

shared/mad/codeql/mad/dynamic/GraphExport.qll

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Contains predicates for converting an arbitrary graph to a set of `typeModel` rows.
33
*/
44

5+
private import codeql.util.Location
6+
57
/**
68
* Concatenates two access paths, separating them by `.` unless one of them is empty.
79
*/
@@ -10,21 +12,20 @@ string join(string x, string y) {
1012
if x = "" or y = "" then result = x + y else result = x + "." + y
1113
}
1214

13-
signature class NodeSig {
14-
/**
15-
* Holds if this node is located in file `path` between line `startline`, column `startcol`,
16-
* and line `endline`, column `endcol`.
17-
*/
18-
predicate hasLocationInfo(string path, int startline, int startcol, int endline, int endcol);
15+
private module WithLocation<LocationSig Location> {
16+
signature class NodeSig {
17+
/** Gets the location of this node if it has one. */
18+
Location getLocation();
1919

20-
/** Gets a string representation of this node. */
21-
string toString();
20+
/** Gets a string representation of this node. */
21+
string toString();
22+
}
2223
}
2324

2425
/**
2526
* Specifies a graph to export in `GraphExport`.
2627
*/
27-
signature module GraphExportSig<NodeSig Node> {
28+
signature module GraphExportSig<LocationSig Location, WithLocation<Location>::NodeSig Node> {
2829
/**
2930
* Holds if an edge `pred -> succ` exist with the access path `path`.
3031
*/
@@ -81,7 +82,9 @@ signature module GraphExportSig<NodeSig Node> {
8182
/**
8283
* Module for exporting an arbitrary graph as models-as-data rows.
8384
*/
84-
module GraphExport<NodeSig Node, GraphExportSig<Node> S> {
85+
module GraphExport<
86+
LocationSig Location, WithLocation<Location>::NodeSig Node, GraphExportSig<Location, Node> S>
87+
{
8588
private import S
8689

8790
private Node getAnExposedNode() {
@@ -163,9 +166,10 @@ module GraphExport<NodeSig Node, GraphExportSig<Node> S> {
163166
node =
164167
rank[k](RelevantNode n, string path, int startline, int startcol, int endline, int endcol |
165168
isSyntheticallyNamedNode(n, prefixTypeName) and
166-
n.hasLocationInfo(path, startline, startcol, endline, endcol)
169+
n.getLocation().hasLocationInfo(path, startline, startcol, endline, endcol)
167170
|
168171
// Use location information for an arbitrary ordering
172+
// TODO: improve support for nodes without a location, currently they can cause FNs
169173
n order by path, startline, startcol, endline, endcol
170174
) and
171175
result = prefixTypeName + "~expr" + k

shared/mad/qlpack.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ name: codeql/mad
22
version: 0.2.14-dev
33
groups: shared
44
library: true
5-
dependencies: null
5+
dependencies:
6+
codeql/util: ${workspace}
67
warnOnImplicitThis: true

0 commit comments

Comments
 (0)