mirror of
https://github.com/denoland/deno.git
synced 2024-11-25 15:29:32 -05:00
fix(std/path/globrex.ts): Use non-capturing groups in globrex() (#3898)
This commit is contained in:
parent
699d10bd9e
commit
971391dbaf
4 changed files with 32 additions and 32 deletions
|
@ -38,9 +38,14 @@ export interface GlobToRegExpOptions extends GlobOptions {
|
|||
*/
|
||||
export function globToRegExp(
|
||||
glob: string,
|
||||
options: GlobToRegExpOptions = {}
|
||||
{ extended = false, globstar = true }: GlobToRegExpOptions = {}
|
||||
): RegExp {
|
||||
const result = globrex(glob, { ...options, strict: false, filepath: true });
|
||||
const result = globrex(glob, {
|
||||
extended,
|
||||
globstar,
|
||||
strict: false,
|
||||
filepath: true
|
||||
});
|
||||
return result.path!.regex;
|
||||
}
|
||||
|
||||
|
|
|
@ -46,18 +46,6 @@ test({
|
|||
}
|
||||
});
|
||||
|
||||
testWalk(
|
||||
async (d: string): Promise<void> => {
|
||||
await mkdir(d + "/a");
|
||||
await touch(d + "/a/x.ts");
|
||||
},
|
||||
async function globInWalk(): Promise<void> {
|
||||
const arr = await walkArray(".", { match: [globToRegExp("*.ts")] });
|
||||
assertEquals(arr.length, 1);
|
||||
assertEquals(arr[0], "a/x.ts");
|
||||
}
|
||||
);
|
||||
|
||||
testWalk(
|
||||
async (d: string): Promise<void> => {
|
||||
await mkdir(d + "/a");
|
||||
|
@ -66,8 +54,10 @@ testWalk(
|
|||
await touch(d + "/b/z.ts");
|
||||
await touch(d + "/b/z.js");
|
||||
},
|
||||
async function globInWalkWildcardFiles(): Promise<void> {
|
||||
const arr = await walkArray(".", { match: [globToRegExp("*.ts")] });
|
||||
async function globInWalkWildcard(): Promise<void> {
|
||||
const arr = await walkArray(".", {
|
||||
match: [globToRegExp(join("*", "*.ts"))]
|
||||
});
|
||||
assertEquals(arr.length, 2);
|
||||
assertEquals(arr[0], "a/x.ts");
|
||||
assertEquals(arr[1], "b/z.ts");
|
||||
|
|
|
@ -3,13 +3,13 @@
|
|||
// Copyright (c) 2018 Terkel Gjervig Nielsen
|
||||
|
||||
const isWin = Deno.build.os === "win";
|
||||
const SEP = isWin ? `(\\\\+|\\/)` : `\\/`;
|
||||
const SEP = isWin ? `(?:\\\\|\\/)` : `\\/`;
|
||||
const SEP_ESC = isWin ? `\\\\` : `/`;
|
||||
const SEP_RAW = isWin ? `\\` : `/`;
|
||||
const GLOBSTAR = `((?:[^${SEP_ESC}/]*(?:${SEP_ESC}|\/|$))*)`;
|
||||
const WILDCARD = `([^${SEP_ESC}/]*)`;
|
||||
const GLOBSTAR = `(?:(?:[^${SEP_ESC}/]*(?:${SEP_ESC}|\/|$))*)`;
|
||||
const WILDCARD = `(?:[^${SEP_ESC}/]*)`;
|
||||
const GLOBSTAR_SEGMENT = `((?:[^${SEP_ESC}/]*(?:${SEP_ESC}|\/|$))*)`;
|
||||
const WILDCARD_SEGMENT = `([^${SEP_ESC}/]*)`;
|
||||
const WILDCARD_SEGMENT = `(?:[^${SEP_ESC}/]*)`;
|
||||
|
||||
export interface GlobrexOptions {
|
||||
// Allow ExtGlob features
|
||||
|
@ -57,6 +57,7 @@ export function globrex(
|
|||
flags = ""
|
||||
}: GlobrexOptions = {}
|
||||
): GlobrexResult {
|
||||
const sepPattern = new RegExp(`^${SEP}${strict ? "" : "+"}$`);
|
||||
let regex = "";
|
||||
let segment = "";
|
||||
let pathRegexStr = "";
|
||||
|
@ -84,7 +85,7 @@ export function globrex(
|
|||
const { split, last, only } = options;
|
||||
if (only !== "path") regex += str;
|
||||
if (filepath && only !== "regex") {
|
||||
pathRegexStr += str.match(new RegExp(`^${SEP}$`)) ? SEP : str;
|
||||
pathRegexStr += str.match(sepPattern) ? SEP : str;
|
||||
if (split) {
|
||||
if (last) segment += str;
|
||||
if (segment !== "") {
|
||||
|
@ -109,15 +110,15 @@ export function globrex(
|
|||
continue;
|
||||
}
|
||||
|
||||
if (c === "/") {
|
||||
add(`\\${c}`, { split: true });
|
||||
if (n === "/" && !strict) regex += "?";
|
||||
if (c.match(sepPattern)) {
|
||||
add(SEP, { split: true });
|
||||
if (n != null && n.match(sepPattern) && !strict) regex += "?";
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c === "(") {
|
||||
if (ext.length) {
|
||||
add(c);
|
||||
add(`${c}?:`);
|
||||
continue;
|
||||
}
|
||||
add(`\\${c}`);
|
||||
|
@ -131,7 +132,7 @@ export function globrex(
|
|||
if (type === "@") {
|
||||
add("{1}");
|
||||
} else if (type === "!") {
|
||||
add("([^/]*)");
|
||||
add(WILDCARD);
|
||||
} else {
|
||||
add(type as string);
|
||||
}
|
||||
|
@ -203,7 +204,7 @@ export function globrex(
|
|||
i++; // skip [
|
||||
let value = "";
|
||||
while (glob[++i] !== ":") value += glob[i];
|
||||
if (value === "alnum") add("(\\w|\\d)");
|
||||
if (value === "alnum") add("(?:\\w|\\d)");
|
||||
else if (value === "space") add("\\s");
|
||||
else if (value === "digit") add("\\d");
|
||||
i++; // skip last ]
|
||||
|
@ -231,7 +232,7 @@ export function globrex(
|
|||
if (c === "{") {
|
||||
if (extended) {
|
||||
inGroup = true;
|
||||
add("(");
|
||||
add("(?:");
|
||||
continue;
|
||||
}
|
||||
add(`\\${c}`);
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
import { test } from "../testing/mod.ts";
|
||||
import { assertEquals } from "../testing/asserts.ts";
|
||||
import { globrex } from "./globrex.ts";
|
||||
import { GlobrexOptions, globrex } from "./globrex.ts";
|
||||
|
||||
const isWin = Deno.build.os === "win";
|
||||
const t = { equal: assertEquals, is: assertEquals };
|
||||
|
@ -13,14 +13,18 @@ function match(
|
|||
glob: string,
|
||||
strUnix: string,
|
||||
strWin?: string | object,
|
||||
opts = {}
|
||||
opts: GlobrexOptions = {}
|
||||
): boolean {
|
||||
if (typeof strWin === "object") {
|
||||
opts = strWin;
|
||||
strWin = "";
|
||||
}
|
||||
const res = globrex(glob, opts);
|
||||
return res.regex.test(isWin && strWin ? strWin : strUnix);
|
||||
const { regex } = globrex(glob, opts);
|
||||
const match = (isWin && strWin ? strWin : strUnix).match(regex);
|
||||
if (match && !regex.flags.includes("g")) {
|
||||
assertEquals(match.length, 1);
|
||||
}
|
||||
return !!match;
|
||||
}
|
||||
|
||||
test({
|
||||
|
|
Loading…
Reference in a new issue