@@ -11,6 +11,8 @@ interface DiscoverFmtPathsOptions {
1111 /** Absolute directory used to resolve input paths. */
1212 cwd : string ;
1313 patterns ?: string [ ] ;
14+ /** Returns whether a scanned directory can be pruned before traversal. */
15+ isDirectoryIgnored ?: ( directoryPath : string ) => boolean ;
1416}
1517
1618const isErrnoException = ( error : unknown ) : error is NodeJS . ErrnoException =>
@@ -201,6 +203,7 @@ class GitIgnoreMatcher {
201203const createTraversalOptions = (
202204 gitIgnore : GitIgnoreMatcher ,
203205 isIncluded ?: ( filePath : string ) => boolean ,
206+ isDirectoryIgnored ?: ( directoryPath : string ) => boolean ,
204207) => {
205208 // tiny-readdir passes only a path to `ignore`, so retain the dirent type briefly.
206209 const directories = new Set < string > ( ) ;
@@ -214,7 +217,7 @@ const createTraversalOptions = (
214217 }
215218
216219 if ( isDirectory ) {
217- return gitIgnore . isIgnored ( targetPath , true ) ;
220+ return gitIgnore . isIgnored ( targetPath , true ) || isDirectoryIgnored ?. ( targetPath ) === true ;
218221 }
219222
220223 return (
@@ -343,6 +346,7 @@ const getTraversalRoots = (cwd: string, directories: string[], globs: string[]):
343346const discoverFmtPaths = async ( {
344347 cwd,
345348 patterns : inputPatterns ,
349+ isDirectoryIgnored,
346350} : DiscoverFmtPathsOptions ) : Promise < string [ ] > => {
347351 const patterns = inputPatterns ?. length ? inputPatterns : [ '.' ] ;
348352 const {
@@ -366,7 +370,7 @@ const discoverFmtPaths = async ({
366370 }
367371
368372 await gitIgnore . loadThrough ( rootPath ) ;
369- if ( gitIgnore . isIgnored ( rootPath , true ) ) {
373+ if ( gitIgnore . isIgnored ( rootPath , true ) || isDirectoryIgnored ?. ( rootPath ) === true ) {
370374 return [ ] ;
371375 }
372376
@@ -384,7 +388,9 @@ const discoverFmtPaths = async ({
384388 return globMatchers . some ( ( matches ) => matches ( relativePath ) ) ;
385389 } ;
386390
387- return ( await readdir ( rootPath , createTraversalOptions ( gitIgnore , isIncluded ) ) ) . files ;
391+ return (
392+ await readdir ( rootPath , createTraversalOptions ( gitIgnore , isIncluded , isDirectoryIgnored ) )
393+ ) . files ;
388394 } ) ,
389395 ) ;
390396
0 commit comments