@@ -226,7 +226,8 @@ module FourSlash {
226
226
} ) ;
227
227
}
228
228
229
- // NEWTODO: Re-implement commented-out section
229
+ // NEWTODO: Re-implement commented-out section
230
+
230
231
//harnessCompiler.addInputFiles(inputFiles);
231
232
//try {
232
233
// var resolvedFiles = harnessCompiler.resolve();
@@ -246,8 +247,15 @@ module FourSlash {
246
247
// harnessCompiler.reset();
247
248
//}
248
249
249
- /// NEWTODO: For now do not resolve, just use the input files inputFiles . forEach ( file => { if ( ! Harness . isLibraryFile ( file . unitName ) ) { this . languageServiceShimHost . addScript ( file . unitName , file . content ) ; } } ) ;
250
- this . languageServiceShimHost . addScript ( 'lib.d.ts' , Harness . Compiler . libTextMinimal ) ;
250
+ /// NEWTODO: For now do not resolve, just use the input files
251
+ inputFiles . forEach ( file => {
252
+ if ( ! Harness . isLibraryFile ( file . unitName ) ) {
253
+ this . languageServiceShimHost . addScript ( file . unitName , file . content ) ;
254
+ }
255
+ } ) ;
256
+
257
+ this . languageServiceShimHost . addScript ( 'lib.d.ts' , Harness . Compiler . libTextMinimal ) ;
258
+
251
259
252
260
// Sneak into the language service and get its compiler so we can examine the syntax trees
253
261
this . languageService = this . languageServiceShimHost . getLanguageService ( ) . languageService ;
@@ -1875,50 +1883,44 @@ module FourSlash {
1875
1883
xmlData . push ( xml ) ;
1876
1884
}
1877
1885
1886
+ // Cache these between executions so we don't have to re-parse them for every test
1887
+ var fourslashSourceFile : ts . SourceFile = undefined ;
1888
+ var libdtsSourceFile : ts . SourceFile = undefined ;
1878
1889
export function runFourSlashTestContent ( content : string , fileName : string ) : TestXmlData {
1879
1890
// Parse out the files and their metadata
1880
1891
var testData = parseTestData ( content , fileName ) ;
1881
1892
1882
1893
currentTestState = new TestState ( testData ) ;
1883
1894
1884
1895
var result = '' ;
1885
- var tsFn = 'tests/cases/fourslash/fourslash.ts' ;
1896
+ var fourslashFilename = 'fourslash.ts' ;
1897
+ var tsFn = 'tests/cases/fourslash/' + fourslashFilename ;
1898
+ fourslashSourceFile = fourslashSourceFile || ts . createSourceFile ( tsFn , Harness . IO . readFile ( tsFn ) , ts . ScriptTarget . ES5 ) ;
1899
+ libdtsSourceFile = libdtsSourceFile || ts . createSourceFile ( 'lib.d.ts' , Harness . Compiler . libTextMinimal , ts . ScriptTarget . ES3 ) ;
1886
1900
1887
- fsOutput . reset ( ) ;
1888
- fsErrors . reset ( ) ;
1901
+ var files : { [ filename : string ] : ts . SourceFile ; } = { } ;
1902
+ files [ fourslashFilename ] = fourslashSourceFile ;
1903
+ files [ fileName ] = ts . createSourceFile ( fileName , content , ts . ScriptTarget . ES5 ) ;
1904
+ files [ 'lib.d.ts' ] = libdtsSourceFile ;
1889
1905
1890
- var harnessCompiler = Harness . Compiler . getCompiler ( ) ;
1891
- harnessCompiler . reset ( ) ;
1906
+ var host = Harness . Compiler . createCompilerHost ( files , ( fn , contents ) => result = contents ) ;
1907
+ var program = ts . createProgram ( [ fileName , fourslashFilename ] , { } , host ) ;
1908
+ var checker = ts . createTypeChecker ( program ) ;
1909
+ checker . checkProgram ( ) ;
1892
1910
1893
- var filesToAdd = [
1894
- { unitName : tsFn , content : Harness . IO . readFile ( tsFn ) } ,
1895
- { unitName : fileName , content : Harness . IO . readFile ( fileName ) }
1896
- ] ;
1897
- harnessCompiler . addInputFiles ( filesToAdd ) ;
1898
-
1899
- var emitterIOHost : Harness . Compiler . IEmitterIOHost = {
1900
- writeFile : ( path : string , contents : string , writeByteOrderMark : boolean ) => fsOutput . Write ( contents ) ,
1901
- resolvePath : ( s : string ) => s
1911
+ var errs = checker . getDiagnostics ( files [ fileName ] ) ;
1912
+ if ( errs . length > 0 ) {
1913
+ throw new Error ( 'Error compiling ' + fileName + ': ' + errs . map ( e => e . messageText ) . join ( '\r\n' ) ) ;
1902
1914
}
1903
-
1904
- harnessCompiler . emitAll ( emitterIOHost ) ;
1905
- fsOutput . Close ( ) ;
1906
- fsErrors . Close ( ) ;
1907
-
1908
- if ( fsErrors . lines . length > 0 ) {
1909
- throw new Error ( 'Error compiling ' + fileName + ': ' + fsErrors . lines . join ( '\r\n' ) ) ;
1910
- }
1911
-
1912
- result = fsOutput . lines . join ( '\r\n' ) ;
1915
+ checker . emitFiles ( ) ;
1916
+ result = result || '' ; // Might have an empty fourslash file
1913
1917
1914
1918
// Compile and execute the test
1915
1919
try {
1916
1920
eval ( result ) ;
1917
1921
} catch ( err ) {
1918
1922
// Debugging: FourSlash.currentTestState.printCurrentFileState();
1919
1923
throw err ;
1920
- } finally {
1921
- harnessCompiler . reset ( ) ;
1922
1924
}
1923
1925
1924
1926
var xmlData = currentTestState . getTestXmlData ( ) ;
0 commit comments