Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: switch homebrewed NodeError → official NodeJS.ErrnoException #14

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions recipes/correct-ts-specifiers/src/get-not-found-url.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pathToFileURL } from 'node:url';

import type { NodeError, ResolvedSpecifier } from './index.d.ts';
import type { FSAbsolutePath, ResolvedSpecifier } from './index.d.ts';

export const getNotFoundUrl = (err: NodeError) =>
export const getNotFoundUrl = (err: NodeJS.ErrnoException & { url?: FSAbsolutePath }) =>
pathToFileURL(err?.url ?? err.message.split("'")[1])?.href as ResolvedSpecifier;
6 changes: 0 additions & 6 deletions recipes/correct-ts-specifiers/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,3 @@ export type Specifier = URL['pathname'] | ResolvedSpecifier;
* @example 'foo/bar'
*/
export type NodeModSpecifier = string | `${string}/${string}`;

export type NodeError = Error &
Partial<{
code: string;
url: FSAbsolutePath;
}>;
3 changes: 1 addition & 2 deletions recipes/correct-ts-specifiers/src/is-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { lstat } from 'node:fs/promises';

import type {
FSAbsolutePath,
NodeError,
NodeModSpecifier,
ResolvedSpecifier,
Specifier,
Expand All @@ -14,7 +13,7 @@ export async function isDir(parentPath: FSAbsolutePath | ResolvedSpecifier, spec
try {
resolvedSpecifier = resolveSpecifier(parentPath, specifier);
} catch (err) {
if ((err as NodeError).code === 'ERR_MODULE_NOT_FOUND') return null;
if ((err as NodeJS.ErrnoException).code === 'ERR_MODULE_NOT_FOUND') return null;
}

try {
Expand Down
7 changes: 5 additions & 2 deletions recipes/correct-ts-specifiers/src/is-ignorable-specifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { extname, sep } from 'node:path';
import { pathToFileURL } from 'node:url';

import { tsExts } from './exts.ts';
import type { FSAbsolutePath, NodeError, ResolvedSpecifier, Specifier } from './index.d.ts';
import type { FSAbsolutePath, ResolvedSpecifier, Specifier } from './index.d.ts';
import { resolvesToNodeModule } from './resolves-to-node-module.ts';
import { getNotFoundUrl } from './get-not-found-url.ts';

Expand Down Expand Up @@ -34,7 +34,10 @@ export function isIgnorableSpecifier(parentPath: FSAbsolutePath, specifier: stri
pathToFileURL(parentPath).href,
) as ResolvedSpecifier; // [1]
} catch (err) {
if (!(err instanceof Error) || !IGNORABLE_RESOLVE_ERRORS.has((err as NodeError).code!))
if (
!(err instanceof Error)
|| !IGNORABLE_RESOLVE_ERRORS.has((err as NodeJS.ErrnoException).code!)
)
throw err;

resolvedSpecifier = getNotFoundUrl(err);
Expand Down
3 changes: 1 addition & 2 deletions recipes/correct-ts-specifiers/src/resolve-specifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
/* node:coverage disable */
import type {
FSAbsolutePath,
NodeError,
NodeModSpecifier,
ResolvedSpecifier,
Specifier,
Expand Down Expand Up @@ -42,7 +41,7 @@ export function resolveSpecifier(
if (!(err instanceof Error)) throw err;

if (
(err as NodeError).code === 'ERR_MODULE_NOT_FOUND' &&
(err as NodeJS.ErrnoException).code === 'ERR_MODULE_NOT_FOUND' &&
resolvesToNodeModule(getNotFoundUrl(err), parentUrl)
) {
return specifier as NodeModSpecifier;
Expand Down
Loading