Skip to content

Commit 9f4a4ff

Browse files
Brian Hufisaacs
authored andcommitted
Fix makeRe() when partial: true
PR-URL: #272 Credit: @BrianHuf Close: #272 Reviewed-by: @isaacs
1 parent 91f1959 commit 9f4a4ff

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/index.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,20 @@ export class Minimatch {
10801080
pp[i + 1] = GLOBSTAR
10811081
}
10821082
})
1083-
return pp.filter(p => p !== GLOBSTAR).join('/')
1083+
const filtered = pp.filter(p => p !== GLOBSTAR)
1084+
1085+
// For partial matches, we need to make the pattern match
1086+
// any prefix of the full path. We do this by generating
1087+
// alternative patterns that match progressively longer prefixes.
1088+
if (this.partial && filtered.length >= 1) {
1089+
const prefixes: string[] = []
1090+
for (let i = 1; i <= filtered.length; i++) {
1091+
prefixes.push(filtered.slice(0, i).join('/'))
1092+
}
1093+
return '(?:' + prefixes.join('|') + ')'
1094+
}
1095+
1096+
return filtered.join('/')
10841097
})
10851098
.join('|')
10861099

@@ -1090,6 +1103,11 @@ export class Minimatch {
10901103
// must match entire pattern
10911104
// ending in a * or ** will make it less strict.
10921105
re = '^' + open + re + close + '$'
1106+
1107+
// In partial mode, '/' should always match as it's a valid prefix for any pattern
1108+
if (this.partial) {
1109+
re = '^(?:\\/|' + open + re.slice(1, -1) + close + ')$'
1110+
}
10931111

10941112
// can match anything, as long as it's not this.
10951113
if (this.negate) re = '^(?!' + re + ').+$'

test/partial.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ t.equal(mm('/a/b/c', '/*/b/x/y/z', { partial: true }), false)
55
t.equal(mm('/', 'x', { partial: true }), true)
66
const m = new mm.Minimatch('/*/b/x/y/z')
77
t.equal(m.match('/a/b', true), true)
8+
9+
t.equal(mm.makeRe('/*/b/x/y/z', { partial: true }).test('/a/b'), true);
10+
t.equal(mm.makeRe('/*/b/x/y/z', { partial: true }).test('/a/b/c'), false);
11+
t.equal(mm.makeRe('x', { partial: true }).test('/'), true);

0 commit comments

Comments
 (0)