Merge pull request #5656 from asgerf/js/files-diagnostics

JS: Add file diagnostics queries
This commit is contained in:
Rasmus Wriedt Larsen
2021-04-29 11:53:11 +02:00
committed by GitHub
16 changed files with 64 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
/**
* @name Extraction errors
* @description List all extraction errors for files in the source code directory.
* @kind diagnostic
* @id js/diagnostics/extraction-errors
*/
import javascript
/** Gets the SARIF severity to associate an error. */
int getSeverity() { result = 2 }
from Error error
where
exists(error.getFile().getRelativePath()) and
error.isFatal()
select error, "Extraction failed in " + error.getFile() + " with error " + error.getMessage(),
getSeverity()

View File

@@ -0,0 +1,14 @@
/**
* @name Successfully extracted files
* @description Lists all files in the source code directory that were extracted without encountering an error in the file.
* @kind diagnostic
* @id js/diagnostics/successfully-extracted-files
*/
import javascript
from File f
where
not exists(Error e | e.isFatal() and e.getFile() = f) and
exists(f.getRelativePath())
select f, ""

View File

@@ -10,6 +10,9 @@ abstract class Error extends Locatable {
abstract string getMessage();
override string toString() { result = getMessage() }
/** Holds if this error prevented the file from being extracted. */
predicate isFatal() { any() }
}
/** A JavaScript parse error encountered during extraction. */
@@ -21,4 +24,6 @@ class JSParseError extends @js_parse_error, Error {
/** Gets the source text of the line this error occurs on. */
string getLine() { js_parse_errors(this, _, _, result) }
override predicate isFatal() { not getTopLevel() instanceof Angular2::TemplateTopLevel }
}

View File

@@ -840,6 +840,8 @@ class RegExpParseError extends Error, @regexp_parse_error {
override string getMessage() { regexp_parse_errors(this, _, result) }
override string toString() { result = getMessage() }
override predicate isFatal() { none() }
}
/**

View File

@@ -0,0 +1,4 @@
| bad1.js:1:7:1:7 | Error: Unexpected token | Extraction failed in bad1.js with error Error: Unexpected token | 2 |
| bad2.ts:1:11:1:11 | Error: Expression expected. | Extraction failed in bad2.ts with error Error: Expression expected. | 2 |
| bad2.ts:1:13:1:13 | Error: Expression expected. | Extraction failed in bad2.ts with error Error: Expression expected. | 2 |
| bad3.html:2:11:2:11 | Error: Unexpected token | Extraction failed in bad3.html with error Error: Unexpected token | 2 |

View File

@@ -0,0 +1 @@
Diagnostics/ExtractionErrors.ql

View File

@@ -0,0 +1,4 @@
| contains-template.js:0:0:0:0 | contains-template.js | |
| good1.js:0:0:0:0 | good1.js | |
| good2.ts:0:0:0:0 | good2.ts | |
| good3.html:0:0:0:0 | good3.html | |

View File

@@ -0,0 +1 @@
Diagnostics/SuccessfullyExtractedFiles.ql

View File

@@ -0,0 +1 @@
let x x x;

View File

@@ -0,0 +1 @@
const z = ??;

View File

@@ -0,0 +1,3 @@
<script>
let q q q q ; // error
</script>

View File

@@ -0,0 +1,4 @@
const obj = {
// Template where we can't parse `x x x` but surrounding file still OK
template: '<b [foo]="x x x"></a>'
};

View File

@@ -0,0 +1 @@
let x = 123;

View File

@@ -0,0 +1 @@
const y: string = "dfg";

View File

@@ -0,0 +1,3 @@
<script>
let qwe = 123;
</script>

View File

@@ -0,0 +1 @@
semmle-extractor-options: --tolerate-parse-errors --experimental