• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import fs from "fs";
2import fsPromises from "fs/promises";
3import _glob from "glob";
4import util from "util";
5
6const glob = util.promisify(_glob);
7
8async function checkErrorBaselines() {
9    const data = await fsPromises.readFile("src/compiler/diagnosticMessages.json", "utf-8");
10    const messages = JSON.parse(data);
11    const keys = Object.keys(messages);
12    console.log("Loaded " + keys.length + " errors");
13
14    for (const k of keys) {
15        messages[k].seen = false;
16    }
17
18    const errRegex = /\(\d+,\d+\): error TS([^:]+):/g;
19    const baseDir = "tests/baselines/reference/";
20
21    const files = (await fsPromises.readdir(baseDir)).filter(f => f.endsWith(".errors.txt"));
22
23    files.forEach(f => {
24        fs.readFile(baseDir + f, "utf-8", (err, baseline) => {
25            if (err) throw err;
26
27            let g;
28            while (g = errRegex.exec(baseline)) {
29                const errCode = +g[1];
30                const msg = keys.filter(k => messages[k].code === errCode)[0];
31                messages[msg].seen = true;
32            }
33        });
34    });
35
36    console.log("== List of errors not present in baselines ==");
37    let count = 0;
38    for (const k of keys) {
39        if (messages[k].seen !== true) {
40            console.log(k);
41            count++;
42        }
43    }
44    console.log(count + " of " + keys.length + " errors are not in baselines");
45}
46
47async function checkSourceFiles() {
48    const data = await fsPromises.readFile("src/compiler/diagnosticInformationMap.generated.ts", "utf-8");
49
50    const errorRegexp = /\s(\w+): \{ code/g;
51    const errorNames = [];
52    let errMatch;
53    while (errMatch = errorRegexp.exec(data)) {
54        errorNames.push(errMatch[1]);
55    }
56
57    let allSrc = "";
58    const files = await glob("./src/**/*.ts");
59    console.log("Reading " + files.length + " source files");
60    for (const file of files) {
61        if (file.indexOf("diagnosticInformationMap.generated.ts") > 0) {
62            continue;
63        }
64
65        const src = fs.readFileSync(file, "utf-8");
66        allSrc = allSrc + src;
67    }
68
69    console.log("Consumed " + allSrc.length + " characters of source");
70
71    let count = 0;
72    console.log("== List of errors not used in source ==");
73    for (const errName of errorNames) {
74        if (allSrc.indexOf(errName) < 0) {
75            console.log(errName);
76            count++;
77        }
78    }
79    console.log(count + " of " + errorNames.length + " errors are not used in source");
80}
81
82async function main() {
83    await checkErrorBaselines();
84    await checkSourceFiles();
85}
86
87main();
88