/
validate.ts
314 lines (266 loc) · 9.59 KB
/
validate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import * as fse from 'fs-extra';
import { validate as validateSchema, ValidationError, ValidatorResult } from 'jsonschema';
import * as LSIF from 'lsif-protocol';
import * as path from 'path';
import * as TJS from 'typescript-json-schema';
import { getInVs } from './shared';
const vertices: { [id: string]: Element } = {};
const edges: { [id: string]: Element } = {};
const visited: { [id: string]: boolean } = {};
const protocolSubPath: string = 'lsif-protocol/lib/protocol.d.ts';
const errors: Error[] = [];
enum Check {
vertexBeforeEdge = 0,
allVerticesUsed,
}
const checks: boolean[] = [true, true];
class Error {
public element: LSIF.Element;
public message: string;
constructor(element: LSIF.Element, message: string) {
this.element = element;
this.message = message;
}
public print(): void {
console.error(
`\n${this.element.type.toUpperCase()} ${this.element.id}: ` +
`FAIL> ${this.message}\n${JSON.stringify(this.element, undefined, 2)}`,
);
}
}
class Element {
public element: LSIF.Element;
public valid: boolean;
constructor(element: LSIF.Element) {
this.element = element;
this.valid = true;
}
public invalidate(): void {
this.valid = false;
}
}
class Statistics {
public passed: number;
public failed: number;
public total: number;
constructor(passed: number, failed: number) {
this.passed = passed;
this.failed = failed;
this.total = passed + failed;
}
}
export function validate(toolOutput: LSIF.Element[], ids: string[]): number {
const nodeModulesPath: string | null = findNodeModulesPath();
if (nodeModulesPath !== null) {
readInput(toolOutput);
checkAllVisited();
checkVertices(toolOutput.filter((e: LSIF.Element) => e.type === 'vertex')
.map((e: LSIF.Element) => e.id.toString()),
nodeModulesPath);
checkEdges( toolOutput.filter((e: LSIF.Element) => e.type === 'edge')
.map((e: LSIF.Element) => e.id.toString()),
nodeModulesPath);
} else {
console.error(`Error: protocol.d.ts was not found`);
return 1;
}
printOutput(ids);
return errors.length === 0 ? 0 : 1;
}
// Returns null if protocol.d.ts is not found
function findNodeModulesPath(): string | null {
let nodeModulesPath: string = path.join(__dirname, '../node_modules');
// Package installed globally
if (fse.pathExistsSync(path.join(nodeModulesPath, protocolSubPath))) {
return nodeModulesPath;
}
// Package installed locally
nodeModulesPath = path.join(__dirname, '../..');
return fse.pathExistsSync(path.join(nodeModulesPath, protocolSubPath)) ? nodeModulesPath : null;
}
function readInput(toolOutput: LSIF.Element[]): void {
const outputMessage: string = 'Reading input...';
process.stdout.write(`${outputMessage}\r`);
for (const object of toolOutput) {
if (object.type === LSIF.ElementTypes.edge) {
const edge: LSIF.Edge = object as LSIF.Edge;
const id: string = edge.id.toString();
edges[id] = new Element(edge);
if (edge.outV === undefined) {
errors.push(new Error(edge, `requires property "outV"`));
edges[id].invalidate();
}
if (!LSIF.Edge.is11(edge) && !LSIF.Edge.is1N(edge)) {
errors.push(new Error(edge, `requires property "inV" or "inVs"`));
edges[id].invalidate();
} else {
checkVertexBeforeEdge(edge);
}
} else if (object.type === 'vertex') {
vertices[object.id.toString()] = new Element(object);
} else {
errors.push(new Error(object, `unknown element type`));
}
}
console.log(`${outputMessage} done`);
}
function checkVertexBeforeEdge(edge: LSIF.Edge): void {
const inVs = getInVs(edge);
const outV = edge.outV.toString();
if (vertices[outV] === undefined ||
// The following will be true if any id in the inV array is not yet defined in the vertices dictionary
inVs.map((inV) => vertices[inV] === undefined).reduce((total, curr) => total || curr)) {
errors.push(new Error(edge, `was emitted before a vertex it refers to`));
edges[edge.id.toString()].invalidate();
checks[Check.vertexBeforeEdge] = false;
}
inVs.forEach((inV) => {
visited[inV] = true;
});
visited[outV] = true;
}
function checkAllVisited(): void {
Object.keys(vertices)
.forEach((key: string) => {
const vertex: LSIF.Vertex = vertices[key].element as LSIF.Vertex;
if (!visited[key] && vertex.label !== 'metaData') {
errors.push(new Error(vertex, `not connected to any other vertex`));
checks[Check.allVerticesUsed] = false;
vertices[key].invalidate();
}
});
}
function checkVertices(ids: string[], nodeModulesPath: string): void {
let outputMessage: string | undefined;
const compilerOptions: TJS.CompilerOptions = {
baseUrl: nodeModulesPath,
};
const program: TJS.Program = TJS.getProgramFromFiles([path.join(nodeModulesPath, protocolSubPath)], compilerOptions);
const vertexSchema: TJS.Definition | null = TJS.generateSchema(program, 'Vertex', { required: true });
let count: number = 1;
const length: number = ids.length;
ids.forEach((key: string) => {
const vertex: LSIF.Vertex = vertices[key].element as LSIF.Vertex;
outputMessage = `Verifying vertex ${count} of ${length}...`;
process.stdout.write(`${outputMessage}\r`);
count++;
const validation: ValidatorResult = validateSchema(vertex, vertexSchema);
if (!validation.valid) {
let errorMessage: string | undefined;
vertices[key].invalidate();
if (vertex.label === undefined) {
errorMessage = `requires property "label"`;
} else if (!Object.values(LSIF.VertexLabels).includes(vertex.label)) {
errorMessage = `unknown label`;
} else {
try {
const className: string = vertex.label[0].toUpperCase() + vertex.label.slice(1);
const specificSchema: TJS.Definition | null = TJS.generateSchema(program, className, { required: true });
const moreValidation: ValidatorResult | null = validateSchema(vertex, specificSchema);
errorMessage = moreValidation.errors.join('; ');
} catch {
// Failed to get more details for the error
errorMessage = 'unable to provide details';
}
}
errors.push(new Error(vertex, errorMessage!));
}
});
if (outputMessage !== undefined) {
console.log(`${outputMessage} done`);
}
}
function checkEdges(ids: string[], nodeModulesPath: string): void {
let outputMessage: string | undefined;
const compilerOptions: TJS.CompilerOptions = {
baseUrl: nodeModulesPath,
};
const program: TJS.Program = TJS.getProgramFromFiles([path.join(nodeModulesPath, protocolSubPath)], compilerOptions);
let count: number = 1;
const length: number = ids.length;
const e11Schema = TJS.generateSchema(program, 'E11', { required: true, noExtraProps: true });
const e1NSchema = TJS.generateSchema(program, 'E1N', { required: true, noExtraProps: true });
const itemSchema = TJS.generateSchema(program, 'ItemEdge', { required: true, noExtraProps: true });
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);
}
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('; ')));
}
});
if (outputMessage !== undefined) {
console.log(`${outputMessage} done`);
}
}
function getCheckMessage(check: Check): string {
switch (check) {
case Check.vertexBeforeEdge:
return 'vertices emitted before connecting edges';
case Check.allVerticesUsed:
return 'all vertices are used in at least one edge';
default:
return 'unexpected check';
}
}
function printOutput(ids: string[]): void {
console.log('\nResults:');
for (let i: number = 0; i < checks.length; i++) {
console.log(`\t${checks[i] ? 'PASS' : 'FAIL'}> ${getCheckMessage(i)}`);
}
console.log();
const verticesStats: Statistics = getStatistics(vertices, ids);
console.log(`Vertices:\t${verticesStats.passed} passed, ` +
`${verticesStats.failed} failed, ${verticesStats.total} total`);
const edgesStats: Statistics = getStatistics(edges, ids);
console.log(`Edges:\t\t${edgesStats.passed} passed, ` +
`${edgesStats.failed} failed, ${edgesStats.total} total`);
errors.forEach((e: Error) => {
// Only print error for the elements verified
if (ids.includes(e.element.id.toString())) {
e.print();
}
});
}
function getStatistics(elements: { [id: string]: Element }, ids: string[]): Statistics {
let passed: number = 0;
let failed: number = 0;
Object.keys(elements).forEach((key: string) => {
if (ids.includes(key)) {
const element: Element = elements[key];
if (element.valid) {
passed++;
} else {
failed++;
}
}
});
return new Statistics(passed, failed);
}