Skip to content

Commit 62e45a2

Browse files
Handle when a related location has no region
1 parent fef55e3 commit 62e45a2

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

extensions/ql-vscode/src/common/sarif-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Sarif from "sarif";
22
import type { HighlightedRegion } from "../variant-analysis/shared/analysis-result";
33
import { ResolvableLocationValue } from "../common/bqrs-cli-types";
44

5-
interface SarifLink {
5+
export interface SarifLink {
66
dest: number;
77
text: string;
88
}

extensions/ql-vscode/src/variant-analysis/sarif-processing.ts

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as sarif from "sarif";
22
import {
3+
SarifLink,
34
parseHighlightedLine,
45
parseSarifPlainTextMessage,
56
parseSarifRegion,
@@ -14,6 +15,7 @@ import {
1415
ThreadFlow,
1516
CodeSnippet,
1617
HighlightedRegion,
18+
AnalysisMessageLocationTokenLocation,
1719
} from "./shared/analysis-result";
1820

1921
// A line of more than 8k characters is probably generated.
@@ -303,24 +305,47 @@ function getMessage(
303305
if (typeof messagePart === "string") {
304306
tokens.push({ t: "text", text: messagePart });
305307
} else {
306-
const relatedLocation = result.relatedLocations!.find(
307-
(rl) => rl.id === messagePart.dest,
308-
);
309-
tokens.push({
310-
t: "location",
311-
text: messagePart.text,
312-
location: {
313-
fileLink: {
314-
fileLinkPrefix,
315-
filePath: relatedLocation!.physicalLocation!.artifactLocation!.uri!,
316-
},
317-
highlightedRegion: getHighlightedRegion(
318-
relatedLocation!.physicalLocation!.region!,
319-
),
320-
},
321-
});
308+
const location = getRelatedLocation(messagePart, result, fileLinkPrefix);
309+
if (location === undefined) {
310+
tokens.push({ t: "text", text: messagePart.text });
311+
} else {
312+
tokens.push({
313+
t: "location",
314+
text: messagePart.text,
315+
location,
316+
});
317+
}
322318
}
323319
}
324320

325321
return { tokens };
326322
}
323+
324+
function getRelatedLocation(
325+
messagePart: SarifLink,
326+
result: sarif.Result,
327+
fileLinkPrefix: string,
328+
): AnalysisMessageLocationTokenLocation | undefined {
329+
const relatedLocation = result.relatedLocations!.find(
330+
(rl) => rl.id === messagePart.dest,
331+
);
332+
if (
333+
relatedLocation === undefined ||
334+
relatedLocation.physicalLocation?.artifactLocation?.uri === undefined ||
335+
relatedLocation.physicalLocation?.artifactLocation?.uri?.startsWith(
336+
"file:",
337+
) ||
338+
relatedLocation.physicalLocation?.region === undefined
339+
) {
340+
return undefined;
341+
}
342+
return {
343+
fileLink: {
344+
fileLinkPrefix,
345+
filePath: relatedLocation.physicalLocation.artifactLocation.uri,
346+
},
347+
highlightedRegion: getHighlightedRegion(
348+
relatedLocation.physicalLocation.region,
349+
),
350+
};
351+
}

extensions/ql-vscode/src/variant-analysis/shared/analysis-result.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ interface AnalysisMessageTextToken {
6363
export interface AnalysisMessageLocationToken {
6464
t: "location";
6565
text: string;
66-
location: {
67-
fileLink: FileLink;
68-
highlightedRegion?: HighlightedRegion;
69-
};
66+
location: AnalysisMessageLocationTokenLocation;
67+
}
68+
69+
export interface AnalysisMessageLocationTokenLocation {
70+
fileLink: FileLink;
71+
highlightedRegion?: HighlightedRegion;
7072
}
7173

7274
export type ResultSeverity = "Recommendation" | "Warning" | "Error";

0 commit comments

Comments
 (0)