Skip to content

Commit d363b81

Browse files
committedApr 29, 2024··
Refactor to hide deprecation warning
Closes GH-27.
1 parent dbb53a5 commit d363b81

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed
 

‎lib/resolve.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
// Last checked on: Apr 29, 2023.
44

55
/**
6+
* @typedef {import('node:fs').Stats} Stats
67
* @typedef {import('./errors.js').ErrnoException} ErrnoException
78
* @typedef {import('./package-json-reader.js').PackageConfig} PackageConfig
89
*/
910

1011
import assert from 'node:assert'
11-
import {Stats, statSync, realpathSync} from 'node:fs'
12+
import {statSync, realpathSync} from 'node:fs'
1213
import process from 'node:process'
1314
import {URL, fileURLToPath, pathToFileURL} from 'node:url'
1415
import path from 'node:path'
@@ -129,14 +130,17 @@ function emitLegacyIndexDeprecation(url, packageJsonUrl, base, main) {
129130

130131
/**
131132
* @param {string} path
132-
* @returns {Stats}
133+
* @returns {Stats | undefined}
133134
*/
134135
function tryStatSync(path) {
135136
// Note: from Node 15 onwards we can use `throwIfNoEntry: false` instead.
136137
try {
137138
return statSync(path)
138139
} catch {
139-
return new Stats()
140+
// Note: in Node code this returns `new Stats`,
141+
// but in Node 22 that’s marked as a deprecated internal API.
142+
// Which, well, we kinda are, but still to prevent that warning,
143+
// just yield `undefined`.
140144
}
141145
}
142146

@@ -251,14 +255,14 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
251255
filePath.endsWith('/') ? filePath.slice(-1) : filePath
252256
)
253257

254-
if (stats.isDirectory()) {
258+
if (stats && stats.isDirectory()) {
255259
const error = new ERR_UNSUPPORTED_DIR_IMPORT(filePath, fileURLToPath(base))
256260
// @ts-expect-error Add this for `import.meta.resolve`.
257261
error.url = String(resolved)
258262
throw error
259263
}
260264

261-
if (!stats.isFile()) {
265+
if (!stats || !stats.isFile()) {
262266
const error = new ERR_MODULE_NOT_FOUND(
263267
filePath || resolved.pathname,
264268
base && fileURLToPath(base),
@@ -996,7 +1000,7 @@ function packageResolve(specifier, base, conditions) {
9961000
let lastPath
9971001
do {
9981002
const stat = tryStatSync(packageJsonPath.slice(0, -13))
999-
if (!stat.isDirectory()) {
1003+
if (!stat || !stat.isDirectory()) {
10001004
lastPath = packageJsonPath
10011005
packageJsonUrl = new URL(
10021006
(isScoped ? '../../../../node_modules/' : '../../../node_modules/') +

0 commit comments

Comments
 (0)
Please sign in to comment.