Skip to content

Commit 927522c

Browse files
committed
JS: Only populate diagnostic locations within the source root
1 parent 94e0828 commit 927522c

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.LinkedHashSet;
2727
import java.util.List;
2828
import java.util.Map;
29+
import java.util.Optional;
2930
import java.util.Set;
3031
import java.util.concurrent.CompletableFuture;
3132
import java.util.concurrent.ExecutorService;
@@ -1238,18 +1239,24 @@ private void doExtract(FileExtractor extractor, Path file, ExtractorState state)
12381239
for (ParseError err : errors) {
12391240
String msg = "A parse error occurred: " + StringUtil.quoteWithBackticks(err.getMessage().trim())
12401241
+ ". Check the syntax of the file. If the file is invalid, correct the error or [exclude](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/customizing-code-scanning) the file from analysis.";
1241-
// file, relative to the source root
1242-
DiagnosticLocation.Builder builder = DiagnosticLocation.builder();
1242+
1243+
Optional<DiagnosticLocation> diagLoc = Optional.empty();
12431244
if (file.startsWith(LGTM_SRC)) {
1244-
builder = builder.setFile(file.subpath(LGTM_SRC.getNameCount(), file.getNameCount()).toString());
1245-
}
1246-
DiagnosticLocation diagLoc = builder
1245+
diagLoc = DiagnosticLocation.builder()
1246+
.setFile(file.subpath(LGTM_SRC.getNameCount(), file.getNameCount()).toString()) // file, relative to the source root
12471247
.setStartLine(err.getPosition().getLine())
12481248
.setStartColumn(err.getPosition().getColumn() + 1) // convert from 0-based to 1-based
12491249
.setEndLine(err.getPosition().getLine())
12501250
.setEndColumn(err.getPosition().getColumn() + 1) // convert from 0-based to 1-based
1251-
.build();
1252-
writeDiagnostics(msg, JSDiagnosticKind.PARSE_ERROR, diagLoc);
1251+
.build()
1252+
.getOk();
1253+
}
1254+
if (diagLoc.isPresent()) {
1255+
writeDiagnostics(msg, JSDiagnosticKind.PARSE_ERROR, diagLoc.get());
1256+
} else {
1257+
msg += "\n\nRelated file: " + file;
1258+
writeDiagnostics(msg, JSDiagnosticKind.PARSE_ERROR);
1259+
}
12531260
}
12541261
logEndProcess(start, "Done extracting " + file);
12551262
} catch (OutOfMemoryError oom) {

0 commit comments

Comments
 (0)