Skip to content

Commit da1b331

Browse files
committed
Various TypeScript type tweaks
1 parent da17a22 commit da1b331

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

index.d.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import * as locatePath from 'locate-path';
1+
import {Options as LocatePathOptions} from 'locate-path';
2+
3+
declare const stop: unique symbol;
24

35
declare namespace findUp {
4-
interface Options {
6+
interface Options extends LocatePathOptions {}
7+
8+
interface MatcherOptions {
59
/**
610
Directory to start from.
711
@@ -10,7 +14,9 @@ declare namespace findUp {
1014
readonly cwd?: string;
1115
}
1216

13-
type Match = string | symbol | undefined;
17+
type StopSymbol = typeof stop;
18+
19+
type Match = string | StopSymbol | undefined;
1420
}
1521

1622
declare const findUp: {
@@ -43,36 +49,36 @@ declare const findUp: {
4349
})();
4450
```
4551
*/
46-
(name: string | string[], options?: locatePath.AsyncOptions): Promise<string | undefined>;
52+
(name: string | string[], options?: findUp.Options): Promise<string | undefined>;
4753

4854
/**
4955
Find a file or directory by walking up parent directories.
5056
5157
@param matcher - Called for each directory in the search. Return a path or `findUp.stop` to stop the search.
5258
@returns The first path found or `undefined` if none could be found.
5359
*/
54-
(matcher: (directory: string) => (findUp.Match | Promise<findUp.Match>), options?: findUp.Options): Promise<string | undefined>;
60+
(matcher: (directory: string) => (findUp.Match | Promise<findUp.Match>), options?: findUp.MatcherOptions): Promise<string | undefined>;
5561

5662
/**
5763
Synchronously find a file or directory by walking up parent directories.
5864
5965
@param name - Name of the file or directory to find. Can be multiple.
6066
@returns The first path found (by respecting the order of `name`s) or `undefined` if none could be found.
6167
*/
62-
sync(name: string | string[], options?: locatePath.Options): string | undefined;
68+
sync(name: string | string[], options?: findUp.Options): string | undefined;
6369

6470
/**
6571
Synchronously find a file or directory by walking up parent directories.
6672
6773
@param matcher - Called for each directory in the search. Return a path or `findUp.stop` to stop the search.
6874
@returns The first path found or `undefined` if none could be found.
6975
*/
70-
sync(matcher: (directory: string) => findUp.Match, options?: findUp.Options): string | undefined;
76+
sync(matcher: (directory: string) => findUp.Match, options?: findUp.MatcherOptions): string | undefined;
7177

7278
/**
7379
Return this in a `matcher` function to stop the search and force `findUp` to immediately return `undefined`.
7480
*/
75-
readonly stop: unique symbol;
81+
readonly stop: findUp.StopSymbol;
7682
};
7783

7884
export = findUp;

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ module.exports = async (name, options = {}) => {
88
let directory = path.resolve(options.cwd || '');
99
const {root} = path.parse(directory);
1010
const paths = [].concat(name);
11+
1112
// eslint-disable-next-line no-constant-condition
1213
while (true) {
1314
// eslint-disable-next-line no-await-in-loop
14-
const foundPath = await (typeof name === 'function' ? name(directory) : locatePath(paths, {...options, cwd: directory}));
15+
const foundPath = await (typeof name === 'function' ?
16+
name(directory) :
17+
locatePath(paths, {...options, cwd: directory}));
1518

1619
if (foundPath === stop) {
1720
return;
@@ -36,7 +39,9 @@ module.exports.sync = (name, options = {}) => {
3639

3740
// eslint-disable-next-line no-constant-condition
3841
while (true) {
39-
const foundPath = typeof name === 'function' ? name(directory) : locatePath.sync(paths, {...options, cwd: directory});
42+
const foundPath = typeof name === 'function' ?
43+
name(directory) :
44+
locatePath.sync(paths, {...options, cwd: directory});
4045

4146
if (foundPath === stop) {
4247
return;

index.test-d.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expectType} from 'tsd';
1+
import {expectType, expectError} from 'tsd';
22
import findUp = require('.');
33

44
expectType<Promise<string | undefined>>(findUp('unicorn.png'));
@@ -9,19 +9,20 @@ expectType<Promise<string | undefined>>(findUp(['rainbow.png', 'unicorn.png'], {
99
expectType<Promise<string | undefined>>(findUp(['rainbow.png', 'unicorn.png'], {allowSymlinks: false}));
1010
expectType<Promise<string | undefined>>(findUp(['rainbow.png', 'unicorn.png'], {type: 'file'}));
1111
expectType<Promise<string | undefined>>(findUp(['rainbow.png', 'unicorn.png'], {type: 'directory'}));
12+
expectError(findUp(['rainbow.png', 'unicorn.png'], {concurrency: 1}))
1213

1314
expectType<Promise<string | undefined>>(findUp(() => 'unicorn.png'));
1415
expectType<Promise<string | undefined>>(findUp(() => 'unicorn.png', {cwd: ''}));
1516
expectType<Promise<string | undefined>>(findUp(() => undefined));
1617
expectType<Promise<string | undefined>>(findUp(() => undefined, {cwd: ''}));
17-
expectType<Promise<string | undefined>>(findUp(() => findUp.stop));
18-
expectType<Promise<string | undefined>>(findUp(() => findUp.stop, {cwd: ''}));
18+
expectType<Promise<string | undefined>>(findUp((): findUp.StopSymbol => findUp.stop));
19+
expectType<Promise<string | undefined>>(findUp((): findUp.StopSymbol => findUp.stop, {cwd: ''}));
1920
expectType<Promise<string | undefined>>(findUp(async () => 'unicorn.png'));
2021
expectType<Promise<string | undefined>>(findUp(async () => 'unicorn.png', {cwd: ''}));
2122
expectType<Promise<string | undefined>>(findUp(async () => undefined));
2223
expectType<Promise<string | undefined>>(findUp(async () => undefined, {cwd: ''}));
23-
expectType<Promise<string | undefined>>(findUp(async () => findUp.stop));
24-
expectType<Promise<string | undefined>>(findUp(async () => findUp.stop, {cwd: ''}));
24+
expectType<Promise<string | undefined>>(findUp(async (): Promise<findUp.StopSymbol> => findUp.stop));
25+
expectType<Promise<string | undefined>>(findUp(async (): Promise<findUp.StopSymbol> => findUp.stop, {cwd: ''}));
2526

2627
expectType<string | undefined>(findUp.sync('unicorn.png'));
2728
expectType<string | undefined>(findUp.sync('unicorn.png', {cwd: ''}));
@@ -36,7 +37,7 @@ expectType<string | undefined>(findUp.sync(() => 'unicorn.png'));
3637
expectType<string | undefined>(findUp.sync(() => 'unicorn.png', {cwd: ''}));
3738
expectType<string | undefined>(findUp.sync(() => undefined));
3839
expectType<string | undefined>(findUp.sync(() => undefined, {cwd: ''}));
39-
expectType<string | undefined>(findUp.sync(() => findUp.stop));
40-
expectType<string | undefined>(findUp.sync(() => findUp.stop, {cwd: ''}));
40+
expectType<string | undefined>(findUp.sync((): findUp.StopSymbol => findUp.stop));
41+
expectType<string | undefined>(findUp.sync((): findUp.StopSymbol => findUp.stop, {cwd: ''}));
4142

4243
expectType<Symbol>(findUp.stop);

0 commit comments

Comments
 (0)