From c4c7d4319245c78bf0b2b0c9418302664aaefe89 Mon Sep 17 00:00:00 2001
From: Jake Bailey <5341706+jakebailey@users.noreply.github.com>
Date: Thu, 26 May 2022 16:45:32 -0700
Subject: [PATCH] Make processDiagnosticMessages generate a module
---
scripts/processDiagnosticMessages.mjs | 36 +++++++++++++--------------
1 file changed, 18 insertions(+), 18 deletions(-)
diff --git a/scripts/processDiagnosticMessages.mjs b/scripts/processDiagnosticMessages.mjs
index cddd618d03abe..9b46eb6630ab4 100644
--- a/scripts/processDiagnosticMessages.mjs
+++ b/scripts/processDiagnosticMessages.mjs
@@ -44,10 +44,7 @@ function main() {
}
}
- const outputFilesDir = path.dirname(inputFilePath);
- const thisFilePathRel = path.relative(process.cwd(), outputFilesDir);
-
- const infoFileOutput = buildInfoFileOutput(diagnosticMessages, `./${path.basename(inputFilePath)}`, thisFilePathRel);
+ const infoFileOutput = buildInfoFileOutput(diagnosticMessages, inputFilePath);
checkForUniqueCodes(diagnosticMessages);
writeFile("diagnosticInformationMap.generated.ts", infoFileOutput);
@@ -72,31 +69,34 @@ function checkForUniqueCodes(diagnosticTable) {
/**
* @param {InputDiagnosticMessageTable} messageTable
* @param {string} inputFilePathRel
- * @param {string} thisFilePathRel
* @returns {string}
*/
-function buildInfoFileOutput(messageTable, inputFilePathRel, thisFilePathRel) {
- let result =
- "// \r\n" +
- "// generated from '" + inputFilePathRel + "' in '" + thisFilePathRel.replace(/\\/g, "/") + "'\r\n" +
- "/* @internal */\r\n" +
- "namespace ts {\r\n" +
- " function diag(code: number, category: DiagnosticCategory, key: string, message: string, reportsUnnecessary?: {}, elidedInCompatabilityPyramid?: boolean, reportsDeprecated?: {}): DiagnosticMessage {\r\n" +
- " return { code, category, key, message, reportsUnnecessary, elidedInCompatabilityPyramid, reportsDeprecated };\r\n" +
- " }\r\n" +
- " export const Diagnostics = {\r\n";
+function buildInfoFileOutput(messageTable, inputFilePathRel) {
+ const result = [
+ "// ",
+ `// generated from '${inputFilePathRel}'`,
+ "",
+ "import { DiagnosticCategory, DiagnosticMessage } from \"./types\";",
+ "",
+ "function diag(code: number, category: DiagnosticCategory, key: string, message: string, reportsUnnecessary?: {}, elidedInCompatabilityPyramid?: boolean, reportsDeprecated?: {}): DiagnosticMessage {",
+ " return { code, category, key, message, reportsUnnecessary, elidedInCompatabilityPyramid, reportsDeprecated };",
+ "}",
+ "",
+ "/** @internal */",
+ "export const Diagnostics = {",
+ ];
messageTable.forEach(({ code, category, reportsUnnecessary, elidedInCompatabilityPyramid, reportsDeprecated }, name) => {
const propName = convertPropertyName(name);
const argReportsUnnecessary = reportsUnnecessary ? `, /*reportsUnnecessary*/ ${reportsUnnecessary}` : "";
const argElidedInCompatabilityPyramid = elidedInCompatabilityPyramid ? `${!reportsUnnecessary ? ", /*reportsUnnecessary*/ undefined" : ""}, /*elidedInCompatabilityPyramid*/ ${elidedInCompatabilityPyramid}` : "";
const argReportsDeprecated = reportsDeprecated ? `${!argElidedInCompatabilityPyramid ? ", /*reportsUnnecessary*/ undefined, /*elidedInCompatabilityPyramid*/ undefined" : ""}, /*reportsDeprecated*/ ${reportsDeprecated}` : "";
- result += ` ${propName}: diag(${code}, DiagnosticCategory.${category}, "${createKey(propName, code)}", ${JSON.stringify(name)}${argReportsUnnecessary}${argElidedInCompatabilityPyramid}${argReportsDeprecated}),\r\n`;
+ result.push(` ${propName}: diag(${code}, DiagnosticCategory.${category}, "${createKey(propName, code)}", ${JSON.stringify(name)}${argReportsUnnecessary}${argElidedInCompatabilityPyramid}${argReportsDeprecated}),`);
});
- result += " };\r\n}";
+ result.push("};");
- return result;
+ return result.join("\r\n");
}
/**