Skip to content

Commit

Permalink
fix crawling parent directories
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperchupuDev committed Feb 19, 2025
1 parent cd92982 commit d5fb84c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface PartialMatcherOptions {
nocase?: boolean;
}

const ONLY_PARENT_DIRECTORIES = /^(\/?\.\.)+$/;

// the result of over 4 months of figuring stuff out and a LOT of help
export function getPartialMatcher(patterns: string[], options?: PartialMatcherOptions): Matcher {
// you might find this code pattern odd, but apparently it's faster than using `.push()`
Expand All @@ -25,6 +27,13 @@ export function getPartialMatcher(patterns: string[], options?: PartialMatcherOp
return (input: string) => {
// no need to `splitPattern` as this is indeed not a pattern
const inputParts = input.split('/');
// if we only have patterns like `src/*` but the input is `../..`
// normally the parent directory would not get crawled
// and as such wrong results would be returned
// to avoid this always return true if the input only consists of .. ../.. etc
if (inputParts[0] === '..' && ONLY_PARENT_DIRECTORIES.test(input)) {
return true;
}
for (let i = 0; i < patterns.length; i++) {
const patternParts = patternsParts[i];
const regex = regexes[i];
Expand Down
7 changes: 6 additions & 1 deletion test/utils/partial-matcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ describe('getPartialMatcher', () => {

test("** doesn't match ..", () => {
const matcher = getPartialMatcher(['**']);
assert.ok(!matcher('..'));
assert.ok(!matcher('../hi'));
});

test('always match inputs with only parent directories', () => {
const matcher = getPartialMatcher(['**']);
assert.ok(matcher('../../..'));
});

test('for now treats parts with / as **', () => {
Expand Down

0 comments on commit d5fb84c

Please sign in to comment.