2020-03-31 20:42:07 +08:00
"use strict" ;
2020-05-15 05:33:08 +08:00
var _ _read = ( this && this . _ _read ) || function ( o , n ) {
var m = typeof Symbol === "function" && o [ Symbol . iterator ] ;
if ( ! m ) return o ;
var i = m . call ( o ) , r , ar = [ ] , e ;
try {
while ( ( n === void 0 || n -- > 0 ) && ! ( r = i . next ( ) ) . done ) ar . push ( r . value ) ;
}
catch ( error ) { e = { error : error } ; }
finally {
try {
if ( r && ! r . done && ( m = i [ "return" ] ) ) m . call ( i ) ;
}
finally { if ( e ) throw e . error ; }
}
return ar ;
} ;
var _ _spread = ( this && this . _ _spread ) || function ( ) {
for ( var ar = [ ] , i = 0 ; i < arguments . length ; i ++ ) ar = ar . concat ( _ _read ( arguments [ i ] ) ) ;
return ar ;
} ;
2020-03-31 20:42:07 +08:00
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
var bs _logger _1 = require ( "bs-logger" ) ;
var path _1 = require ( "path" ) ;
2020-04-30 20:40:07 +08:00
var constants _1 = require ( "../constants" ) ;
2020-03-31 20:42:07 +08:00
var messages _1 = require ( "../util/messages" ) ;
2020-04-30 20:40:07 +08:00
var compiler _utils _1 = require ( "./compiler-utils" ) ;
2020-05-15 05:33:08 +08:00
var memoize = require ( "lodash.memoize" ) ;
2020-04-30 20:40:07 +08:00
function doTypeChecking ( configs , fileName , service , logger ) {
if ( configs . shouldReportDiagnostic ( fileName ) ) {
var diagnostics = service . getSemanticDiagnostics ( fileName ) . concat ( service . getSyntacticDiagnostics ( fileName ) ) ;
configs . raiseDiagnostics ( diagnostics , fileName , logger ) ;
}
}
2020-05-15 05:33:08 +08:00
exports . initializeLanguageServiceInstance = function ( configs , memoryCache , logger ) {
2020-03-31 20:42:07 +08:00
var _a ;
2020-05-15 05:33:08 +08:00
logger . debug ( 'initializeLanguageServiceInstance(): create typescript compiler' ) ;
2020-04-30 20:40:07 +08:00
var ts = configs . compilerModule ;
var cwd = configs . cwd ;
var cacheDir = configs . tsCacheDir ;
2020-05-15 05:33:08 +08:00
var _b = configs . parsedTsConfig , options = _b . options , fileNames = _b . fileNames ;
2020-04-30 20:40:07 +08:00
var serviceHostTraceCtx = ( _a = {
2020-03-31 20:42:07 +08:00
namespace : 'ts:serviceHost' ,
call : null
} ,
_a [ bs _logger _1 . LogContexts . logLevel ] = bs _logger _1 . LogLevels . trace ,
_a ) ;
2020-05-15 05:33:08 +08:00
function isFileInCache ( fileName ) {
return memoryCache . files . has ( fileName ) && memoryCache . files . get ( fileName ) . version !== 0 ;
}
2020-03-31 20:42:07 +08:00
var projectVersion = 1 ;
2020-04-30 20:40:07 +08:00
var updateMemoryCache = function ( contents , fileName ) {
2020-05-15 05:33:08 +08:00
logger . debug ( { fileName : fileName } , 'updateMemoryCache(): update memory cache for language service' ) ;
2020-04-30 20:40:07 +08:00
var shouldIncrementProjectVersion = false ;
2020-05-15 05:33:08 +08:00
var hit = isFileInCache ( fileName ) ;
if ( ! hit ) {
memoryCache . files . set ( fileName , {
text : contents ,
version : 1 ,
} ) ;
2020-04-30 20:40:07 +08:00
shouldIncrementProjectVersion = true ;
2020-03-31 20:42:07 +08:00
}
2020-05-15 05:33:08 +08:00
else {
var previousContents = memoryCache . files . get ( fileName ) . text ;
if ( previousContents !== contents ) {
memoryCache . files . set ( fileName , {
text : contents ,
version : memoryCache . files . get ( fileName ) . version + 1 ,
} ) ;
if ( hit )
shouldIncrementProjectVersion = true ;
}
if ( ! fileNames . includes ( fileName ) ) {
2020-04-30 20:40:07 +08:00
shouldIncrementProjectVersion = true ;
2020-05-15 05:33:08 +08:00
}
2020-03-31 20:42:07 +08:00
}
2020-04-30 20:40:07 +08:00
if ( shouldIncrementProjectVersion )
projectVersion ++ ;
2020-03-31 20:42:07 +08:00
} ;
var serviceHost = {
getProjectVersion : function ( ) { return String ( projectVersion ) ; } ,
2020-05-15 05:33:08 +08:00
getScriptFileNames : function ( ) { return _ _spread ( memoryCache . files . keys ( ) ) ; } ,
2020-03-31 20:42:07 +08:00
getScriptVersion : function ( fileName ) {
2020-04-30 20:40:07 +08:00
var normalizedFileName = path _1 . normalize ( fileName ) ;
2020-05-15 05:33:08 +08:00
var version = memoryCache . files . get ( normalizedFileName ) . version ;
2020-03-31 20:42:07 +08:00
return version === undefined ? undefined : String ( version ) ;
} ,
getScriptSnapshot : function ( fileName ) {
2020-05-15 05:33:08 +08:00
var _a ;
2020-04-30 20:40:07 +08:00
var normalizedFileName = path _1 . normalize ( fileName ) ;
2020-05-15 05:33:08 +08:00
var hit = memoryCache . files . has ( normalizedFileName ) && memoryCache . files . get ( normalizedFileName ) . version !== 0 ;
logger . trace ( { normalizedFileName : normalizedFileName , cacheHit : hit } , 'getScriptSnapshot():' , 'cache' , hit ? 'hit' : 'miss' ) ;
if ( ! hit ) {
memoryCache . files . set ( normalizedFileName , {
text : ts . sys . readFile ( normalizedFileName ) ,
version : 1 ,
} ) ;
}
var contents = ( _a = memoryCache . files . get ( normalizedFileName ) ) === null || _a === void 0 ? void 0 : _a . text ;
2020-04-30 20:40:07 +08:00
if ( contents === undefined )
2020-03-31 20:42:07 +08:00
return ;
return ts . ScriptSnapshot . fromString ( contents ) ;
} ,
fileExists : memoize ( ts . sys . fileExists ) ,
readFile : logger . wrap ( serviceHostTraceCtx , 'readFile' , memoize ( ts . sys . readFile ) ) ,
readDirectory : memoize ( ts . sys . readDirectory ) ,
getDirectories : memoize ( ts . sys . getDirectories ) ,
directoryExists : memoize ( ts . sys . directoryExists ) ,
realpath : memoize ( ts . sys . realpath ) ,
2020-04-30 20:40:07 +08:00
getNewLine : function ( ) { return constants _1 . LINE _FEED ; } ,
2020-03-31 20:42:07 +08:00
getCurrentDirectory : function ( ) { return cwd ; } ,
getCompilationSettings : function ( ) { return options ; } ,
getDefaultLibFileName : function ( ) { return ts . getDefaultLibFilePath ( options ) ; } ,
getCustomTransformers : function ( ) { return configs . tsCustomTransformers ; } ,
} ;
2020-05-15 05:33:08 +08:00
logger . debug ( 'initializeLanguageServiceInstance(): creating language service' ) ;
var service = ts . createLanguageService ( serviceHost , ts . createDocumentRegistry ( ) ) ;
2020-03-31 20:42:07 +08:00
return {
compileFn : function ( code , fileName ) {
2020-05-15 05:33:08 +08:00
logger . debug ( { fileName : fileName } , 'compileFn(): compiling using language service' ) ;
updateMemoryCache ( code , fileName ) ;
var output = service . getEmitOutput ( fileName ) ;
logger . debug ( { fileName : fileName } , 'compileFn(): computing diagnostics using language service' ) ;
doTypeChecking ( configs , fileName , service , logger ) ;
2020-04-30 20:40:07 +08:00
if ( cacheDir ) {
2020-05-15 05:33:08 +08:00
if ( compiler _utils _1 . isTestFile ( configs . testMatchPatterns , fileName ) ) {
compiler _utils _1 . cacheResolvedModules ( fileName , code , memoryCache , service . getProgram ( ) , cacheDir , logger ) ;
2020-04-30 20:40:07 +08:00
}
else {
Object . entries ( memoryCache . resolvedModules )
. filter ( function ( entry ) {
2020-05-15 05:33:08 +08:00
return entry [ 1 ] . modulePaths . find ( function ( modulePath ) { return modulePath === fileName ; } ) && ! memoryCache . files . has ( entry [ 0 ] ) ;
2020-04-30 20:40:07 +08:00
} )
. forEach ( function ( entry ) {
var testFileName = entry [ 0 ] ;
2020-05-15 05:33:08 +08:00
var testFileContent = entry [ 1 ] . testFileContent ;
logger . debug ( { fileName : fileName } , 'compileFn(): computing diagnostics for test file that imports this module using language service' ) ;
updateMemoryCache ( testFileContent , testFileName ) ;
2020-04-30 20:40:07 +08:00
doTypeChecking ( configs , testFileName , service , logger ) ;
} ) ;
}
}
2020-03-31 20:42:07 +08:00
if ( output . emitSkipped ) {
2020-05-15 05:33:08 +08:00
throw new TypeError ( path _1 . relative ( cwd , fileName ) + ": Emit skipped for language service" ) ;
2020-03-31 20:42:07 +08:00
}
if ( ! output . outputFiles . length ) {
throw new TypeError ( messages _1 . interpolate ( "Unable to require `.d.ts` file for file: {{file}}.\nThis is usually the result of a faulty configuration or import. Make sure there is a `.js`, `.json` or another executable extension available alongside `{{file}}`." , {
2020-05-15 05:33:08 +08:00
file : path _1 . basename ( fileName ) ,
2020-03-31 20:42:07 +08:00
} ) ) ;
}
return [ output . outputFiles [ 1 ] . text , output . outputFiles [ 0 ] . text ] ;
} ,
program : service . getProgram ( ) ,
} ;
} ;