Skip to content

Commit dababb1

Browse files
committed
Merge pull request #5273 from Microsoft/shortenLocMessages
Shorten loc messages
2 parents 60d8cf3 + 90cc848 commit dababb1

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

scripts/processDiagnosticMessages.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ function main(): void {
1818
return;
1919
}
2020

21+
function writeFile(fileName: string, contents: string) {
22+
// TODO: Fix path joining
23+
var inputDirectory = inputFilePath.substr(0,inputFilePath.lastIndexOf("/"));
24+
var fileOutputPath = inputDirectory + "/" + fileName;
25+
sys.writeFile(fileOutputPath, contents);
26+
}
27+
2128
var inputFilePath = sys.args[0].replace(/\\/g, "/");
2229
var inputStr = sys.readFile(inputFilePath);
2330

@@ -28,11 +35,10 @@ function main(): void {
2835

2936
var infoFileOutput = buildInfoFileOutput(diagnosticMessages, nameMap);
3037
checkForUniqueCodes(names, diagnosticMessages);
38+
writeFile("diagnosticInformationMap.generated.ts", infoFileOutput);
3139

32-
// TODO: Fix path joining
33-
var inputDirectory = inputFilePath.substr(0,inputFilePath.lastIndexOf("/"));
34-
var fileOutputPath = inputDirectory + "/diagnosticInformationMap.generated.ts";
35-
sys.writeFile(fileOutputPath, infoFileOutput);
40+
var messageOutput = buildDiagnosticMessageOutput(diagnosticMessages, nameMap);
41+
writeFile("diagnosticMessages.generated.json", messageOutput);
3642
}
3743

3844
function checkForUniqueCodes(messages: string[], diagnosticTable: InputDiagnosticMessageTable) {
@@ -85,12 +91,14 @@ function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, nameMap:
8591
for (var i = 0; i < names.length; i++) {
8692
var name = names[i];
8793
var diagnosticDetails = messageTable[name];
94+
var propName = convertPropertyName(nameMap[name]);
8895

8996
result +=
90-
' ' + convertPropertyName(nameMap[name]) +
97+
' ' + propName +
9198
': { code: ' + diagnosticDetails.code +
9299
', category: DiagnosticCategory.' + diagnosticDetails.category +
93-
', key: "' + name.replace(/[\"]/g, '\\"') + '"' +
100+
', key: "' + createKey(propName, diagnosticDetails.code) + '"' +
101+
', message: "' + name.replace(/[\"]/g, '\\"') + '"' +
94102
' },\r\n';
95103
}
96104

@@ -99,6 +107,30 @@ function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, nameMap:
99107
return result;
100108
}
101109

110+
function buildDiagnosticMessageOutput(messageTable: InputDiagnosticMessageTable, nameMap: ts.Map<string>): string {
111+
var result =
112+
'{';
113+
var names = Utilities.getObjectKeys(messageTable);
114+
for (var i = 0; i < names.length; i++) {
115+
var name = names[i];
116+
var diagnosticDetails = messageTable[name];
117+
var propName = convertPropertyName(nameMap[name]);
118+
119+
result += '\r\n "' + createKey(propName, diagnosticDetails.code) + '"' + ' : "' + name.replace(/[\"]/g, '\\"') + '"';
120+
if (i !== names.length - 1) {
121+
result += ',';
122+
}
123+
}
124+
125+
result += '\r\n}';
126+
127+
return result;
128+
}
129+
130+
function createKey(name: string, code: number) : string {
131+
return name.slice(0, 100) + '_' + code;
132+
}
133+
102134
function convertPropertyName(origName: string): string {
103135
var result = origName.split("").map(char => {
104136
if (char === '*') { return "_Asterisk"; }

src/compiler/core.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,10 @@ namespace ts {
365365

366366
export let localizedDiagnosticMessages: Map<string> = undefined;
367367

368-
export function getLocaleSpecificMessage(message: string) {
369-
return localizedDiagnosticMessages && localizedDiagnosticMessages[message]
370-
? localizedDiagnosticMessages[message]
371-
: message;
368+
export function getLocaleSpecificMessage(message: DiagnosticMessage) {
369+
return localizedDiagnosticMessages && localizedDiagnosticMessages[message.key]
370+
? localizedDiagnosticMessages[message.key]
371+
: message.message;
372372
}
373373

374374
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ...args: any[]): Diagnostic;
@@ -383,7 +383,7 @@ namespace ts {
383383
Debug.assert(end <= file.text.length, `end must be the bounds of the file. ${ end } > ${ file.text.length }`);
384384
}
385385

386-
let text = getLocaleSpecificMessage(message.key);
386+
let text = getLocaleSpecificMessage(message);
387387

388388
if (arguments.length > 4) {
389389
text = formatStringFromArgs(text, arguments, 4);
@@ -402,7 +402,7 @@ namespace ts {
402402

403403
export function createCompilerDiagnostic(message: DiagnosticMessage, ...args: any[]): Diagnostic;
404404
export function createCompilerDiagnostic(message: DiagnosticMessage): Diagnostic {
405-
let text = getLocaleSpecificMessage(message.key);
405+
let text = getLocaleSpecificMessage(message);
406406

407407
if (arguments.length > 1) {
408408
text = formatStringFromArgs(text, arguments, 1);
@@ -421,7 +421,7 @@ namespace ts {
421421

422422
export function chainDiagnosticMessages(details: DiagnosticMessageChain, message: DiagnosticMessage, ...args: any[]): DiagnosticMessageChain;
423423
export function chainDiagnosticMessages(details: DiagnosticMessageChain, message: DiagnosticMessage): DiagnosticMessageChain {
424-
let text = getLocaleSpecificMessage(message.key);
424+
let text = getLocaleSpecificMessage(message);
425425

426426
if (arguments.length > 2) {
427427
text = formatStringFromArgs(text, arguments, 2);

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,7 @@ namespace ts {
20132013
key: string;
20142014
category: DiagnosticCategory;
20152015
code: number;
2016+
message: string;
20162017
}
20172018

20182019
/**

src/services/services.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7360,7 +7360,7 @@ namespace ts {
73607360
let sourceFile = current.getSourceFile();
73617361
var canonicalName = getCanonicalFileName(ts.normalizePath(sourceFile.fileName));
73627362
if (sourceFile && getCanonicalFileName(ts.normalizePath(sourceFile.fileName)) === getCanonicalFileName(ts.normalizePath(defaultLibFileName))) {
7363-
return getRenameInfoError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library.key));
7363+
return getRenameInfoError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library));
73647364
}
73657365
}
73667366
}
@@ -7382,7 +7382,7 @@ namespace ts {
73827382
}
73837383
}
73847384

7385-
return getRenameInfoError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_this_element.key));
7385+
return getRenameInfoError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_this_element));
73867386

73877387
function getRenameInfoError(localizedErrorMessage: string): RenameInfo {
73887388
return {

0 commit comments

Comments
 (0)