1- import { InMemoryFileSystemHost , ts } from "@ts-morph/common" ;
1+ import { getLibFiles , InMemoryFileSystemHost , ts } from "@ts-morph/common" ;
22import { expect } from "chai" ;
33import { EOL } from "os" ;
44import { createProject , createProjectSync , Project , ProjectOptions } from "../Project" ;
@@ -10,32 +10,46 @@ describe(nameof(Project), () => {
1010
1111 function doTestsForProject ( create : ( options : ProjectOptions ) => Promise < Project > ) {
1212 it ( "should add the files from tsconfig.json by default with the target in the tsconfig.json" , async ( ) => {
13- const fileSystem = new InMemoryFileSystemHost ( { skipLoadingLibFiles : true } ) ;
13+ const fileSystem = new InMemoryFileSystemHost ( ) ;
1414 fileSystem . writeFileSync ( "tsconfig.json" , `{ "compilerOptions": { "rootDir": "test", "target": "ES5" }, "include": ["test"] }` ) ;
1515 fileSystem . writeFileSync ( "/otherFile.ts" , "" ) ;
1616 fileSystem . writeFileSync ( "/test/file.ts" , "" ) ;
1717 fileSystem . writeFileSync ( "/test/test2/file2.ts" , "" ) ;
18- const project = await create ( { tsConfigFilePath : "tsconfig.json" , fileSystem } ) ;
18+ const project = await create ( {
19+ tsConfigFilePath : "tsconfig.json" ,
20+ fileSystem,
21+ skipLoadingLibFiles : true ,
22+ } ) ;
1923 expect ( project . getSourceFiles ( ) . map ( s => s . fileName ) . sort ( ) ) . to . deep . equal ( [ "/test/file.ts" , "/test/test2/file2.ts" ] . sort ( ) ) ;
2024 expect ( project . getSourceFiles ( ) . map ( s => s . languageVersion ) ) . to . deep . equal ( [ ts . ScriptTarget . ES5 , ts . ScriptTarget . ES5 ] ) ;
2125 } ) ;
2226
2327 it ( "should add the files from tsconfig.json by default and also take into account the passed in compiler options" , async ( ) => {
24- const fileSystem = new InMemoryFileSystemHost ( { skipLoadingLibFiles : true } ) ;
28+ const fileSystem = new InMemoryFileSystemHost ( ) ;
2529 fileSystem . writeFileSync ( "tsconfig.json" , `{ "compilerOptions": { "target": "ES5" } }` ) ;
2630 fileSystem . writeFileSync ( "/otherFile.ts" , "" ) ;
2731 fileSystem . writeFileSync ( "/test/file.ts" , "" ) ;
2832 fileSystem . writeFileSync ( "/test/test2/file2.ts" , "" ) ;
29- const project = await create ( { tsConfigFilePath : "tsconfig.json" , compilerOptions : { rootDir : "/test/test2" } , fileSystem } ) ;
33+ const project = await create ( {
34+ tsConfigFilePath : "tsconfig.json" ,
35+ compilerOptions : { rootDir : "/test/test2" } ,
36+ fileSystem,
37+ skipLoadingLibFiles : true ,
38+ } ) ;
3039 expect ( project . getSourceFiles ( ) . map ( s => s . fileName ) . sort ( ) ) . to . deep . equal ( [ "/otherFile.ts" , "/test/file.ts" , "/test/test2/file2.ts" ] . sort ( ) ) ;
3140 } ) ;
3241
3342 it ( "should not add the files from tsconfig.json when specifying not to" , async ( ) => {
34- const fileSystem = new InMemoryFileSystemHost ( { skipLoadingLibFiles : true } ) ;
43+ const fileSystem = new InMemoryFileSystemHost ( ) ;
3544 fileSystem . writeFileSync ( "tsconfig.json" , `{ "compilerOptions": { "rootDir": "test", "target": "ES5" } }` ) ;
3645 fileSystem . writeFileSync ( "/test/file.ts" , "" ) ;
3746 fileSystem . writeFileSync ( "/test/test2/file2.ts" , "" ) ;
38- const project = await create ( { tsConfigFilePath : "tsconfig.json" , skipAddingFilesFromTsConfig : true , fileSystem } ) ;
47+ const project = await create ( {
48+ tsConfigFilePath : "tsconfig.json" ,
49+ skipAddingFilesFromTsConfig : true ,
50+ fileSystem,
51+ skipLoadingLibFiles : true ,
52+ } ) ;
3953 expect ( project . getSourceFiles ( ) . map ( s => s . fileName ) . sort ( ) ) . to . deep . equal ( [ ] ) ;
4054 } ) ;
4155
@@ -211,24 +225,56 @@ describe(nameof(Project), () => {
211225 describe ( nameof < ProjectOptions > ( o => o . skipLoadingLibFiles ) , ( ) => {
212226 it ( "should not skip loading lib files when empty" , async ( ) => {
213227 const project = await create ( { useInMemoryFileSystem : true } ) ;
214- const result = project . fileSystem . readDirSync ( "/node_modules/typescript/lib" ) ;
215- expect ( result . some ( r => r . includes ( "lib.d.ts" ) ) ) . to . be . true ;
228+ const sourceFile = project . createSourceFile ( "test.ts" , "const t: String = '';" ) ;
229+ const program = project . createProgram ( ) ;
230+ expect ( ts . getPreEmitDiagnostics ( program ) . length ) . to . equal ( 0 ) ;
231+
232+ const typeChecker = program . getTypeChecker ( ) ;
233+ const varDecl = ( sourceFile . statements [ 0 ] as ts . VariableStatement ) . declarationList . declarations [ 0 ] ;
234+ const varDeclType = typeChecker . getTypeAtLocation ( varDecl . type ! ) ;
235+ const stringDec = varDeclType . getSymbol ( ) ! . declarations [ 0 ] ;
236+ expect ( stringDec . getSourceFile ( ) . fileName ) . to . equal ( "/node_modules/typescript/lib/lib.es5.d.ts" ) ;
216237 } ) ;
217238
218- it ( "should not skip loading lib files when true" , async ( ) => {
239+ it ( "should skip loading lib files when true" , async ( ) => {
219240 const project = await create ( { useInMemoryFileSystem : true , skipLoadingLibFiles : true } ) ;
220- expect ( project . fileSystem . directoryExistsSync ( "/node_modules" ) ) . to . be . false ;
241+ const sourceFile = project . createSourceFile ( "test.ts" , "const t: String = '';" ) ;
242+ const program = project . createProgram ( ) ;
243+ expect ( ts . getPreEmitDiagnostics ( program ) . length ) . to . equal ( 10 ) ;
244+
245+ const typeChecker = program . getTypeChecker ( ) ;
246+ const varDecl = ( sourceFile . statements [ 0 ] as ts . VariableStatement ) . declarationList . declarations [ 0 ] ;
247+ const varDeclType = typeChecker . getTypeAtLocation ( varDecl . type ! ) ;
248+ expect ( varDeclType . getSymbol ( ) ) . to . be . undefined ;
221249 } ) ;
222250
223- it ( "should throw when providing skipLoadingLibFiles without using n in-memory file system " , async ( ) => {
251+ it ( "should throw when providing skipLoadingLibFiles and a libFolderPath " , async ( ) => {
224252 try {
225- await create ( { skipLoadingLibFiles : true } ) ;
253+ await create ( { skipLoadingLibFiles : true , libFolderPath : "" } ) ;
226254 expect . fail ( "should have thrown" ) ;
227255 } catch ( err ) {
228- expect ( err . message ) . to . equal ( "The skipLoadingLibFiles option can only be true when useInMemoryFileSystem is true ." ) ;
256+ expect ( err . message ) . to . equal ( "Cannot set skipLoadingLibFiles to true when libFolderPath is provided ." ) ;
229257 }
230258 } ) ;
231259 } ) ;
260+
261+ describe ( nameof < ProjectOptions > ( o => o . libFolderPath ) , ( ) => {
262+ it ( "should support specifying a different folder for the lib files" , async ( ) => {
263+ const fileSystem = new InMemoryFileSystemHost ( ) ;
264+ for ( const file of getLibFiles ( ) )
265+ fileSystem . writeFileSync ( `/other/${ file . fileName } ` , file . text ) ;
266+ const project = await create ( { fileSystem, libFolderPath : "/other" } ) ;
267+ const sourceFile = project . createSourceFile ( "test.ts" , "const t: String = '';" ) ;
268+ const program = project . createProgram ( ) ;
269+ expect ( ts . getPreEmitDiagnostics ( program ) . length ) . to . equal ( 0 ) ;
270+
271+ const typeChecker = program . getTypeChecker ( ) ;
272+ const varDecl = ( sourceFile . statements [ 0 ] as ts . VariableStatement ) . declarationList . declarations [ 0 ] ;
273+ const varDeclType = typeChecker . getTypeAtLocation ( varDecl . type ! ) ;
274+ const stringDec = varDeclType . getSymbol ( ) ! . declarations [ 0 ] ;
275+ expect ( stringDec . getSourceFile ( ) . fileName ) . to . equal ( "/other/lib.es5.d.ts" ) ;
276+ } ) ;
277+ } ) ;
232278 }
233279 } ) ;
234280
@@ -376,14 +422,14 @@ describe(nameof(Project), () => {
376422 } ) ;
377423
378424 it ( "should add the files from tsconfig.json" , async ( ) => {
379- const fileSystem = new InMemoryFileSystemHost ( { skipLoadingLibFiles : true } ) ;
425+ const fileSystem = new InMemoryFileSystemHost ( ) ;
380426 fileSystem . writeFileSync ( "tsconfig.json" ,
381427 `{ "compilerOptions": { "rootDir": "test", "target": "ES5" }, "include": ["test"], "exclude": ["/test/exclude"] }` ) ;
382428 fileSystem . writeFileSync ( "/otherFile.ts" , "" ) ;
383429 fileSystem . writeFileSync ( "/test/file.ts" , "" ) ;
384430 fileSystem . writeFileSync ( "/test/test2/file2.ts" , "" ) ;
385431 fileSystem . writeFileSync ( "/test/exclude/file.ts" , "" ) ;
386- const project = await createProject ( { fileSystem } ) ;
432+ const project = await createProject ( { fileSystem, skipLoadingLibFiles : true } ) ;
387433 expect ( project . getSourceFiles ( ) ) . to . deep . equal ( [ ] ) ;
388434 const returnedFiles = await action ( project , "tsconfig.json" ) ;
389435 const expectedFiles = [ "/test/file.ts" , "/test/test2/file2.ts" ] . sort ( ) ;
@@ -401,12 +447,12 @@ describe(nameof(Project), () => {
401447
402448 function doTestsForMethod ( action : ( project : Project , globs : string | readonly string [ ] ) => Promise < ts . SourceFile [ ] > ) {
403449 it ( "should add the source files based on a file glob" , async ( ) => {
404- const fileSystem = new InMemoryFileSystemHost ( { skipLoadingLibFiles : true } ) ;
450+ const fileSystem = new InMemoryFileSystemHost ( ) ;
405451 fileSystem . writeFileSync ( "/otherFile.ts" , "" ) ;
406452 fileSystem . writeFileSync ( "/test/file.ts" , "" ) ;
407453 fileSystem . writeFileSync ( "/test/test2/file2.ts" , "" ) ;
408454 fileSystem . writeFileSync ( "/test/other/file.ts" , "" ) ;
409- const project = await createProject ( { fileSystem } ) ;
455+ const project = await createProject ( { fileSystem, skipLoadingLibFiles : true } ) ;
410456 expect ( project . getSourceFiles ( ) ) . to . deep . equal ( [ ] ) ;
411457 const returnedFiles = await action ( project , "/test/**/*.ts" ) ;
412458 const expectedFiles = [ "/test/file.ts" , "/test/test2/file2.ts" , "/test/other/file.ts" ] . sort ( ) ;
@@ -427,7 +473,7 @@ describe(nameof(Project), () => {
427473 } ) ;
428474
429475 async function fileDependencyResolutionSetup ( options : ProjectOptions = { } , create : ( options : ProjectOptions ) => Promise < Project > ) {
430- const fileSystem = new InMemoryFileSystemHost ( { skipLoadingLibFiles : true } ) ;
476+ const fileSystem = new InMemoryFileSystemHost ( ) ;
431477
432478 fileSystem . writeFileSync ( "/package.json" , `{ "name": "testing", "version": "0.0.1" }` ) ;
433479 fileSystem . writeFileSync ( "/node_modules/library/package.json" ,
@@ -443,7 +489,7 @@ describe(nameof(Project), () => {
443489 fileSystem . writeFileSync ( "/other/referenced-file.d.ts" , "declare function nameof(): void;" ) ;
444490 fileSystem . writeFileSync ( "/tsconfig.json" , `{ "files": ["src/main.ts"] }` ) ;
445491
446- const project = await create ( { tsConfigFilePath : "tsconfig.json" , fileSystem, ...options } ) ;
492+ const project = await create ( { tsConfigFilePath : "tsconfig.json" , fileSystem, ...options , skipLoadingLibFiles : true } ) ;
447493 return {
448494 project,
449495 initialFiles : [ "/src/main.ts" ] ,
@@ -560,7 +606,6 @@ describe(nameof(Project), () => {
560606 expect ( moduleResolutionHost . getDirectories ! ( "/" ) ) . to . deep . equal ( [
561607 "/dir1" ,
562608 "/dir2" ,
563- "/node_modules" ,
564609 ] ) ;
565610 } ) ;
566611
@@ -573,7 +618,6 @@ describe(nameof(Project), () => {
573618 "/dir1" ,
574619 "/dir2" ,
575620 "/dir3" ,
576- "/node_modules" ,
577621 ] ) ;
578622 } ) ;
579623
0 commit comments