Skip to content

Commit

Permalink
Add typescript isolatedDeclarations for improved docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Jan 10, 2025
1 parent 1182965 commit 0ee8e18
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
23 changes: 13 additions & 10 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export type Path = string;
export const STR_DATA = 'data';
export const STR_END = 'end';
export const STR_CLOSE = 'close';
export const EMPTY_FN = () => {};
export const IDENTITY_FN = (val: unknown) => val;
export const EMPTY_FN = (): void => {};
export const IDENTITY_FN = (val: unknown): unknown => val;

const pl = process.platform;
export const isWindows = pl === 'win32';
export const isMacos = pl === 'darwin';
export const isLinux = pl === 'linux';
export const isFreeBSD = pl === 'freebsd';
export const isIBMi = osType() === 'OS400';
export const isWindows: boolean = pl === 'win32';
export const isMacos: boolean = pl === 'darwin';
export const isLinux: boolean = pl === 'linux';
export const isFreeBSD: boolean = pl === 'freebsd';
export const isIBMi: boolean = osType() === 'OS400';

export const EVENTS = {
ALL: 'all',
Expand Down Expand Up @@ -375,7 +375,10 @@ export class NodeFsHandler {
* @param listener on fs change
* @returns closer for the watcher instance
*/
_watchWithNodeFs(path: string, listener: (path: string, newStats?: any) => void | Promise<void>) {
_watchWithNodeFs(
path: string,
listener: (path: string, newStats?: any) => void | Promise<void>
): (() => void) | undefined {
const opts = this.fsw.options;
const directory = sysPath.dirname(path);
const basename = sysPath.basename(path);
Expand Down Expand Up @@ -532,7 +535,7 @@ export class NodeFsHandler {
dir: Path,
depth: number,
throttler: Throttler
) {
): Promise<unknown> | undefined {
// Normalize the directory name on Windows
directory = sysPath.join(directory, '');

Expand Down Expand Up @@ -677,7 +680,7 @@ export class NodeFsHandler {
priorWh: WatchHelper | undefined,
depth: number,
target?: string
) {
): Promise<string | false | undefined> {
const ready = this.fsw._emitReady;
if (this.fsw._isIgnored(path) || this.fsw.closed) {
ready();
Expand Down
28 changes: 14 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,13 @@ class DirEntry {
this.items = new Set<Path>();
}

add(item: string) {
add(item: string): void {
const { items } = this;
if (!items) return;
if (item !== ONE_DOT && item !== TWO_DOTS) items.add(item);
}

async remove(item: string) {
async remove(item: string): Promise<void> {
const { items } = this;
if (!items) return;
items.delete(item);
Expand All @@ -236,7 +236,7 @@ class DirEntry {
}
}

has(item: string) {
has(item: string): boolean | undefined {
const { items } = this;
if (!items) return;
return items.has(item);
Expand All @@ -248,7 +248,7 @@ class DirEntry {
return [...items.values()];
}

dispose() {
dispose(): void {
this.items.clear();
this.path = '';
this._removeWatcher = EMPTY_FN;
Expand Down Expand Up @@ -581,7 +581,7 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
return watchList;
}

emitWithAll(event: EventName, args: EmitArgs) {
emitWithAll(event: EventName, args: EmitArgs): void {
this.emit(event, ...args);
if (event !== EV.ERROR) this.emit(EV.ALL, event, ...args);
}
Expand All @@ -597,7 +597,7 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
* @param stats arguments to be passed with event
* @returns the error if defined, otherwise the value of the FSWatcher instance's `closed` flag
*/
async _emit(event: EventName, path: Path, stats?: Stats) {
async _emit(event: EventName, path: Path, stats?: Stats): Promise<this | undefined> {
if (this.closed) return;

const opts = this.options;
Expand Down Expand Up @@ -735,7 +735,7 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
return thr;
}

_incrReadyCount() {
_incrReadyCount(): number {
return this._readyCount++;
}

Expand All @@ -752,7 +752,7 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
threshold: number,
event: EventName,
awfEmit: (err?: Error, stat?: Stats) => void
) {
): void {
const awf = this.options.awaitWriteFinish;
if (typeof awf !== 'object') return;
const pollInterval = awf.pollInterval as unknown as number;
Expand Down Expand Up @@ -821,7 +821,7 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
return this._userIgnored(path, stats);
}

_isntIgnored(path: Path, stat?: Stats) {
_isntIgnored(path: Path, stat?: Stats): boolean {
return !this._isIgnored(path, stat);
}

Expand Down Expand Up @@ -926,7 +926,7 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
/**
* Closes all watchers for a path
*/
_closePath(path: Path) {
_closePath(path: Path): void {
this._closeFile(path);
const dir = sysPath.dirname(path);
this._getWatchedDir(dir).remove(sysPath.basename(path));
Expand All @@ -935,14 +935,14 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
/**
* Closes only file-specific watchers
*/
_closeFile(path: Path) {
_closeFile(path: Path): void {
const closers = this._closers.get(path);
if (!closers) return;
closers.forEach((closer) => closer());
this._closers.delete(path);
}

_addPathCloser(path: Path, closer: () => void) {
_addPathCloser(path: Path, closer: () => void): void {
if (!closer) return;
let list = this._closers.get(path);
if (!list) {
Expand All @@ -952,7 +952,7 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
list.push(closer);
}

_readdirp(root: Path, opts?: Partial<ReaddirpOptions>) {
_readdirp(root: Path, opts?: Partial<ReaddirpOptions>): ReaddirpStream | undefined {
if (this.closed) return;
const options = { type: EV.ALL, alwaysStat: true, lstat: true, ...opts, depth: 0 };
let stream: ReaddirpStream | undefined = readdirp(root, options);
Expand Down Expand Up @@ -985,4 +985,4 @@ export function watch(paths: string | string[], options: ChokidarOptions = {}):
return watcher;
}

export default { watch, FSWatcher };
export default { watch: watch as typeof watch, FSWatcher: FSWatcher as typeof FSWatcher };
3 changes: 2 additions & 1 deletion tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"moduleResolution": "bundler",
"noImplicitReturns": false,
"sourceMap": false,
"declarationMap": false
"declarationMap": false,
"isolatedDeclarations": true
},
"include": [
"src"
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"outDir": ".",
"noImplicitReturns": false,
"sourceMap": false,
"declarationMap": false
"declarationMap": false,
"isolatedDeclarations": true
},
"include": [
"src"
Expand Down

0 comments on commit 0ee8e18

Please sign in to comment.