Skip to content

Commit b19edfb

Browse files
committed
[New] add type declarations
1 parent 4e0aff6 commit b19edfb

9 files changed

Lines changed: 244 additions & 2 deletions

File tree

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ trim_trailing_whitespace = true
99
insert_final_newline = true
1010
max_line_length = 200
1111

12-
[*.{,m}js]
12+
[*.{d.ts,{,m}js}]
1313
block_comment_start = /*
1414
block_comment = *
1515
block_comment_end = */

async.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import type resolveAsync = require('./lib/async');
2+
3+
export = resolveAsync;

index.d.mts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { default as resolve } from './index.js';
2+
3+
/** Asynchronously resolve a module path, like `require.resolve()`, on behalf of files. */
4+
export declare const async: typeof resolve;
5+
6+
/** Synchronously resolve a module path, like `require.resolve()`, on behalf of files. */
7+
export declare const sync: typeof resolve.sync;

index.d.ts

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/**
2+
* Asynchronously resolve a module path, like `require.resolve()`, on behalf of files.
3+
*
4+
* @param id - The module identifier to resolve.
5+
* @param options - Resolution options.
6+
* @param callback - Called with `(err, resolved, pkg)` when resolution completes.
7+
*/
8+
declare function resolve(id: string, callback: resolve.Callback): void;
9+
declare function resolve(id: string, options: resolve.AsyncOptions, callback: resolve.Callback): void;
10+
11+
declare namespace resolve {
12+
/** Asynchronously resolve a module path, like `require.resolve()`, on behalf of files. */
13+
function async(id: string, callback: Callback): void;
14+
function async(id: string, options: AsyncOptions, callback: Callback): void;
15+
16+
/**
17+
* Synchronously resolve a module path, like `require.resolve()`, on behalf of files.
18+
*
19+
* @param id - The module identifier to resolve.
20+
* @param options - Resolution options.
21+
* @returns The resolved file path.
22+
* @throws If the module cannot be found.
23+
*/
24+
function sync(id: string, options?: SyncOptions): string;
25+
26+
/** A parsed package.json object. */
27+
interface PackageJSON {
28+
[key: string]: unknown;
29+
name?: string;
30+
main?: string;
31+
exports?: unknown;
32+
engines?: { node?: string };
33+
}
34+
35+
/** Callback for asynchronous resolution. */
36+
type Callback = (err: Error | null, resolved?: string, pkg?: PackageJSON) => void;
37+
38+
/** Options shared by both async and sync resolution. */
39+
interface BaseOptions {
40+
/** Directory to resolve from. Defaults to `__dirname` of the calling file. */
41+
basedir?: string;
42+
/** The `package.json` object associated with the calling module. */
43+
package?: PackageJSON;
44+
/** File extensions to search, in order. Defaults to `['.js']`. */
45+
extensions?: ReadonlyArray<string>;
46+
/** Whether to include core modules (e.g. `fs`, `path`) in results. Defaults to `true`. */
47+
includeCoreModules?: boolean;
48+
/** Whether to preserve symlinks instead of resolving them. Defaults to `false`. */
49+
preserveSymlinks?: boolean;
50+
/** Additional lookup paths. */
51+
paths?: ReadonlyArray<string> | ((request: string, start: string, getNodeModulesDirs: () => string[], opts: BaseOptions) => string[]);
52+
/** The filename used for error messages and as a fallback for basedir. */
53+
filename?: string;
54+
/** The directory name(s) to use for node_modules lookups. Defaults to `['node_modules']`. */
55+
moduleDirectory?: string | ReadonlyArray<string>;
56+
/**
57+
* Transform a package.json object before its `main` field is used.
58+
*
59+
* @param pkg - The parsed package.json.
60+
* @param pkgFile - The path to the package.json file.
61+
* @param dir - The directory containing the package.json.
62+
* @returns The (possibly modified) package.json object.
63+
*/
64+
packageFilter?: (pkg: PackageJSON, pkgFile: string, dir: string) => PackageJSON;
65+
/**
66+
* Transform a resolved path before it is used.
67+
*
68+
* @param pkg - The parsed package.json.
69+
* @param path - The resolved path.
70+
* @param relativePath - The path relative to the package directory.
71+
* @returns An alternative path, or undefined/falsy to use the original.
72+
*/
73+
pathFilter?: (pkg: PackageJSON, path: string, relativePath: string) => string | undefined;
74+
/**
75+
* Override the default node_modules candidate iterator.
76+
*
77+
* @param request - The module being resolved.
78+
* @param start - The starting directory.
79+
* @param thunk - A function returning the default candidate directories.
80+
* @param opts - The resolve options.
81+
* @returns An array of candidate directories.
82+
*/
83+
packageIterator?: (request: string, start: string, thunk: () => string[], opts: BaseOptions) => string[];
84+
/**
85+
* An exports category string, as defined by `node-exports-info`.
86+
* Mutually exclusive with `engines`.
87+
*/
88+
exportsCategory?: string;
89+
/**
90+
* When `true`, reads the consumer's `engines.node` to determine the exports category.
91+
* When a string, it is treated as a semver range for engines.node.
92+
* Mutually exclusive with `exportsCategory`.
93+
*/
94+
engines?: boolean | string;
95+
/** Custom conditions for package.json `exports` resolution. */
96+
conditions?: ReadonlyArray<string>;
97+
}
98+
99+
/** Options for asynchronous resolution. */
100+
interface AsyncOptions extends BaseOptions {
101+
/**
102+
* Check whether a path is a file.
103+
*
104+
* @param file - The path to check.
105+
* @param cb - Called with `(err, isFile)`.
106+
*/
107+
isFile?: (file: string, cb: (err: Error | null, isFile?: boolean) => void) => void;
108+
/**
109+
* Check whether a path is a directory.
110+
*
111+
* @param dir - The path to check.
112+
* @param cb - Called with `(err, isDirectory)`.
113+
*/
114+
isDirectory?: (dir: string, cb: (err: Error | null, isDirectory?: boolean) => void) => void;
115+
/**
116+
* Resolve a path's real location, following symlinks.
117+
*
118+
* @param file - The path to resolve.
119+
* @param cb - Called with `(err, realPath)`.
120+
*/
121+
realpath?: (file: string, cb: (err: Error | null, realPath?: string) => void) => void;
122+
/**
123+
* Read a file's contents.
124+
* Mutually exclusive with `readPackage`.
125+
*
126+
* @param file - The path to read.
127+
* @param cb - Called with `(err, contents)`.
128+
*/
129+
readFile?: (file: string, cb: (err: Error | null, contents?: string | Buffer) => void) => void;
130+
/**
131+
* Read and parse a package.json file.
132+
* Mutually exclusive with `readFile`.
133+
*
134+
* @param readFile - The file-reading function.
135+
* @param pkgFile - The path to the package.json.
136+
* @param cb - Called with `(err, pkg)`.
137+
*/
138+
readPackage?: (readFile: (file: string, cb: (err: Error | null, contents?: string | Buffer) => void) => void, pkgFile: string, cb: (err: Error | null, pkg?: PackageJSON) => void) => void;
139+
}
140+
141+
/** Options for synchronous resolution. */
142+
interface SyncOptions extends BaseOptions {
143+
/**
144+
* Check whether a path is a file.
145+
*
146+
* @param file - The path to check.
147+
* @returns `true` if the path is a file.
148+
*/
149+
isFile?: (file: string) => boolean;
150+
/**
151+
* Check whether a path is a directory.
152+
*
153+
* @param dir - The path to check.
154+
* @returns `true` if the path is a directory.
155+
*/
156+
isDirectory?: (dir: string) => boolean;
157+
/**
158+
* Resolve a path's real location, following symlinks.
159+
*
160+
* @param file - The path to resolve.
161+
* @returns The resolved real path.
162+
*/
163+
realpathSync?: (file: string) => string;
164+
/**
165+
* Read a file's contents synchronously.
166+
* Mutually exclusive with `readPackageSync`.
167+
*
168+
* @param file - The path to read.
169+
* @returns The file contents.
170+
*/
171+
readFileSync?: (file: string) => string | Buffer;
172+
/**
173+
* Read and parse a package.json file synchronously.
174+
* Mutually exclusive with `readFileSync`.
175+
*
176+
* @param readFileSync - The synchronous file-reading function.
177+
* @param pkgFile - The path to the package.json.
178+
* @returns The parsed package.json object.
179+
*/
180+
readPackageSync?: (readFileSync: (file: string) => string | Buffer, pkgFile: string) => PackageJSON;
181+
}
182+
}
183+
184+
export = resolve;

