Repository navigation
Expand file tree
/
Copy pathglob.ts
More file actions
62 lines (53 loc) · 1.73 KB
/
Copy pathglob.ts
File metadata and controls
62 lines (53 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Minimal glob matching for `lory.exclude`.
*
* VS Code's own matcher is only reachable through `workspace.findFiles`, which
* searches the workspace. Exclusion is checked on every keystroke against a
* single known path, so this stays local, synchronous, and allocation-light.
*
* Supports the subset the exclude patterns actually use: `**`, `*`, `?`, and
* brace-free literals. Deliberately not a full glob implementation.
*/
const cache = new Map<string, RegExp>();
export function matchGlob(pattern: string, path: string): boolean {
return toRegExp(pattern).test(path.replace(/\\/g, '/'));
}
/** True when any pattern matches either path form. */
export function matchesAny(patterns: string[], ...paths: string[]): boolean {
return patterns.some((pattern) => paths.some((path) => matchGlob(pattern, path)));
}
function toRegExp(pattern: string): RegExp {
const cached = cache.get(pattern);
if (cached) {
return cached;
}
let out = '';
for (let i = 0; i < pattern.length; i++) {
const ch = pattern[i]!;
if (ch === '*') {
if (pattern[i + 1] === '*') {
// `**/` spans zero or more segments, so `**/x.js` matches a bare `x.js`
// as well as `a/b/x.js`. Without the optional group, excluding
// `**/node_modules/**` would miss a top-level `node_modules`.
if (pattern[i + 2] === '/') {
out += '(?:[^/]*\\/)*';
i += 2;
} else {
out += '.*';
i += 1;
}
} else {
out += '[^/]*';
}
continue;
}
if (ch === '?') {
out += '[^/]';
continue;
}
out += ch.replace(/[.+^${}()|[\]\\]/g, '\\$&');
}
const regex = new RegExp(`^${out}$`);
cache.set(pattern, regex);
return regex;
}