Skip to content

Commit 8962374

Browse files
committed
fix: 🐛 use glob-to-regex library
1 parent 4f0cbff commit 8962374

File tree

3 files changed

+9
-75
lines changed

3 files changed

+9
-75
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
"dependencies": {
127127
"@jsonjoy.com/json-pack": "^1.11.0",
128128
"@jsonjoy.com/util": "^1.9.0",
129+
"glob-to-regex.js": "^1.0.1",
129130
"thingies": "^2.5.0",
130131
"tree-dump": "^1.0.3",
131132
"tslib": "^2.0.0"

src/node/glob.ts

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,16 @@
11
import * as pathModule from 'path';
2-
import { PathLike } from './types/misc';
2+
import {toRegex} from 'glob-to-regex.js';
33
import { IGlobOptions } from './types/options';
44
import { pathToFilename } from './util';
55
import Dirent from './Dirent';
66

7-
const { sep, join, relative, resolve } = pathModule.posix;
8-
9-
/**
10-
* Convert a glob pattern to a regular expression
11-
* Supports: *, ?, **, [abc], [!abc], [a-z]
12-
*/
13-
function globToRegex(pattern: string): RegExp {
14-
let regexStr = '';
15-
let i = 0;
16-
17-
while (i < pattern.length) {
18-
const char = pattern[i];
19-
20-
switch (char) {
21-
case '*':
22-
if (pattern[i + 1] === '*') {
23-
// Handle **
24-
if (pattern[i + 2] === '/' || i + 2 === pattern.length) {
25-
regexStr += '(?:.*\\/)?'; // Match zero or more directories
26-
i += pattern[i + 2] === '/' ? 3 : 2;
27-
} else {
28-
regexStr += '[^/]*'; // Single *
29-
i++;
30-
}
31-
} else {
32-
regexStr += '[^/]*'; // Single *
33-
i++;
34-
}
35-
break;
36-
case '?':
37-
regexStr += '[^/]';
38-
i++;
39-
break;
40-
case '[':
41-
regexStr += '[';
42-
i++;
43-
if (i < pattern.length && pattern[i] === '!') {
44-
regexStr += '^';
45-
i++;
46-
}
47-
while (i < pattern.length && pattern[i] !== ']') {
48-
if (pattern[i] === '\\') {
49-
regexStr += '\\\\';
50-
i++;
51-
}
52-
regexStr += pattern[i];
53-
i++;
54-
}
55-
regexStr += ']';
56-
i++;
57-
break;
58-
case '.':
59-
case '^':
60-
case '$':
61-
case '+':
62-
case '{':
63-
case '}':
64-
case '(':
65-
case ')':
66-
case '|':
67-
case '\\':
68-
regexStr += '\\' + char;
69-
i++;
70-
break;
71-
default:
72-
regexStr += char;
73-
i++;
74-
break;
75-
}
76-
}
77-
78-
return new RegExp('^' + regexStr + '$');
79-
}
7+
const { join, relative, resolve } = pathModule.posix;
808

819
/**
8210
* Check if a path matches a glob pattern
8311
*/
8412
function matchesPattern(path: string, pattern: string): boolean {
85-
const regex = globToRegex(pattern);
13+
const regex = toRegex(pattern);
8614
return regex.test(path);
8715
}
8816

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3282,6 +3282,11 @@ glob-parent@^5.1.2, glob-parent@~5.1.2:
32823282
dependencies:
32833283
is-glob "^4.0.1"
32843284

3285+
glob-to-regex.js@^1.0.1:
3286+
version "1.0.1"
3287+
resolved "https://registry.yarnpkg.com/glob-to-regex.js/-/glob-to-regex.js-1.0.1.tgz#f71cc9cb8441471a9318626160bc8a35e1306b21"
3288+
integrity sha512-CG/iEvgQqfzoVsMUbxSJcwbG2JwyZ3naEqPkeltwl0BSS8Bp83k3xlGms+0QdWFUAwV+uvo80wNswKF6FWEkKg==
3289+
32853290
glob-to-regexp@^0.4.1:
32863291
version "0.4.1"
32873292
resolved "https://registry.npmjs.org/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz"

0 commit comments

Comments
 (0)