Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions fs/expand_glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export async function* expandGlob(
let fixedRoot = isGlobAbsolute ? winRoot ?? "/" : absRoot;
while (segments.length > 0 && !isGlob(segments[0]!)) {
const seg = segments.shift()!;
fixedRoot = joinGlobs([fixedRoot, seg], globOptions);
fixedRoot = joinGlobs([fixedRoot, unescapeGlobSegment(seg)], globOptions);
}

let fixedRootInfo: WalkEntry;
Expand Down Expand Up @@ -460,7 +460,7 @@ export function* expandGlobSync(
let fixedRoot = isGlobAbsolute ? winRoot ?? "/" : absRoot;
while (segments.length > 0 && !isGlob(segments[0]!)) {
const seg = segments.shift()!;
fixedRoot = joinGlobs([fixedRoot, seg], globOptions);
fixedRoot = joinGlobs([fixedRoot, unescapeGlobSegment(seg)], globOptions);
}

let fixedRootInfo: WalkEntry;
Expand Down Expand Up @@ -532,3 +532,29 @@ export function* expandGlobSync(
}
yield* currentMatches;
}

const globEscapeChar = Deno.build.os === "windows" ? "`" : `\\`;
const globMetachars = "*?{}[]()|+@!";
function unescapeGlobSegment(segment: string): string {
let result = "";
let lastIndex = 0;
for (let i = 0; i < segment.length; i++) {
const char = segment[i];
if (char === globEscapeChar) {
const nextChar = segment[i + 1];
if (nextChar && globMetachars.includes(nextChar)) {
// append the slice before the escape char, then the metachar
result += segment.slice(lastIndex, i) + nextChar;
i++; // skip next char since we already processed it
lastIndex = i + 1;
}
}
}
// no escaped, return the original segment
if (lastIndex === 0) {
return segment;
}
// append any remaining characters
result += segment.slice(lastIndex);
return result;
}
24 changes: 24 additions & 0 deletions fs/expand_glob_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,27 @@ Deno.test(
);
},
);

const escapeChar = Deno.build.os === "windows" ? "`" : "\\";

Deno.test("expandGlob() finds directory with escaped brackets", async function () {
assertEquals(
await expandGlobArray(`a${escapeChar}[b${escapeChar}]c`, EG_OPTIONS),
["a[b]c"],
);
assertEquals(
await expandGlobArray(`a${escapeChar}[b${escapeChar}]c/fo[o]`, EG_OPTIONS),
[join("a[b]c", "foo")],
);
});

Deno.test("expandGlobSync() finds directory with escaped brackets", function () {
assertEquals(
expandGlobSyncArray(`a${escapeChar}[b${escapeChar}]c`, EG_OPTIONS),
["a[b]c"],
);
assertEquals(
expandGlobSyncArray(`a${escapeChar}[b${escapeChar}]c/fo[o]`, EG_OPTIONS),
[join("a[b]c", "foo")],
);
});
Loading