Skip to content

Commit

Permalink
Refactor to hide deprecation warning
Browse files Browse the repository at this point in the history
Closes GH-27.
  • Loading branch information
wooorm committed Apr 29, 2024
1 parent dbb53a5 commit d363b81
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
// Last checked on: Apr 29, 2023.

/**
* @typedef {import('node:fs').Stats} Stats
* @typedef {import('./errors.js').ErrnoException} ErrnoException
* @typedef {import('./package-json-reader.js').PackageConfig} PackageConfig
*/

import assert from 'node:assert'
import {Stats, statSync, realpathSync} from 'node:fs'
import {statSync, realpathSync} from 'node:fs'
import process from 'node:process'
import {URL, fileURLToPath, pathToFileURL} from 'node:url'
import path from 'node:path'
Expand Down Expand Up @@ -129,14 +130,17 @@ function emitLegacyIndexDeprecation(url, packageJsonUrl, base, main) {

/**
* @param {string} path
* @returns {Stats}
* @returns {Stats | undefined}
*/
function tryStatSync(path) {
// Note: from Node 15 onwards we can use `throwIfNoEntry: false` instead.
try {
return statSync(path)
} catch {
return new Stats()
// Note: in Node code this returns `new Stats`,
// but in Node 22 that’s marked as a deprecated internal API.
// Which, well, we kinda are, but still to prevent that warning,
// just yield `undefined`.
}
}

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

if (stats.isDirectory()) {
if (stats && stats.isDirectory()) {
const error = new ERR_UNSUPPORTED_DIR_IMPORT(filePath, fileURLToPath(base))
// @ts-expect-error Add this for `import.meta.resolve`.
error.url = String(resolved)
throw error
}

if (!stats.isFile()) {
if (!stats || !stats.isFile()) {
const error = new ERR_MODULE_NOT_FOUND(
filePath || resolved.pathname,
base && fileURLToPath(base),
Expand Down Expand Up @@ -996,7 +1000,7 @@ function packageResolve(specifier, base, conditions) {
let lastPath
do {
const stat = tryStatSync(packageJsonPath.slice(0, -13))
if (!stat.isDirectory()) {
if (!stat || !stat.isDirectory()) {
lastPath = packageJsonPath
packageJsonUrl = new URL(
(isScoped ? '../../../../node_modules/' : '../../../node_modules/') +
Expand Down

0 comments on commit d363b81

Please # to comment.