forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocessDiagnosticMessages.ts
179 lines (137 loc) · 5.66 KB
/
processDiagnosticMessages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/// <reference path="../src/compiler/sys.ts" />
interface DiagnosticDetails {
category: string;
code: number;
}
interface InputDiagnosticMessageTable {
[msg: string]: DiagnosticDetails;
}
interface IIndexable<V> {
[key: string]: V;
}
function main(): void {
if (sys.args.length < 1) {
sys.write("Usage:" + sys.newLine)
sys.write("\tnode processDiagnosticMessages.js <diagnostic-json-input-file>" + sys.newLine);
return;
}
var inputFilePath = sys.args[0].replace(/\\/g, "/");
var inputStr = sys.readFile(inputFilePath);
var diagnosticMesages: InputDiagnosticMessageTable = JSON.parse(inputStr);
var names = Utilities.getObjectKeys(diagnosticMesages);
var nameMap = buildUniqueNameMap(names);
var infoFileOutput = buildInfoFileOutput(diagnosticMesages, nameMap);
// TODO: Fix path joining
var inputDirectory = inputFilePath.substr(0,inputFilePath.lastIndexOf("/"));
var fileOutputPath = inputDirectory + "/diagnosticInformationMap.generated.ts";
sys.writeFile(fileOutputPath, infoFileOutput);
}
function buildUniqueNameMap(names: string[]): IIndexable<string> {
var nameMap: IIndexable<string> = {};
var uniqueNames = NameGenerator.ensureUniqueness(names, /*isFixed */ undefined, /* isCaseSensitive */ false);
for (var i = 0; i < names.length; i++) {
nameMap[names[i]] = uniqueNames[i];
}
return nameMap;
}
function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, nameMap: IIndexable<string>): string {
var result =
'// <auto-generated />\r\n' +
'/// <reference path="types.ts" />\r\n' +
'module ts {\r\n' +
' 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];
result +=
' ' + convertPropertyName(nameMap[name]) +
': { code: ' + diagnosticDetails.code +
', category: DiagnosticCategory.' + diagnosticDetails.category +
', key: "' + name.replace('"', '\\"') +
'" },\r\n';
}
result += ' };\r\n}';
return result;
}
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 {
export function ensureUniqueness(names: string[], isFixed: boolean[] = names.map(() => false), isCaseSensitive: boolean = false): string[] {
var names = names.map(x => x);
ensureUniquenessInPlace(names, isFixed, isCaseSensitive);
return names;
}
function ensureUniquenessInPlace(names: string[], isFixed: boolean[], isCaseSensitive: boolean): void {
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) {
var newName = name + suffix++;
if (!proposedNames.some((name) => Utilities.stringEquals(name, newName, isCaseSensitive))) {
proposedNames[collisionIndex] = newName;
break;
}
}
}
}
}
module Utilities {
/// Return a list of all indices where a string occurs.
export function collectMatchingIndices(name: string, proposedNames: string[], isCaseSensitive: boolean = false): number[] {
var matchingIndices: number[] = [];
for (var i = 0; i < proposedNames.length; i++) {
if (stringEquals(name, proposedNames[i], isCaseSensitive)) {
matchingIndices.push(i);
}
}
return matchingIndices;
}
export function stringEquals(s1: string, s2: string, caseSensitive: boolean = false): boolean {
if (caseSensitive) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
}
return s1 == s2;
}
// Like Object.keys
export function getObjectKeys(obj: any): string[]{
var result: string[] = [];
for (var name in obj) {
if (obj.hasOwnProperty(name)) {
result.push(name);
}
}
return result;
}
}
main();