From 856d5f3206362510bd32e5e1034220bd20633540 Mon Sep 17 00:00:00 2001 From: v1rtl Date: Fri, 20 Sep 2024 08:24:19 +0000 Subject: [PATCH] improve internal types a little bit --- src/handler.ts | 28 ++++++++++++++-------------- src/index.ts | 10 +++++----- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/handler.ts b/src/handler.ts index feb19975..2d727b67 100644 --- a/src/handler.ts +++ b/src/handler.ts @@ -12,7 +12,7 @@ export const STR_DATA = 'data'; export const STR_END = 'end'; export const STR_CLOSE = 'close'; export const EMPTY_FN = () => {}; -export const IDENTITY_FN = (val: any) => val; +export const IDENTITY_FN = (val: unknown) => val; const pl = process.platform; export const isWindows = pl === 'win32'; @@ -82,7 +82,7 @@ const isBinaryPath = (filePath: string) => binaryExtensions.has(sysPath.extname(filePath).slice(1).toLowerCase()); // TODO: emit errors properly. Example: EMFILE on Macos. -const foreach = (val: any, fn: any) => { +const foreach = (val: V, fn: (arg: V) => unknown) => { if (val instanceof Set) { val.forEach(fn); } else { @@ -90,15 +90,15 @@ const foreach = (val: any, fn: any) => { } }; -const addAndConvert = (main: any, prop: any, item: any) => { - let container = main[prop]; +const addAndConvert = (main: Record, prop: string, item: unknown) => { + let container = (main as Record)[prop]; if (!(container instanceof Set)) { - main[prop] = container = new Set([container]); + (main as Record)[prop] = container = new Set([container]); } - container.add(item); + (container as Set).add(item); }; -const clearItem = (cont: any) => (key: string) => { +const clearItem = (cont: Record) => (key: string) => { const set = cont[key]; if (set instanceof Set) { set.clear(); @@ -116,7 +116,7 @@ const delFromSet = (main: any, prop: any, item: any) => { } }; -const isEmptySet = (val: any) => (val instanceof Set ? val.size === 0 : !val); +const isEmptySet = (val: unknown) => (val instanceof Set ? val.size === 0 : !val); // fs_watch helpers @@ -180,9 +180,9 @@ function createFsWatchInstance( const fsWatchBroadcast = ( fullPath: Path, listenerType: string, - val1?: any, - val2?: any, - val3?: any + val1?: unknown, + val2?: unknown, + val3?: unknown ) => { const cont = FsWatchInstances.get(fullPath); if (!cont) return; @@ -283,7 +283,7 @@ const setFsWatchListener = ( // object to hold per-process fs_watchFile instances // (may be shared across chokidar FSWatcher instances) -const FsWatchFileInstances = new Map(); +const FsWatchFileInstances = new Map(); /** * Instantiates the fs_watchFile interface or binds listeners @@ -330,12 +330,12 @@ const setFsWatchFileListener = ( rawEmitters: rawEmitter, options, watcher: watchFile(fullPath, options, (curr, prev) => { - foreach(cont.rawEmitters, (rawEmitter: any) => { + foreach(cont.rawEmitters, (rawEmitter) => { rawEmitter(EV.CHANGE, fullPath, { curr, prev }); }); const currmtime = curr.mtimeMs; if (curr.size !== prev.size || currmtime > prev.mtimeMs || currmtime === 0) { - foreach(cont.listeners, (listener: any) => listener(path, curr)); + foreach(cont.listeners, (listener) => listener(path, curr)); } }), }; diff --git a/src/index.ts b/src/index.ts index 921e0195..3cdfa5e4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -186,11 +186,11 @@ const normalizePathToUnix = (path: Path) => toUnix(sysPath.normalize(toUnix(path // TODO: refactor const normalizeIgnored = (cwd = '') => - (path: any): any => { + (path: unknown): string => { if (typeof path === 'string') { return normalizePathToUnix(sysPath.isAbsolute(path) ? path : sysPath.join(cwd, path)); } else { - return path; + return path as string; } }; @@ -207,10 +207,10 @@ const EMPTY_SET = Object.freeze(new Set()); */ class DirEntry { path: Path; - _removeWatcher: any; + _removeWatcher: (dir: string, base: string) => void; items: Set; - constructor(dir: Path, removeWatcher: any) { + constructor(dir: Path, removeWatcher: (dir: string, base: string) => void) { this.path = dir; this._removeWatcher = removeWatcher; this.items = new Set(); @@ -270,7 +270,7 @@ export class WatchHelper { followSymlinks: boolean; statMethod: 'stat' | 'lstat'; - constructor(path: string, follow: boolean, fsw: any) { + constructor(path: string, follow: boolean, fsw: FSWatcher) { this.fsw = fsw; const watchPath = path; this.path = path = path.replace(REPLACER_RE, '');