@@ -164,6 +164,16 @@ namespace ts {
164164 return undefined ;
165165 }
166166
167+ /** Works like Array.prototype.findIndex, returning `-1` if no element satisfying the predicate is found. */
168+ export function findIndex < T > ( array : T [ ] , predicate : ( element : T , index : number ) => boolean ) : number {
169+ for ( let i = 0 ; i < array . length ; i ++ ) {
170+ if ( predicate ( array [ i ] , i ) ) {
171+ return i ;
172+ }
173+ }
174+ return - 1 ;
175+ }
176+
167177 /**
168178 * Returns the first truthy result of `callback`, or else fails.
169179 * This is like `forEach`, but never returns undefined.
@@ -1810,68 +1820,43 @@ namespace ts {
18101820 // If there are no "includes", then just put everything in results[0].
18111821 const results : string [ ] [ ] = includeFileRegexes ? includeFileRegexes . map ( ( ) => [ ] ) : [ [ ] ] ;
18121822
1823+ const comparer = useCaseSensitiveFileNames ? compareStrings : compareStringsCaseInsensitive ;
18131824 for ( const basePath of patterns . basePaths ) {
1814- forEachFileInRecursiveDirectories ( basePath , combinePaths ( currentDirectory , basePath ) , { useCaseSensitiveFileNames , getFileSystemEntries , includeDirectory , visitFile } ) ;
1825+ visitDirectory ( basePath , combinePaths ( currentDirectory , basePath ) ) ;
18151826 }
18161827
18171828 return flatten ( results ) ;
18181829
1819- function includeDirectory ( absoluteDirectoryName : string ) : boolean {
1820- return ( ! includeDirectoryRegex || includeDirectoryRegex . test ( absoluteDirectoryName ) ) &&
1821- ( ! excludeRegex || ! excludeRegex . test ( absoluteDirectoryName ) ) ;
1822- }
1823-
1824- function visitFile ( fileName : string , absoluteFileName : string ) : void {
1825- if ( extensions && ! fileExtensionIsAny ( fileName , extensions ) ||
1826- excludeRegex && excludeRegex . test ( absoluteFileName ) ) {
1827- return ;
1828- }
1829-
1830- if ( ! includeFileRegexes ) {
1831- results [ 0 ] . push ( fileName ) ;
1832- }
1833- else {
1834- for ( let i = 0 ; i < includeFileRegexes . length ; i ++ ) {
1835- if ( includeFileRegexes [ i ] . test ( absoluteFileName ) ) {
1836- results [ i ] . push ( fileName ) ;
1837- // Only include a file once.
1838- break ;
1830+ function visitDirectory ( path : string , absolutePath : string ) {
1831+ let { files, directories } = getFileSystemEntries ( path ) ;
1832+ files = files . slice ( ) . sort ( comparer ) ;
1833+ directories = directories . slice ( ) . sort ( comparer ) ;
1834+
1835+ for ( const current of files ) {
1836+ const name = combinePaths ( path , current ) ;
1837+ const absoluteName = combinePaths ( absolutePath , current ) ;
1838+ if ( extensions && ! fileExtensionIsAny ( name , extensions ) ) continue ;
1839+ if ( excludeRegex && excludeRegex . test ( absoluteName ) ) continue ;
1840+ if ( ! includeFileRegexes ) {
1841+ results [ 0 ] . push ( name ) ;
1842+ }
1843+ else {
1844+ const includeIndex = findIndex ( includeFileRegexes , re => re . test ( absoluteName ) ) ;
1845+ if ( includeIndex !== - 1 ) {
1846+ results [ includeIndex ] . push ( name ) ;
18391847 }
18401848 }
18411849 }
1842- }
1843- }
1844-
1845- interface RecursiveDirectoryVisitor {
1846- useCaseSensitiveFileNames : boolean ;
1847- getFileSystemEntries : ( path : string ) => FileSystemEntries ;
1848- includeDirectory : ( absoluteDirectoryName : string ) => boolean ;
1849- visitFile : ( fileName : string , absoluteFileName : string ) => void ;
1850- }
1851-
1852- function forEachFileInRecursiveDirectories ( start : string , absoluteStart : string , visitor : RecursiveDirectoryVisitor ) : void {
1853- visitDirectory ( start , absoluteStart ) ;
1854-
1855- function visitDirectory ( path : string , absolutePath : string ) {
1856- let { files, directories } = visitor . getFileSystemEntries ( path ) ;
1857- files = sorted ( files ) ;
1858- directories = sorted ( directories ) ;
1859-
1860- for ( const file of files ) {
1861- visitor . visitFile ( combinePaths ( path , file ) , combinePaths ( absolutePath , file ) ) ;
1862- }
18631850
1864- for ( const dir of directories ) {
1865- const absoluteName = combinePaths ( absolutePath , dir ) ;
1866- if ( visitor . includeDirectory ( absoluteName ) ) {
1867- visitDirectory ( combinePaths ( path , dir ) , absoluteName ) ;
1851+ for ( const current of directories ) {
1852+ const name = combinePaths ( path , current ) ;
1853+ const absoluteName = combinePaths ( absolutePath , current ) ;
1854+ if ( ( ! includeDirectoryRegex || includeDirectoryRegex . test ( absoluteName ) ) &&
1855+ ( ! excludeRegex || ! excludeRegex . test ( absoluteName ) ) ) {
1856+ visitDirectory ( name , absoluteName ) ;
18681857 }
18691858 }
18701859 }
1871-
1872- function sorted ( names : string [ ] ) : string [ ] {
1873- return names . slice ( ) . sort ( visitor . useCaseSensitiveFileNames ? compareStrings : compareStringsCaseInsensitive ) ;
1874- }
18751860 }
18761861
18771862 /**
0 commit comments