diff options
author | Nayeem Rahman <muhammed.9939@gmail.com> | 2020-02-06 22:25:39 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-06 17:25:39 -0500 |
commit | 971391dbaf8f5273eb2f16edd22b0c6cf694be46 (patch) | |
tree | 901a658522b9080221bb931c1504361c90ec8d18 /std/path | |
parent | 699d10bd9e5f19ad2f4ffb82225c86690a092c07 (diff) |
fix(std/path/globrex.ts): Use non-capturing groups in globrex() (#3898)
Diffstat (limited to 'std/path')
-rw-r--r-- | std/path/glob.ts | 9 | ||||
-rw-r--r-- | std/path/glob_test.ts | 18 | ||||
-rw-r--r-- | std/path/globrex.ts | 25 | ||||
-rw-r--r-- | std/path/globrex_test.ts | 12 |
4 files changed, 32 insertions, 32 deletions
diff --git a/std/path/glob.ts b/std/path/glob.ts index faccb8d77..daee22f98 100644 --- a/std/path/glob.ts +++ b/std/path/glob.ts @@ -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; } diff --git a/std/path/glob_test.ts b/std/path/glob_test.ts index 6cacd2eeb..7f67ca219 100644 --- a/std/path/glob_test.ts +++ b/std/path/glob_test.ts @@ -49,25 +49,15 @@ 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"); await mkdir(d + "/b"); await touch(d + "/a/x.ts"); 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"); diff --git a/std/path/globrex.ts b/std/path/globrex.ts index 5bf0e16a0..695294834 100644 --- a/std/path/globrex.ts +++ b/std/path/globrex.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}`); diff --git a/std/path/globrex_test.ts b/std/path/globrex_test.ts index 31607216d..f091ebe9b 100644 --- a/std/path/globrex_test.ts +++ b/std/path/globrex_test.ts @@ -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({ |