Skip to content

Commit

Permalink
fixup! module: add some typings to internal/modules/esm/resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Jul 24, 2021
1 parent 4d4323f commit 9277bac
Showing 1 changed file with 51 additions and 45 deletions.
96 changes: 51 additions & 45 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,22 @@ const DEFAULT_CONDITIONS_SET = new SafeSet(DEFAULT_CONDITIONS);

/**
* @typedef {string|string[]|Record<string,unknown>} Exports
* @typedef {{exports:ExportConfig;name?: string;main?: string;type?:"module"|"commonjs";}} PackageConfig
* @typedef {'module' | 'commonjs'} PackageType
* @typedef {{
* exports?: ExportConfig;
* name?: string;
* main?: string;
* type?: PackageType;
* }} PackageConfig
*/

const emittedPackageWarnings = new SafeSet();

/**
* @param {string} match
* @param {URL} pjsonUrl
* @param {boolean} isExports
* @param {string | URL | undefined} base
* @param {string} match
* @param {URL} pjsonUrl
* @param {boolean} isExports
* @param {string | URL | undefined} base
* @returns {void}
*/
function emitFolderMapDeprecation(match, pjsonUrl, isExports, base) {
Expand All @@ -90,11 +96,11 @@ function emitFolderMapDeprecation(match, pjsonUrl, isExports, base) {
}

/**
* @param {URL} url
* @param {URL} packageJSONUrl
* @param {string | URL | undefined} base
* @param {string} main
* @returns
* @param {URL} url
* @param {URL} packageJSONUrl
* @param {string | URL | undefined} base
* @param {string} main
* @returns
*/
function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
const { format } = defaultGetFormat(url);
Expand Down Expand Up @@ -143,16 +149,16 @@ const realpathCache = new SafeMap();
const packageJSONCache = new SafeMap(); /* string -> PackageConfig */

/**
* @param {string | URL} path
* @param {string | URL} path
* @returns {import('fs').Stats}
*/
const tryStatSync =
(path) => statSync(path, { throwIfNoEntry: false }) ?? new Stats();

/**
* @param {string} path
* @param {string} specifier
* @param {string | URL | undefined} base
* @param {string} path
* @param {string} specifier
* @param {string | URL | undefined} base
* @returns {PackageConfig}
*/
function getPackageConfig(path, specifier, base) {
Expand Down Expand Up @@ -208,7 +214,7 @@ function getPackageConfig(path, specifier, base) {
}

/**
* @param {URL | string} resolved
* @param {URL | string} resolved
* @returns {PackageConfig}
*/
function getPackageScopeConfig(resolved) {
Expand Down Expand Up @@ -304,7 +310,7 @@ function legacyMainResolve(packageJSONUrl, packageConfig, base) {
}

/**
* @param {URL} search
* @param {URL} search
* @returns {URL | undefined}
*/
function resolveExtensionsWithTryExactName(search) {
Expand All @@ -315,7 +321,7 @@ function resolveExtensionsWithTryExactName(search) {
const extensions = ['.js', '.json', '.node', '.mjs'];

/**
* @param {URL} search
* @param {URL} search
* @returns {URL | undefined}
*/
function resolveExtensions(search) {
Expand All @@ -328,7 +334,7 @@ function resolveExtensions(search) {
}

/**
* @param {URL} search
* @param {URL} search
* @returns {URL | undefined}
*/
function resolveDirectoryEntry(search) {
Expand All @@ -349,8 +355,8 @@ function resolveDirectoryEntry(search) {

const encodedSepRegEx = /%2F|%2C/i;
/**
* @param {URL} resolved
* @param {string | URL | undefined} base
* @param {URL} resolved
* @param {string | URL | undefined} base
* @returns {URL | undefined}
*/
function finalizeResolution(resolved, base) {
Expand Down Expand Up @@ -388,9 +394,9 @@ function finalizeResolution(resolved, base) {
}

/**
* @param {string} specifier
* @param {URL} packageJSONUrl
* @param {string | URL | undefined} base
* @param {string} specifier
* @param {URL} packageJSONUrl
* @param {string | URL | undefined} base
*/
function throwImportNotDefined(specifier, packageJSONUrl, base) {
throw new ERR_PACKAGE_IMPORT_NOT_DEFINED(
Expand All @@ -399,9 +405,9 @@ function throwImportNotDefined(specifier, packageJSONUrl, base) {
}

/**
* @param {string} specifier
* @param {URL} packageJSONUrl
* @param {string | URL | undefined} base
* @param {string} specifier
* @param {URL} packageJSONUrl
* @param {string | URL | undefined} base
*/
function throwExportsNotFound(subpath, packageJSONUrl, base) {
throw new ERR_PACKAGE_PATH_NOT_EXPORTED(
Expand All @@ -410,11 +416,11 @@ function throwExportsNotFound(subpath, packageJSONUrl, base) {
}

/**
*
* @param {string | URL} subpath
* @param {URL} packageJSONUrl
* @param {boolean} internal
* @param {string | URL | undefined} base
*
* @param {string | URL} subpath
* @param {URL} packageJSONUrl
* @param {boolean} internal
* @param {string | URL | undefined} base
*/
function throwInvalidSubpath(subpath, packageJSONUrl, internal, base) {
const reason = `request is not a valid subpath for the "${internal ?
Expand Down Expand Up @@ -558,11 +564,11 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
}

/**
*
* @param {Exports} exports
* @param {URL} packageJSONUrl
* @param {string | URL | undefined} base
* @returns
*
* @param {Exports} exports
* @param {URL} packageJSONUrl
* @param {string | URL | undefined} base
* @returns
*/
function isConditionalExportsMainSugar(exports, packageJSONUrl, base) {
if (typeof exports === 'string' || ArrayIsArray(exports)) return true;
Expand Down Expand Up @@ -647,10 +653,10 @@ function packageExportsResolve(
}

/**
* @param {string} name
* @param {string | URL | undefined} base
* @param {Set<string>} conditions
* @returns
* @param {string} name
* @param {string | URL | undefined} base
* @param {Set<string>} conditions
* @returns
*/
function packageImportsResolve(name, base, conditions) {
if (name === '#' || StringPrototypeStartsWith(name, '#/')) {
Expand Down Expand Up @@ -708,17 +714,17 @@ function packageImportsResolve(name, base, conditions) {
}

/**
* @param {URL} url
* @returns {string}
* @param {URL} url
* @returns {PackageType}
*/
function getPackageType(url) {
const packageConfig = getPackageScopeConfig(url);
return packageConfig.type;
}

/**
* @param {string} specifier
* @param {string | URL | undefined} base
* @param {string} specifier
* @param {string | URL | undefined} base
* @returns {{packageName: string, packageSubpath: string, isScoped: boolean}}
*/
function parsePackageName(specifier, base) {
Expand Down Expand Up @@ -814,7 +820,7 @@ function packageResolve(specifier, base, conditions) {
}

/**
* @param {string} specifier
* @param {string} specifier
* @returns {boolean}
*/
function isBareSpecifier(specifier) {
Expand Down

0 comments on commit 9277bac

Please sign in to comment.