import * as ts from 'typescript' ; import { combinePaths } from '../../../common/pathUtils' ; import * as host from '../testHost' ; import { parseTestData } from './fourSlashParser' ; import { FourSlashData } from './fourSlashTypes' ; import { HostSpecificFeatures , TestState } from './testState' ; import { Consts } from './testState.Consts' ; export type TestStateFactory = ( basePath : string , testData : FourSlashData , mountPaths ? : Map < string , string > , hostSpecificFeatures ? : HostSpecificFeatures ) => TestState ; param basePath param fileName export function runFourSlashTest ( basePath , fileName , cb ? , mountPaths ? , hostSpecificFeatures ? , testStateFactory ? ) { const content = host . HOST . readFile ( fileName ) ! ; runFourSlashTestContent ( basePath , fileName , content , cb , mountPaths , hostSpecificFeatures , testStateFactory ) ; } param basePath param fileName param content export function runFourSlashTestContent ( basePath , fileName , content , cb ? , mountPaths ? , hostSpecificFeatures ? , testStateFactory ? ) { const absoluteBasePath = combinePaths ( '/' , basePath ) ; const absoluteFileName = combinePaths ( '/' , fileName ) ; const testData = parseTestData ( absoluteBasePath , content , absoluteFileName ) ; const state = testStateFactory !== undefined ? testStateFactory ( absoluteBasePath , testData , mountPaths , hostSpecificFeatures ) : new TestState ( absoluteBasePath , testData , mountPaths , hostSpecificFeatures ) ; const output = ts . transpileModule ( content , { reportDiagnostics : true , compilerOptions : { target : ts . ScriptTarget . ES2015 } , } ) ; if ( output . diagnostics ! . length > 0 ) { throw new Error ( `Syntax error in ${ absoluteBasePath }: ${ output . diagnostics ! [ 0 ] . messageText }` ) ; } runCode ( output . outputText , state , cb ) ; } async function runCode ( code , state , cb ? ) { try { const wrappedCode = `(async function(helper, Consts) {
${ code }
})` ; const f = eval ( wrappedCode ) ; await f ( state , Consts ) ; markDone ( ) ; } catch ( ex ) { markDone ( ex ) ; } function markDone ( ... args ) { if ( cb ) { cb ( ... args ) ; } } }