Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { map } from 'ix/iterable'
import RelateUrl from 'relateurl'
import { URL } from 'url'
/**
* Options to make sure that RelateUrl only outputs relative URLs and performs not other "smart" modifications.
* They would mess up things like prefix checking.
*/
const RELATE_URL_OPTIONS: RelateUrl.Options = {
// Make sure RelateUrl does not prefer root-relative URLs if shorter
output: RelateUrl.PATH_RELATIVE,
// Make sure RelateUrl does not remove trailing slash if present
removeRootTrailingSlash: false,
// Make sure RelateUrl does not remove default ports
defaultPorts: {},
}
/**
* Like `path.relative()` but for URLs.
* Inverse of `url.resolve()` or `new URL(relative, base)`.
*/
export const relativeUrl = (from: URL, to: URL): string => RelateUrl.relate(from.href, to.href, RELATE_URL_OPTIONS)
/**
* A Map of URIs to values.
*/
export class URLMap implements Map {
public insert(element: lsif.Vertex | lsif.Edge): void {
if (element.type === lsif.ElementTypes.vertex) {
switch (element.label) {
case lsif.VertexLabels.metaData:
this.handleMetaData(element)
break
case lsif.VertexLabels.document: {
if (!this.projectRoot) {
throw new Error('No metadata defined.')
}
const path = RelateUrl.relate(this.projectRoot.href + '/', new URL(element.uri).href, {
defaultPorts: {},
output: RelateUrl.PATH_RELATIVE,
removeRootTrailingSlash: false,
})
this.documentPaths.set(element.id, path)
this.containsData.set(element.id, new Set())
break
}
// The remaining vertex handlers stash data into an appropriate map. This data
// may be retrieved when an edge that references it is seen, or when a document
// is finalized.
case lsif.VertexLabels.range:
this.rangeData.set(element.id, {
startLine: element.start.line,
startCharacter: element.start.character,