summaryrefslogtreecommitdiff
path: root/fs
diff options
context:
space:
mode:
Diffstat (limited to 'fs')
-rw-r--r--fs/glob_test.ts6
-rw-r--r--fs/globrex.ts24
-rw-r--r--fs/globrex_test.ts21
-rw-r--r--fs/path/mod.ts4
-rw-r--r--fs/path/parse_format_test.ts67
-rw-r--r--fs/walk.ts93
-rw-r--r--fs/walk_test.ts32
7 files changed, 124 insertions, 123 deletions
diff --git a/fs/glob_test.ts b/fs/glob_test.ts
index 2dc509dee..fc3704ae3 100644
--- a/fs/glob_test.ts
+++ b/fs/glob_test.ts
@@ -14,16 +14,16 @@ async function touch(path: string): Promise<void> {
async function walkArray(
dirname: string = ".",
options: WalkOptions = {}
-): Promise<Array<string>> {
+): Promise<string[]> {
const arr: string[] = [];
for await (const f of walk(dirname, { ...options })) {
arr.push(f.path.replace(/\\/g, "/"));
}
arr.sort();
- const arr_sync = Array.from(walkSync(dirname, options), (f: FileInfo) =>
+ const arrSync = Array.from(walkSync(dirname, options), (f: FileInfo) =>
f.path.replace(/\\/g, "/")
).sort();
- assertEquals(arr, arr_sync);
+ assertEquals(arr, arrSync);
return arr;
}
diff --git a/fs/globrex.ts b/fs/globrex.ts
index 7f05146bd..c3cf7a337 100644
--- a/fs/globrex.ts
+++ b/fs/globrex.ts
@@ -12,6 +12,15 @@ const WILDCARD = `([^/]*)`;
const GLOBSTAR_SEGMENT = `((?:[^${SEP_ESC}]*(?:${SEP_ESC}|$))*)`;
const WILDCARD_SEGMENT = `([^${SEP_ESC}]*)`;
+export interface GlobrexResult {
+ regex: RegExp;
+ path?: {
+ regex: string | RegExp;
+ segments: RegExp[];
+ globstar?: RegExp;
+ };
+}
+
/**
* Convert any glob pattern to a JavaScript Regexp object
* @param {String} glob Glob pattern to convert
@@ -32,12 +41,12 @@ export function globrex(
filepath = false,
flags = ""
}: GlobOptions = {}
-) {
+): GlobrexResult {
let regex = "";
let segment = "";
let path: {
regex: string | RegExp;
- segments: Array<RegExp>;
+ segments: RegExp[];
globstar?: RegExp;
} = { regex: "", segments: [] };
@@ -59,7 +68,7 @@ export function globrex(
function add(
str,
options: AddOptions = { split: false, last: false, only: "" }
- ) {
+ ): void {
const { split, last, only } = options;
if (only !== "path") regex += str;
if (filepath && only !== "regex") {
@@ -283,14 +292,7 @@ export function globrex(
if (filepath) path.regex = `^${path.regex}$`;
}
- const result: {
- regex: RegExp;
- path?: {
- regex: string | RegExp;
- segments: Array<RegExp>;
- globstar?: RegExp;
- };
- } = { regex: new RegExp(regex, flags) };
+ const result: GlobrexResult = { regex: new RegExp(regex, flags) };
// Push the last segment
if (filepath) {
diff --git a/fs/globrex_test.ts b/fs/globrex_test.ts
index df36dc550..8d43b5352 100644
--- a/fs/globrex_test.ts
+++ b/fs/globrex_test.ts
@@ -4,12 +4,12 @@
import { test } from "../testing/mod.ts";
import { assertEquals } from "../testing/asserts.ts";
-import { globrex } from "./globrex.ts";
+import { globrex, GlobrexResult } from "./globrex.ts";
const isWin = Deno.build.os === "win";
const t = { equal: assertEquals, is: assertEquals };
-function match(glob, strUnix, strWin?, opts = {}) {
+function match(glob, strUnix, strWin?, opts = {}): boolean {
if (typeof strWin === "object") {
opts = strWin;
strWin = false;
@@ -18,14 +18,14 @@ function match(glob, strUnix, strWin?, opts = {}) {
return res.regex.test(isWin && strWin ? strWin : strUnix);
}
-function matchRegex(t, pattern, ifUnix, ifWin, opts) {
+function matchRegex(t, pattern, ifUnix, ifWin, opts): GlobrexResult {
const res = globrex(pattern, opts);
const { regex } = opts.filepath ? res.path : res;
t.is(regex.toString(), isWin ? ifWin : ifUnix, "~> regex matches expectant");
return res;
}
-function matchSegments(t, pattern, ifUnix, ifWin, opts) {
+function matchSegments(t, pattern, ifUnix, ifWin, opts): GlobrexResult {
const res = globrex(pattern, { filepath: true, ...opts });
const str = res.path.segments.join(" ");
const exp = (isWin ? ifWin : ifUnix).join(" ");
@@ -191,7 +191,7 @@ test({
t.equal(match("f?o", "fooo", { extended: true }), false);
t.equal(match("f?oo", "foo", { extended: true }), false);
- const tester = globstar => {
+ const tester = (globstar): void => {
t.equal(
match("f?o", "foo", { extended: true, globstar, flags: "g" }),
true
@@ -235,7 +235,7 @@ test({
t.equal(match("fo[!tz]", "fot", { extended: true }), false);
t.equal(match("fo[!tz]", "fob", { extended: true }), true);
- const tester = globstar => {
+ const tester = (globstar): void => {
t.equal(
match("fo[oz]", "foo", { extended: true, globstar, flags: "g" }),
true
@@ -321,7 +321,7 @@ test({
t.equal(match("foo{bar,baaz}", "foobuzz", { extended: true }), false);
t.equal(match("foo{bar,b*z}", "foobuzz", { extended: true }), true);
- const tester = globstar => {
+ const tester = (globstar): void => {
t.equal(
match("foo{bar,baaz}", "foobaaz", {
extended: true,
@@ -405,7 +405,7 @@ test({
false
);
- const tester = globstar => {
+ const tester = (globstar): void => {
t.equal(
match(
"http://?o[oz].b*z.com/{*.js,*.html}",
@@ -456,7 +456,7 @@ test({
test({
name: "globrex: standard globstar",
fn() {
- const tester = globstar => {
+ const tester = (globstar): void => {
t.equal(
match(
"http://foo.com/**/{*.js,*.html}",
@@ -491,7 +491,7 @@ test({
test({
name: "globrex: remaining chars should match themself",
fn() {
- const tester = globstar => {
+ const tester = (globstar): void => {
const testExtStr = "\\/$^+.()=!|,.*";
t.equal(match(testExtStr, testExtStr, { extended: true }), true);
t.equal(
@@ -849,7 +849,6 @@ test({
name: "globrex: filepath path segments",
fn() {
let opts = { extended: true },
- res,
win,
unix;
diff --git a/fs/path/mod.ts b/fs/path/mod.ts
index 4ca630ed6..ffcc721a9 100644
--- a/fs/path/mod.ts
+++ b/fs/path/mod.ts
@@ -847,7 +847,7 @@ export const win32 = {
parse(path: string): ParsedPath {
assertPath(path);
- let ret = { root: "", dir: "", base: "", ext: "", name: "" } as ParsedPath;
+ let ret: ParsedPath = { root: "", dir: "", base: "", ext: "", name: "" };
const len = path.length;
if (len === 0) return ret;
@@ -1324,7 +1324,7 @@ export const posix = {
parse(path: string): ParsedPath {
assertPath(path);
- let ret = { root: "", dir: "", base: "", ext: "", name: "" } as ParsedPath;
+ let ret: ParsedPath = { root: "", dir: "", base: "", ext: "", name: "" };
if (path.length === 0) return ret;
let isAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
let start: number;
diff --git a/fs/path/parse_format_test.ts b/fs/path/parse_format_test.ts
index 29d840453..05327a139 100644
--- a/fs/path/parse_format_test.ts
+++ b/fs/path/parse_format_test.ts
@@ -79,6 +79,39 @@ const unixSpecialCaseFormatTests = [
[{}, ""]
];
+function checkParseFormat(path, paths): void {
+ paths.forEach(function(p) {
+ const element = p[0];
+ const output = path.parse(element);
+ assertEquals(typeof output.root, "string");
+ assertEquals(typeof output.dir, "string");
+ assertEquals(typeof output.base, "string");
+ assertEquals(typeof output.ext, "string");
+ assertEquals(typeof output.name, "string");
+ assertEquals(path.format(output), element);
+ assertEquals(output.rooroot, undefined);
+ assertEquals(output.dir, output.dir ? path.dirname(element) : "");
+ assertEquals(output.base, path.basename(element));
+ });
+}
+
+function checkSpecialCaseParseFormat(path, testCases): void {
+ testCases.forEach(function(testCase) {
+ const element = testCase[0];
+ const expect = testCase[1];
+ const output = path.parse(element);
+ Object.keys(expect).forEach(function(key) {
+ assertEquals(output[key], expect[key]);
+ });
+ });
+}
+
+function checkFormat(path, testCases): void {
+ testCases.forEach(function(testCase) {
+ assertEquals(path.format(testCase[0]), testCase[1]);
+ });
+}
+
test(function parseWin32() {
checkParseFormat(path.win32, winPaths);
checkSpecialCaseParseFormat(path.win32, winSpecialCaseParseTests);
@@ -116,6 +149,7 @@ const windowsTrailingTests = [
}
]
];
+
const posixTrailingTests = [
["./", { root: "", dir: "", base: ".", ext: "", name: "." }],
["//", { root: "/", dir: "/", base: "", ext: "", name: "" }],
@@ -127,39 +161,6 @@ const posixTrailingTests = [
]
];
-function checkParseFormat(path, paths) {
- paths.forEach(function(p) {
- const element = p[0];
- const output = path.parse(element);
- assertEquals(typeof output.root, "string");
- assertEquals(typeof output.dir, "string");
- assertEquals(typeof output.base, "string");
- assertEquals(typeof output.ext, "string");
- assertEquals(typeof output.name, "string");
- assertEquals(path.format(output), element);
- assertEquals(output.rooroot, undefined);
- assertEquals(output.dir, output.dir ? path.dirname(element) : "");
- assertEquals(output.base, path.basename(element));
- });
-}
-
-function checkSpecialCaseParseFormat(path, testCases) {
- testCases.forEach(function(testCase) {
- const element = testCase[0];
- const expect = testCase[1];
- const output = path.parse(element);
- Object.keys(expect).forEach(function(key) {
- assertEquals(output[key], expect[key]);
- });
- });
-}
-
-function checkFormat(path, testCases) {
- testCases.forEach(function(testCase) {
- assertEquals(path.format(testCase[0]), testCase[1]);
- });
-}
-
test(function parseTrailingWin32() {
windowsTrailingTests.forEach(function(p) {
const actual = path.win32.parse(p[0] as string);
diff --git a/fs/walk.ts b/fs/walk.ts
index 24c800e59..1e433e18b 100644
--- a/fs/walk.ts
+++ b/fs/walk.ts
@@ -6,9 +6,52 @@ export interface WalkOptions {
exts?: string[];
match?: RegExp[];
skip?: RegExp[];
- // FIXME don't use `any` here?
- onError?: (err: any) => void;
- followSymlinks?: Boolean;
+ onError?: (err: Error) => void;
+ followSymlinks?: boolean;
+}
+
+function patternTest(patterns: RegExp[], path: string): boolean {
+ // Forced to reset last index on regex while iterating for have
+ // consistent results.
+ // See: https://stackoverflow.com/a/1520853
+ return patterns.some(pattern => {
+ let r = pattern.test(path);
+ pattern.lastIndex = 0;
+ return r;
+ });
+}
+
+function include(f: FileInfo, options: WalkOptions): boolean {
+ if (options.exts && !options.exts.some(ext => f.path.endsWith(ext))) {
+ return false;
+ }
+ if (options.match && !patternTest(options.match, f.path)) {
+ return false;
+ }
+ if (options.skip && patternTest(options.skip, f.path)) {
+ return false;
+ }
+ return true;
+}
+
+async function resolve(f: FileInfo): Promise<FileInfo> {
+ // This is the full path, unfortunately if we were to make it relative
+ // it could resolve to a symlink and cause an infinite loop.
+ const fpath = await readlink(f.path);
+ f = await stat(fpath);
+ // workaround path not being returned by stat
+ f.path = fpath;
+ return f;
+}
+
+function resolveSync(f: FileInfo): FileInfo {
+ // This is the full path, unfortunately if we were to make it relative
+ // it could resolve to a symlink and cause an infinite loop.
+ const fpath = readlinkSync(f.path);
+ f = statSync(fpath);
+ // workaround path not being returned by stat
+ f.path = fpath;
+ return f;
}
/** Generate all files in a directory recursively.
@@ -94,47 +137,3 @@ export function* walkSync(
}
}
}
-
-function include(f: FileInfo, options: WalkOptions): Boolean {
- if (options.exts && !options.exts.some(ext => f.path.endsWith(ext))) {
- return false;
- }
- if (options.match && !patternTest(options.match, f.path)) {
- return false;
- }
- if (options.skip && patternTest(options.skip, f.path)) {
- return false;
- }
- return true;
-}
-
-function patternTest(patterns: RegExp[], path: string) {
- // Forced to reset last index on regex while iterating for have
- // consistent results.
- // See: https://stackoverflow.com/a/1520853
- return patterns.some(pattern => {
- let r = pattern.test(path);
- pattern.lastIndex = 0;
- return r;
- });
-}
-
-async function resolve(f: FileInfo): Promise<FileInfo> {
- // This is the full path, unfortunately if we were to make it relative
- // it could resolve to a symlink and cause an infinite loop.
- const fpath = await readlink(f.path);
- f = await stat(fpath);
- // workaround path not being returned by stat
- f.path = fpath;
- return f;
-}
-
-function resolveSync(f: FileInfo): FileInfo {
- // This is the full path, unfortunately if we were to make it relative
- // it could resolve to a symlink and cause an infinite loop.
- const fpath = readlinkSync(f.path);
- f = statSync(fpath);
- // workaround path not being returned by stat
- f.path = fpath;
- return f;
-}
diff --git a/fs/walk_test.ts b/fs/walk_test.ts
index 93073c620..4c72de2c7 100644
--- a/fs/walk_test.ts
+++ b/fs/walk_test.ts
@@ -11,15 +11,15 @@ export async function testWalk(
t: TestFunction
): Promise<void> {
const name = t.name;
- async function fn() {
- const orig_cwd = cwd();
+ async function fn(): Promise<void> {
+ const origCwd = cwd();
const d = await makeTempDir();
chdir(d);
try {
await setup(d);
await t();
} finally {
- chdir(orig_cwd);
+ chdir(origCwd);
remove(d, { recursive: true });
}
}
@@ -29,23 +29,23 @@ export async function testWalk(
async function walkArray(
dirname: string = ".",
options: WalkOptions = {}
-): Promise<Array<string>> {
+): Promise<string[]> {
const arr: string[] = [];
for await (const f of walk(dirname, { ...options })) {
arr.push(f.path.replace(/\\/g, "/"));
}
arr.sort();
- const arr_sync = Array.from(walkSync(dirname, options), (f: FileInfo) =>
+ const arrSync = Array.from(walkSync(dirname, options), (f: FileInfo) =>
f.path.replace(/\\/g, "/")
).sort();
- assertEquals(arr, arr_sync);
+ assertEquals(arr, arrSync);
return arr;
}
async function touch(path: string): Promise<void> {
await open(path, "w");
}
-function assertReady(expectedLength: number) {
+function assertReady(expectedLength: number): void {
const arr = Array.from(walkSync(), (f: FileInfo) => f.path);
assertEquals(arr.length, expectedLength);
}
@@ -77,11 +77,11 @@ testWalk(
},
async function iteratable() {
let count = 0;
- for (const f of walkSync()) {
+ for (const _ of walkSync()) {
count += 1;
}
assertEquals(count, 1);
- for await (const f of walk()) {
+ for await (const _ of walk()) {
count += 1;
}
assertEquals(count, 2);
@@ -107,11 +107,11 @@ testWalk(
},
async function depth() {
assertReady(1);
- const arr_3 = await walkArray(".", { maxDepth: 3 });
- assertEquals(arr_3.length, 0);
- const arr_5 = await walkArray(".", { maxDepth: 5 });
- assertEquals(arr_5.length, 1);
- assertEquals(arr_5[0], "./a/b/c/d/x");
+ const arr3 = await walkArray(".", { maxDepth: 3 });
+ assertEquals(arr3.length, 0);
+ const arr5 = await walkArray(".", { maxDepth: 5 });
+ assertEquals(arr5.length, 1);
+ assertEquals(arr5[0], "./a/b/c/d/x");
}
);
@@ -214,12 +214,12 @@ testWalk(
}
);
-testWalk(async (d: string) => {}, async function onError() {
+testWalk(async (_d: string) => {}, async function onError() {
assertReady(0);
const ignored = await walkArray("missing");
assertEquals(ignored.length, 0);
let errors = 0;
- const arr = await walkArray("missing", { onError: e => (errors += 1) });
+ await walkArray("missing", { onError: _e => (errors += 1) });
// It's 2 since walkArray iterates over both sync and async.
assertEquals(errors, 2);
});