diff --git a/scripts/processDiagnosticMessages.ts b/scripts/processDiagnosticMessages.ts index 20085022c04..dd66564b134 100644 --- a/scripts/processDiagnosticMessages.ts +++ b/scripts/processDiagnosticMessages.ts @@ -1,4 +1,5 @@ /// +/// interface DiagnosticDetails { category: string; @@ -9,57 +10,55 @@ interface DiagnosticDetails { type InputDiagnosticMessageTable = ts.Map; function main(): void { - var sys = ts.sys; + const sys = ts.sys; if (sys.args.length < 1) { - sys.write("Usage:" + sys.newLine) + sys.write("Usage:" + sys.newLine); sys.write("\tnode processDiagnosticMessages.js " + sys.newLine); return; } function writeFile(fileName: string, contents: string) { - // TODO: Fix path joining - var inputDirectory = inputFilePath.substr(0,inputFilePath.lastIndexOf("/")); - var fileOutputPath = inputDirectory + "/" + fileName; + const inputDirectory = ts.getDirectoryPath(inputFilePath); + const fileOutputPath = ts.combinePaths(inputDirectory, fileName); sys.writeFile(fileOutputPath, contents); } - var inputFilePath = sys.args[0].replace(/\\/g, "/"); - var inputStr = sys.readFile(inputFilePath); + const inputFilePath = sys.args[0].replace(/\\/g, "/"); + const inputStr = sys.readFile(inputFilePath); - var diagnosticMessagesJson: { [key: string]: DiagnosticDetails } = JSON.parse(inputStr); - // Check that there are no duplicates. - const seenNames = ts.createMap(); - for (const name of Object.keys(diagnosticMessagesJson)) { - if (seenNames.has(name)) - throw new Error(`Name ${name} appears twice`); - seenNames.set(name, true); - } + const diagnosticMessagesJson: { [key: string]: DiagnosticDetails } = JSON.parse(inputStr); const diagnosticMessages: InputDiagnosticMessageTable = ts.createMapFromTemplate(diagnosticMessagesJson); - var infoFileOutput = buildInfoFileOutput(diagnosticMessages); + const outputFilesDir = ts.getDirectoryPath(inputFilePath); + const thisFilePathRel = ts.getRelativePathToDirectoryOrUrl(outputFilesDir, sys.getExecutingFilePath(), + sys.getCurrentDirectory(), ts.createGetCanonicalFileName(sys.useCaseSensitiveFileNames), /* isAbsolutePathAnUrl */ false); + + const infoFileOutput = buildInfoFileOutput(diagnosticMessages, "./diagnosticInformationMap.generated.ts", thisFilePathRel); checkForUniqueCodes(diagnosticMessages); writeFile("diagnosticInformationMap.generated.ts", infoFileOutput); - var messageOutput = buildDiagnosticMessageOutput(diagnosticMessages); + const messageOutput = buildDiagnosticMessageOutput(diagnosticMessages); writeFile("diagnosticMessages.generated.json", messageOutput); } function checkForUniqueCodes(diagnosticTable: InputDiagnosticMessageTable) { const allCodes: { [key: number]: true | undefined } = []; diagnosticTable.forEach(({ code }) => { - if (allCodes[code]) + if (allCodes[code]) { throw new Error(`Diagnostic code ${code} appears more than once.`); + } allCodes[code] = true; }); } -function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable): string { - var result = - '// \r\n' + - '/// \r\n' + - '/* @internal */\r\n' + - 'namespace ts {\r\n' + +function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, inputFilePathRel: string, thisFilePathRel: string): string { + let result = + "// \r\n" + + "// generated from '" + inputFilePathRel + "' by '" + thisFilePathRel + "'\r\n" + + "/// \r\n" + + "/* @internal */\r\n" + + "namespace ts {\r\n" + " function diag(code: number, category: DiagnosticCategory, key: string, message: string): DiagnosticMessage {\r\n" + " return { code, category, key, message };\r\n" + " }\r\n" + @@ -70,20 +69,20 @@ function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable): string result += ` ${propName}: diag(${code}, DiagnosticCategory.${category}, "${createKey(propName, code)}", ${JSON.stringify(name)}),\r\n`; }); - result += ' };\r\n}'; + result += " };\r\n}"; return result; } function buildDiagnosticMessageOutput(messageTable: InputDiagnosticMessageTable): string { - let result = '{'; + let result = "{"; messageTable.forEach(({ code }, name) => { const propName = convertPropertyName(name); result += `\r\n "${createKey(propName, code)}" : "${name.replace(/[\"]/g, '\\"')}",`; }); // Shave trailing comma, then add newline and ending brace - result = result.slice(0, result.length - 1) + '\r\n}'; + result = result.slice(0, result.length - 1) + "\r\n}"; // Assert that we generated valid JSON JSON.parse(result); @@ -91,15 +90,15 @@ function buildDiagnosticMessageOutput(messageTable: InputDiagnosticMessageTable) return result; } -function createKey(name: string, code: number) : string { - return name.slice(0, 100) + '_' + code; +function createKey(name: string, code: number): string { + return name.slice(0, 100) + "_" + code; } function convertPropertyName(origName: string): string { - var result = origName.split("").map(char => { - if (char === '*') { return "_Asterisk"; } - if (char === '/') { return "_Slash"; } - if (char === ':') { return "_Colon"; } + let result = origName.split("").map(char => { + if (char === "*") { return "_Asterisk"; } + if (char === "/") { return "_Slash"; } + if (char === ":") { return "_Colon"; } return /\w/.test(char) ? char : "_"; }).join(""); @@ -107,7 +106,7 @@ function convertPropertyName(origName: string): string { result = result.replace(/_+/g, "_"); // remove any leading underscore, unless it is followed by a number. - result = result.replace(/^_([^\d])/, "$1") + result = result.replace(/^_([^\d])/, "$1"); // get rid of all trailing underscores. result = result.replace(/_$/, "");