2018-06-10 20:58:16 +03:00
|
|
|
import path = require("path");
|
|
|
|
import fs = require("fs");
|
|
|
|
|
2014-07-13 03:04:16 +04:00
|
|
|
interface DiagnosticDetails {
|
|
|
|
category: string;
|
|
|
|
code: number;
|
2018-04-10 20:15:15 +03:00
|
|
|
reportsUnnecessary?: {};
|
2014-10-21 04:38:50 +04:00
|
|
|
isEarly?: boolean;
|
2019-09-24 02:08:44 +03:00
|
|
|
elidedInCompatabilityPyramid?: boolean;
|
2014-07-13 03:04:16 +04:00
|
|
|
}
|
|
|
|
|
2018-06-10 20:58:16 +03:00
|
|
|
type InputDiagnosticMessageTable = Map<string, DiagnosticDetails>;
|
2014-07-13 03:04:16 +04:00
|
|
|
|
|
|
|
function main(): void {
|
2018-06-10 20:58:16 +03:00
|
|
|
if (process.argv.length < 3) {
|
|
|
|
console.log("Usage:");
|
|
|
|
console.log("\tnode processDiagnosticMessages.js <diagnostic-json-input-file>");
|
2014-07-13 03:04:16 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:22:22 +03:00
|
|
|
function writeFile(fileName: string, contents: string) {
|
2018-06-10 20:58:16 +03:00
|
|
|
fs.writeFile(path.join(path.dirname(inputFilePath), fileName), contents, { encoding: "utf-8" }, err => {
|
|
|
|
if (err) throw err;
|
2019-06-14 09:46:02 +03:00
|
|
|
});
|
2015-10-15 21:22:22 +03:00
|
|
|
}
|
|
|
|
|
2018-06-10 20:58:16 +03:00
|
|
|
const inputFilePath = process.argv[2].replace(/\\/g, "/");
|
|
|
|
console.log(`Reading diagnostics from ${inputFilePath}`);
|
|
|
|
const inputStr = fs.readFileSync(inputFilePath, { encoding: "utf-8" });
|
2016-12-06 01:13:32 +03:00
|
|
|
|
2017-11-08 23:56:39 +03:00
|
|
|
const diagnosticMessagesJson: { [key: string]: DiagnosticDetails } = JSON.parse(inputStr);
|
2014-07-13 03:04:16 +04:00
|
|
|
|
2018-06-10 20:58:16 +03:00
|
|
|
const diagnosticMessages: InputDiagnosticMessageTable = new Map();
|
|
|
|
for (const key in diagnosticMessagesJson) {
|
|
|
|
if (Object.hasOwnProperty.call(diagnosticMessagesJson, key)) {
|
|
|
|
diagnosticMessages.set(key, diagnosticMessagesJson[key]);
|
|
|
|
}
|
|
|
|
}
|
2017-07-28 20:11:57 +03:00
|
|
|
|
2018-06-10 20:58:16 +03:00
|
|
|
const outputFilesDir = path.dirname(inputFilePath);
|
|
|
|
const thisFilePathRel = path.relative(process.cwd(), outputFilesDir);
|
2017-11-08 23:56:39 +03:00
|
|
|
|
|
|
|
const infoFileOutput = buildInfoFileOutput(diagnosticMessages, "./diagnosticInformationMap.generated.ts", thisFilePathRel);
|
2017-07-28 20:11:57 +03:00
|
|
|
checkForUniqueCodes(diagnosticMessages);
|
2015-10-15 21:22:22 +03:00
|
|
|
writeFile("diagnosticInformationMap.generated.ts", infoFileOutput);
|
2015-09-20 22:14:56 +03:00
|
|
|
|
2017-11-08 23:56:39 +03:00
|
|
|
const messageOutput = buildDiagnosticMessageOutput(diagnosticMessages);
|
2015-10-15 21:22:22 +03:00
|
|
|
writeFile("diagnosticMessages.generated.json", messageOutput);
|
2014-07-13 03:04:16 +04:00
|
|
|
}
|
|
|
|
|
2017-07-28 20:11:57 +03:00
|
|
|
function checkForUniqueCodes(diagnosticTable: InputDiagnosticMessageTable) {
|
|
|
|
const allCodes: { [key: number]: true | undefined } = [];
|
|
|
|
diagnosticTable.forEach(({ code }) => {
|
2017-11-08 23:56:39 +03:00
|
|
|
if (allCodes[code]) {
|
2017-07-28 20:11:57 +03:00
|
|
|
throw new Error(`Diagnostic code ${code} appears more than once.`);
|
2017-11-08 23:56:39 +03:00
|
|
|
}
|
2017-07-28 20:11:57 +03:00
|
|
|
allCodes[code] = true;
|
|
|
|
});
|
2015-09-20 22:14:56 +03:00
|
|
|
}
|
|
|
|
|
2017-11-08 23:56:39 +03:00
|
|
|
function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, inputFilePathRel: string, thisFilePathRel: string): string {
|
|
|
|
let result =
|
|
|
|
"// <auto-generated />\r\n" +
|
2019-06-14 12:18:27 +03:00
|
|
|
"// generated from '" + inputFilePathRel + "' by '" + thisFilePathRel.replace(/\\/g, "/") + "'\r\n" +
|
2017-11-08 23:56:39 +03:00
|
|
|
"/* @internal */\r\n" +
|
|
|
|
"namespace ts {\r\n" +
|
2019-09-24 02:08:44 +03:00
|
|
|
" function diag(code: number, category: DiagnosticCategory, key: string, message: string, reportsUnnecessary?: {}, elidedInCompatabilityPyramid?: boolean): DiagnosticMessage {\r\n" +
|
|
|
|
" return { code, category, key, message, reportsUnnecessary, elidedInCompatabilityPyramid };\r\n" +
|
2017-07-28 20:11:57 +03:00
|
|
|
" }\r\n" +
|
2017-11-06 20:23:47 +03:00
|
|
|
" export const Diagnostics = {\r\n";
|
2019-09-24 02:08:44 +03:00
|
|
|
messageTable.forEach(({ code, category, reportsUnnecessary, elidedInCompatabilityPyramid }, name) => {
|
2017-07-28 20:11:57 +03:00
|
|
|
const propName = convertPropertyName(name);
|
2018-04-10 20:15:15 +03:00
|
|
|
const argReportsUnnecessary = reportsUnnecessary ? `, /*reportsUnnecessary*/ ${reportsUnnecessary}` : "";
|
2019-09-24 02:08:44 +03:00
|
|
|
const argElidedInCompatabilityPyramid = elidedInCompatabilityPyramid ? `${!reportsUnnecessary ? ", /*reportsUnnecessary*/ undefined" : ""}, /*elidedInCompatabilityPyramid*/ ${elidedInCompatabilityPyramid}` : "";
|
|
|
|
result += ` ${propName}: diag(${code}, DiagnosticCategory.${category}, "${createKey(propName, code)}", ${JSON.stringify(name)}${argReportsUnnecessary}${argElidedInCompatabilityPyramid}),\r\n`;
|
2017-07-28 20:11:57 +03:00
|
|
|
});
|
2014-07-13 03:04:16 +04:00
|
|
|
|
2017-11-08 23:56:39 +03:00
|
|
|
result += " };\r\n}";
|
2014-07-13 03:04:16 +04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-07-27 22:30:37 +03:00
|
|
|
function buildDiagnosticMessageOutput(messageTable: InputDiagnosticMessageTable): string {
|
2017-11-08 23:56:39 +03:00
|
|
|
let result = "{";
|
2017-07-28 20:11:57 +03:00
|
|
|
messageTable.forEach(({ code }, name) => {
|
|
|
|
const propName = convertPropertyName(name);
|
2017-07-31 22:51:25 +03:00
|
|
|
result += `\r\n "${createKey(propName, code)}" : "${name.replace(/[\"]/g, '\\"')}",`;
|
2017-07-28 20:11:57 +03:00
|
|
|
});
|
2015-10-15 21:22:22 +03:00
|
|
|
|
2017-07-31 22:51:25 +03:00
|
|
|
// Shave trailing comma, then add newline and ending brace
|
2017-11-08 23:56:39 +03:00
|
|
|
result = result.slice(0, result.length - 1) + "\r\n}";
|
2017-07-31 22:51:25 +03:00
|
|
|
|
|
|
|
// Assert that we generated valid JSON
|
|
|
|
JSON.parse(result);
|
2015-10-15 21:22:22 +03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-11-08 23:56:39 +03:00
|
|
|
function createKey(name: string, code: number): string {
|
|
|
|
return name.slice(0, 100) + "_" + code;
|
2015-10-15 21:22:22 +03:00
|
|
|
}
|
|
|
|
|
2014-07-13 03:04:16 +04:00
|
|
|
function convertPropertyName(origName: string): string {
|
2017-11-08 23:56:39 +03:00
|
|
|
let result = origName.split("").map(char => {
|
|
|
|
if (char === "*") { return "_Asterisk"; }
|
|
|
|
if (char === "/") { return "_Slash"; }
|
|
|
|
if (char === ":") { return "_Colon"; }
|
2014-07-13 03:04:16 +04:00
|
|
|
return /\w/.test(char) ? char : "_";
|
|
|
|
}).join("");
|
|
|
|
|
|
|
|
// get rid of all multi-underscores
|
|
|
|
result = result.replace(/_+/g, "_");
|
|
|
|
|
|
|
|
// remove any leading underscore, unless it is followed by a number.
|
2017-11-08 23:56:39 +03:00
|
|
|
result = result.replace(/^_([^\d])/, "$1");
|
2014-07-13 03:04:16 +04:00
|
|
|
|
|
|
|
// get rid of all trailing underscores.
|
|
|
|
result = result.replace(/_$/, "");
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-06-12 19:04:29 +03:00
|
|
|
main();
|