Skip to content

Commit 8881042

Browse files
committed
add location to the parse-error diagnostics
1 parent c460eae commit 8881042

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.semmle.util.data.StringUtil;
5454
import com.semmle.util.diagnostics.DiagnosticLevel;
5555
import com.semmle.util.diagnostics.DiagnosticWriter;
56+
import com.semmle.util.diagnostics.DiagnosticLocation;
5657
import com.semmle.util.exception.CatastrophicError;
5758
import com.semmle.util.exception.Exceptions;
5859
import com.semmle.util.exception.ResourceError;
@@ -545,14 +546,25 @@ public DiagnosticLevel getLevel() {
545546
* {@link DiagnosticWriter} for more details.
546547
*/
547548
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 {
548560
if (diagnostics.get() == null) {
549561
warn("No diagnostics directory, so not writing diagnostic: " + message);
550562
return;
551563
}
552564

553565
// DiagnosticLevel level, String extractorName, String sourceId, String sourceName, String markdown
554566
diagnostics.get().writeMarkdown(error.getLevel(), "javascript", "javascript/" + error.getId(), error.getName(),
555-
message);
567+
message, location);
556568
}
557569

558570
private DiagnosticWriter initDiagnosticsWriter(int count) {
@@ -1219,7 +1231,19 @@ private void doExtract(FileExtractor extractor, Path file, ExtractorState state)
12191231
if (!extractor.getConfig().isExterns()) seenFiles = true;
12201232
for (ParseError err : loc.getParseErrors()) {
12211233
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);
12231247
}
12241248
logEndProcess(start, "Done extracting " + file);
12251249
} catch (OutOfMemoryError oom) {

0 commit comments

Comments
 (0)