minimatch
Advanced tools
Comparing version 7.0.1 to 7.1.0
@@ -18,2 +18,3 @@ export interface MinimatchOptions { | ||
preserveMultipleSlashes?: boolean; | ||
optimizationLevel?: number; | ||
} | ||
@@ -65,2 +66,5 @@ export declare const minimatch: { | ||
preprocess(globParts: string[][]): string[][]; | ||
adjascentGlobstarOptimize(globParts: string[][]): string[][]; | ||
levelOneOptimize(globParts: string[][]): string[][]; | ||
levelTwoFileOptimize(parts: string | string[]): string[]; | ||
firstPhasePreProcess(globParts: string[][]): string[][]; | ||
@@ -67,0 +71,0 @@ secondPhasePreProcess(globParts: string[][]): string[][]; |
@@ -308,6 +308,91 @@ "use strict"; | ||
} | ||
globParts = this.firstPhasePreProcess(globParts); | ||
globParts = this.secondPhasePreProcess(globParts); | ||
const { optimizationLevel = 1 } = this.options; | ||
if (optimizationLevel >= 2) { | ||
// aggressive optimization for the purpose of fs walking | ||
globParts = this.firstPhasePreProcess(globParts); | ||
globParts = this.secondPhasePreProcess(globParts); | ||
} | ||
else if (optimizationLevel >= 1) { | ||
// just basic optimizations to remove some .. parts | ||
globParts = this.levelOneOptimize(globParts); | ||
} | ||
else { | ||
globParts = this.adjascentGlobstarOptimize(globParts); | ||
} | ||
return globParts; | ||
} | ||
// just get rid of adjascent ** portions | ||
adjascentGlobstarOptimize(globParts) { | ||
return globParts.map(parts => { | ||
let gs = -1; | ||
while (-1 !== (gs = parts.indexOf('**', gs + 1))) { | ||
let i = gs; | ||
while (parts[i + 1] === '**') { | ||
i++; | ||
} | ||
if (i !== gs) { | ||
parts.splice(gs, i - gs); | ||
} | ||
} | ||
return parts; | ||
}); | ||
} | ||
// get rid of adjascent ** and resolve .. portions | ||
levelOneOptimize(globParts) { | ||
return globParts.map(parts => { | ||
parts = parts.reduce((set, 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; | ||
}, []); | ||
return parts.length === 0 ? [''] : parts; | ||
}); | ||
} | ||
levelTwoFileOptimize(parts) { | ||
if (!Array.isArray(parts)) { | ||
parts = this.slashSplit(parts); | ||
} | ||
let didSomething = false; | ||
do { | ||
didSomething = false; | ||
// <pre>/<e>/<rest> -> <pre>/<rest> | ||
if (!this.preserveMultipleSlashes) { | ||
for (let i = 1; i < parts.length - 1; i++) { | ||
const p = parts[i]; | ||
// don't squeeze out UNC patterns | ||
if (i === 1 && p === '' && parts[0] === '') | ||
continue; | ||
if (p === '.' || p === '') { | ||
didSomething = true; | ||
parts.splice(i, 1); | ||
i--; | ||
} | ||
} | ||
if (parts[0] === '.') { | ||
didSomething = true; | ||
parts.shift(); | ||
} | ||
} | ||
// <pre>/<p>/../<rest> -> <pre>/<rest> | ||
let dd = 0; | ||
while (-1 !== (dd = parts.indexOf('..', dd + 1))) { | ||
const p = parts[dd - 1]; | ||
if (p && p !== '.' && p !== '..' && p !== '**') { | ||
didSomething = true; | ||
parts.splice(dd - 1, 2); | ||
dd -= 2; | ||
} | ||
} | ||
} while (didSomething); | ||
return parts.length === 0 ? [''] : parts; | ||
} | ||
// First phase: single-pattern processing | ||
@@ -522,2 +607,8 @@ // <pre> is 1 or more portions | ||
} | ||
// resolve and reduce . and .. portions in the file as well. | ||
// dont' need to do the second phase, because it's only one string[] | ||
const { optimizationLevel = 1 } = this.options; | ||
if (optimizationLevel >= 2) { | ||
file = this.levelTwoFileOptimize(file); | ||
} | ||
this.debug('matchOne', this, { file, pattern }); | ||
@@ -620,5 +711,2 @@ this.debug('matchOne', file.length, pattern.length); | ||
let hit; | ||
while (f === '.' && fi < fl - 1) { | ||
f = file[++fi]; | ||
} | ||
if (typeof p === 'string') { | ||
@@ -625,0 +713,0 @@ hit = f === p; |
@@ -18,2 +18,3 @@ export interface MinimatchOptions { | ||
preserveMultipleSlashes?: boolean; | ||
optimizationLevel?: number; | ||
} | ||
@@ -65,2 +66,5 @@ export declare const minimatch: { | ||
preprocess(globParts: string[][]): string[][]; | ||
adjascentGlobstarOptimize(globParts: string[][]): string[][]; | ||
levelOneOptimize(globParts: string[][]): string[][]; | ||
levelTwoFileOptimize(parts: string | string[]): string[]; | ||
firstPhasePreProcess(globParts: string[][]): string[][]; | ||
@@ -67,0 +71,0 @@ secondPhasePreProcess(globParts: string[][]): string[][]; |
@@ -296,6 +296,91 @@ export const minimatch = (p, pattern, options = {}) => { | ||
} | ||
globParts = this.firstPhasePreProcess(globParts); | ||
globParts = this.secondPhasePreProcess(globParts); | ||
const { optimizationLevel = 1 } = this.options; | ||
if (optimizationLevel >= 2) { | ||
// aggressive optimization for the purpose of fs walking | ||
globParts = this.firstPhasePreProcess(globParts); | ||
globParts = this.secondPhasePreProcess(globParts); | ||
} | ||
else if (optimizationLevel >= 1) { | ||
// just basic optimizations to remove some .. parts | ||
globParts = this.levelOneOptimize(globParts); | ||
} | ||
else { | ||
globParts = this.adjascentGlobstarOptimize(globParts); | ||
} | ||
return globParts; | ||
} | ||
// just get rid of adjascent ** portions | ||
adjascentGlobstarOptimize(globParts) { | ||
return globParts.map(parts => { | ||
let gs = -1; | ||
while (-1 !== (gs = parts.indexOf('**', gs + 1))) { | ||
let i = gs; | ||
while (parts[i + 1] === '**') { | ||
i++; | ||
} | ||
if (i !== gs) { | ||
parts.splice(gs, i - gs); | ||
} | ||
} | ||
return parts; | ||
}); | ||
} | ||
// get rid of adjascent ** and resolve .. portions | ||
levelOneOptimize(globParts) { | ||
return globParts.map(parts => { | ||
parts = parts.reduce((set, 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; | ||
}, []); | ||
return parts.length === 0 ? [''] : parts; | ||
}); | ||
} | ||
levelTwoFileOptimize(parts) { | ||
if (!Array.isArray(parts)) { | ||
parts = this.slashSplit(parts); | ||
} | ||
let didSomething = false; | ||
do { | ||
didSomething = false; | ||
// <pre>/<e>/<rest> -> <pre>/<rest> | ||
if (!this.preserveMultipleSlashes) { | ||
for (let i = 1; i < parts.length - 1; i++) { | ||
const p = parts[i]; | ||
// don't squeeze out UNC patterns | ||
if (i === 1 && p === '' && parts[0] === '') | ||
continue; | ||
if (p === '.' || p === '') { | ||
didSomething = true; | ||
parts.splice(i, 1); | ||
i--; | ||
} | ||
} | ||
if (parts[0] === '.') { | ||
didSomething = true; | ||
parts.shift(); | ||
} | ||
} | ||
// <pre>/<p>/../<rest> -> <pre>/<rest> | ||
let dd = 0; | ||
while (-1 !== (dd = parts.indexOf('..', dd + 1))) { | ||
const p = parts[dd - 1]; | ||
if (p && p !== '.' && p !== '..' && p !== '**') { | ||
didSomething = true; | ||
parts.splice(dd - 1, 2); | ||
dd -= 2; | ||
} | ||
} | ||
} while (didSomething); | ||
return parts.length === 0 ? [''] : parts; | ||
} | ||
// First phase: single-pattern processing | ||
@@ -510,2 +595,8 @@ // <pre> is 1 or more portions | ||
} | ||
// resolve and reduce . and .. portions in the file as well. | ||
// dont' need to do the second phase, because it's only one string[] | ||
const { optimizationLevel = 1 } = this.options; | ||
if (optimizationLevel >= 2) { | ||
file = this.levelTwoFileOptimize(file); | ||
} | ||
this.debug('matchOne', this, { file, pattern }); | ||
@@ -608,5 +699,2 @@ this.debug('matchOne', file.length, pattern.length); | ||
let hit; | ||
while (f === '.' && fi < fl - 1) { | ||
f = file[++fi]; | ||
} | ||
if (typeof p === 'string') { | ||
@@ -613,0 +701,0 @@ hit = f === p; |
@@ -5,3 +5,3 @@ { | ||
"description": "a glob matcher in javascript", | ||
"version": "7.0.1", | ||
"version": "7.1.0", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -267,7 +267,64 @@ # minimatch | ||
### optimizationLevel | ||
A number indicating the level of optimization that should be done | ||
to the pattern prior to parsing and using it for matches. | ||
Globstar parts `**` are always converted to `*` when `noglobstar` | ||
is set, and multiple adjascent `**` parts are converted into a | ||
single `**` (ie, `a/**/**/b` will be treated as `a/**/b`, as this | ||
is equivalent in all cases). | ||
- `0` - Make no further changes. In this mode, `.` and `..` are | ||
maintained in the pattern, meaning that they must also appear | ||
in the same position in the test path string. Eg, a pattern | ||
like `a/*/../c` will match the string `a/b/../c` but not the | ||
string `a/c`. | ||
- `1` - (default) Remove cases where a double-dot `..` follows a | ||
pattern portion that is not `**`, `.`, `..`, or empty `''`. For | ||
example, the pattern `./a/b/../*` is converted to `./a/*`, and | ||
so it will match the path string `./a/c`, but not the path | ||
string `./a/b/../c`. Dots and empty path portions in the | ||
pattern are preserved. | ||
- `2` (or higher) - Much more aggressive optimizations, suitable | ||
for use with file-walking cases: | ||
- Remove cases where a double-dot `..` follows a pattern | ||
portion that is not `**`, `.`, or empty `''`. Remove empty | ||
and `.` portions of the pattern, where safe to do so (ie, | ||
anywhere other than the last position, the first position, or | ||
the second position in a pattern starting with `/`, as this | ||
may indicate a UNC path on Windows). | ||
- Convert patterns containing `<pre>/**/../<p>/<rest>` into the | ||
equivalent `<pre>/{..,**}/<p>/<rest>`, where `<p>` is a | ||
a pattern portion other than `.`, `..`, `**`, or empty | ||
`''`. | ||
- Dedupe patterns where a `**` portion is present in one and | ||
omitted in another, and it is not the final path portion, and | ||
they are otherwise equivalent. So `{a/**/b,a/b}` becomes | ||
`a/**/b`, because `**` matches against an empty path portion. | ||
- Dedupe patterns where a `*` portion is present in one, and a | ||
non-dot pattern other than `**`, `.`, `..`, or `''` is in the | ||
same position in the other. So `a/{*,x}/b` becomes `a/*/b`, | ||
because `*` can match against `x`. | ||
While these optimizations improve the performance of | ||
file-walking use cases such as [glob](http://npm.im/glob) (ie, | ||
the reason this module exists), there are cases where it will | ||
fail to match a literal string that would have been matched in | ||
optimization level 1 or 0. | ||
Specifically, while the `Minimatch.match()` method will | ||
optimize the file path string in the same ways, resulting in | ||
the same matches, it will fail when tested with the regular | ||
expression provided by `Minimatch.makeRe()`, unless the path | ||
string is first processed with | ||
`minimatch.levelTwoFileOptimize()` or similar. | ||
## Comparisons to other fnmatch/glob implementations | ||
While strict compliance with the existing standards is a worthwhile | ||
goal, some discrepancies exist between minimatch and other | ||
implementations, and are intentional. | ||
While strict compliance with the existing standards is a | ||
worthwhile goal, some discrepancies exist between minimatch and | ||
other implementations. Some are intentional, and some are | ||
unavoidable. | ||
@@ -304,2 +361,17 @@ If the pattern starts with a `!` character, then it is negated. Set the | ||
Negated extglob patterns are handled as closely as possible to | ||
Bash semantics, but there are some cases with negative extglobs | ||
which are exceedingly difficult to express in a JavaScript | ||
regular expression. In particular the negated pattern | ||
`<start>!(<pattern>*|)*` will in bash match anything that does | ||
not start with `<start><pattern>`. However, | ||
`<start>!(<pattern>*)*` _will_ match paths starting with | ||
`<start><pattern>`, because the empty string can match against | ||
the negated portion. In this library, `<start>!(<pattern>*|)*` | ||
will _not_ match any pattern starting with `<start>`, due to a | ||
difference in precisely which patterns are considered "greedy" in | ||
Regular Expressions vs bash path expansion. This may be fixable, | ||
but not without incurring some complexity and performance costs, | ||
and the trade-off seems to not be worth pursuing. | ||
Note that `fnmatch(3)` in libc is an extremely naive string comparison | ||
@@ -306,0 +378,0 @@ matcher, which does not do anything special for slashes. This library is |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
380
212850
14
2796