Skip to content

Commit b81e71d

Browse files
author
Andy
authored
processDiagnosticMessages: Simplify code and emit (#17463)
1 parent 74e4903 commit b81e71d

File tree

1 file changed

+31
-57
lines changed

1 file changed

+31
-57
lines changed

scripts/processDiagnosticMessages.ts

Lines changed: 31 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ interface DiagnosticDetails {
66
isEarly?: boolean;
77
}
88

9-
interface InputDiagnosticMessageTable {
10-
[msg: string]: DiagnosticDetails;
11-
}
9+
type InputDiagnosticMessageTable = ts.Map<DiagnosticDetails>;
1210

1311
function main(): void {
1412
var sys = ts.sys;
@@ -28,50 +26,32 @@ function main(): void {
2826
var inputFilePath = sys.args[0].replace(/\\/g, "/");
2927
var inputStr = sys.readFile(inputFilePath);
3028

31-
var diagnosticMessages: InputDiagnosticMessageTable = JSON.parse(inputStr);
32-
33-
var names = Object.keys(diagnosticMessages);
29+
var diagnosticMessagesJson: { [key: string]: DiagnosticDetails } = JSON.parse(inputStr);
3430
// Check that there are no duplicates.
3531
const seenNames = ts.createMap<true>();
36-
for (const name of names) {
32+
for (const name of Object.keys(diagnosticMessagesJson)) {
3733
if (seenNames.has(name))
3834
throw new Error(`Name ${name} appears twice`);
3935
seenNames.set(name, true);
4036
}
4137

38+
const diagnosticMessages: InputDiagnosticMessageTable = ts.createMapFromTemplate(diagnosticMessagesJson);
39+
4240
var infoFileOutput = buildInfoFileOutput(diagnosticMessages);
43-
checkForUniqueCodes(names, diagnosticMessages);
41+
checkForUniqueCodes(diagnosticMessages);
4442
writeFile("diagnosticInformationMap.generated.ts", infoFileOutput);
4543

4644
var messageOutput = buildDiagnosticMessageOutput(diagnosticMessages);
4745
writeFile("diagnosticMessages.generated.json", messageOutput);
4846
}
4947

50-
function checkForUniqueCodes(messages: string[], diagnosticTable: InputDiagnosticMessageTable) {
51-
const originalMessageForCode: string[] = [];
52-
let numConflicts = 0;
53-
54-
for (const currentMessage of messages) {
55-
const code = diagnosticTable[currentMessage].code;
56-
57-
if (code in originalMessageForCode) {
58-
const originalMessage = originalMessageForCode[code];
59-
ts.sys.write("\x1b[91m"); // High intensity red.
60-
ts.sys.write("Error");
61-
ts.sys.write("\x1b[0m"); // Reset formatting.
62-
ts.sys.write(`: Diagnostic code '${code}' conflicts between "${originalMessage}" and "${currentMessage}".`);
63-
ts.sys.write(ts.sys.newLine + ts.sys.newLine);
64-
65-
numConflicts++;
66-
}
67-
else {
68-
originalMessageForCode[code] = currentMessage;
69-
}
70-
}
71-
72-
if (numConflicts > 0) {
73-
throw new Error(`Found ${numConflicts} conflict(s) in diagnostic codes.`);
74-
}
48+
function checkForUniqueCodes(diagnosticTable: InputDiagnosticMessageTable) {
49+
const allCodes: { [key: number]: true | undefined } = [];
50+
diagnosticTable.forEach(({ code }) => {
51+
if (allCodes[code])
52+
throw new Error(`Diagnostic code ${code} appears more than once.`);
53+
allCodes[code] = true;
54+
});
7555
}
7656

7757
function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable): string {
@@ -80,39 +60,34 @@ function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable): string
8060
'/// <reference path="types.ts" />\r\n' +
8161
'/* @internal */\r\n' +
8262
'namespace ts {\r\n' +
63+
" function diag(code: number, category: DiagnosticCategory, key: string, message: string): DiagnosticMessage {\r\n" +
64+
" return { code, category, key, message };\r\n" +
65+
" }\r\n" +
8366
' export const Diagnostics = {\r\n';
84-
for (const name of Object.keys(messageTable)) {
85-
var diagnosticDetails = messageTable[name];
86-
var propName = convertPropertyName(name);
87-
88-
result +=
89-
' ' + propName +
90-
': { code: ' + diagnosticDetails.code +
91-
', category: DiagnosticCategory.' + diagnosticDetails.category +
92-
', key: "' + createKey(propName, diagnosticDetails.code) + '"' +
93-
', message: "' + name.replace(/[\"]/g, '\\"') + '"' +
94-
' },\r\n';
95-
}
67+
messageTable.forEach(({ code, category }, name) => {
68+
const propName = convertPropertyName(name);
69+
result += ` ${propName}: diag(${code}, DiagnosticCategory.${category}, "${createKey(propName, code)}", ${JSON.stringify(name)}),\r\n`;
70+
});
9671

9772
result += ' };\r\n}';
9873

9974
return result;
10075
}
10176

10277
function buildDiagnosticMessageOutput(messageTable: InputDiagnosticMessageTable): string {
103-
var result =
104-
'{';
105-
var names = Object.keys(messageTable);
106-
for (var i = 0; i < names.length; i++) {
107-
var name = names[i];
108-
var diagnosticDetails = messageTable[name];
109-
var propName = convertPropertyName(name);
110-
111-
result += '\r\n "' + createKey(propName, diagnosticDetails.code) + '"' + ' : "' + name.replace(/[\"]/g, '\\"') + '"';
112-
if (i !== names.length - 1) {
78+
let result = '{';
79+
let first = true;
80+
messageTable.forEach(({ code }, name) => {
81+
if (!first) {
82+
first = false;
83+
}
84+
else {
11385
result += ',';
11486
}
115-
}
87+
88+
const propName = convertPropertyName(name);
89+
result += `\r\n "${createKey(propName, code)}": "${name.replace(/[\"]/g, '\\"')}"`;
90+
});
11691

11792
result += '\r\n}';
11893

@@ -131,7 +106,6 @@ function convertPropertyName(origName: string): string {
131106
return /\w/.test(char) ? char : "_";
132107
}).join("");
133108

134-
135109
// get rid of all multi-underscores
136110
result = result.replace(/_+/g, "_");
137111

0 commit comments

Comments
 (0)