@@ -6,9 +6,7 @@ interface DiagnosticDetails {
6
6
isEarly ?: boolean ;
7
7
}
8
8
9
- interface InputDiagnosticMessageTable {
10
- [ msg : string ] : DiagnosticDetails ;
11
- }
9
+ type InputDiagnosticMessageTable = ts . Map < DiagnosticDetails > ;
12
10
13
11
function main ( ) : void {
14
12
var sys = ts . sys ;
@@ -28,50 +26,32 @@ function main(): void {
28
26
var inputFilePath = sys . args [ 0 ] . replace ( / \\ / g, "/" ) ;
29
27
var inputStr = sys . readFile ( inputFilePath ) ;
30
28
31
- var diagnosticMessages : InputDiagnosticMessageTable = JSON . parse ( inputStr ) ;
32
-
33
- var names = Object . keys ( diagnosticMessages ) ;
29
+ var diagnosticMessagesJson : { [ key : string ] : DiagnosticDetails } = JSON . parse ( inputStr ) ;
34
30
// Check that there are no duplicates.
35
31
const seenNames = ts . createMap < true > ( ) ;
36
- for ( const name of names ) {
32
+ for ( const name of Object . keys ( diagnosticMessagesJson ) ) {
37
33
if ( seenNames . has ( name ) )
38
34
throw new Error ( `Name ${ name } appears twice` ) ;
39
35
seenNames . set ( name , true ) ;
40
36
}
41
37
38
+ const diagnosticMessages : InputDiagnosticMessageTable = ts . createMapFromTemplate ( diagnosticMessagesJson ) ;
39
+
42
40
var infoFileOutput = buildInfoFileOutput ( diagnosticMessages ) ;
43
- checkForUniqueCodes ( names , diagnosticMessages ) ;
41
+ checkForUniqueCodes ( diagnosticMessages ) ;
44
42
writeFile ( "diagnosticInformationMap.generated.ts" , infoFileOutput ) ;
45
43
46
44
var messageOutput = buildDiagnosticMessageOutput ( diagnosticMessages ) ;
47
45
writeFile ( "diagnosticMessages.generated.json" , messageOutput ) ;
48
46
}
49
47
50
- function checkForUniqueCodes ( messages : string [ ] , diagnosticTable : InputDiagnosticMessageTable ) {
51
- const originalMessageForCode : string [ ] = [ ] ;
52
- let numConflicts = 0 ;
53
-
54
- for ( const currentMessage of messages ) {
55
- const code = diagnosticTable [ currentMessage ] . code ;
56
-
57
- if ( code in originalMessageForCode ) {
58
- const originalMessage = originalMessageForCode [ code ] ;
59
- ts . sys . write ( "\x1b[91m" ) ; // High intensity red.
60
- ts . sys . write ( "Error" ) ;
61
- ts . sys . write ( "\x1b[0m" ) ; // Reset formatting.
62
- ts . sys . write ( `: Diagnostic code '${ code } ' conflicts between "${ originalMessage } " and "${ currentMessage } ".` ) ;
63
- ts . sys . write ( ts . sys . newLine + ts . sys . newLine ) ;
64
-
65
- numConflicts ++ ;
66
- }
67
- else {
68
- originalMessageForCode [ code ] = currentMessage ;
69
- }
70
- }
71
-
72
- if ( numConflicts > 0 ) {
73
- throw new Error ( `Found ${ numConflicts } conflict(s) in diagnostic codes.` ) ;
74
- }
48
+ function checkForUniqueCodes ( diagnosticTable : InputDiagnosticMessageTable ) {
49
+ const allCodes : { [ key : number ] : true | undefined } = [ ] ;
50
+ diagnosticTable . forEach ( ( { code } ) => {
51
+ if ( allCodes [ code ] )
52
+ throw new Error ( `Diagnostic code ${ code } appears more than once.` ) ;
53
+ allCodes [ code ] = true ;
54
+ } ) ;
75
55
}
76
56
77
57
function buildInfoFileOutput ( messageTable : InputDiagnosticMessageTable ) : string {
@@ -80,39 +60,34 @@ function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable): string
80
60
'/// <reference path="types.ts" />\r\n' +
81
61
'/* @internal */\r\n' +
82
62
'namespace ts {\r\n' +
63
+ " function diag(code: number, category: DiagnosticCategory, key: string, message: string): DiagnosticMessage {\r\n" +
64
+ " return { code, category, key, message };\r\n" +
65
+ " }\r\n" +
83
66
' export const Diagnostics = {\r\n' ;
84
- for ( const name of Object . keys ( messageTable ) ) {
85
- var diagnosticDetails = messageTable [ name ] ;
86
- var propName = convertPropertyName ( name ) ;
87
-
88
- result +=
89
- ' ' + propName +
90
- ': { code: ' + diagnosticDetails . code +
91
- ', category: DiagnosticCategory.' + diagnosticDetails . category +
92
- ', key: "' + createKey ( propName , diagnosticDetails . code ) + '"' +
93
- ', message: "' + name . replace ( / [ \" ] / g, '\\"' ) + '"' +
94
- ' },\r\n' ;
95
- }
67
+ messageTable . forEach ( ( { code, category } , name ) => {
68
+ const propName = convertPropertyName ( name ) ;
69
+ result += ` ${ propName } : diag(${ code } , DiagnosticCategory.${ category } , "${ createKey ( propName , code ) } ", ${ JSON . stringify ( name ) } ),\r\n` ;
70
+ } ) ;
96
71
97
72
result += ' };\r\n}' ;
98
73
99
74
return result ;
100
75
}
101
76
102
77
function buildDiagnosticMessageOutput ( messageTable : InputDiagnosticMessageTable ) : string {
103
- var result =
104
- '{' ;
105
- var names = Object . keys ( messageTable ) ;
106
- for ( var i = 0 ; i < names . length ; i ++ ) {
107
- var name = names [ i ] ;
108
- var diagnosticDetails = messageTable [ name ] ;
109
- var propName = convertPropertyName ( name ) ;
110
-
111
- result += '\r\n "' + createKey ( propName , diagnosticDetails . code ) + '"' + ' : "' + name . replace ( / [ \" ] / g, '\\"' ) + '"' ;
112
- if ( i !== names . length - 1 ) {
78
+ let result = '{' ;
79
+ let first = true ;
80
+ messageTable . forEach ( ( { code } , name ) => {
81
+ if ( ! first ) {
82
+ first = false ;
83
+ }
84
+ else {
113
85
result += ',' ;
114
86
}
115
- }
87
+
88
+ const propName = convertPropertyName ( name ) ;
89
+ result += `\r\n "${ createKey ( propName , code ) } ": "${ name . replace ( / [ \" ] / g, '\\"' ) } "` ;
90
+ } ) ;
116
91
117
92
result += '\r\n}' ;
118
93
@@ -131,7 +106,6 @@ function convertPropertyName(origName: string): string {
131
106
return / \w / . test ( char ) ? char : "_" ;
132
107
} ) . join ( "" ) ;
133
108
134
-
135
109
// get rid of all multi-underscores
136
110
result = result . replace ( / _ + / g, "_" ) ;
137
111
0 commit comments