2014-07-13 01:04:16 +02:00
|
|
|
/// <reference path="../src/compiler/sys.ts" />
|
|
|
|
|
|
|
|
interface DiagnosticDetails {
|
|
|
|
category: string;
|
|
|
|
code: number;
|
2014-10-21 02:38:50 +02:00
|
|
|
isEarly?: boolean;
|
2014-07-13 01:04:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface InputDiagnosticMessageTable {
|
|
|
|
[msg: string]: DiagnosticDetails;
|
|
|
|
}
|
|
|
|
|
|
|
|
function main(): void {
|
2014-12-11 23:46:54 +01:00
|
|
|
var sys = ts.sys;
|
2014-07-13 01:04:16 +02:00
|
|
|
if (sys.args.length < 1) {
|
|
|
|
sys.write("Usage:" + sys.newLine)
|
|
|
|
sys.write("\tnode processDiagnosticMessages.js <diagnostic-json-input-file>" + sys.newLine);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-15 20:22:22 +02:00
|
|
|
function writeFile(fileName: string, contents: string) {
|
|
|
|
// TODO: Fix path joining
|
|
|
|
var inputDirectory = inputFilePath.substr(0,inputFilePath.lastIndexOf("/"));
|
|
|
|
var fileOutputPath = inputDirectory + "/" + fileName;
|
|
|
|
sys.writeFile(fileOutputPath, contents);
|
|
|
|
}
|
|
|
|
|
2014-07-13 01:04:16 +02:00
|
|
|
var inputFilePath = sys.args[0].replace(/\\/g, "/");
|
|
|
|
var inputStr = sys.readFile(inputFilePath);
|
|
|
|
|
2015-09-20 21:14:56 +02:00
|
|
|
var diagnosticMessages: InputDiagnosticMessageTable = JSON.parse(inputStr);
|
2014-07-13 01:04:16 +02:00
|
|
|
|
2015-09-20 21:14:56 +02:00
|
|
|
var names = Utilities.getObjectKeys(diagnosticMessages);
|
2014-07-13 01:04:16 +02:00
|
|
|
var nameMap = buildUniqueNameMap(names);
|
|
|
|
|
2015-09-20 21:14:56 +02:00
|
|
|
var infoFileOutput = buildInfoFileOutput(diagnosticMessages, nameMap);
|
|
|
|
checkForUniqueCodes(names, diagnosticMessages);
|
2015-10-15 20:22:22 +02:00
|
|
|
writeFile("diagnosticInformationMap.generated.ts", infoFileOutput);
|
2015-09-20 21:14:56 +02:00
|
|
|
|
2015-10-15 20:22:22 +02:00
|
|
|
var messageOutput = buildDiagnosticMessageOutput(diagnosticMessages, nameMap);
|
|
|
|
writeFile("diagnosticMessages.generated.json", messageOutput);
|
2014-07-13 01:04:16 +02:00
|
|
|
}
|
|
|
|
|
2015-09-20 21:14:56 +02:00
|
|
|
function checkForUniqueCodes(messages: string[], diagnosticTable: InputDiagnosticMessageTable) {
|
|
|
|
const originalMessageForCode: string[] = [];
|
2015-09-20 21:41:28 +02:00
|
|
|
let numConflicts = 0;
|
2015-09-20 21:14:56 +02:00
|
|
|
|
|
|
|
for (const currentMessage of messages) {
|
|
|
|
const code = diagnosticTable[currentMessage].code;
|
|
|
|
|
|
|
|
if (code in originalMessageForCode) {
|
|
|
|
const originalMessage = originalMessageForCode[code];
|
2015-09-20 21:41:28 +02:00
|
|
|
ts.sys.write("\x1b[91m"); // High intensity red.
|
|
|
|
ts.sys.write("Error");
|
2015-09-20 21:14:56 +02:00
|
|
|
ts.sys.write("\x1b[0m"); // Reset formatting.
|
|
|
|
ts.sys.write(`: Diagnostic code '${code}' conflicts between "${originalMessage}" and "${currentMessage}".`);
|
|
|
|
ts.sys.write(ts.sys.newLine + ts.sys.newLine);
|
2015-09-20 21:41:28 +02:00
|
|
|
|
|
|
|
numConflicts++;
|
2015-09-20 21:14:56 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
originalMessageForCode[code] = currentMessage;
|
|
|
|
}
|
|
|
|
}
|
2015-09-20 21:41:28 +02:00
|
|
|
|
|
|
|
if (numConflicts > 0) {
|
|
|
|
throw new Error(`Found ${numConflicts} conflict(s) in diagnostic codes.`);
|
|
|
|
}
|
2015-09-20 21:14:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function buildUniqueNameMap(names: string[]): ts.Map<string> {
|
|
|
|
var nameMap: ts.Map<string> = {};
|
2014-07-13 01:04:16 +02:00
|
|
|
|
2014-08-05 02:52:58 +02:00
|
|
|
var uniqueNames = NameGenerator.ensureUniqueness(names, /* isCaseSensitive */ false, /* isFixed */ undefined);
|
2014-07-13 01:04:16 +02:00
|
|
|
|
|
|
|
for (var i = 0; i < names.length; i++) {
|
|
|
|
nameMap[names[i]] = uniqueNames[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return nameMap;
|
|
|
|
}
|
|
|
|
|
2015-09-20 21:14:56 +02:00
|
|
|
function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, nameMap: ts.Map<string>): string {
|
2014-07-13 01:04:16 +02:00
|
|
|
var result =
|
|
|
|
'// <auto-generated />\r\n' +
|
|
|
|
'/// <reference path="types.ts" />\r\n' +
|
2015-04-14 00:55:56 +02:00
|
|
|
'/* @internal */\r\n' +
|
2015-06-12 18:01:48 +02:00
|
|
|
'namespace ts {\r\n' +
|
2014-07-13 01:04:16 +02:00
|
|
|
' export var Diagnostics = {\r\n';
|
|
|
|
var names = Utilities.getObjectKeys(messageTable);
|
|
|
|
for (var i = 0; i < names.length; i++) {
|
|
|
|
var name = names[i];
|
|
|
|
var diagnosticDetails = messageTable[name];
|
2015-10-15 20:22:22 +02:00
|
|
|
var propName = convertPropertyName(nameMap[name]);
|
2014-07-13 01:04:16 +02:00
|
|
|
|
|
|
|
result +=
|
2015-10-15 20:22:22 +02:00
|
|
|
' ' + propName +
|
2014-07-13 01:04:16 +02:00
|
|
|
': { code: ' + diagnosticDetails.code +
|
|
|
|
', category: DiagnosticCategory.' + diagnosticDetails.category +
|
2015-10-15 20:22:22 +02:00
|
|
|
', key: "' + createKey(propName, diagnosticDetails.code) + '"' +
|
|
|
|
', message: "' + name.replace(/[\"]/g, '\\"') + '"' +
|
2014-10-21 02:38:50 +02:00
|
|
|
' },\r\n';
|
2014-07-13 01:04:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result += ' };\r\n}';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-15 20:22:22 +02:00
|
|
|
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]);
|
|
|
|
|
2015-10-15 20:46:50 +02:00
|
|
|
result += '\r\n "' + createKey(propName, diagnosticDetails.code) + '"' + ' : "' + name.replace(/[\"]/g, '\\"') + '"';
|
2015-10-15 20:22:22 +02:00
|
|
|
if (i !== names.length - 1) {
|
|
|
|
result += ',';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 20:46:50 +02:00
|
|
|
result += '\r\n}';
|
2015-10-15 20:22:22 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createKey(name: string, code: number) : string {
|
|
|
|
return name.slice(0, 100) + '_' + code;
|
|
|
|
}
|
|
|
|
|
2014-07-13 01:04:16 +02:00
|
|
|
function convertPropertyName(origName: string): string {
|
|
|
|
var result = origName.split("").map(char => {
|
|
|
|
if (char === '*') { return "_Asterisk"; }
|
|
|
|
if (char === '/') { return "_Slash"; }
|
|
|
|
if (char === ':') { return "_Colon"; }
|
|
|
|
return /\w/.test(char) ? char : "_";
|
|
|
|
}).join("");
|
|
|
|
|
|
|
|
|
|
|
|
// get rid of all multi-underscores
|
|
|
|
result = result.replace(/_+/g, "_");
|
|
|
|
|
|
|
|
// remove any leading underscore, unless it is followed by a number.
|
|
|
|
result = result.replace(/^_([^\d])/, "$1")
|
|
|
|
|
|
|
|
// get rid of all trailing underscores.
|
|
|
|
result = result.replace(/_$/, "");
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
module NameGenerator {
|
2014-08-05 02:52:58 +02:00
|
|
|
export function ensureUniqueness(names: string[], isCaseSensitive: boolean, isFixed?: boolean[]): string[]{
|
|
|
|
if (!isFixed) {
|
|
|
|
isFixed = names.map(() => false)
|
|
|
|
}
|
2014-07-13 01:04:16 +02:00
|
|
|
|
2014-08-05 02:52:58 +02:00
|
|
|
var names = names.slice();
|
|
|
|
ensureUniquenessInPlace(names, isCaseSensitive, isFixed);
|
2014-07-13 01:04:16 +02:00
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2014-08-05 02:52:58 +02:00
|
|
|
function ensureUniquenessInPlace(names: string[], isCaseSensitive: boolean, isFixed: boolean[]): void {
|
2014-07-13 01:04:16 +02:00
|
|
|
for (var i = 0; i < names.length; i++) {
|
|
|
|
var name = names[i];
|
|
|
|
var collisionIndices = Utilities.collectMatchingIndices(name, names, isCaseSensitive);
|
|
|
|
|
|
|
|
// We will always have one "collision" because getCollisionIndices returns the index of name itself as well;
|
|
|
|
// so if we only have one collision, then there are no issues.
|
|
|
|
if (collisionIndices.length < 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCollisions(name, names, isFixed, collisionIndices, isCaseSensitive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleCollisions(name: string, proposedNames: string[], isFixed: boolean[], collisionIndices: number[], isCaseSensitive: boolean): void {
|
|
|
|
var suffix = 1;
|
|
|
|
|
|
|
|
for (var i = 0; i < collisionIndices.length; i++) {
|
|
|
|
var collisionIndex = collisionIndices[i];
|
|
|
|
|
|
|
|
if (isFixed[collisionIndex]) {
|
|
|
|
// can't do anything about this name.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
2014-08-05 02:52:58 +02:00
|
|
|
var newName = name + suffix;
|
|
|
|
suffix++;
|
2014-07-13 01:04:16 +02:00
|
|
|
|
2014-08-05 02:52:58 +02:00
|
|
|
// Check if we've synthesized a unique name, and if so
|
|
|
|
// replace the conflicting name with the new one.
|
|
|
|
if (!proposedNames.some(name => Utilities.stringEquals(name, newName, isCaseSensitive))) {
|
2014-07-13 01:04:16 +02:00
|
|
|
proposedNames[collisionIndex] = newName;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module Utilities {
|
|
|
|
/// Return a list of all indices where a string occurs.
|
2014-08-05 02:52:58 +02:00
|
|
|
export function collectMatchingIndices(name: string, proposedNames: string[], isCaseSensitive: boolean): number[] {
|
2014-07-13 01:04:16 +02:00
|
|
|
var matchingIndices: number[] = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < proposedNames.length; i++) {
|
|
|
|
if (stringEquals(name, proposedNames[i], isCaseSensitive)) {
|
|
|
|
matchingIndices.push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return matchingIndices;
|
|
|
|
}
|
|
|
|
|
2014-08-05 02:52:58 +02:00
|
|
|
export function stringEquals(s1: string, s2: string, caseSensitive: boolean): boolean {
|
2014-08-01 02:20:57 +02:00
|
|
|
if (caseSensitive) {
|
2014-07-13 01:04:16 +02:00
|
|
|
s1 = s1.toLowerCase();
|
|
|
|
s2 = s2.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
return s1 == s2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Like Object.keys
|
2015-09-20 21:14:56 +02:00
|
|
|
export function getObjectKeys(obj: any): string[] {
|
2014-07-13 01:04:16 +02:00
|
|
|
var result: string[] = [];
|
|
|
|
|
|
|
|
for (var name in obj) {
|
|
|
|
if (obj.hasOwnProperty(name)) {
|
|
|
|
result.push(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|