Skip to content

Commit

Permalink
fix: need two parts following a **/.. to optimize
Browse files Browse the repository at this point in the history
If **/../p is expanded to {**,..}/p, then that means that it can match p
even if the directory that contains p doesn't have any child directories
to have fallen up into its parent with the ..

However, if there are *two* parts following the **/.. then that means
that **/../p/q will mean that there must have been child directories in
p's parent, because p is a child directory in p's parent, and the
required walking overhead is cut in half as before.
  • Loading branch information
isaacs committed Feb 26, 2023
1 parent 77e1438 commit 695d5da
Show file tree
Hide file tree
Showing 3 changed files with 475 additions and 16 deletions.
21 changes: 17 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ const defaultPlatform: Platform = (
: 'posix'
) as Platform
type Sep = '\\' | '/'
const path:{[k:string]:{sep:Sep}} = { win32: { sep: '\\' }, posix: { sep: '/' } }
const path: { [k: string]: { sep: Sep } } = {
win32: { sep: '\\' },
posix: { sep: '/' },
}
/* c8 ignore stop */

export const sep = defaultPlatform === 'win32' ? path.win32.sep : path.posix.sep
Expand Down Expand Up @@ -538,7 +541,7 @@ export class Minimatch {
// and ** cannot be reduced out by a .. pattern part like a regexp
// or most strings (other than .., ., and '') can be.
//
// <pre>/**/../<p>/<rest> -> {<pre>/../<p>/<rest>,<pre>/**/<p>/<rest>}
// <pre>/**/../<p>/<p>/<rest> -> {<pre>/../<p>/<p>/<rest>,<pre>/**/<p>/<p>/<rest>}
// <pre>/<e>/<rest> -> <pre>/<rest>
// <pre>/<p>/../<rest> -> <pre>/<rest>
// **/**/<rest> -> **/<rest>
Expand All @@ -549,7 +552,7 @@ export class Minimatch {
let didSomething = false
do {
didSomething = false
// <pre>/**/../<p>/<rest> -> {<pre>/../<p>/<rest>,<pre>/**/<p>/<rest>}
// <pre>/**/../<p>/<p>/<rest> -> {<pre>/../<p>/<p>/<rest>,<pre>/**/<p>/<p>/<rest>}
for (let parts of globParts) {
let gs: number = -1
while (-1 !== (gs = parts.indexOf('**', gs + 1))) {
Expand All @@ -566,8 +569,18 @@ export class Minimatch {

let next = parts[gs + 1]
const p = parts[gs + 2]
const p2 = parts[gs + 3]
if (next !== '..') continue
if (!p || p === '.' || p === '..') continue
if (
!p ||
p === '.' ||
p === '..' ||
!p2 ||
p2 === '.' ||
p2 === '..'
) {
continue
}
didSomething = true
// edit parts in place, and push the new one
parts.splice(gs, 1)
Expand Down
Loading

0 comments on commit 695d5da

Please sign in to comment.