Skip to content

Commit

Permalink
Internally rename moduleMap/hasteModuleMap -> hasteMap
Browse files Browse the repository at this point in the history
Summary:
What we currently call `moduleMap: ModuleMap` in fact:
 - Is keyed on Haste names, so is our Haste map in the purest sense - it's unused when Haste resolution is disabled.
 - Isn't actually a map of modules, since it also maps Haste *packages*.

Now that the main export of `metro-file-map` is no longer itself called `HasteMap`, we can align the naming of this internal type + variable with its function.

Changelog: Internal

Reviewed By: motiz88

Differential Revision: D45033202

fbshipit-source-id: f91ce8ac6f47537aec81b4208e0fd687e5a2e53e
  • Loading branch information
robhogan authored and facebook-github-bot committed Jul 5, 2023
1 parent 8240186 commit c6bd02e
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@
import type {
DuplicatesSet,
HTypeValue,
IModuleMap,
ModuleMetaData,
IHasteMap,
HasteMapItemMetaData,
Path,
RawModuleMap,
ReadOnlyRawModuleMap,
RawHasteMap,
ReadOnlyRawHasteMap,
} from './flow-types';

import H from './constants';
import {DuplicateHasteCandidatesError} from './lib/DuplicateHasteCandidatesError';
import * as fastPath from './lib/fast_path';

const EMPTY_OBJ: {[string]: ModuleMetaData} = {};
const EMPTY_OBJ: {[string]: HasteMapItemMetaData} = {};
const EMPTY_MAP = new Map<'g' | 'native' | string, ?DuplicatesSet>();

