11import * as sarif from "sarif" ;
22import {
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+ }
0 commit comments