@@ -30,14 +30,20 @@ function main(): void {
30
30
31
31
var diagnosticMessages : InputDiagnosticMessageTable = JSON . parse ( inputStr ) ;
32
32
33
- var names = Utilities . getObjectKeys ( diagnosticMessages ) ;
34
- var nameMap = buildUniqueNameMap ( names ) ;
33
+ var names = Object . keys ( diagnosticMessages ) ;
34
+ // Check that there are no duplicates.
35
+ const seenNames = ts . createMap < true > ( ) ;
36
+ for ( const name of names ) {
37
+ if ( seenNames . has ( name ) )
38
+ throw new Error ( `Name ${ name } appears twice` ) ;
39
+ seenNames . set ( name , true ) ;
40
+ }
35
41
36
- var infoFileOutput = buildInfoFileOutput ( diagnosticMessages , nameMap ) ;
42
+ var infoFileOutput = buildInfoFileOutput ( diagnosticMessages ) ;
37
43
checkForUniqueCodes ( names , diagnosticMessages ) ;
38
44
writeFile ( "diagnosticInformationMap.generated.ts" , infoFileOutput ) ;
39
45
40
- var messageOutput = buildDiagnosticMessageOutput ( diagnosticMessages , nameMap ) ;
46
+ var messageOutput = buildDiagnosticMessageOutput ( diagnosticMessages ) ;
41
47
writeFile ( "diagnosticMessages.generated.json" , messageOutput ) ;
42
48
}
43
49
@@ -68,30 +74,16 @@ function checkForUniqueCodes(messages: string[], diagnosticTable: InputDiagnosti
68
74
}
69
75
}
70
76
71
- function buildUniqueNameMap ( names : string [ ] ) : ts . Map < string > {
72
- var nameMap = ts . createMap < string > ( ) ;
73
-
74
- var uniqueNames = NameGenerator . ensureUniqueness ( names , /* isCaseSensitive */ false , /* isFixed */ undefined ) ;
75
-
76
- for ( var i = 0 ; i < names . length ; i ++ ) {
77
- nameMap . set ( names [ i ] , uniqueNames [ i ] ) ;
78
- }
79
-
80
- return nameMap ;
81
- }
82
-
83
- function buildInfoFileOutput ( messageTable : InputDiagnosticMessageTable , nameMap : ts . Map < string > ) : string {
77
+ function buildInfoFileOutput ( messageTable : InputDiagnosticMessageTable ) : string {
84
78
var result =
85
79
'// <auto-generated />\r\n' +
86
80
'/// <reference path="types.ts" />\r\n' +
87
81
'/* @internal */\r\n' +
88
82
'namespace ts {\r\n' +
89
83
' export const Diagnostics = {\r\n' ;
90
- var names = Utilities . getObjectKeys ( messageTable ) ;
91
- for ( var i = 0 ; i < names . length ; i ++ ) {
92
- var name = names [ i ] ;
84
+ for ( const name of Object . keys ( messageTable ) ) {
93
85
var diagnosticDetails = messageTable [ name ] ;
94
- var propName = convertPropertyName ( nameMap . get ( name ) ) ;
86
+ var propName = convertPropertyName ( name ) ;
95
87
96
88
result +=
97
89
' ' + propName +
@@ -107,14 +99,14 @@ function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, nameMap:
107
99
return result ;
108
100
}
109
101
110
- function buildDiagnosticMessageOutput ( messageTable : InputDiagnosticMessageTable , nameMap : ts . Map < string > ) : string {
102
+ function buildDiagnosticMessageOutput ( messageTable : InputDiagnosticMessageTable ) : string {
111
103
var result =
112
104
'{' ;
113
- var names = Utilities . getObjectKeys ( messageTable ) ;
105
+ var names = Object . keys ( messageTable ) ;
114
106
for ( var i = 0 ; i < names . length ; i ++ ) {
115
107
var name = names [ i ] ;
116
108
var diagnosticDetails = messageTable [ name ] ;
117
- var propName = convertPropertyName ( nameMap . get ( name ) ) ;
109
+ var propName = convertPropertyName ( name ) ;
118
110
119
111
result += '\r\n "' + createKey ( propName , diagnosticDetails . code ) + '"' + ' : "' + name . replace ( / [ \" ] / g, '\\"' ) + '"' ;
120
112
if ( i !== names . length - 1 ) {
@@ -152,93 +144,4 @@ function convertPropertyName(origName: string): string {
152
144
return result ;
153
145
}
154
146
155
- module NameGenerator {
156
- export function ensureUniqueness ( names : string [ ] , isCaseSensitive : boolean , isFixed ?: boolean [ ] ) : string [ ] {
157
- if ( ! isFixed ) {
158
- isFixed = names . map ( ( ) => false )
159
- }
160
-
161
- var names = names . slice ( ) ;
162
- ensureUniquenessInPlace ( names , isCaseSensitive , isFixed ) ;
163
- return names ;
164
- }
165
-
166
- function ensureUniquenessInPlace ( names : string [ ] , isCaseSensitive : boolean , isFixed : boolean [ ] ) : void {
167
- for ( var i = 0 ; i < names . length ; i ++ ) {
168
- var name = names [ i ] ;
169
- var collisionIndices = Utilities . collectMatchingIndices ( name , names , isCaseSensitive ) ;
170
-
171
- // We will always have one "collision" because getCollisionIndices returns the index of name itself as well;
172
- // so if we only have one collision, then there are no issues.
173
- if ( collisionIndices . length < 2 ) {
174
- continue ;
175
- }
176
-
177
- handleCollisions ( name , names , isFixed , collisionIndices , isCaseSensitive ) ;
178
- }
179
- }
180
-
181
- function handleCollisions ( name : string , proposedNames : string [ ] , isFixed : boolean [ ] , collisionIndices : number [ ] , isCaseSensitive : boolean ) : void {
182
- var suffix = 1 ;
183
-
184
- for ( var i = 0 ; i < collisionIndices . length ; i ++ ) {
185
- var collisionIndex = collisionIndices [ i ] ;
186
-
187
- if ( isFixed [ collisionIndex ] ) {
188
- // can't do anything about this name.
189
- continue ;
190
- }
191
-
192
- while ( true ) {
193
- var newName = name + suffix ;
194
- suffix ++ ;
195
-
196
- // Check if we've synthesized a unique name, and if so
197
- // replace the conflicting name with the new one.
198
- if ( ! proposedNames . some ( name => Utilities . stringEquals ( name , newName , isCaseSensitive ) ) ) {
199
- proposedNames [ collisionIndex ] = newName ;
200
- break ;
201
- }
202
- }
203
- }
204
- }
205
- }
206
-
207
- module Utilities {
208
- /// Return a list of all indices where a string occurs.
209
- export function collectMatchingIndices ( name : string , proposedNames : string [ ] , isCaseSensitive : boolean ) : number [ ] {
210
- var matchingIndices : number [ ] = [ ] ;
211
-
212
- for ( var i = 0 ; i < proposedNames . length ; i ++ ) {
213
- if ( stringEquals ( name , proposedNames [ i ] , isCaseSensitive ) ) {
214
- matchingIndices . push ( i ) ;
215
- }
216
- }
217
-
218
- return matchingIndices ;
219
- }
220
-
221
- export function stringEquals ( s1 : string , s2 : string , caseSensitive : boolean ) : boolean {
222
- if ( caseSensitive ) {
223
- s1 = s1 . toLowerCase ( ) ;
224
- s2 = s2 . toLowerCase ( ) ;
225
- }
226
-
227
- return s1 == s2 ;
228
- }
229
-
230
- // Like Object.keys
231
- export function getObjectKeys ( obj : any ) : string [ ] {
232
- var result : string [ ] = [ ] ;
233
-
234
- for ( var name in obj ) {
235
- if ( obj . hasOwnProperty ( name ) ) {
236
- result . push ( name ) ;
237
- }
238
- }
239
-
240
- return result ;
241
- }
242
- }
243
-
244
147
main ( ) ;
0 commit comments