Skip to content

Shorten loc messages #5273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions scripts/processDiagnosticMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ function main(): void {
return;
}

function writeFile(fileName: string, contents: string) {
// TODO: Fix path joining
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this is a TODO instead of fixing it in the PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it works ... not sure what @DanielRosenwasser Daniel meant here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var inputDirectory = inputFilePath.substr(0,inputFilePath.lastIndexOf("/"));
var fileOutputPath = inputDirectory + "/" + fileName;
sys.writeFile(fileOutputPath, contents);
}

var inputFilePath = sys.args[0].replace(/\\/g, "/");
var inputStr = sys.readFile(inputFilePath);

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

var infoFileOutput = buildInfoFileOutput(diagnosticMessages, nameMap);
checkForUniqueCodes(names, diagnosticMessages);
writeFile("diagnosticInformationMap.generated.ts", infoFileOutput);

// TODO: Fix path joining
var inputDirectory = inputFilePath.substr(0,inputFilePath.lastIndexOf("/"));
var fileOutputPath = inputDirectory + "/diagnosticInformationMap.generated.ts";
sys.writeFile(fileOutputPath, infoFileOutput);
var messageOutput = buildDiagnosticMessageOutput(diagnosticMessages, nameMap);
writeFile("diagnosticMessages.generated.json", messageOutput);
}

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

result +=
' ' + convertPropertyName(nameMap[name]) +
' ' + propName +
': { code: ' + diagnosticDetails.code +
', category: DiagnosticCategory.' + diagnosticDetails.category +
', key: "' + name.replace(/[\"]/g, '\\"') + '"' +
', key: "' + createKey(propName, diagnosticDetails.code) + '"' +
', message: "' + name.replace(/[\"]/g, '\\"') + '"' +
' },\r\n';
}

Expand All @@ -99,6 +107,30 @@ function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, nameMap:
return result;
}

function buildDiagnosticMessageOutput(messageTable: InputDiagnosticMessageTable, nameMap: ts.Map<string>): string {
var result =
'{';
var names = Utilities.getObjectKeys(messageTable);
for (var i = 0; i < names.length; i++) {
var name = names[i];
var diagnosticDetails = messageTable[name];
var propName = convertPropertyName(nameMap[name]);

result += '\r\n "' + createKey(propName, diagnosticDetails.code) + '"' + ' : "' + name.replace(/[\"]/g, '\\"') + '"';
if (i !== names.length - 1) {
result += ',';
}
}

result += '\r\n}';

return result;
}

function createKey(name: string, code: number) : string {
return name.slice(0, 100) + '_' + code;
}

function convertPropertyName(origName: string): string {
var result = origName.split("").map(char => {
if (char === '*') { return "_Asterisk"; }
Expand Down
14 changes: 7 additions & 7 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,10 @@ namespace ts {

export let localizedDiagnosticMessages: Map<string> = undefined;

export function getLocaleSpecificMessage(message: string) {
return localizedDiagnosticMessages && localizedDiagnosticMessages[message]
? localizedDiagnosticMessages[message]
: message;
export function getLocaleSpecificMessage(message: DiagnosticMessage) {
return localizedDiagnosticMessages && localizedDiagnosticMessages[message.key]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For an empty string key, you'll always use the non-localized message unless you use message.key in localizedDiagnosticMessages. Though, that would be the most ridiculous edge case I may have ever brought up.

? localizedDiagnosticMessages[message.key]
: message.message;
}

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

let text = getLocaleSpecificMessage(message.key);
let text = getLocaleSpecificMessage(message);

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

export function createCompilerDiagnostic(message: DiagnosticMessage, ...args: any[]): Diagnostic;
export function createCompilerDiagnostic(message: DiagnosticMessage): Diagnostic {
let text = getLocaleSpecificMessage(message.key);
let text = getLocaleSpecificMessage(message);

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

export function chainDiagnosticMessages(details: DiagnosticMessageChain, message: DiagnosticMessage, ...args: any[]): DiagnosticMessageChain;
export function chainDiagnosticMessages(details: DiagnosticMessageChain, message: DiagnosticMessage): DiagnosticMessageChain {
let text = getLocaleSpecificMessage(message.key);
let text = getLocaleSpecificMessage(message);

if (arguments.length > 2) {
text = formatStringFromArgs(text, arguments, 2);
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,7 @@ namespace ts {
key: string;
category: DiagnosticCategory;
code: number;
message: string;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7330,7 +7330,7 @@ namespace ts {
let sourceFile = current.getSourceFile();
var canonicalName = getCanonicalFileName(ts.normalizePath(sourceFile.fileName));
if (sourceFile && getCanonicalFileName(ts.normalizePath(sourceFile.fileName)) === getCanonicalFileName(ts.normalizePath(defaultLibFileName))) {
return getRenameInfoError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library.key));
return getRenameInfoError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library));
}
}
}
Expand All @@ -7352,7 +7352,7 @@ namespace ts {
}
}

return getRenameInfoError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_this_element.key));
return getRenameInfoError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_this_element));

function getRenameInfoError(localizedErrorMessage: string): RenameInfo {
return {
Expand Down