Skip to content

Commit 2e5b6fe

Browse files
Add funcitonality to warn on duplicate codes.
1 parent 92f5f59 commit 2e5b6fe

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

scripts/processDiagnosticMessages.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ interface InputDiagnosticMessageTable {
1010
[msg: string]: DiagnosticDetails;
1111
}
1212

13-
interface IIndexable<V> {
14-
[key: string]: V;
15-
}
16-
1713
function main(): void {
1814
var sys = ts.sys;
1915
if (sys.args.length < 1) {
@@ -25,21 +21,42 @@ function main(): void {
2521
var inputFilePath = sys.args[0].replace(/\\/g, "/");
2622
var inputStr = sys.readFile(inputFilePath);
2723

28-
var diagnosticMesages: InputDiagnosticMessageTable = JSON.parse(inputStr);
24+
var diagnosticMessages: InputDiagnosticMessageTable = JSON.parse(inputStr);
2925

30-
var names = Utilities.getObjectKeys(diagnosticMesages);
26+
var names = Utilities.getObjectKeys(diagnosticMessages);
3127
var nameMap = buildUniqueNameMap(names);
3228

33-
var infoFileOutput = buildInfoFileOutput(diagnosticMesages, nameMap);
34-
29+
var infoFileOutput = buildInfoFileOutput(diagnosticMessages, nameMap);
30+
checkForUniqueCodes(names, diagnosticMessages);
31+
3532
// TODO: Fix path joining
3633
var inputDirectory = inputFilePath.substr(0,inputFilePath.lastIndexOf("/"));
3734
var fileOutputPath = inputDirectory + "/diagnosticInformationMap.generated.ts";
3835
sys.writeFile(fileOutputPath, infoFileOutput);
3936
}
4037

41-
function buildUniqueNameMap(names: string[]): IIndexable<string> {
42-
var nameMap: IIndexable<string> = {};
38+
function checkForUniqueCodes(messages: string[], diagnosticTable: InputDiagnosticMessageTable) {
39+
const originalMessageForCode: string[] = [];
40+
41+
for (const currentMessage of messages) {
42+
const code = diagnosticTable[currentMessage].code;
43+
44+
if (code in originalMessageForCode) {
45+
const originalMessage = originalMessageForCode[code];
46+
ts.sys.write("\x1b[93m"); // High intensity yellow.
47+
ts.sys.write("Warning");
48+
ts.sys.write("\x1b[0m"); // Reset formatting.
49+
ts.sys.write(`: Diagnostic code '${code}' conflicts between "${originalMessage}" and "${currentMessage}".`);
50+
ts.sys.write(ts.sys.newLine + ts.sys.newLine);
51+
}
52+
else {
53+
originalMessageForCode[code] = currentMessage;
54+
}
55+
}
56+
}
57+
58+
function buildUniqueNameMap(names: string[]): ts.Map<string> {
59+
var nameMap: ts.Map<string> = {};
4360

4461
var uniqueNames = NameGenerator.ensureUniqueness(names, /* isCaseSensitive */ false, /* isFixed */ undefined);
4562

@@ -50,7 +67,7 @@ function buildUniqueNameMap(names: string[]): IIndexable<string> {
5067
return nameMap;
5168
}
5269

53-
function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, nameMap: IIndexable<string>): string {
70+
function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, nameMap: ts.Map<string>): string {
5471
var result =
5572
'// <auto-generated />\r\n' +
5673
'/// <reference path="types.ts" />\r\n' +
@@ -172,7 +189,7 @@ module Utilities {
172189
}
173190

174191
// Like Object.keys
175-
export function getObjectKeys(obj: any): string[]{
192+
export function getObjectKeys(obj: any): string[] {
176193
var result: string[] = [];
177194

178195
for (var name in obj) {

0 commit comments

Comments
 (0)