Skip to content

Commit

Permalink
metro-bundler: ResolutionRequest: extract FileNameResolver
Browse files Browse the repository at this point in the history
Summary: I want to untangle `ResolutionRequest` once and for all, that starts by pulling stuff out :-)

Reviewed By: cpojer

Differential Revision: D5155316

fbshipit-source-id: a46ee9b40c6705edcac169adcfdffe25058ec810
  • Loading branch information
Jean Lauliac authored and facebook-github-bot committed May 31, 2017
1 parent 3fecc28 commit 3aedbbb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
46 changes: 46 additions & 0 deletions packager/src/node-haste/DependencyGraph/FileNameResolver.js
@@ -0,0 +1,46 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
* @format
*/

'use strict';

const path = require('path');

export type Options = {|
+dirPath: string,
+doesFileExist: (filePath: string) => boolean,
|};

/**
* When resolving a single module we want to keep track of the list of paths
* we tried to find. This class is a way to aggregate all the tries easily.
*/
class FileNameResolver {
_options: Options;
_tentativeFileNames: Array<string>;

constructor(options: Options) {
this._options = options;
this._tentativeFileNames = [];
}

getTentativeFileNames(): $ReadOnlyArray<string> {
return this._tentativeFileNames;
}

tryToResolveFileName(fileName: string): boolean {
this._tentativeFileNames.push(fileName);
const filePath = path.join(this._options.dirPath, fileName);
return this._options.doesFileExist(filePath);
}
}

module.exports = FileNameResolver;
30 changes: 1 addition & 29 deletions packager/src/node-haste/DependencyGraph/ResolutionRequest.js
Expand Up @@ -13,6 +13,7 @@
'use strict';

const AsyncTaskGroup = require('../lib/AsyncTaskGroup');
const FileNameResolver = require('./FileNameResolver');
const MapWithDefaults = require('../lib/MapWithDefaults');

const debug = require('debug')('RNP:DependencyGraph');
Expand Down Expand Up @@ -839,35 +840,6 @@ function resolutionHash(modulePath, depName) {
return `${path.resolve(modulePath)}:${depName}`;
}

type FileNameResolverOptions = {|
+dirPath: string,
+doesFileExist: (filePath: string) => boolean,
|};

/**
* When resolving a single module we want to keep track of the list of paths
* we tried to find.
*/
class FileNameResolver {
_options: FileNameResolverOptions;
_tentativeFileNames: Array<string>;

constructor(options: FileNameResolverOptions) {
this._options = options;
this._tentativeFileNames = [];
}

getTentativeFileNames(): $ReadOnlyArray<string> {
return this._tentativeFileNames;
}

tryToResolveFileName(fileName: string): boolean {
this._tentativeFileNames.push(fileName);
const filePath = path.join(this._options.dirPath, fileName);
return this._options.doesFileExist(filePath);
}
}

class UnableToResolveError extends Error {
type: string;
from: string;
Expand Down

0 comments on commit 3aedbbb

Please sign in to comment.