|
53 | 53 | import com.semmle.util.data.StringUtil; |
54 | 54 | import com.semmle.util.diagnostics.DiagnosticLevel; |
55 | 55 | import com.semmle.util.diagnostics.DiagnosticWriter; |
| 56 | +import com.semmle.util.diagnostics.DiagnosticLocation; |
56 | 57 | import com.semmle.util.exception.CatastrophicError; |
57 | 58 | import com.semmle.util.exception.Exceptions; |
58 | 59 | import com.semmle.util.exception.ResourceError; |
@@ -545,14 +546,25 @@ public DiagnosticLevel getLevel() { |
545 | 546 | * {@link DiagnosticWriter} for more details. |
546 | 547 | */ |
547 | 548 | public void writeDiagnostics(String message, JSDiagnosticKind error) throws IOException { |
| 549 | + writeDiagnostics(message, error, null); |
| 550 | + } |
| 551 | + |
| 552 | + |
| 553 | + /** |
| 554 | + * Persist a diagnostic message with a location to a file in the diagnostics directory. |
| 555 | + * See {@link JSDiagnosticKind} for the kinds of errors that can be reported, |
| 556 | + * and see |
| 557 | + * {@link DiagnosticWriter} for more details. |
| 558 | + */ |
| 559 | + public void writeDiagnostics(String message, JSDiagnosticKind error, DiagnosticLocation location) throws IOException { |
548 | 560 | if (diagnostics.get() == null) { |
549 | 561 | warn("No diagnostics directory, so not writing diagnostic: " + message); |
550 | 562 | return; |
551 | 563 | } |
552 | 564 |
|
553 | 565 | // DiagnosticLevel level, String extractorName, String sourceId, String sourceName, String markdown |
554 | 566 | diagnostics.get().writeMarkdown(error.getLevel(), "javascript", "javascript/" + error.getId(), error.getName(), |
555 | | - message); |
| 567 | + message, location); |
556 | 568 | } |
557 | 569 |
|
558 | 570 | private DiagnosticWriter initDiagnosticsWriter(int count) { |
@@ -1219,7 +1231,19 @@ private void doExtract(FileExtractor extractor, Path file, ExtractorState state) |
1219 | 1231 | if (!extractor.getConfig().isExterns()) seenFiles = true; |
1220 | 1232 | for (ParseError err : loc.getParseErrors()) { |
1221 | 1233 | String msg = "A parse error occurred: " + err.getMessage() + ". Check the syntax of the file. If the file is invalid, correct the error or exclude the file from analysis."; |
1222 | | - writeDiagnostics(msg, JSDiagnosticKind.PARSE_ERROR); |
| 1234 | + // file, relative to the source root |
| 1235 | + String relativeFilePath = file.toString(); |
| 1236 | + if (relativeFilePath.startsWith(LGTM_SRC.toString())) { |
| 1237 | + relativeFilePath = relativeFilePath.substring(LGTM_SRC.toString().length() + 1); |
| 1238 | + } |
| 1239 | + DiagnosticLocation diagLoc = DiagnosticLocation.builder() |
| 1240 | + .setFile(relativeFilePath) |
| 1241 | + .setStartLine(err.getPosition().getLine()) |
| 1242 | + .setStartColumn(err.getPosition().getColumn()) |
| 1243 | + .setEndLine(err.getPosition().getLine()) |
| 1244 | + .setEndColumn(err.getPosition().getColumn()) |
| 1245 | + .build(); |
| 1246 | + writeDiagnostics(msg, JSDiagnosticKind.PARSE_ERROR, diagLoc); |
1223 | 1247 | } |
1224 | 1248 | logEndProcess(start, "Done extracting " + file); |
1225 | 1249 | } catch (OutOfMemoryError oom) { |
|
0 commit comments