5
5
/// <reference path='..\compiler\commandLineParser.ts'/>
6
6
7
7
module RWC {
8
- class RWCEmitter implements Harness . Compiler . IEmitterIOHost {
9
- public outputs : { [ filename : string ] : string ; } = { } ;
10
-
11
- constructor ( ) { }
12
-
13
- writeFile ( path : string , contents : string , writeByteOrderMark : boolean ) {
14
- if ( path in this . outputs ) throw new Error ( 'Emitter attempted to write to "' + path + '" twice' ) ;
15
- this . outputs [ path ] = contents ;
16
- }
17
-
18
- directoryExists ( s : string ) {
19
- return false ;
20
- }
21
- fileExists ( s : string ) {
22
- return true ;
23
- }
24
- resolvePath ( s : string ) {
25
- return s ;
26
- }
27
- }
28
-
29
8
function runWithIOLog ( ioLog : IOLog , fn : ( ) => void ) {
30
9
var oldSys = sys ;
31
10
@@ -41,32 +20,26 @@ module RWC {
41
20
}
42
21
}
43
22
44
- function collateOutputs ( emitterIOHost : RWCEmitter , fnTest : ( s : string ) => { } , clean ?: ( s : string ) => string ) {
23
+ function collateOutputs ( outputFiles : Harness . Compiler . GeneratedFile [ ] , clean ?: ( s : string ) => string ) {
45
24
// Collect, test, and sort the filenames
46
- var files : string [ ] = [ ] ;
47
- for ( var fn in emitterIOHost . outputs ) {
48
- if ( emitterIOHost . outputs . hasOwnProperty ( fn ) && fnTest ( fn ) ) {
49
- files . push ( fn ) ;
50
- }
51
- }
52
25
function cleanName ( fn : string ) {
53
26
var lastSlash = Harness . Path . switchToForwardSlashes ( fn ) . lastIndexOf ( '/' ) ;
54
27
return fn . substr ( lastSlash + 1 ) . toLowerCase ( ) ;
55
28
}
56
- files . sort ( ( a , b ) => cleanName ( a ) . localeCompare ( cleanName ( b ) ) ) ;
29
+ outputFiles . sort ( ( a , b ) => cleanName ( a . fileName ) . localeCompare ( cleanName ( b . fileName ) ) ) ;
57
30
58
31
// Emit them
59
32
var result = '' ;
60
- files . forEach ( fn => {
33
+ ts . forEach ( outputFiles , outputFile => {
61
34
// Some extra spacing if this isn't the first file
62
35
if ( result . length ) result = result + '\r\n\r\n' ;
63
36
64
37
// Filename header + content
65
- result = result + '/*====== ' + fn + ' ======*/\r\n' ;
38
+ result = result + '/*====== ' + outputFile . fileName + ' ======*/\r\n' ;
66
39
if ( clean ) {
67
- result = result + clean ( emitterIOHost . outputs [ fn ] ) ;
40
+ result = result + clean ( outputFile . code ) ;
68
41
} else {
69
- result = result + emitterIOHost . outputs [ fn ] ;
42
+ result = result + outputFile . code ;
70
43
}
71
44
} ) ;
72
45
return result ;
@@ -86,90 +59,54 @@ module RWC {
86
59
} ) ;
87
60
} ) ;
88
61
89
- var emitterIOHost = new RWCEmitter ( ) ;
62
+ var inputFiles : { unitName : string ; content : string ; } [ ] = [ ] ;
63
+ var otherFiles : { unitName : string ; content : string ; } [ ] = [ ] ;
64
+ var compilerResult : Harness . Compiler . CompilerResult ;
90
65
it ( 'can compile' , ( ) => {
91
66
runWithIOLog ( ioLog , ( ) => {
92
67
harnessCompiler . reset ( ) ;
93
- var inputList : string [ ] = opts . filenames ;
94
- var noDefaultLib = false ;
95
- var libPath = Harness . IO . directoryName ( sys . getExecutingFilePath ( ) ) + '/lib.d.ts' ;
96
-
97
- if ( ! opts . options . noResolve ) {
98
- var filemap : any = { } ;
99
- var host : ts . CompilerHost = {
100
- getCurrentDirectory : ( ) => sys . getCurrentDirectory ( ) ,
101
- getCancellationToken : ( ) : any => undefined ,
102
- getSourceFile : ( fileName , languageVersion ) => {
103
- var fileContents : string ;
104
- try {
105
- if ( libPath === fileName ) {
106
- fileContents = Harness . IO . readFile ( Harness . libFolder + "lib.d.ts" ) ;
107
- }
108
- else {
109
- fileContents = sys . readFile ( fileName ) ;
110
- }
111
- }
112
- catch ( e ) {
113
- // Leave fileContents undefined;
114
- }
115
- return ts . createSourceFile ( fileName , fileContents , languageVersion ) ;
116
- } ,
117
- getDefaultLibFilename : ( ) => libPath ,
118
- writeFile : ( fn , contents ) => emitterIOHost . writeFile ( fn , contents , false ) ,
119
- getCanonicalFileName : ts . getCanonicalFileName ,
120
- useCaseSensitiveFileNames : ( ) => sys . useCaseSensitiveFileNames ,
121
- getNewLine : ( ) => sys . newLine
122
- } ;
123
-
124
- var resolvedProgram = ts . createProgram ( opts . filenames , opts . options , host ) ;
125
- resolvedProgram . getSourceFiles ( ) . forEach ( sourceFile => {
126
- noDefaultLib = noDefaultLib || sourceFile . hasNoDefaultLib ;
127
- if ( inputList . indexOf ( sourceFile . filename ) === - 1 ) {
128
- inputList . push ( sourceFile . filename ) ;
129
- }
130
- } ) ;
131
- }
132
-
133
- if ( ! opts . options . noLib && ! noDefaultLib ) {
134
- inputList . push ( libPath ) ;
135
- }
136
-
137
- harnessCompiler . reset ( ) ;
138
- harnessCompiler . setCompilerSettingsFromOptions ( opts . options ) ;
139
68
140
69
// Load the files
141
- inputList . forEach ( ( item : string ) => {
142
- var resolvedPath = libPath === item ? item : Harness . Path . switchToForwardSlashes ( sys . resolvePath ( item ) ) ;
143
- try {
144
- if ( libPath === item ) {
145
- var content = Harness . IO . readFile ( Harness . libFolder + "lib.d.ts" ) ;
146
- }
147
- else {
148
- var content = sys . readFile ( resolvedPath ) ;
149
- }
70
+ ts . forEach ( opts . filenames , fileName => {
71
+ inputFiles . push ( getHarnessCompilerInputUnit ( fileName ) ) ;
72
+ } ) ;
73
+
74
+ if ( ! opts . options . noLib ) {
75
+ // Find the lib.d.ts file in the input file and add it to the input files list
76
+ var libFile = ts . forEach ( ioLog . filesRead , fileRead => Harness . isLibraryFile ( fileRead . path ) ? fileRead . path : undefined ) ;
77
+ if ( libFile ) {
78
+ inputFiles . push ( getHarnessCompilerInputUnit ( libFile ) ) ;
150
79
}
151
- catch ( e ) {
152
- // Leave content undefined.
80
+ }
81
+
82
+ ts . forEach ( ioLog . filesRead , fileRead => {
83
+ var resolvedPath = Harness . Path . switchToForwardSlashes ( sys . resolvePath ( fileRead . path ) ) ;
84
+ var inInputList = ts . forEach ( inputFiles , inputFile => inputFile . unitName === resolvedPath ) ;
85
+ if ( ! inInputList ) {
86
+ // Add the file to other files
87
+ otherFiles . push ( getHarnessCompilerInputUnit ( fileRead . path ) ) ;
153
88
}
154
- harnessCompiler . addInputFile ( { unitName : resolvedPath , content : content } ) ;
155
89
} ) ;
156
90
157
- harnessCompiler . setCompilerOptions ( ) ;
91
+ // do not use lib since we shouldnt be reading any files that arent in the ioLog
92
+ opts . options . noLib = true ;
158
93
159
94
// Emit the results
160
- harnessCompiler . emitAll ( emitterIOHost ) ;
161
- var compilationErrors = harnessCompiler . reportCompilationErrors ( ) ;
162
-
163
- // Create an error baseline
164
- compilationErrors . forEach ( err => {
165
- if ( err . filename ) {
166
- errors += err . filename + ' (' + err . line + "," + err . character + "): " + err . message + '\r\n' ;
167
- }
168
- else {
169
- errors += err . message + '\r\n' ;
170
- }
171
- } ) ;
95
+ harnessCompiler . compileFiles ( inputFiles , otherFiles , compileResult => {
96
+ compilerResult = compileResult ;
97
+ } , /*settingsCallback*/ undefined , opts . options ) ;
172
98
} ) ;
99
+
100
+ function getHarnessCompilerInputUnit ( fileName : string ) {
101
+ var resolvedPath = Harness . Path . switchToForwardSlashes ( sys . resolvePath ( fileName ) ) ;
102
+ try {
103
+ var content = sys . readFile ( resolvedPath ) ;
104
+ }
105
+ catch ( e ) {
106
+ // Leave content undefined.
107
+ }
108
+ return { unitName : resolvedPath , content : content } ;
109
+ }
173
110
} ) ;
174
111
175
112
// Baselines
@@ -178,27 +115,46 @@ module RWC {
178
115
179
116
it ( 'has the expected emitted code' , ( ) => {
180
117
Harness . Baseline . runBaseline ( 'has the expected emitted code' , baseName + '.output.js' , ( ) => {
181
- return collateOutputs ( emitterIOHost , fn => Harness . Compiler . isJS ( fn ) , s => SyntacticCleaner . clean ( s ) ) ;
118
+ return collateOutputs ( compilerResult . files , s => SyntacticCleaner . clean ( s ) ) ;
182
119
} , false , baselineOpts ) ;
183
120
} ) ;
184
121
185
122
it ( 'has the expected declaration file content' , ( ) => {
186
123
Harness . Baseline . runBaseline ( 'has the expected declaration file content' , baseName + '.d.ts' , ( ) => {
187
- var result = collateOutputs ( emitterIOHost , fn => Harness . Compiler . isDTS ( fn ) ) ;
188
- return result . length > 0 ? result : null ;
124
+ if ( compilerResult . errors . length || ! compilerResult . declFilesCode . length ) {
125
+ return null ;
126
+ }
127
+ return collateOutputs ( compilerResult . declFilesCode ) ;
189
128
} , false , baselineOpts ) ;
190
129
} ) ;
191
130
192
131
it ( 'has the expected source maps' , ( ) => {
193
132
Harness . Baseline . runBaseline ( 'has the expected source maps' , baseName + '.map' , ( ) => {
194
- var result = collateOutputs ( emitterIOHost , fn => fn . substr ( fn . length - '.map' . length ) === '.map' ) ;
195
- return result . length > 0 ? result : null ;
133
+ if ( ! compilerResult . sourceMaps . length ) {
134
+ return null ;
135
+ }
136
+
137
+ return collateOutputs ( compilerResult . sourceMaps ) ;
196
138
} , false , baselineOpts ) ;
197
139
} ) ;
198
140
141
+ it ( 'has correct source map record' , ( ) => {
142
+ if ( compilerResult . sourceMapRecord ) {
143
+ Harness . Baseline . runBaseline ( 'has correct source map record' , baseName + '.sourcemap.txt' , ( ) => {
144
+ return compilerResult . sourceMapRecord ;
145
+ } , false , baselineOpts ) ;
146
+ }
147
+ } ) ;
148
+
199
149
it ( 'has the expected errors' , ( ) => {
200
150
Harness . Baseline . runBaseline ( 'has the expected errors' , baseName + '.errors.txt' , ( ) => {
201
- return errors . length > 0 ? errors : null ;
151
+ if ( compilerResult . errors . length === 0 ) {
152
+ return null ;
153
+ }
154
+
155
+ return Harness . Compiler . minimalDiagnosticsToString ( compilerResult . errors ) +
156
+ sys . newLine + sys . newLine +
157
+ Harness . Compiler . getErrorBaseline ( inputFiles . concat ( otherFiles ) , compilerResult . errors ) ;
202
158
} , false , baselineOpts ) ;
203
159
} ) ;
204
160
0 commit comments