Skip to content

Commit

Permalink
Extract DuplicateHasteCandidatesError from ModuleMap implementation
Browse files Browse the repository at this point in the history
Summary:
`DuplicateHasteCandidatesError` was previously defined within `ModuleMap.js` and exported as a static property of `ModuleMap`.

Move it into its own file in preparation for making `ModuleMap` a private implementation of a public interface.

Changelog: Internal

Reviewed By: jacdebug

Differential Revision: D41035344

fbshipit-source-id: af6b2e0ed53a0f5306fea10a939addfab3e00c26
  • Loading branch information
robhogan authored and facebook-github-bot committed Nov 8, 2022
1 parent 951c1d9 commit a4afc45
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 61 deletions.
55 changes: 1 addition & 54 deletions packages/metro-file-map/src/ModuleMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import type {
} 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_MAP = new Map<'g' | 'native' | string, ?DuplicatesSet>();

export default class ModuleMap implements IModuleMap {
static DuplicateHasteCandidatesError: Class<DuplicateHasteCandidatesError>;
+_raw: RawModuleMap;

constructor(raw: RawModuleMap) {
Expand Down Expand Up @@ -161,56 +161,3 @@ export default class ModuleMap implements IModuleMap {
});
}
}

class DuplicateHasteCandidatesError extends Error {
hasteName: string;
platform: string | null;
supportsNativePlatform: boolean;
duplicatesSet: DuplicatesSet;

constructor(
name: string,
platform: string,
supportsNativePlatform: boolean,
duplicatesSet: DuplicatesSet,
) {
const platformMessage = getPlatformMessage(platform);
super(
`The name \`${name}\` was looked up in the Haste module map. It ` +
'cannot be resolved, because there exists several different ' +
'files, or packages, that provide a module for ' +
`that particular name and platform. ${platformMessage} You must ` +
'delete or exclude files until there remains only one of these:\n\n' +
Array.from(duplicatesSet)
.map(
([dupFilePath, dupFileType]) =>
` * \`${dupFilePath}\` (${getTypeMessage(dupFileType)})\n`,
)
.sort()
.join(''),
);
this.hasteName = name;
this.platform = platform;
this.supportsNativePlatform = supportsNativePlatform;
this.duplicatesSet = duplicatesSet;
}
}

function getPlatformMessage(platform: string) {
if (platform === H.GENERIC_PLATFORM) {
return 'The platform is generic (no extension).';
}
return `The platform extension is \`${platform}\`.`;
}

function getTypeMessage(type: number) {
switch (type) {
case H.MODULE:
return 'module';
case H.PACKAGE:
return 'package';
}
return 'unknown';
}

ModuleMap.DuplicateHasteCandidatesError = DuplicateHasteCandidatesError;
5 changes: 3 additions & 2 deletions packages/metro-file-map/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1552,8 +1552,9 @@ describe('HasteMap', () => {
moduleMap.getModule('Pear');
throw new Error('should be unreachable');
} catch (error) {
const {DuplicateHasteCandidatesError} =
require('../ModuleMap').default;
const {
DuplicateHasteCandidatesError,
} = require('../lib/DuplicateHasteCandidatesError');
expect(error).toBeInstanceOf(DuplicateHasteCandidatesError);
expect(error.hasteName).toBe('Pear');
expect(error.platform).toBe('g');
Expand Down
3 changes: 1 addition & 2 deletions packages/metro-file-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,9 @@ type InternalOptions = {

type WorkerInterface = {worker: typeof worker, getSha1: typeof getSha1};

export const DuplicateHasteCandidatesError =
HasteModuleMap.DuplicateHasteCandidatesError;
export {default as ModuleMap} from './ModuleMap';
export {DiskCacheManager} from './cache/DiskCacheManager';
export {DuplicateHasteCandidatesError} from './lib/DuplicateHasteCandidatesError';
export type {IModuleMap} from './flow-types';
export type {default as FS} from './HasteFS';
export type {HealthCheckResult} from './Watcher';
Expand Down
65 changes: 65 additions & 0 deletions packages/metro-file-map/src/lib/DuplicateHasteCandidatesError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react_native
*/

import type {DuplicatesSet} from '../flow-types';

import H from '../constants';

export class DuplicateHasteCandidatesError extends Error {
hasteName: string;
platform: string | null;
supportsNativePlatform: boolean;
duplicatesSet: DuplicatesSet;

constructor(
name: string,
platform: string,
supportsNativePlatform: boolean,
duplicatesSet: DuplicatesSet,
) {
const platformMessage = getPlatformMessage(platform);
super(
`The name \`${name}\` was looked up in the Haste module map. It ` +
'cannot be resolved, because there exists several different ' +
'files, or packages, that provide a module for ' +
`that particular name and platform. ${platformMessage} You must ` +
'delete or exclude files until there remains only one of these:\n\n' +
Array.from(duplicatesSet)
.map(
([dupFilePath, dupFileType]) =>
` * \`${dupFilePath}\` (${getTypeMessage(dupFileType)})\n`,
)
.sort()
.join(''),
);
this.hasteName = name;
this.platform = platform;
this.supportsNativePlatform = supportsNativePlatform;
this.duplicatesSet = duplicatesSet;
}
}

function getPlatformMessage(platform: string) {
if (platform === H.GENERIC_PLATFORM) {
return 'The platform is generic (no extension).';
}
return `The platform extension is \`${platform}\`.`;
}

function getTypeMessage(type: number) {
switch (type) {
case H.MODULE:
return 'module';
case H.PACKAGE:
return 'package';
}
return 'unknown';
}
7 changes: 4 additions & 3 deletions packages/metro/src/node-haste/DependencyGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import type MetroFileMap, {
} from 'metro-file-map';
import type Module from './Module';

import {ModuleMap as MetroFileMapModuleMap} from 'metro-file-map';
import {
DuplicateHasteCandidatesError,
ModuleMap as MetroFileMapModuleMap,
} from 'metro-file-map';

const canonicalize = require('metro-core/src/canonicalize');
const createHasteMap = require('./DependencyGraph/createHasteMap');
Expand All @@ -36,8 +39,6 @@ const nullthrows = require('nullthrows');
const path = require('path');
import type {ResolverInputOptions} from '../shared/types.flow';

const {DuplicateHasteCandidatesError} = MetroFileMapModuleMap;

const NULL_PLATFORM = Symbol();

function getOrCreateMap<T>(
Expand Down

0 comments on commit a4afc45

Please sign in to comment.