Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public handleMoniker(moniker: Moniker): void {
if (moniker.kind !== MonikerKind.import || moniker.scheme !== TscMoniker.scheme) {
return;
}
this.ensureIdGenerator(moniker.id);
emit(moniker);
const tscMoniker = TscMoniker.parse(moniker.identifier);
if (!TscMoniker.hasPath(tscMoniker)) {
return;
}
let parts = tscMoniker.path.split('/');
let packagePath: string | undefined;
let monikerPath: string | undefined;
for (let i = parts.length - 1; i >= 0; i--) {
let part = parts[i];
if (part === 'node_modules') {
// End is exclusive and one for the name
const packageIndex = i + (parts[i + 1].startsWith('@') ? 3 : 2);
result = result.filter((element: LSIF.Element) => {
/* ToDo@jumattos The element is a vertex here as well. Uncomment the if and test are failing*/
if (element.type !== ElementTypes.edge) {
return false;
}
let edge: Edge = element as Edge;
if (Edge.is11(edge)) {
return includes(inV, edge.inV);
} else {
console.log(JSON.stringify(edge, undefined, 0));
for (let item of edge.inVs) {
if (includes(inV, item)) {
return true;
}
}
return false;
}
});
result = result.filter((element: LSIF.Element) => {
result = result.filter((element: LSIF.Element) => {
/* ToDo@jumattos The element is a vertex here as well. Uncomment the if and test are failing*/
if (element.type !== ElementTypes.edge) {
return false;
}
let edge: Edge = element as Edge;
if (Edge.is11(edge)) {
return includes(inV, edge.inV);
} else {
console.log(JSON.stringify(edge, undefined, 0));
for (let item of edge.inVs) {
if (includes(inV, item)) {
return true;
}
}
return false;
}
});
result = result.filter((element: LSIF.Element) => {
hash.update(JSON.stringify(compressed, undefined, 0));
}
}
// Assume that folding ranges are already sorted
if (this.blob.foldingRanges) {
const compressor = foldingRangeCompressor;
for (let range of this.blob.foldingRanges) {
const compressed = compressor.compress(range, options);
hash.update(JSON.stringify(compressed, undefined, 0));
}
}
// Unsure if we need to sort the children by range or not?
if (this.blob.documentSymbols && this.blob.documentSymbols.length > 0) {
const first = this.blob.documentSymbols[0];
const compressor = lsp.DocumentSymbol.is(first) ? undefined : assertDefined(Compressor.getVertexCompressor(VertexLabels.range));
if (compressor === undefined) {
throw new Error(`Document symbol compression not supported`);
}
const inline = (result: any[], value: RangeBasedDocumentSymbol) => {
const item: any[] = [];
const rangeHash = assertDefined(rangeHashes.get(value.id));
item.push(rangeHash);
if (value.children && value.children.length > 0) {
const children: any[] = [];
for (let child of value.children) {
inline(children, child);
}
item.push(children);
}
result.push(item);
};
private computeHash(): string {
const hash = crypto.createHash('md5');
hash.update(this.blob.contents);
const options: CompressorOptions = { mode: 'hash' };
const compressor = assertDefined(Compressor.getVertexCompressor(VertexLabels.range));
const rangeHashes: Map = new Map();
for (let key of Object.keys(this.blob.ranges)) {
const range = this.blob.ranges[key];
const rangeHash = crypto.createHash('md5').update(JSON.stringify(compressor.compress(range, options), undefined, 0)).digest('base64');
rangeHashes.set(Number(key), rangeHash);
}
for (let item of Array.from(rangeHashes.values()).sort(Strings.compare)) {
hash.update(item);
}
// moniker
if (this.blob.monikers !== undefined) {
const monikers = LiteralMap.values(this.blob.monikers).sort(Monikers.compare);
const compressor = assertDefined(Compressor.getVertexCompressor(VertexLabels.moniker));
for (let moniker of monikers) {
const compressed = compressor.compress(moniker, options);
const options: CompressorOptions = { mode: 'hash' };
const compressor = assertDefined(Compressor.getVertexCompressor(VertexLabels.range));
const rangeHashes: Map = new Map();
for (let key of Object.keys(this.blob.ranges)) {
const range = this.blob.ranges[key];
const rangeHash = crypto.createHash('md5').update(JSON.stringify(compressor.compress(range, options), undefined, 0)).digest('base64');
rangeHashes.set(Number(key), rangeHash);
}
for (let item of Array.from(rangeHashes.values()).sort(Strings.compare)) {
hash.update(item);
}
// moniker
if (this.blob.monikers !== undefined) {
const monikers = LiteralMap.values(this.blob.monikers).sort(Monikers.compare);
const compressor = assertDefined(Compressor.getVertexCompressor(VertexLabels.moniker));
for (let moniker of monikers) {
const compressed = compressor.compress(moniker, options);
hash.update(JSON.stringify(compressed, undefined, 0));
}
}
// Assume that folding ranges are already sorted
if (this.blob.foldingRanges) {
const compressor = foldingRangeCompressor;
for (let range of this.blob.foldingRanges) {
const compressed = compressor.compress(range, options);
hash.update(JSON.stringify(compressed, undefined, 0));
}
}
// Unsure if we need to sort the children by range or not?
if (this.blob.documentSymbols && this.blob.documentSymbols.length > 0) {
count++;
if ((!LSIF.Edge.is11(edge) && !(LSIF.Edge.is1N(edge))) || edge.outV === undefined) {
// This error was caught before
return;
}
if (edge.label === undefined || !Object.values(LSIF.EdgeLabels).includes(edge.label)) {
errors.push(new Error(edges[key].element, edge.label ? `requires property "label"` : `unknown label`));
edges[key].invalidate();
return;
}
let validation: ValidatorResult;
switch (edge.label) {
case LSIF.EdgeLabels.item:
validation = validateSchema(edge, itemSchema);
break;
case LSIF.EdgeLabels.contains:
validation = validateSchema(edge, e1NSchema);
break;
default:
validation = validateSchema(edge, e11Schema);
}
const validationErrors: ValidationError[] = validation.errors.filter((error) => error.property === 'instance');
if (validationErrors.length > 0) {
edges[key].invalidate();
errors.push(new Error(edges[key].element, validationErrors.join('; ')));
}
});
ids.forEach((key: string) => {
const edge: LSIF.Edge = edges[key].element as LSIF.Edge;
outputMessage = `Verifying edge ${count} of ${length}...`;
process.stdout.write(`${outputMessage}\r`);
count++;
if ((!LSIF.Edge.is11(edge) && !(LSIF.Edge.is1N(edge))) || edge.outV === undefined) {
// This error was caught before
return;
}
if (edge.label === undefined || !Object.values(LSIF.EdgeLabels).includes(edge.label)) {
errors.push(new Error(edges[key].element, edge.label ? `requires property "label"` : `unknown label`));
edges[key].invalidate();
return;
}
let validation: ValidatorResult;
switch (edge.label) {
case LSIF.EdgeLabels.item:
validation = validateSchema(edge, itemSchema);
break;
case LSIF.EdgeLabels.contains:
validation = validateSchema(edge, e1NSchema);
break;
default:
validation = validateSchema(edge, e11Schema);
}
rd.on('line', (line) => {
let element: Edge | Vertex = JSON.parse(line);
if (element.type === ElementTypes.vertex) {
switch(element.label) {
case VertexLabels.moniker:
if (element.kind === MonikerKind.local) {
emit(line);
} else {
if (exportLinker !== undefined) {
exportLinker.handleMoniker(element);
}
importLinker.handleMoniker(element);
}
break;
default:
emit(line);
}
} else {
emit(line);
rd.on('line', (line) => {
if (!line) {
return;
}
let element: Edge | Vertex;
try {
element = JSON.parse(line);
} catch (err) {
console.log(`Parsing failed for line:\n${line}`);
throw err;
}
if (element.type === ElementTypes.vertex && element.label === VertexLabels.metaData) {
let convertMetaData = (data: CompressorProperty): CompressorPropertyDescription => {
let result: CompressorPropertyDescription = {
name: data.name as string,
index: data.index,
compressionKind: data.compressionKind
};
if (data.shortForm !== undefined) {
let long: Set = new Set();
let short: Set = new Set();
result.shortForm = [];
for (let elem of data.shortForm) {
let [key, value] = elem;
if (long.has(key)) {
throw new Error(`Duplicate key ${key} in short form.`);
}
long.add(key);