@@ -4,18 +4,18 @@ import { IProviderConfig } from "./extension"
44import { cleanHostname } from "./utils"
55
66interface IBaseGetUrls {
7- readonly selection : [ number , number ]
7+ readonly selection : [ number | null , number | null ]
88 readonly head : Head
9- readonly relativeFilePath : string
9+ readonly relativeFilePath : string | null
1010}
1111
1212export interface IUrlInfo {
13- readonly blobUrl : string
14- readonly repoUrl : string
15- readonly blameUrl : string
16- readonly historyUrl : string
17- readonly prUrl : string
18- readonly compareUrl : string
13+ readonly blobUrl : string | null
14+ readonly repoUrl : string | null
15+ readonly blameUrl : string | null
16+ readonly historyUrl : string | null
17+ readonly prUrl : string | null
18+ readonly compareUrl : string | null
1919}
2020
2121interface IOrgInfo {
@@ -120,12 +120,16 @@ export class Github extends BaseProvider {
120120 const rootUrl = `https://${ repoInfo . hostname } /`
121121 const [ start , end ] = selection
122122 // Github uses 1-based indexing
123- const lines = `L${ start + 1 } -L${ end + 1 } `
123+ const lines =
124+ start != null && end != null ? `L${ start + 1 } -L${ end + 1 } ` : null
124125 const repoUrl = new url . URL (
125126 path . join ( repoInfo . org , repoInfo . repo ) ,
126127 rootUrl ,
127128 ) . toString ( )
128129 const createUrl = ( mode : string , hash = true ) => {
130+ if ( relativeFilePath == null ) {
131+ return null
132+ }
129133 const u = new url . URL (
130134 path . join (
131135 repoInfo . org ,
@@ -136,7 +140,7 @@ export class Github extends BaseProvider {
136140 ) ,
137141 rootUrl ,
138142 )
139- if ( hash ) {
143+ if ( hash && lines ) {
140144 u . hash = lines
141145 }
142146 return u . toString ( )
@@ -182,12 +186,16 @@ export class Gitlab extends BaseProvider {
182186 const rootUrl = `https://${ repoInfo . hostname } /`
183187 const [ start , end ] = selection
184188 // The format is L34-56 (this is one character off from Github)
185- const lines = `L${ start + 1 } -${ end + 1 } `
189+ const lines =
190+ start != null && end != null ? `L${ start + 1 } -${ end + 1 } ` : null
186191 const repoUrl = new url . URL (
187192 path . join ( repoInfo . org , repoInfo . repo ) ,
188193 rootUrl ,
189194 ) . toString ( )
190195 const createUrl = ( mode : string , hash = true ) => {
196+ if ( relativeFilePath == null ) {
197+ return null
198+ }
191199 const u = new url . URL (
192200 path . join (
193201 repoInfo . org ,
@@ -198,7 +206,7 @@ export class Gitlab extends BaseProvider {
198206 ) ,
199207 rootUrl ,
200208 )
201- if ( hash ) {
209+ if ( hash && lines ) {
202210 u . hash = lines
203211 }
204212 return u . toString ( )
@@ -246,12 +254,16 @@ export class Bitbucket extends BaseProvider {
246254 // https://bitbucket.org/recipeyak/recipeyak/src/master/app/main.py#lines-12:15
247255 const rootUrl = `https://${ repoInfo . hostname } /`
248256 const [ start , end ] = selection
249- const lines = `lines-${ start + 1 } :${ end + 1 } `
257+ const lines =
258+ start != null && end != null ? `lines-${ start + 1 } :${ end + 1 } ` : null
250259 const repoUrl = new url . URL (
251260 path . join ( repoInfo . org , repoInfo . repo ) ,
252261 rootUrl ,
253262 ) . toString ( )
254263 const createUrl = ( mode : string , hash = true ) => {
264+ if ( relativeFilePath == null ) {
265+ return null
266+ }
255267 const u = new url . URL (
256268 path . join (
257269 repoInfo . org ,
@@ -262,7 +274,7 @@ export class Bitbucket extends BaseProvider {
262274 ) ,
263275 rootUrl ,
264276 )
265- if ( hash ) {
277+ if ( hash && lines ) {
266278 u . hash = lines
267279 }
268280 return u . toString ( )
@@ -319,24 +331,32 @@ export class VisualStudio extends BaseProvider {
319331 // https://bitbucket.org/recipeyak/recipeyak/src/master/app/main.py#lines-12:15
320332 const rootUrl = `https://${ repoInfo . hostname } /`
321333 const [ start , end ] = selection
322- const lines = `&line=${ start + 1 } &lineEnd=${ end + 1 } `
334+ const lines =
335+ start != null && end != null
336+ ? `&line=${ start + 1 } &lineEnd=${ end + 1 } `
337+ : null
323338 const repoUrl = new url . URL (
324339 path . join ( repoInfo . org , "_git" , repoInfo . repo ) ,
325340 rootUrl ,
326341 )
327342 let filePath = relativeFilePath
328- if ( ! filePath . startsWith ( "/" ) ) {
343+ if ( filePath != null && ! filePath . startsWith ( "/" ) ) {
329344 filePath = "/" + filePath
330345 }
331346 const version =
332347 head . kind === "branch" ? `GB${ head . value } ` : `GC${ head . value } `
333- const baseSearch = `path=${ encodeURIComponent ( filePath ) } &version=${ version } `
348+ const baseSearch =
349+ filePath != null
350+ ? `path=${ encodeURIComponent ( filePath ) } &version=${ version } `
351+ : null
334352 const blobUrl = new url . URL ( repoUrl . toString ( ) )
335353 const blameUrl = new url . URL ( repoUrl . toString ( ) )
336354 const historyUrl = new url . URL ( repoUrl . toString ( ) )
337- blobUrl . search = baseSearch + lines
338- blameUrl . search = baseSearch + lines + "&_a=annotate"
339- historyUrl . search = baseSearch + "&_a=history"
355+ if ( baseSearch != null ) {
356+ blobUrl . search = baseSearch + lines
357+ blameUrl . search = baseSearch + lines + "&_a=annotate"
358+ historyUrl . search = baseSearch + "&_a=history"
359+ }
340360 const compareUrl = new url . URL (
341361 path . join ( repoInfo . org , "_git" , repoInfo . repo , "branches" ) ,
342362 rootUrl ,
0 commit comments