diff --git a/scripts/processDiagnosticMessages.ts b/scripts/processDiagnosticMessages.ts index fd5f825b8483b..2da698884474d 100644 --- a/scripts/processDiagnosticMessages.ts +++ b/scripts/processDiagnosticMessages.ts @@ -38,10 +38,7 @@ function main(): void { } } - 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); @@ -59,28 +56,32 @@ function checkForUniqueCodes(diagnosticTable: InputDiagnosticMessageTable) { }); } -function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, inputFilePathRel: string, thisFilePathRel: string): string { - 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: InputDiagnosticMessageTable, inputFilePathRel: string): string { + 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"); } function buildDiagnosticMessageOutput(messageTable: InputDiagnosticMessageTable): string {