lib/async.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type resolve = require('../index.js');
2+
3+
/**
4+
* Asynchronously resolve a module path, like `require.resolve()`, on behalf of files.
5+
*
6+
* @param id - The module identifier to resolve.
7+
* @param options - Resolution options.
8+
* @param callback - Called with `(err, resolved, pkg)` when resolution completes.
9+
*/
10+
declare function resolveAsync(id: string, callback: resolve.Callback): void;
11+
declare function resolveAsync(id: string, options: resolve.AsyncOptions, callback: resolve.Callback): void;
12+
13+
export = resolveAsync;

lib/sync.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type resolve = require('../index.js');
2+
3+
/**
4+
* Synchronously resolve a module path, like `require.resolve()`, on behalf of files.
5+
*
6+
* @param id - The module identifier to resolve.
7+
* @param options - Resolution options.
8+
* @returns The resolved file path.
9+
* @throws If the module cannot be found.
10+
*/
11+
declare function resolveSync(id: string, options?: resolve.SyncOptions): string;
12+
13+
export = resolveSync;

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"prepublish": "not-in-publish || npm run prepublishOnly",
3535
"prelint": "eclint check $(git ls-files | grep -Ev test\\/list-exports$ | xargs find 2> /dev/null | grep -vE 'node_modules|\\.git')",
3636
"lint": "eslint .",
37+
"postlint": "tsc -p . && attw -P",
3738
"pretests-only": "npm run submodule:update && cd ./test/resolver/nested_symlinks && node mylib/sync && node mylib/async",
3839
"tests-only": "tape test/*.js",
3940
"pretest": "npm run lint",
@@ -60,7 +61,10 @@
6061
"supports-preserve-symlinks-flag": "^1.0.0"
6162
},
6263
"devDependencies": {
64+
"@arethetypeswrong/cli": "^0.18.2",
6365
"@ljharb/eslint-config": "^22.2.2",
66+
"@ljharb/tsconfig": "^0.3.2",
67+
"@types/node": "^25.6.0",
6468
"array.prototype.map": "^1.0.8",
6569
"copy-dir": "^1.3.0",
6670
"eclint": "^2.8.1",
@@ -74,7 +78,8 @@
7478
"safe-publish-latest": "^2.0.0",
7579
"tap": "^0.4.13",
7680
"tape": "^5.9.0",
77-
"tmp": "^0.0.31"
81+
"tmp": "^0.0.31",
82+
"typescript": "next"
7883
},
7984
"publishConfig": {
8085
"ignore": [

sync.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import type resolveSync = require('./lib/sync');
2+
3+
export = resolveSync;

tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "@ljharb/tsconfig",
3+
"compilerOptions": {
4+
"target": "ES2021",
5+
"checkJs": false,
6+
"maxNodeModuleJsDepth": 0,
7+
"types": ["node"]
8+
},
9+
"exclude": [
10+
"coverage",
11+
"example",
12+
"test"
13+
]
14+
}

0 commit comments

Comments
 (0)