Skip to content

Commit 80d1aa0

Browse files
NaridaLmhegazy
authored andcommitted
processDiagnosticMessages.ts: linted, removed unused code (microsoft#18697)
Added following line to generated output: "// generated from 'src/diagnosticMessages.json' by 'scripts/processDiagnosticMessages.ts'\r\n" + Fixes microsoft#3591
1 parent 76a3be7 commit 80d1aa0

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

scripts/processDiagnosticMessages.ts

+33-34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// <reference path="../src/compiler/sys.ts" />
2+
/// <reference path="../src/compiler/core.ts" />
23

34
interface DiagnosticDetails {
45
category: string;
@@ -9,57 +10,55 @@ interface DiagnosticDetails {
910
type InputDiagnosticMessageTable = ts.Map<DiagnosticDetails>;
1011

1112
function main(): void {
12-
var sys = ts.sys;
13+
const sys = ts.sys;
1314
if (sys.args.length < 1) {
14-
sys.write("Usage:" + sys.newLine)
15+
sys.write("Usage:" + sys.newLine);
1516
sys.write("\tnode processDiagnosticMessages.js <diagnostic-json-input-file>" + sys.newLine);
1617
return;
1718
}
1819

1920
function writeFile(fileName: string, contents: string) {
20-
// TODO: Fix path joining
21-
var inputDirectory = inputFilePath.substr(0,inputFilePath.lastIndexOf("/"));
22-
var fileOutputPath = inputDirectory + "/" + fileName;
21+
const inputDirectory = ts.getDirectoryPath(inputFilePath);
22+
const fileOutputPath = ts.combinePaths(inputDirectory, fileName);
2323
sys.writeFile(fileOutputPath, contents);
2424
}
2525

26-
var inputFilePath = sys.args[0].replace(/\\/g, "/");
27-
var inputStr = sys.readFile(inputFilePath);
26+
const inputFilePath = sys.args[0].replace(/\\/g, "/");
27+
const inputStr = sys.readFile(inputFilePath);
2828

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

3831
const diagnosticMessages: InputDiagnosticMessageTable = ts.createMapFromTemplate(diagnosticMessagesJson);
3932

40-
var infoFileOutput = buildInfoFileOutput(diagnosticMessages);
33+
const outputFilesDir = ts.getDirectoryPath(inputFilePath);
34+
const thisFilePathRel = ts.getRelativePathToDirectoryOrUrl(outputFilesDir, sys.getExecutingFilePath(),
35+
sys.getCurrentDirectory(), ts.createGetCanonicalFileName(sys.useCaseSensitiveFileNames), /* isAbsolutePathAnUrl */ false);
36+
37+
const infoFileOutput = buildInfoFileOutput(diagnosticMessages, "./diagnosticInformationMap.generated.ts", thisFilePathRel);
4138
checkForUniqueCodes(diagnosticMessages);
4239
writeFile("diagnosticInformationMap.generated.ts", infoFileOutput);
4340

44-
var messageOutput = buildDiagnosticMessageOutput(diagnosticMessages);
41+
const messageOutput = buildDiagnosticMessageOutput(diagnosticMessages);
4542
writeFile("diagnosticMessages.generated.json", messageOutput);
4643
}
4744

4845
function checkForUniqueCodes(diagnosticTable: InputDiagnosticMessageTable) {
4946
const allCodes: { [key: number]: true | undefined } = [];
5047
diagnosticTable.forEach(({ code }) => {
51-
if (allCodes[code])
48+
if (allCodes[code]) {
5249
throw new Error(`Diagnostic code ${code} appears more than once.`);
50+
}
5351
allCodes[code] = true;
5452
});
5553
}
5654

57-
function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable): string {
58-
var result =
59-
'// <auto-generated />\r\n' +
60-
'/// <reference path="types.ts" />\r\n' +
61-
'/* @internal */\r\n' +
62-
'namespace ts {\r\n' +
55+
function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, inputFilePathRel: string, thisFilePathRel: string): string {
56+
let result =
57+
"// <auto-generated />\r\n" +
58+
"// generated from '" + inputFilePathRel + "' by '" + thisFilePathRel + "'\r\n" +
59+
"/// <reference path=\"types.ts\" />\r\n" +
60+
"/* @internal */\r\n" +
61+
"namespace ts {\r\n" +
6362
" function diag(code: number, category: DiagnosticCategory, key: string, message: string): DiagnosticMessage {\r\n" +
6463
" return { code, category, key, message };\r\n" +
6564
" }\r\n" +
@@ -70,44 +69,44 @@ function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable): string
7069
result += ` ${propName}: diag(${code}, DiagnosticCategory.${category}, "${createKey(propName, code)}", ${JSON.stringify(name)}),\r\n`;
7170
});
7271

73-
result += ' };\r\n}';
72+
result += " };\r\n}";
7473

7574
return result;
7675
}
7776

7877
function buildDiagnosticMessageOutput(messageTable: InputDiagnosticMessageTable): string {
79-
let result = '{';
78+
let result = "{";
8079
messageTable.forEach(({ code }, name) => {
8180
const propName = convertPropertyName(name);
8281
result += `\r\n "${createKey(propName, code)}" : "${name.replace(/[\"]/g, '\\"')}",`;
8382
});
8483

8584
// Shave trailing comma, then add newline and ending brace
86-
result = result.slice(0, result.length - 1) + '\r\n}';
85+
result = result.slice(0, result.length - 1) + "\r\n}";
8786

8887
// Assert that we generated valid JSON
8988
JSON.parse(result);
9089

9190
return result;
9291
}
9392

94-
function createKey(name: string, code: number) : string {
95-
return name.slice(0, 100) + '_' + code;
93+
function createKey(name: string, code: number): string {
94+
return name.slice(0, 100) + "_" + code;
9695
}
9796

9897
function convertPropertyName(origName: string): string {
99-
var result = origName.split("").map(char => {
100-
if (char === '*') { return "_Asterisk"; }
101-
if (char === '/') { return "_Slash"; }
102-
if (char === ':') { return "_Colon"; }
98+
let result = origName.split("").map(char => {
99+
if (char === "*") { return "_Asterisk"; }
100+
if (char === "/") { return "_Slash"; }
101+
if (char === ":") { return "_Colon"; }
103102
return /\w/.test(char) ? char : "_";
104103
}).join("");
105104

106105
// get rid of all multi-underscores
107106
result = result.replace(/_+/g, "_");
108107

109108
// remove any leading underscore, unless it is followed by a number.
110-
result = result.replace(/^_([^\d])/, "$1")
109+
result = result.replace(/^_([^\d])/, "$1");
111110

112111
// get rid of all trailing underscores.
113112
result = result.replace(/_$/, "");

0 commit comments

Comments
 (0)