@@ -3,6 +3,10 @@ import { type Options as FdirOptions, fdir } from 'fdir';
33import picomatch from 'picomatch' ;
44import { escapePath , getPartialMatcher , isDynamicPattern , splitPattern } from './utils.ts' ;
55
6+ const PARENT_DIRECTORY = / ^ ( \/ ? \. \. ) + / ;
7+ const ESCAPING_BACKSLASHES = / \\ (? = [ ( ) [ \] { } ! * + ? @ | ] ) / g;
8+ const BACKSLASHES = / \\ / g;
9+
610export interface GlobOptions {
711 absolute ?: boolean ;
812 cwd ?: string ;
@@ -40,13 +44,13 @@ function normalizePattern(
4044 result += '/**' ;
4145 }
4246
43- if ( path . isAbsolute ( result . replace ( / \\ (? = [ ( ) [ \] { } ! * + ? @ | ] ) / g , '' ) ) ) {
47+ if ( path . isAbsolute ( result . replace ( ESCAPING_BACKSLASHES , '' ) ) ) {
4448 result = posix . relative ( escapePath ( cwd ) , result ) ;
4549 } else {
4650 result = posix . normalize ( result ) ;
4751 }
4852
49- const parentDirectoryMatch = / ^ ( \/ ? \. \. ) + / . exec ( result ) ;
53+ const parentDirectoryMatch = PARENT_DIRECTORY . exec ( result ) ;
5054 if ( parentDirectoryMatch ?. [ 0 ] ) {
5155 const potentialRoot = posix . join ( cwd , parentDirectoryMatch [ 0 ] ) ;
5256 if ( properties . root . length > potentialRoot . length ) {
@@ -238,7 +242,7 @@ function crawl(options: GlobOptions, cwd: string, sync: boolean) {
238242 }
239243
240244 // backslashes are removed so that inferred roots like `C:/New folder \\(1\\)` work
241- properties . root = properties . root . replace ( / \\ / g , '' ) ;
245+ properties . root = properties . root . replace ( BACKSLASHES , '' ) ;
242246 const api = new fdir ( fdirOptions ) . crawl ( properties . root ) ;
243247
244248 if ( cwd === properties . root || options . absolute ) {
@@ -266,7 +270,7 @@ export async function glob(
266270 Array . isArray ( patternsOrOptions ) || typeof patternsOrOptions === 'string'
267271 ? { ...options , patterns : patternsOrOptions }
268272 : patternsOrOptions ;
269- const cwd = opts . cwd ? path . resolve ( opts . cwd ) . replace ( / \\ / g , '/' ) : process . cwd ( ) . replace ( / \\ / g , '/' ) ;
273+ const cwd = opts . cwd ? path . resolve ( opts . cwd ) . replace ( BACKSLASHES , '/' ) : process . cwd ( ) . replace ( BACKSLASHES , '/' ) ;
270274
271275 return crawl ( opts , cwd , false ) ;
272276}
@@ -282,7 +286,7 @@ export function globSync(patternsOrOptions: string | string[] | GlobOptions, opt
282286 Array . isArray ( patternsOrOptions ) || typeof patternsOrOptions === 'string'
283287 ? { ...options , patterns : patternsOrOptions }
284288 : patternsOrOptions ;
285- const cwd = opts . cwd ? path . resolve ( opts . cwd ) . replace ( / \\ / g , '/' ) : process . cwd ( ) . replace ( / \\ / g , '/' ) ;
289+ const cwd = opts . cwd ? path . resolve ( opts . cwd ) . replace ( BACKSLASHES , '/' ) : process . cwd ( ) . replace ( BACKSLASHES , '/' ) ;
286290
287291 return crawl ( opts , cwd , true ) ;
288292}
0 commit comments