Skip to content

Commit

Permalink
optimize out .. pattern parts where they're redundant
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Feb 13, 2023
1 parent ee4861b commit 8c42d42
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ export class Minimatch {
// ** is * anyway
this.globParts = rawGlobParts
} else {
// do this swap BEFORE the reduce, so that we can turn a string
// of **/*/**/* into */*/**/** and then reduce the **'s into one
for (const parts of rawGlobParts) {
let swapped: boolean
do {
Expand All @@ -397,9 +399,17 @@ export class Minimatch {
}
this.globParts = rawGlobParts.map(parts =>
parts.reduce((set: string[], part) => {
if (part !== '**' || set[set.length - 1] !== '**') {
set.push(part)
const prev = set[set.length - 1]
if (part === '**' && prev === '**') {
return set
}
if (part === '..') {
if (prev && prev !== '..' && prev !== '.' && prev !== '**') {
set.pop()
return set
}
}
set.push(part)
return set
}, [])
)
Expand Down
12 changes: 12 additions & 0 deletions tap-snapshots/test/basic.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,18 @@ exports[`test/basic.js TAP basic tests > makeRe s/\\..*// 1`] = `
/^(?:s\\/(?=.)\\.\\.[^/]*?\\/)$/
`

exports[`test/basic.js TAP basic tests > makeRe x/*/../../a/b/c 1`] = `
/^(?:a\\/b\\/c)$/
`

exports[`test/basic.js TAP basic tests > makeRe x/*/../a/b/c 1`] = `
/^(?:x\\/a\\/b\\/c)$/
`

exports[`test/basic.js TAP basic tests > makeRe x/z/../*/a/b/c 1`] = `
/^(?:x\\/(?!\\.)(?=.)[^/]*?\\/a\\/b\\/c)$/
`

exports[`test/basic.js TAP basic tests > makeRe {/*,*} 1`] = `
/^(?:\\/(?!\\.)(?=.)[^/]*?|(?!\\.)(?=.)[^/]*?)$/
`
Expand Down
11 changes: 11 additions & 0 deletions test/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,17 @@ module.exports = [
['.a', '.a.js', '.js', 'a', 'a.js', 'js', 'a.JS', '.a.JS', '.JS', 'JS'],
{ dot: true },
],

() => (files = [
'x/y/z/a/b/c',
'x/y/a/b/c',
'x/z/a/b/c',
'x/a/b/c',
'a/b/c',
]),
['x/*/../a/b/c', ['x/a/b/c']],
['x/z/../*/a/b/c', ['x/y/a/b/c', 'x/z/a/b/c']],
['x/*/../../a/b/c', ['a/b/c']],
]

Object.defineProperty(module.exports, 'files', {
Expand Down

0 comments on commit 8c42d42

Please sign in to comment.