Skip to content

Commit 9f477c7

Browse files
committed
Directly use picomatch
Optimize matching by caching the functions that micromatch would have generated internally.
1 parent d09161a commit 9f477c7

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

lib/globs.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
const path = require('path');
33
const globby = require('globby');
44
const ignoreByDefault = require('ignore-by-default');
5-
const micromatch = require('micromatch');
5+
const picomatch = require('picomatch');
66
const slash = require('slash');
77

88
const defaultIgnorePatterns = [...ignoreByDefault.directories(), '**/node_modules'];
9-
const defaultMicromatchIgnorePatterns = [
9+
const defaultPicomatchIgnorePatterns = [
1010
...defaultIgnorePatterns,
11-
// Unlike globby(), micromatch needs a complete pattern when ignoring directories.
11+
// Unlike globby(), picomatch needs a complete pattern when ignoring directories.
1212
...defaultIgnorePatterns.map(pattern => `${pattern}/**/*`)
1313
];
1414

15+
const defaultMatchNoIgnore = picomatch(defaultPicomatchIgnorePatterns);
16+
1517
const defaultIgnoredByWatcherPatterns = [
1618
'**/*.snap.md', // No need to rerun tests when the Markdown files change.
1719
'ava.config.js', // Config is not reloaded so avoid rerunning tests when it changes.
@@ -141,35 +143,37 @@ const matchingCache = new WeakMap();
141143
const processMatchingPatterns = input => {
142144
let result = matchingCache.get(input);
143145
if (!result) {
144-
const ignore = [...defaultMicromatchIgnorePatterns];
146+
const ignore = [...defaultPicomatchIgnorePatterns];
145147
const patterns = input.filter(pattern => {
146148
if (pattern.startsWith('!')) {
147-
// Unlike globby(), micromatch needs a complete pattern when ignoring directories.
149+
// Unlike globby(), picomatch needs a complete pattern when ignoring directories.
148150
ignore.push(pattern.slice(1), `${pattern.slice(1)}/**/*`);
149151
return false;
150152
}
151153

152154
return true;
153155
});
154156

155-
result = {patterns, ignore};
157+
result = {
158+
match: picomatch(patterns, {ignore}),
159+
matchNoIgnore: picomatch(patterns)
160+
};
156161
matchingCache.set(input, result);
157162
}
158163

159164
return result;
160165
};
161166

162167
function matches(file, patterns) {
163-
let ignore;
164-
({patterns, ignore} = processMatchingPatterns(patterns));
165-
return micromatch.some(file, patterns, {ignore});
168+
const {match} = processMatchingPatterns(patterns);
169+
return match(file);
166170
}
167171

168172
exports.matches = matches;
169173

170174
const matchesIgnorePatterns = (file, patterns) => {
171-
({patterns} = processMatchingPatterns(patterns));
172-
return micromatch.some(file, [...patterns, ...defaultMicromatchIgnorePatterns]);
175+
const {matchNoIgnore} = processMatchingPatterns(patterns);
176+
return matchNoIgnore(file) || defaultMatchNoIgnore(file);
173177
};
174178

175179
function normalizeFileForMatching(cwd, file) {

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@
8989
"lodash": "^4.17.15",
9090
"matcher": "^2.1.0",
9191
"md5-hex": "^3.0.1",
92-
"micromatch": "^4.0.2",
9392
"ms": "^2.1.2",
9493
"ora": "^4.0.3",
9594
"p-map": "^3.0.0",
95+
"picomatch": "^2.1.1",
9696
"pkg-conf": "^3.1.0",
9797
"plur": "^3.1.1",
9898
"pretty-ms": "^5.1.0",

0 commit comments

Comments
 (0)