Skip to content

Commit af0723c

Browse files
authored
Merge pull request github#5656 from asgerf/js/files-diagnostics
JS: Add file diagnostics queries
2 parents 84d4394 + d2fad18 commit af0723c

16 files changed

+64
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @name Extraction errors
3+
* @description List all extraction errors for files in the source code directory.
4+
* @kind diagnostic
5+
* @id js/diagnostics/extraction-errors
6+
*/
7+
8+
import javascript
9+
10+
/** Gets the SARIF severity to associate an error. */
11+
int getSeverity() { result = 2 }
12+
13+
from Error error
14+
where
15+
exists(error.getFile().getRelativePath()) and
16+
error.isFatal()
17+
select error, "Extraction failed in " + error.getFile() + " with error " + error.getMessage(),
18+
getSeverity()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @name Successfully extracted files
3+
* @description Lists all files in the source code directory that were extracted without encountering an error in the file.
4+
* @kind diagnostic
5+
* @id js/diagnostics/successfully-extracted-files
6+
*/
7+
8+
import javascript
9+
10+
from File f
11+
where
12+
not exists(Error e | e.isFatal() and e.getFile() = f) and
13+
exists(f.getRelativePath())
14+
select f, ""

javascript/ql/src/semmle/javascript/Errors.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ abstract class Error extends Locatable {
1010
abstract string getMessage();
1111

1212
override string toString() { result = getMessage() }
13+
14+
/** Holds if this error prevented the file from being extracted. */
15+
predicate isFatal() { any() }
1316
}
1417

1518
/** A JavaScript parse error encountered during extraction. */
@@ -21,4 +24,6 @@ class JSParseError extends @js_parse_error, Error {
2124

2225
/** Gets the source text of the line this error occurs on. */
2326
string getLine() { js_parse_errors(this, _, _, result) }
27+
28+
override predicate isFatal() { not getTopLevel() instanceof Angular2::TemplateTopLevel }
2429
}

javascript/ql/src/semmle/javascript/Regexp.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,8 @@ class RegExpParseError extends Error, @regexp_parse_error {
840840
override string getMessage() { regexp_parse_errors(this, _, result) }
841841

842842
override string toString() { result = getMessage() }
843+
844+
override predicate isFatal() { none() }
843845
}
844846

845847
/**
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| bad1.js:1:7:1:7 | Error: Unexpected token | Extraction failed in bad1.js with error Error: Unexpected token | 2 |
2+
| bad2.ts:1:11:1:11 | Error: Expression expected. | Extraction failed in bad2.ts with error Error: Expression expected. | 2 |
3+
| bad2.ts:1:13:1:13 | Error: Expression expected. | Extraction failed in bad2.ts with error Error: Expression expected. | 2 |
4+
| bad3.html:2:11:2:11 | Error: Unexpected token | Extraction failed in bad3.html with error Error: Unexpected token | 2 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Diagnostics/ExtractionErrors.ql
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| contains-template.js:0:0:0:0 | contains-template.js | |
2+
| good1.js:0:0:0:0 | good1.js | |
3+
| good2.ts:0:0:0:0 | good2.ts | |
4+
| good3.html:0:0:0:0 | good3.html | |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Diagnostics/SuccessfullyExtractedFiles.ql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let x x x;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const z = ??;

0 commit comments

Comments
 (0)