refactor: centralize isPlainObject, isRecord, isErrno, isLoopbackHost utilities (#12926)

This commit is contained in:
max
2026-02-09 17:02:55 -08:00
committed by GitHub
parent 70f9edeec7
commit 8d75a496bf
37 changed files with 97 additions and 226 deletions

View File

@@ -1,5 +1,6 @@
import fs from "node:fs/promises";
import path from "node:path";
import { hasErrnoCode } from "../infra/errors.js";
// ---------------------------------------------------------------------------
// Types
@@ -52,16 +53,6 @@ export function isScannable(filePath: string): boolean {
return SCANNABLE_EXTENSIONS.has(path.extname(filePath).toLowerCase());
}
function isErrno(err: unknown, code: string): boolean {
if (!err || typeof err !== "object") {
return false;
}
if (!("code" in err)) {
return false;
}
return (err as { code?: unknown }).code === code;
}
// ---------------------------------------------------------------------------
// Rule definitions
// ---------------------------------------------------------------------------
@@ -327,7 +318,7 @@ async function resolveForcedFiles(params: {
try {
st = await fs.stat(includePath);
} catch (err) {
if (isErrno(err, "ENOENT")) {
if (hasErrnoCode(err, "ENOENT")) {
continue;
}
throw err;
@@ -374,7 +365,7 @@ async function readScannableSource(filePath: string, maxFileBytes: number): Prom
try {
st = await fs.stat(filePath);
} catch (err) {
if (isErrno(err, "ENOENT")) {
if (hasErrnoCode(err, "ENOENT")) {
return null;
}
throw err;
@@ -385,7 +376,7 @@ async function readScannableSource(filePath: string, maxFileBytes: number): Prom
try {
return await fs.readFile(filePath, "utf-8");
} catch (err) {
if (isErrno(err, "ENOENT")) {
if (hasErrnoCode(err, "ENOENT")) {
return null;
}
throw err;