@@ -396,7 +396,9 @@ namespace ts.server {
396396 }
397397
398398 removeFile ( info : ScriptInfo , detachFromProject = true ) {
399- this . removeRootFileIfNecessary ( info ) ;
399+ if ( this . isRoot ( info ) ) {
400+ this . removeRoot ( info ) ;
401+ }
400402 this . lsHost . notifyFileRemoved ( info ) ;
401403 this . cachedUnresolvedImportsPerFile . remove ( info . path ) ;
402404
@@ -567,9 +569,6 @@ namespace ts.server {
567569
568570 setCompilerOptions ( compilerOptions : CompilerOptions ) {
569571 if ( compilerOptions ) {
570- if ( this . projectKind === ProjectKind . Inferred ) {
571- compilerOptions . allowJs = true ;
572- }
573572 compilerOptions . allowNonTsExtensions = true ;
574573 if ( changesAffectModuleResolution ( this . compilerOptions , compilerOptions ) ) {
575574 // reset cached unresolved imports if changes in compiler options affected module resolution
@@ -698,11 +697,9 @@ namespace ts.server {
698697 }
699698
700699 // remove a root file from project
701- private removeRootFileIfNecessary ( info : ScriptInfo ) : void {
702- if ( this . isRoot ( info ) ) {
703- remove ( this . rootFiles , info ) ;
704- this . rootFilesMap . remove ( info . path ) ;
705- }
700+ protected removeRoot ( info : ScriptInfo ) : void {
701+ remove ( this . rootFiles , info ) ;
702+ this . rootFilesMap . remove ( info . path ) ;
706703 }
707704 }
708705
@@ -717,6 +714,32 @@ namespace ts.server {
717714 }
718715 } ) ( ) ;
719716
717+ private _isJsInferredProject = false ;
718+
719+ toggleJsInferredProject ( isJsInferredProject : boolean ) {
720+ if ( isJsInferredProject !== this . _isJsInferredProject ) {
721+ this . _isJsInferredProject = isJsInferredProject ;
722+ this . setCompilerOptions ( ) ;
723+ }
724+ }
725+
726+ setCompilerOptions ( options ?: CompilerOptions ) {
727+ // Avoid manipulating the given options directly
728+ const newOptions = options ? clone ( options ) : this . getCompilerOptions ( ) ;
729+ if ( ! newOptions ) {
730+ return ;
731+ }
732+
733+ if ( this . _isJsInferredProject && typeof newOptions . maxNodeModuleJsDepth !== "number" ) {
734+ newOptions . maxNodeModuleJsDepth = 2 ;
735+ }
736+ else if ( ! this . _isJsInferredProject ) {
737+ newOptions . maxNodeModuleJsDepth = undefined ;
738+ }
739+ newOptions . allowJs = true ;
740+ super . setCompilerOptions ( newOptions ) ;
741+ }
742+
720743 // Used to keep track of what directories are watched for this project
721744 directoriesWatchedForTsconfig : string [ ] = [ ] ;
722745
@@ -731,6 +754,22 @@ namespace ts.server {
731754 /*compileOnSaveEnabled*/ false ) ;
732755 }
733756
757+ addRoot ( info : ScriptInfo ) {
758+ if ( ! this . _isJsInferredProject && info . isJavaScript ( ) ) {
759+ this . toggleJsInferredProject ( /*isJsInferredProject*/ true ) ;
760+ }
761+ super . addRoot ( info ) ;
762+ }
763+
764+ removeRoot ( info : ScriptInfo ) {
765+ if ( this . _isJsInferredProject && info . isJavaScript ( ) ) {
766+ if ( filter ( this . getRootScriptInfos ( ) , info => info . isJavaScript ( ) ) . length === 0 ) {
767+ this . toggleJsInferredProject ( /*isJsInferredProject*/ false ) ;
768+ }
769+ }
770+ super . removeRoot ( info ) ;
771+ }
772+
734773 getProjectRootPath ( ) {
735774 // Single inferred project does not have a project root.
736775 if ( this . projectService . useSingleInferredProject ) {
0 commit comments