export default class ModuleMap implements IModuleMap {
+#raw: RawModuleMap;
export default class HasteMap implements IHasteMap {
+#raw: RawHasteMap;

constructor(raw: RawModuleMap) {
constructor(raw: RawHasteMap) {
this.#raw = raw;
}

Expand Down Expand Up @@ -61,7 +61,7 @@ export default class ModuleMap implements IModuleMap {

// FIXME: This is only used by Meta-internal validation and should be
// removed or replaced with a less leaky API.
getRawModuleMap(): ReadOnlyRawModuleMap {
getRawHasteMap(): ReadOnlyRawHasteMap {
return {
duplicates: this.#raw.duplicates,
map: this.#raw.map,
Expand All @@ -81,7 +81,7 @@ export default class ModuleMap implements IModuleMap {
name: string,
platform: ?string,
supportsNativePlatform: boolean,
): ModuleMetaData | null {
): HasteMapItemMetaData | null {
const map = this.#raw.map.get(name) || EMPTY_OBJ;
const dupMap = this.#raw.duplicates.get(name) || EMPTY_MAP;
if (platform != null) {
Expand Down Expand Up @@ -142,8 +142,8 @@ export default class ModuleMap implements IModuleMap {
);
}

static create(rootDir: Path): ModuleMap {
return new ModuleMap({
static create(rootDir: Path): HasteMap {
return new HasteMap({
duplicates: new Map(),
map: new Map(),
rootDir,
Expand Down
56 changes: 28 additions & 28 deletions packages/metro-file-map/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ describe('FileMap', () => {
const originalData = await new FileMap(defaultConfig).build();

// Haste Melon present in its original location.
expect(originalData.hasteModuleMap.getModule('Melon')).toEqual(
expect(originalData.hasteMap.getModule('Melon')).toEqual(
path.join('/', 'project', 'vegetables', 'Melon.js'),
);

Expand All @@ -688,7 +688,7 @@ describe('FileMap', () => {
expect(console.error).not.toHaveBeenCalled();

// Haste Melon is in its new location and not duplicated.
expect(newData.hasteModuleMap.getModule('Melon')).toEqual(
expect(newData.hasteMap.getModule('Melon')).toEqual(
path.join('/', 'project', 'fruits', 'Melon.js'),
);
});
Expand Down Expand Up @@ -1494,11 +1494,11 @@ describe('FileMap', () => {
fm_it.only = (title, fn, options) =>
fm_it(title, fn, {...options, only: true});

fm_it('build returns a "live" fileSystem and hasteModuleMap', async hm => {
const {fileSystem, hasteModuleMap} = await hm.build();
fm_it('build returns a "live" fileSystem and hasteMap', async hm => {
const {fileSystem, hasteMap} = await hm.build();
const filePath = path.join('/', 'project', 'fruits', 'Banana.js');
expect(fileSystem.getModuleName(filePath)).toBeDefined();
expect(hasteModuleMap.getModule('Banana')).toBe(filePath);
expect(hasteMap.getModule('Banana')).toBe(filePath);
mockDeleteFile(path.join('/', 'project', 'fruits'), 'Banana.js');
mockDeleteFile(path.join('/', 'project', 'fruits'), 'Banana.js');
const {eventsQueue} = await waitForItToChange(hm);
Expand All @@ -1515,7 +1515,7 @@ describe('FileMap', () => {
expect(eventsQueue).toEqual([deletedBanana]);
// Verify that the initial result has been updated
expect(fileSystem.getModuleName(filePath)).toBeNull();
expect(hasteModuleMap.getModule('Banana')).toBeNull();
expect(hasteMap.getModule('Banana')).toBeNull();
});

const MOCK_CHANGE_FILE = {
Expand Down Expand Up @@ -1549,7 +1549,7 @@ describe('FileMap', () => {
};

fm_it('handles several change events at once', async hm => {
const {fileSystem, hasteModuleMap} = await hm.build();
const {fileSystem, hasteMap} = await hm.build();
mockFs[path.join('/', 'project', 'fruits', 'Tomato.js')] = `
// Tomato!
`;
Expand Down Expand Up @@ -1589,8 +1589,8 @@ describe('FileMap', () => {
path.join('/', 'project', 'fruits', 'Tomato.js'),
),
).not.toBeNull();
expect(hasteModuleMap.getModule('Tomato')).toBeDefined();
expect(hasteModuleMap.getModule('Pear')).toBe(
expect(hasteMap.getModule('Tomato')).toBeDefined();
expect(hasteMap.getModule('Pear')).toBe(
path.join('/', 'project', 'fruits', 'Pear.js'),
);
});
Expand Down Expand Up @@ -1815,7 +1815,7 @@ describe('FileMap', () => {
fm_it(
'symlink deletion is handled without affecting the symlink target',
async hm => {
const {fileSystem, hasteModuleMap} = await hm.build();
const {fileSystem, hasteMap} = await hm.build();

const symlinkPath = path.join(
'/',
Expand All @@ -1827,7 +1827,7 @@ describe('FileMap', () => {

expect(fileSystem.getModuleName(symlinkPath)).toEqual('Strawberry');
expect(fileSystem.getModuleName(realPath)).toEqual('Strawberry');
expect(hasteModuleMap.getModule('Strawberry', 'g')).toEqual(realPath);
expect(hasteMap.getModule('Strawberry', 'g')).toEqual(realPath);

// Delete the symlink
const e = mockEmitters[path.join('/', 'project', 'fruits')];
Expand All @@ -1851,17 +1851,17 @@ describe('FileMap', () => {
expect(fileSystem.exists(realPath)).toBe(true);
expect(fileSystem.getModuleName(symlinkPath)).toEqual(null);
expect(fileSystem.getModuleName(realPath)).toEqual('Strawberry');
expect(hasteModuleMap.getModule('Strawberry', 'g')).toEqual(realPath);
expect(hasteMap.getModule('Strawberry', 'g')).toEqual(realPath);
},
{config: {enableSymlinks: true}},
);

fm_it(
'correctly tracks changes to both platform-specific versions of a single module name',
async hm => {
const {hasteModuleMap, fileSystem} = await hm.build();
expect(hasteModuleMap.getModule('Orange', 'ios')).toBeTruthy();
expect(hasteModuleMap.getModule('Orange', 'android')).toBeTruthy();
const {hasteMap, fileSystem} = await hm.build();
expect(hasteMap.getModule('Orange', 'ios')).toBeTruthy();
expect(hasteMap.getModule('Orange', 'android')).toBeTruthy();
const e = mockEmitters[path.join('/', 'project', 'fruits')];
e.emit(
'all',
Expand Down Expand Up @@ -1901,11 +1901,11 @@ describe('FileMap', () => {
path.join('/', 'project', 'fruits', 'Orange.android.js'),
),
).toBeTruthy();
const iosVariant = hasteModuleMap.getModule('Orange', 'ios');
const iosVariant = hasteMap.getModule('Orange', 'ios');
expect(iosVariant).toBe(
path.join('/', 'project', 'fruits', 'Orange.ios.js'),
);
const androidVariant = hasteModuleMap.getModule('Orange', 'android');
const androidVariant = hasteMap.getModule('Orange', 'android');
expect(androidVariant).toBe(
path.join('/', 'project', 'fruits', 'Orange.android.js'),
);
Expand All @@ -1926,8 +1926,8 @@ describe('FileMap', () => {
const oldPath = path.join('/', 'project', 'vegetables', 'Melon.js');
const newPath = path.join('/', 'project', 'fruits', 'Melon.js');

const {hasteModuleMap} = await hm.build();
expect(hasteModuleMap.getModule('Melon')).toEqual(oldPath);
const {hasteMap} = await hm.build();
expect(hasteMap.getModule('Melon')).toEqual(oldPath);

// Move vegetables/Melon.js -> fruits/Melon.js
mockFs[newPath] = mockFs[oldPath];
Expand Down Expand Up @@ -1967,12 +1967,12 @@ describe('FileMap', () => {
type: 'add',
},
]);
expect(hasteModuleMap.getModule('Melon')).toEqual(newPath);
expect(hasteMap.getModule('Melon')).toEqual(newPath);
});

describe('recovery from duplicate module IDs', () => {
async function setupDuplicates(hm) {
const {fileSystem, hasteModuleMap} = await hm.build();
const {fileSystem, hasteMap} = await hm.build();
mockFs[path.join('/', 'project', 'fruits', 'Pear.js')] = `
// Pear!
`;
Expand Down Expand Up @@ -2001,7 +2001,7 @@ describe('FileMap', () => {
),
).toBe(true);
try {
hasteModuleMap.getModule('Pear');
hasteMap.getModule('Pear');
throw new Error('should be unreachable');
} catch (error) {
const {
Expand All @@ -2025,7 +2025,7 @@ describe('FileMap', () => {
fm_it(
'recovers when the oldest version of the duplicates is fixed',
async hm => {
const {hasteModuleMap} = await hm.build();
const {hasteMap} = await hm.build();
await setupDuplicates(hm);
mockFs[path.join('/', 'project', 'fruits', 'Pear.js')] = null;
mockFs[path.join('/', 'project', 'fruits', 'Pear2.js')] = `
Expand All @@ -2047,17 +2047,17 @@ describe('FileMap', () => {
MOCK_CHANGE_FILE,
);
await waitForItToChange(hm);
expect(hasteModuleMap.getModule('Pear')).toBe(
expect(hasteMap.getModule('Pear')).toBe(
path.join('/', 'project', 'fruits', 'another', 'Pear.js'),
);
expect(hasteModuleMap.getModule('Pear2')).toBe(
expect(hasteMap.getModule('Pear2')).toBe(
path.join('/', 'project', 'fruits', 'Pear2.js'),
);
},
);

fm_it('recovers when the most recent duplicate is fixed', async hm => {
const {hasteModuleMap} = await hm.build();
const {hasteMap} = await hm.build();
await setupDuplicates(hm);
mockFs[path.join('/', 'project', 'fruits', 'another', 'Pear.js')] =
null;
Expand All @@ -2080,10 +2080,10 @@ describe('FileMap', () => {
MOCK_CHANGE_FILE,
);
await waitForItToChange(hm);
expect(hasteModuleMap.getModule('Pear')).toBe(
expect(hasteMap.getModule('Pear')).toBe(
path.join('/', 'project', 'fruits', 'Pear.js'),
);
expect(hasteModuleMap.getModule('Pear2')).toBe(
expect(hasteMap.getModule('Pear2')).toBe(
path.join('/', 'project', 'fruits', 'another', 'Pear2.js'),
);
});
Expand Down
30 changes: 15 additions & 15 deletions packages/metro-file-map/src/flow-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

'use strict';

import type ModuleMap from './ModuleMap';
import type HasteMap from './HasteMap';
import type {PerfLoggerFactory, RootPerfLogger, PerfLogger} from 'metro-config';
import type {AbortSignal} from 'node-abort-controller';

Expand Down Expand Up @@ -42,15 +42,15 @@ export type BuildParameters = $ReadOnly<{

export type BuildResult = {
fileSystem: FileSystem,
hasteModuleMap: ModuleMap,
hasteMap: HasteMap,
mockMap: MockMap,
};

export type CacheData = $ReadOnly<{
clocks: WatchmanClocks,
map: RawModuleMap['map'],
map: RawHasteMap['map'],
mocks: RawMockMap,
duplicates: RawModuleMap['duplicates'],
duplicates: RawHasteMap['duplicates'],
fileSystemData: mixed,
}>;

Expand Down Expand Up @@ -209,7 +209,7 @@ export interface MockMap {
getMockModule(name: string): ?Path;
}

export interface IModuleMap {
export interface IHasteMap {
getModule(
name: string,
platform?: ?string,
Expand All @@ -223,16 +223,16 @@ export interface IModuleMap {
_supportsNativePlatform: ?boolean,
): ?Path;

getRawModuleMap(): ReadOnlyRawModuleMap;
getRawHasteMap(): ReadOnlyRawHasteMap;
}

export type ModuleMapData = Map<string, ModuleMapItem>;
export type HasteMapData = Map<string, HasteMapItem>;

export type ModuleMapItem = {
[platform: string]: ModuleMetaData,
export type HasteMapItem = {
[platform: string]: HasteMapItemMetaData,
__proto__: null,
};
export type ModuleMetaData = [/* path */ string, /* type */ number];
export type HasteMapItemMetaData = [/* path */ string, /* type */ number];

export interface MutableFileSystem extends FileSystem {
remove(filePath: Path): ?FileMetaData;
Expand All @@ -244,19 +244,19 @@ export type Path = string;

export type RawMockMap = Map<string, Path>;

export type RawModuleMap = {
export type RawHasteMap = {
rootDir: Path,
duplicates: DuplicatesIndex,
map: ModuleMapData,
map: HasteMapData,
};

export type ReadOnlyRawModuleMap = $ReadOnly<{
export type ReadOnlyRawHasteMap = $ReadOnly<{
rootDir: Path,
duplicates: $ReadOnlyMap<
string,
$ReadOnlyMap<string, $ReadOnlyMap<string, number>>,
>,
map: $ReadOnlyMap<string, ModuleMapItem>,
map: $ReadOnlyMap<string, HasteMapItem>,
}>;

export type ReadOnlyRawMockMap = $ReadOnlyMap<string, Path>;
Expand All @@ -280,7 +280,7 @@ export type WorkerMessage = $ReadOnly<{
export type WorkerMetadata = $ReadOnly<{
dependencies?: ?$ReadOnlyArray<string>,
id?: ?string,
module?: ?ModuleMetaData,
module?: ?HasteMapItemMetaData,
sha1?: ?string,
symlinkTarget?: ?string,
}>;
Loading

0 comments on commit c6bd02e

Please sign in to comment.