Skip to content

Commit 1d0e5d1

Browse files
committed
JS: Add DataFlow::Node.getLocation
1 parent 35a8e7c commit 1d0e5d1

20 files changed

+332
-53
lines changed

config/identical-files.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@
255255
"cpp/ql/lib/semmle/code/cpp/XML.qll",
256256
"csharp/ql/lib/semmle/code/csharp/XML.qll",
257257
"java/ql/lib/semmle/code/xml/XML.qll",
258-
"javascript/ql/lib/semmle/javascript/XML.qll",
259258
"python/ql/lib/semmle/python/xml/XML.qll"
260259
],
261260
"DuplicationProblems.inc.qhelp": [
@@ -372,4 +371,4 @@
372371
"python/ql/test/experimental/dataflow/model-summaries/InlineTaintTest.ext.yml",
373372
"python/ql/test/experimental/dataflow/model-summaries/NormalDataflowTest.ext.yml"
374373
]
375-
}
374+
}

javascript/ql/lib/semmle/javascript/AST.qll

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import javascript
66
private import internal.StmtContainers
77
private import semmle.javascript.internal.CachedStages
8+
private import semmle.javascript.internal.Locations
89

910
/**
1011
* A program element corresponding to JavaScript code, such as an expression
@@ -23,15 +24,20 @@ private import semmle.javascript.internal.CachedStages
2324
* ```
2425
*/
2526
class AstNode extends @ast_node, NodeInStmtContainer {
26-
override Location getLocation() { hasLocation(this, result) }
27+
override DbLocation getLocation() {
28+
exists(@location loc |
29+
hasLocation(this, loc) and
30+
result = TDbLocation(loc)
31+
)
32+
}
2733

2834
override File getFile() {
2935
result = this.getLocation().getFile() // Specialized for performance reasons
3036
}
3137

3238
/** Gets the first token belonging to this element. */
3339
Token getFirstToken() {
34-
exists(Location l1, Location l2 |
40+
exists(DbLocation l1, DbLocation l2 |
3541
l1 = this.getLocation() and
3642
l2 = result.getLocation() and
3743
l1.getFile() = l2.getFile() and
@@ -42,7 +48,7 @@ class AstNode extends @ast_node, NodeInStmtContainer {
4248

4349
/** Gets the last token belonging to this element. */
4450
Token getLastToken() {
45-
exists(Location l1, Location l2 |
51+
exists(DbLocation l1, DbLocation l2 |
4652
l1 = this.getLocation() and
4753
l2 = result.getLocation() and
4854
l1.getFile() = l2.getFile() and

javascript/ql/lib/semmle/javascript/CFG.qll

+7-1
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@
275275

276276
import javascript
277277
private import internal.StmtContainers
278+
private import semmle.javascript.internal.Locations
278279

279280
/**
280281
* A node in the control flow graph, which is an expression, a statement,
@@ -357,7 +358,12 @@ class ControlFlowNode extends @cfg_node, Locatable, NodeInStmtContainer {
357358
* examples include guard nodes and entry/exit nodes.
358359
*/
359360
class SyntheticControlFlowNode extends @synthetic_cfg_node, ControlFlowNode {
360-
override Location getLocation() { hasLocation(this, result) }
361+
override DbLocation getLocation() {
362+
exists(@location loc |
363+
hasLocation(this, loc) and
364+
result = TDbLocation(loc)
365+
)
366+
}
361367
}
362368

363369
/** A synthetic CFG node marking the entry point of a function or toplevel script. */

javascript/ql/lib/semmle/javascript/Comments.qll

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** Provides classes for working with JavaScript comments. */
22

33
import javascript
4+
private import semmle.javascript.internal.Locations
45

56
/**
67
* A JavaScript source-code comment.
@@ -15,7 +16,12 @@ import javascript
1516
* </pre>
1617
*/
1718
class Comment extends @comment, Locatable {
18-
override Location getLocation() { hasLocation(this, result) }
19+
override DbLocation getLocation() {
20+
exists(@location loc |
21+
hasLocation(this, loc) and
22+
result = TDbLocation(loc)
23+
)
24+
}
1925

2026
/** Gets the toplevel element this comment belongs to. */
2127
TopLevel getTopLevel() { comments(this, _, result, _, _) }

javascript/ql/lib/semmle/javascript/Errors.qll

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
/** Provides classes for working with syntax errors. */
22

33
import javascript
4+
private import semmle.javascript.internal.Locations
45

56
/** An error encountered during extraction. */
67
abstract class Error extends Locatable {
7-
override Location getLocation() { hasLocation(this, result) }
8+
override DbLocation getLocation() {
9+
exists(@location loc |
10+
hasLocation(this, loc) and
11+
result = TDbLocation(loc)
12+
)
13+
}
814

915
/** Gets the message associated with this error. */
1016
abstract string getMessage();

javascript/ql/lib/semmle/javascript/Files.qll

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javascript
44
private import NodeModuleResolutionImpl
55
private import codeql.util.FileSystem
6+
private import internal.Locations
67

78
private module FsInput implements InputSig {
89
abstract class ContainerBase extends @container {
@@ -83,7 +84,12 @@ class File extends Container, Impl::File {
8384
*
8485
* Note that files have special locations starting and ending at line zero, column zero.
8586
*/
86-
Location getLocation() { hasLocation(this, result) }
87+
DbLocation getLocation() {
88+
exists(@location loc |
89+
hasLocation(this, loc) and
90+
result = TDbLocation(loc)
91+
)
92+
}
8793

8894
/** Gets the number of lines in this file. */
8995
int getNumberOfLines() { result = sum(int loc | numlines(this, loc, _, _) | loc) }

javascript/ql/lib/semmle/javascript/HTML.qll

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** Provides classes for working with HTML documents. */
22

33
import javascript
4+
private import internal.Locations
45

56
module HTML {
67
/**
@@ -43,7 +44,12 @@ module HTML {
4344
class Element extends Locatable, @xmlelement {
4445
Element() { exists(FileContainingHtml f | xmlElements(this, _, _, _, f)) }
4546

46-
override Location getLocation() { xmllocations(this, result) }
47+
override DbLocation getLocation() {
48+
exists(@location loc |
49+
xmllocations(this, loc) and
50+
result = TDbLocation(loc)
51+
)
52+
}
4753

4854
/**
4955
* Gets the name of this HTML element.
@@ -122,7 +128,12 @@ module HTML {
122128
class Attribute extends Locatable, @xmlattribute {
123129
Attribute() { exists(FileContainingHtml f | xmlAttrs(this, _, _, _, _, f)) }
124130

125-
override Location getLocation() { xmllocations(this, result) }
131+
override DbLocation getLocation() {
132+
exists(@location loc |
133+
xmllocations(this, loc) and
134+
result = TDbLocation(loc)
135+
)
136+
}
126137

127138
/**
128139
* Gets the inline script of this attribute, if any.
@@ -327,7 +338,12 @@ module HTML {
327338
*/
328339
predicate isCData() { xmlChars(this, _, _, _, 1, _) }
329340

330-
override Location getLocation() { xmllocations(this, result) }
341+
override DbLocation getLocation() {
342+
exists(@location loc |
343+
xmllocations(this, loc) and
344+
result = TDbLocation(loc)
345+
)
346+
}
331347
}
332348

333349
/**
@@ -350,6 +366,11 @@ module HTML {
350366

351367
override string toString() { xmlComments(this, result, _, _) }
352368

353-
override Location getLocation() { xmllocations(this, result) }
369+
override DbLocation getLocation() {
370+
exists(@location loc |
371+
xmllocations(this, loc) and
372+
result = TDbLocation(loc)
373+
)
374+
}
354375
}
355376
}

javascript/ql/lib/semmle/javascript/JSDoc.qll

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import javascript
44
private import semmle.javascript.internal.CachedStages
5+
private import semmle.javascript.internal.Locations
56

67
/**
78
* A JSDoc comment.
@@ -18,7 +19,12 @@ private import semmle.javascript.internal.CachedStages
1819
* </pre>
1920
*/
2021
class JSDoc extends @jsdoc, Locatable {
21-
override Location getLocation() { hasLocation(this, result) }
22+
override DbLocation getLocation() {
23+
exists(@location loc |
24+
hasLocation(this, loc) and
25+
result = TDbLocation(loc)
26+
)
27+
}
2228

2329
/** Gets the description text of this JSDoc comment. */
2430
string getDescription() { jsdoc(this, result, _) }
@@ -75,7 +81,12 @@ abstract class Documentable extends AstNode {
7581
* ```
7682
*/
7783
class JSDocTypeExprParent extends @jsdoc_type_expr_parent, Locatable {
78-
override Location getLocation() { hasLocation(this, result) }
84+
override DbLocation getLocation() {
85+
exists(@location loc |
86+
hasLocation(this, loc) and
87+
result = TDbLocation(loc)
88+
)
89+
}
7990

8091
/** Gets the JSDoc comment to which this element belongs. */
8192
JSDoc getJSDocComment() { none() }

javascript/ql/lib/semmle/javascript/JSON.qll

+15-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import javascript
6+
private import semmle.javascript.internal.Locations
67

78
/**
89
* A JSON-encoded value, which may be a primitive value, an array or an object.
@@ -20,7 +21,12 @@ import javascript
2021
* ```
2122
*/
2223
class JsonValue extends @json_value, Locatable {
23-
override Location getLocation() { json_locations(this, result) }
24+
override DbLocation getLocation() {
25+
exists(@location loc |
26+
json_locations(this, loc) and
27+
result = TDbLocation(loc)
28+
)
29+
}
2430

2531
/** Gets the parent value to which this value belongs, if any. */
2632
JsonValue getParent() { json(this, _, result, _, _) }
@@ -35,9 +41,9 @@ class JsonValue extends @json_value, Locatable {
3541

3642
/** Gets the JSON file containing this value. */
3743
File getJsonFile() {
38-
exists(Location loc |
44+
exists(@location loc |
3945
json_locations(this, loc) and
40-
result = loc.getFile()
46+
result = TDbLocation(loc).(DbLocation).getFile()
4147
)
4248
}
4349

@@ -172,7 +178,12 @@ class JsonObject extends @json_object, JsonValue {
172178
* An error reported by the JSON parser.
173179
*/
174180
class JsonParseError extends @json_parse_error, Error {
175-
override Location getLocation() { json_locations(this, result) }
181+
override DbLocation getLocation() {
182+
exists(@location loc |
183+
json_locations(this, loc) and
184+
result = TDbLocation(loc)
185+
)
186+
}
176187

177188
override string getMessage() { json_errors(this, result) }
178189
}

javascript/ql/lib/semmle/javascript/Lines.qll

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import javascript
9+
private import semmle.javascript.internal.Locations
910

1011
/**
1112
* A line of text (code, comment, or whitespace) in a source file.
@@ -14,7 +15,12 @@ import javascript
1415
* extracted with the `--extract-program-text` flag.
1516
*/
1617
class Line extends @line, Locatable {
17-
override Location getLocation() { hasLocation(this, result) }
18+
override DbLocation getLocation() {
19+
exists(@location loc |
20+
hasLocation(this, loc) and
21+
result = TDbLocation(loc)
22+
)
23+
}
1824

1925
/** Gets the toplevel element this line belongs to. */
2026
TopLevel getTopLevel() { lines(this, result, _, _) }

0 commit comments

Comments
 (0)