1
1
/// <reference path="../src/compiler/sys.ts" />
2
+ /// <reference path="../src/compiler/core.ts" />
2
3
3
4
interface DiagnosticDetails {
4
5
category : string ;
@@ -9,57 +10,55 @@ interface DiagnosticDetails {
9
10
type InputDiagnosticMessageTable = ts . Map < DiagnosticDetails > ;
10
11
11
12
function main ( ) : void {
12
- var sys = ts . sys ;
13
+ const sys = ts . sys ;
13
14
if ( sys . args . length < 1 ) {
14
- sys . write ( "Usage:" + sys . newLine )
15
+ sys . write ( "Usage:" + sys . newLine ) ;
15
16
sys . write ( "\tnode processDiagnosticMessages.js <diagnostic-json-input-file>" + sys . newLine ) ;
16
17
return ;
17
18
}
18
19
19
20
function writeFile ( fileName : string , contents : string ) {
20
- // TODO: Fix path joining
21
- var inputDirectory = inputFilePath . substr ( 0 , inputFilePath . lastIndexOf ( "/" ) ) ;
22
- var fileOutputPath = inputDirectory + "/" + fileName ;
21
+ const inputDirectory = ts . getDirectoryPath ( inputFilePath ) ;
22
+ const fileOutputPath = ts . combinePaths ( inputDirectory , fileName ) ;
23
23
sys . writeFile ( fileOutputPath , contents ) ;
24
24
}
25
25
26
- var inputFilePath = sys . args [ 0 ] . replace ( / \\ / g, "/" ) ;
27
- var inputStr = sys . readFile ( inputFilePath ) ;
26
+ const inputFilePath = sys . args [ 0 ] . replace ( / \\ / g, "/" ) ;
27
+ const inputStr = sys . readFile ( inputFilePath ) ;
28
28
29
- var diagnosticMessagesJson : { [ key : string ] : DiagnosticDetails } = JSON . parse ( inputStr ) ;
30
- // Check that there are no duplicates.
31
- const seenNames = ts . createMap < true > ( ) ;
32
- for ( const name of Object . keys ( diagnosticMessagesJson ) ) {
33
- if ( seenNames . has ( name ) )
34
- throw new Error ( `Name ${ name } appears twice` ) ;
35
- seenNames . set ( name , true ) ;
36
- }
29
+ const diagnosticMessagesJson : { [ key : string ] : DiagnosticDetails } = JSON . parse ( inputStr ) ;
37
30
38
31
const diagnosticMessages : InputDiagnosticMessageTable = ts . createMapFromTemplate ( diagnosticMessagesJson ) ;
39
32
40
- var infoFileOutput = buildInfoFileOutput ( diagnosticMessages ) ;
33
+ const outputFilesDir = ts . getDirectoryPath ( inputFilePath ) ;
34
+ const thisFilePathRel = ts . getRelativePathToDirectoryOrUrl ( outputFilesDir , sys . getExecutingFilePath ( ) ,
35
+ sys . getCurrentDirectory ( ) , ts . createGetCanonicalFileName ( sys . useCaseSensitiveFileNames ) , /* isAbsolutePathAnUrl */ false ) ;
36
+
37
+ const infoFileOutput = buildInfoFileOutput ( diagnosticMessages , "./diagnosticInformationMap.generated.ts" , thisFilePathRel ) ;
41
38
checkForUniqueCodes ( diagnosticMessages ) ;
42
39
writeFile ( "diagnosticInformationMap.generated.ts" , infoFileOutput ) ;
43
40
44
- var messageOutput = buildDiagnosticMessageOutput ( diagnosticMessages ) ;
41
+ const messageOutput = buildDiagnosticMessageOutput ( diagnosticMessages ) ;
45
42
writeFile ( "diagnosticMessages.generated.json" , messageOutput ) ;
46
43
}
47
44
48
45
function checkForUniqueCodes ( diagnosticTable : InputDiagnosticMessageTable ) {
49
46
const allCodes : { [ key : number ] : true | undefined } = [ ] ;
50
47
diagnosticTable . forEach ( ( { code } ) => {
51
- if ( allCodes [ code ] )
48
+ if ( allCodes [ code ] ) {
52
49
throw new Error ( `Diagnostic code ${ code } appears more than once.` ) ;
50
+ }
53
51
allCodes [ code ] = true ;
54
52
} ) ;
55
53
}
56
54
57
- function buildInfoFileOutput ( messageTable : InputDiagnosticMessageTable ) : string {
58
- var result =
59
- '// <auto-generated />\r\n' +
60
- '/// <reference path="types.ts" />\r\n' +
61
- '/* @internal */\r\n' +
62
- 'namespace ts {\r\n' +
55
+ function buildInfoFileOutput ( messageTable : InputDiagnosticMessageTable , inputFilePathRel : string , thisFilePathRel : string ) : string {
56
+ let result =
57
+ "// <auto-generated />\r\n" +
58
+ "// generated from '" + inputFilePathRel + "' by '" + thisFilePathRel + "'\r\n" +
59
+ "/// <reference path=\"types.ts\" />\r\n" +
60
+ "/* @internal */\r\n" +
61
+ "namespace ts {\r\n" +
63
62
" function diag(code: number, category: DiagnosticCategory, key: string, message: string): DiagnosticMessage {\r\n" +
64
63
" return { code, category, key, message };\r\n" +
65
64
" }\r\n" +
@@ -70,44 +69,44 @@ function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable): string
70
69
result += ` ${ propName } : diag(${ code } , DiagnosticCategory.${ category } , "${ createKey ( propName , code ) } ", ${ JSON . stringify ( name ) } ),\r\n` ;
71
70
} ) ;
72
71
73
- result += ' };\r\n}' ;
72
+ result += " };\r\n}" ;
74
73
75
74
return result ;
76
75
}
77
76
78
77
function buildDiagnosticMessageOutput ( messageTable : InputDiagnosticMessageTable ) : string {
79
- let result = '{' ;
78
+ let result = "{" ;
80
79
messageTable . forEach ( ( { code } , name ) => {
81
80
const propName = convertPropertyName ( name ) ;
82
81
result += `\r\n "${ createKey ( propName , code ) } " : "${ name . replace ( / [ \" ] / g, '\\"' ) } ",` ;
83
82
} ) ;
84
83
85
84
// Shave trailing comma, then add newline and ending brace
86
- result = result . slice ( 0 , result . length - 1 ) + ' \r\n}' ;
85
+ result = result . slice ( 0 , result . length - 1 ) + " \r\n}" ;
87
86
88
87
// Assert that we generated valid JSON
89
88
JSON . parse ( result ) ;
90
89
91
90
return result ;
92
91
}
93
92
94
- function createKey ( name : string , code : number ) : string {
95
- return name . slice ( 0 , 100 ) + '_' + code ;
93
+ function createKey ( name : string , code : number ) : string {
94
+ return name . slice ( 0 , 100 ) + "_" + code ;
96
95
}
97
96
98
97
function convertPropertyName ( origName : string ) : string {
99
- var result = origName . split ( "" ) . map ( char => {
100
- if ( char === '*' ) { return "_Asterisk" ; }
101
- if ( char === '/' ) { return "_Slash" ; }
102
- if ( char === ':' ) { return "_Colon" ; }
98
+ let result = origName . split ( "" ) . map ( char => {
99
+ if ( char === "*" ) { return "_Asterisk" ; }
100
+ if ( char === "/" ) { return "_Slash" ; }
101
+ if ( char === ":" ) { return "_Colon" ; }
103
102
return / \w / . test ( char ) ? char : "_" ;
104
103
} ) . join ( "" ) ;
105
104
106
105
// get rid of all multi-underscores
107
106
result = result . replace ( / _ + / g, "_" ) ;
108
107
109
108
// remove any leading underscore, unless it is followed by a number.
110
- result = result . replace ( / ^ _ ( [ ^ \d ] ) / , "$1" )
109
+ result = result . replace ( / ^ _ ( [ ^ \d ] ) / , "$1" ) ;
111
110
112
111
// get rid of all trailing underscores.
113
112
result = result . replace ( / _ $ / , "" ) ;
0 commit comments