1+ import { createRemoteFileRef } from "../common/location-link-utils" ;
12import {
3+ isUrlValueResolvable ,
24 UrlValue ,
3- ResolvableLocationValue ,
5+ UrlValueResolvable ,
6+ } from "./raw-result-types" ;
7+ import {
48 LineColumnLocation ,
9+ UrlValue as BqrsUrlValue ,
510 WholeFileLocation ,
611} from "./bqrs-cli-types" ;
7- import { createRemoteFileRef } from "../common/location-link-utils" ;
12+
13+ /**
14+ * Checks whether the file path is empty. If so, we do not want to render this location
15+ * as a link.
16+ */
17+ export function isEmptyPath ( uriStr : string ) {
18+ return ! uriStr || uriStr === "file:/" ;
19+ }
820
921/**
1022 * The CodeQL filesystem libraries use this pattern in `getURL()` predicates
@@ -20,13 +32,25 @@ const FILE_LOCATION_REGEX = /file:\/\/(.+):([0-9]+):([0-9]+):([0-9]+):([0-9]+)/;
2032 * @param loc The location to test.
2133 */
2234export function tryGetResolvableLocation (
23- loc : UrlValue | undefined ,
24- ) : ResolvableLocationValue | undefined {
25- let resolvedLoc ;
35+ loc : BqrsUrlValue | undefined ,
36+ ) : UrlValueResolvable | undefined {
37+ let resolvedLoc : UrlValueResolvable | undefined ;
2638 if ( loc === undefined ) {
2739 resolvedLoc = undefined ;
28- } else if ( isWholeFileLoc ( loc ) || isLineColumnLoc ( loc ) ) {
29- resolvedLoc = loc as ResolvableLocationValue ;
40+ } else if ( isWholeFileLoc ( loc ) ) {
41+ resolvedLoc = {
42+ type : "wholeFileLocation" ,
43+ uri : loc . uri ,
44+ } ;
45+ } else if ( isLineColumnLoc ( loc ) ) {
46+ resolvedLoc = {
47+ type : "lineColumnLocation" ,
48+ uri : loc . uri ,
49+ startLine : loc . startLine ,
50+ startColumn : loc . startColumn ,
51+ endLine : loc . endLine ,
52+ endColumn : loc . endColumn ,
53+ } ;
3054 } else if ( isStringLoc ( loc ) ) {
3155 resolvedLoc = tryGetLocationFromString ( loc ) ;
3256 } else {
@@ -38,15 +62,17 @@ export function tryGetResolvableLocation(
3862
3963export function tryGetLocationFromString (
4064 loc : string ,
41- ) : ResolvableLocationValue | undefined {
65+ ) : UrlValueResolvable | undefined {
4266 const matches = FILE_LOCATION_REGEX . exec ( loc ) ;
4367 if ( matches && matches . length > 1 && matches [ 1 ] ) {
4468 if ( isWholeFileMatch ( matches ) ) {
4569 return {
70+ type : "wholeFileLocation" ,
4671 uri : matches [ 1 ] ,
47- } as WholeFileLocation ;
72+ } ;
4873 } else {
4974 return {
75+ type : "lineColumnLocation" ,
5076 uri : matches [ 1 ] ,
5177 startLine : Number ( matches [ 2 ] ) ,
5278 startColumn : Number ( matches [ 3 ] ) ,
@@ -68,17 +94,7 @@ function isWholeFileMatch(matches: RegExpExecArray): boolean {
6894 ) ;
6995}
7096
71- /**
72- * Checks whether the file path is empty. If so, we do not want to render this location
73- * as a link.
74- *
75- * @param uri A file uri
76- */
77- export function isEmptyPath ( uriStr : string ) {
78- return ! uriStr || uriStr === "file:/" ;
79- }
80-
81- export function isLineColumnLoc ( loc : UrlValue ) : loc is LineColumnLocation {
97+ export function isLineColumnLoc ( loc : BqrsUrlValue ) : loc is LineColumnLocation {
8298 return (
8399 typeof loc !== "string" &&
84100 ! isEmptyPath ( loc . uri ) &&
@@ -89,23 +105,36 @@ export function isLineColumnLoc(loc: UrlValue): loc is LineColumnLocation {
89105 ) ;
90106}
91107
92- export function isWholeFileLoc ( loc : UrlValue ) : loc is WholeFileLocation {
108+ export function isWholeFileLoc ( loc : BqrsUrlValue ) : loc is WholeFileLocation {
93109 return (
94110 typeof loc !== "string" && ! isEmptyPath ( loc . uri ) && ! isLineColumnLoc ( loc )
95111 ) ;
96112}
97113
98- export function isStringLoc ( loc : UrlValue ) : loc is string {
114+ export function isStringLoc ( loc : BqrsUrlValue ) : loc is string {
99115 return typeof loc === "string" ;
100116}
101117
118+ export function tryGetBqrsRemoteLocation (
119+ loc : BqrsUrlValue | undefined ,
120+ fileLinkPrefix : string ,
121+ sourceLocationPrefix : string | undefined ,
122+ ) : string | undefined {
123+ const resolvedLoc = tryGetResolvableLocation ( loc ) ;
124+
125+ return tryGetRemoteLocation (
126+ resolvedLoc ,
127+ fileLinkPrefix ,
128+ sourceLocationPrefix ,
129+ ) ;
130+ }
131+
102132export function tryGetRemoteLocation (
103133 loc : UrlValue | undefined ,
104134 fileLinkPrefix : string ,
105135 sourceLocationPrefix : string | undefined ,
106136) : string | undefined {
107- const resolvableLocation = tryGetResolvableLocation ( loc ) ;
108- if ( ! resolvableLocation ) {
137+ if ( ! loc || ! isUrlValueResolvable ( loc ) ) {
109138 return undefined ;
110139 }
111140
@@ -115,34 +144,36 @@ export function tryGetRemoteLocation(
115144 // "file:${sourceLocationPrefix}/relative/path/to/file"
116145 // So we need to strip off the first part to get the relative path.
117146 if ( sourceLocationPrefix ) {
118- if ( ! resolvableLocation . uri . startsWith ( `file:${ sourceLocationPrefix } /` ) ) {
147+ if ( ! loc . uri . startsWith ( `file:${ sourceLocationPrefix } /` ) ) {
119148 return undefined ;
120149 }
121- trimmedLocation = resolvableLocation . uri . replace (
122- `file:${ sourceLocationPrefix } /` ,
123- "" ,
124- ) ;
150+ trimmedLocation = loc . uri . replace ( `file:${ sourceLocationPrefix } /` , "" ) ;
125151 } else {
126152 // If the source location prefix is empty (e.g. for older remote queries), we assume that the database
127153 // was created on a Linux actions runner and has the format:
128154 // "file:/home/runner/work/<repo>/<repo>/relative/path/to/file"
129155 // So we need to drop the first 6 parts of the path.
130- if ( ! resolvableLocation . uri . startsWith ( "file:/home/runner/work/" ) ) {
156+ if ( ! loc . uri . startsWith ( "file:/home/runner/work/" ) ) {
131157 return undefined ;
132158 }
133- const locationParts = resolvableLocation . uri . split ( "/" ) ;
159+ const locationParts = loc . uri . split ( "/" ) ;
134160 trimmedLocation = locationParts . slice ( 6 , locationParts . length ) . join ( "/" ) ;
135161 }
136162
137163 const fileLink = {
138164 fileLinkPrefix,
139165 filePath : trimmedLocation ,
140166 } ;
167+
168+ if ( loc . type === "wholeFileLocation" ) {
169+ return createRemoteFileRef ( fileLink ) ;
170+ }
171+
141172 return createRemoteFileRef (
142173 fileLink ,
143- resolvableLocation . startLine ,
144- resolvableLocation . endLine ,
145- resolvableLocation . startColumn ,
146- resolvableLocation . endColumn ,
174+ loc . startLine ,
175+ loc . endLine ,
176+ loc . startColumn ,
177+ loc . endColumn ,
147178 ) ;
148179}
0 commit comments