Skip to content

Commit d9374d7

Browse files
committed
Make processDiagnosticMessages generate a module
1 parent aeff496 commit d9374d7

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

Diff for: scripts/processDiagnosticMessages.mjs

+18-18
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ function main() {
4444
}
4545
}
4646

47-
const outputFilesDir = path.dirname(inputFilePath);
48-
const thisFilePathRel = path.relative(process.cwd(), outputFilesDir);
49-
50-
const infoFileOutput = buildInfoFileOutput(diagnosticMessages, `./${path.basename(inputFilePath)}`, thisFilePathRel);
47+
const infoFileOutput = buildInfoFileOutput(diagnosticMessages, inputFilePath);
5148
checkForUniqueCodes(diagnosticMessages);
5249
writeFile("diagnosticInformationMap.generated.ts", infoFileOutput);
5350

@@ -72,31 +69,34 @@ function checkForUniqueCodes(diagnosticTable) {
7269
/**
7370
* @param {InputDiagnosticMessageTable} messageTable
7471
* @param {string} inputFilePathRel
75-
* @param {string} thisFilePathRel
7672
* @returns {string}
7773
*/
78-
function buildInfoFileOutput(messageTable, inputFilePathRel, thisFilePathRel) {
79-
let result =
80-
"// <auto-generated />\r\n" +
81-
"// generated from '" + inputFilePathRel + "' in '" + thisFilePathRel.replace(/\\/g, "/") + "'\r\n" +
82-
"/* @internal */\r\n" +
83-
"namespace ts {\r\n" +
84-
" function diag(code: number, category: DiagnosticCategory, key: string, message: string, reportsUnnecessary?: {}, elidedInCompatabilityPyramid?: boolean, reportsDeprecated?: {}): DiagnosticMessage {\r\n" +
85-
" return { code, category, key, message, reportsUnnecessary, elidedInCompatabilityPyramid, reportsDeprecated };\r\n" +
86-
" }\r\n" +
87-
" export const Diagnostics = {\r\n";
74+
function buildInfoFileOutput(messageTable, inputFilePathRel) {
75+
const result = [
76+
"// <auto-generated />",
77+
`// generated from '${inputFilePathRel}'`,
78+
"",
79+
"import { DiagnosticCategory, DiagnosticMessage } from \"./types\";",
80+
"",
81+
"function diag(code: number, category: DiagnosticCategory, key: string, message: string, reportsUnnecessary?: {}, elidedInCompatabilityPyramid?: boolean, reportsDeprecated?: {}): DiagnosticMessage {",
82+
" return { code, category, key, message, reportsUnnecessary, elidedInCompatabilityPyramid, reportsDeprecated };",
83+
"}",
84+
"",
85+
"/** @internal */",
86+
"export const Diagnostics = {",
87+
];
8888
messageTable.forEach(({ code, category, reportsUnnecessary, elidedInCompatabilityPyramid, reportsDeprecated }, name) => {
8989
const propName = convertPropertyName(name);
9090
const argReportsUnnecessary = reportsUnnecessary ? `, /*reportsUnnecessary*/ ${reportsUnnecessary}` : "";
9191
const argElidedInCompatabilityPyramid = elidedInCompatabilityPyramid ? `${!reportsUnnecessary ? ", /*reportsUnnecessary*/ undefined" : ""}, /*elidedInCompatabilityPyramid*/ ${elidedInCompatabilityPyramid}` : "";
9292
const argReportsDeprecated = reportsDeprecated ? `${!argElidedInCompatabilityPyramid ? ", /*reportsUnnecessary*/ undefined, /*elidedInCompatabilityPyramid*/ undefined" : ""}, /*reportsDeprecated*/ ${reportsDeprecated}` : "";
9393

94-
result += ` ${propName}: diag(${code}, DiagnosticCategory.${category}, "${createKey(propName, code)}", ${JSON.stringify(name)}${argReportsUnnecessary}${argElidedInCompatabilityPyramid}${argReportsDeprecated}),\r\n`;
94+
result.push(` ${propName}: diag(${code}, DiagnosticCategory.${category}, "${createKey(propName, code)}", ${JSON.stringify(name)}${argReportsUnnecessary}${argElidedInCompatabilityPyramid}${argReportsDeprecated}),`);
9595
});
9696

97-
result += " };\r\n}";
97+
result.push("};");
9898

99-
return result;
99+
return result.join("\r\n");
100100
}
101101

102102
/**

0 commit comments

Comments
 (0)