Skip to content

Commit ee6ccc3

Browse files
robhoganfacebook-github-bot
authored andcommitted
Relabel + clarify getPackageForModule, getClosestPackage, etc
Summary: This is a simple relabelling to make clear when resolution-related methods expect *absolute* paths, as opposed to origin-relative, package-relative or bare specifiers that are present under the name `modulePath`. It should make the next diffs a little easier to reason about. Changelog: Internal Reviewed By: huntie Differential Revision: D56788884 fbshipit-source-id: 5c6a3d5c2d7f11519325756a26529c4fa9238013
1 parent 0c066e5 commit ee6ccc3

File tree

6 files changed

+33
-28
lines changed

6 files changed

+33
-28
lines changed

packages/metro-resolver/src/resolve.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ function resolve(
138138
return {type: 'empty'};
139139
}
140140

141+
// candidate should be absolute here - we assume that redirectModulePath
142+
// always returns an absolute path when given an absolute path.
141143
const result = resolvePackage(context, candidate, platform);
142144
if (result.type === 'resolved') {
143145
return result.resolution;
@@ -252,11 +254,11 @@ function resolvePackage(
252254
* The absolute path to a file or directory that may be contained within an
253255
* npm package, e.g. from being joined with `context.extraNodeModules`.
254256
*/
255-
modulePath: string,
257+
absoluteCandidatePath: string,
256258
platform: string | null,
257259
): Result<Resolution, FileAndDirCandidates> {
258260
if (context.unstable_enablePackageExports) {
259-
const pkg = context.getPackageForModule(modulePath);
261+
const pkg = context.getPackageForModule(absoluteCandidatePath);
260262
const exportsField = pkg?.packageJson.exports;
261263

262264
if (pkg != null && exportsField != null) {
@@ -276,7 +278,7 @@ function resolvePackage(
276278
const packageExportsResult = resolvePackageTargetFromExports(
277279
{...context, unstable_conditionNames: conditionNamesOverride},
278280
pkg.rootPath,
279-
modulePath,
281+
absoluteCandidatePath,
280282
exportsField,
281283
platform,
282284
);
@@ -302,7 +304,7 @@ function resolvePackage(
302304
}
303305
}
304306

305-
return resolveModulePath(context, modulePath, platform);
307+
return resolveModulePath(context, absoluteCandidatePath, platform);
306308
}
307309

308310
/**

packages/metro-resolver/src/types.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,13 @@ export type ResolutionContext = $ReadOnly<{
121121
getPackage: (packageJsonPath: string) => ?PackageJson,
122122

123123
/**
124-
* Get the package information and parsed `package.json` file for for a given
125-
* module path, if it is contained within an npm package.
124+
* Get the closest package scope and parsed `package.json` for a given
125+
* absolute candidate path (which need not exist), or null if there is no
126+
* package.json closer than the nearest node_modules directory.
126127
*
127128
* @deprecated See https://github.com/facebook/metro/commit/29c77bff31e2475a086bc3f04073f485da8f9ff0
128129
*/
129-
getPackageForModule: (modulePath: string) => ?PackageInfo,
130+
getPackageForModule: (absoluteModulePath: string) => ?PackageInfo,
130131

131132
/**
132133
* The dependency descriptor, within the origin module, corresponding to the

packages/metro-resolver/types/types.d.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,15 @@ export interface ResolutionContext {
9797
readonly getPackage: (packageJsonPath: string) => PackageJson | null;
9898

9999
/**
100-
* Get the package information and parsed `package.json` file for for a given
101-
* module path, if it is contained within an npm package.
100+
* Get the closest package scope and parsed `package.json` for a given
101+
* absolute candidate path (which need not exist), or null if there is no
102+
* package.json closer than the nearest node_modules directory.
102103
*
103-
* @deprecated
104+
* @deprecated See https://github.com/facebook/metro/commit/29c77bff31e2475a086bc3f04073f485da8f9ff0
104105
*/
105-
readonly getPackageForModule: (modulePath: string) => PackageInfo | null;
106+
readonly getPackageForModule: (
107+
absoluteModulePath: string,
108+
) => PackageInfo | null;
106109

107110
/**
108111
* The dependency descriptor, within the origin module, corresponding to the

packages/metro/src/node-haste/DependencyGraph.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ class DependencyGraph extends EventEmitter {
152152
return self;
153153
}
154154

155-
_getClosestPackage(filePath: string): ?string {
156-
const parsedPath = path.parse(filePath);
155+
_getClosestPackage(absoluteModulePath: string): ?string {
156+
const parsedPath = path.parse(absoluteModulePath);
157157
const root = parsedPath.root;
158158
let dir = path.join(parsedPath.dir, parsedPath.base);
159159

@@ -242,7 +242,8 @@ class DependencyGraph extends EventEmitter {
242242

243243
_createModuleCache(): ModuleCache {
244244
return new ModuleCache({
245-
getClosestPackage: filePath => this._getClosestPackage(filePath),
245+
getClosestPackage: absoluteModulePath =>
246+
this._getClosestPackage(absoluteModulePath),
246247
});
247248
}
248249

packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export type ModuleishCache<TPackage> = interface {
5353
platform?: string,
5454
supportsNativePlatform?: boolean,
5555
): TPackage,
56-
getPackageOf(modulePath: string): ?TPackage,
56+
getPackageOf(absolutePath: string): ?TPackage,
5757
};
5858

5959
type Options<TPackage> = $ReadOnly<{
@@ -179,8 +179,8 @@ class ModuleResolver<TPackage: Packageish> {
179179
resolveHastePackage: (name: string) =>
180180
this._options.getHastePackagePath(name, platform),
181181
getPackage: this._getPackage,
182-
getPackageForModule: (modulePath: string) =>
183-
this._getPackageForModule(fromModule, modulePath),
182+
getPackageForModule: (absoluteModulePath: string) =>
183+
this._getPackageForModule(absoluteModulePath),
184184
},
185185
dependency,
186186
),
@@ -249,14 +249,11 @@ class ModuleResolver<TPackage: Packageish> {
249249
return null;
250250
};
251251

252-
_getPackageForModule = (
253-
fromModule: Moduleish,
254-
modulePath: string,
255-
): ?PackageInfo => {
252+
_getPackageForModule = (absolutePath: string): ?PackageInfo => {
256253
let pkg;
257254

258255
try {
259-
pkg = this._options.moduleCache.getPackageOf(modulePath);
256+
pkg = this._options.moduleCache.getPackageOf(absolutePath);
260257
} catch (e) {
261258
// Do nothing. The standard module cache does not trigger any error, but
262259
// the ModuleGraph one does, if the module does not exist.

packages/metro/src/node-haste/ModuleCache.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
const Module = require('./Module');
1515
const Package = require('./Package');
1616

17-
type GetClosestPackageFn = (filePath: string) => ?string;
17+
type GetClosestPackageFn = (absoluteFilePath: string) => ?string;
1818

1919
class ModuleCache {
2020
_getClosestPackage: GetClosestPackageFn;
@@ -69,21 +69,22 @@ class ModuleCache {
6969
return this.getPackageOf(module.path);
7070
}
7171

72-
getPackageOf(modulePath: string): ?Package {
73-
let packagePath: ?string = this._packagePathByModulePath[modulePath];
72+
getPackageOf(absoluteModulePath: string): ?Package {
73+
let packagePath: ?string =
74+
this._packagePathByModulePath[absoluteModulePath];
7475
if (packagePath && this._packageCache[packagePath]) {
7576
return this._packageCache[packagePath];
7677
}
7778

78-
packagePath = this._getClosestPackage(modulePath);
79+
packagePath = this._getClosestPackage(absoluteModulePath);
7980
if (!packagePath) {
8081
return null;
8182
}
8283

83-
this._packagePathByModulePath[modulePath] = packagePath;
84+
this._packagePathByModulePath[absoluteModulePath] = packagePath;
8485
const modulePaths =
8586
this._modulePathsByPackagePath[packagePath] ?? new Set();
86-
modulePaths.add(modulePath);
87+
modulePaths.add(absoluteModulePath);
8788
this._modulePathsByPackagePath[packagePath] = modulePaths;
8889

8990
return this.getPackage(packagePath);

0 commit comments

Comments
 (0)