More cleanup to unused method/props on Module/ModuleCache

Reviewed By: jeanlauliac

Differential Revision: D7894078

fbshipit-source-id: 74616cdb6ff1bd71a34a5a53fb5ab031adf23f5a
This commit is contained in:
Rafael Oleza 2018-05-11 15:05:29 -07:00 committed by Facebook Github Bot
parent 058c2ac117
commit bb9bfc261b
7 changed files with 14 additions and 120 deletions

View File

@ -116,11 +116,7 @@ describe('Bundler', function() {
});
});
const module = new Module({
file: '/root/foo.js',
localPath: 'foo.js',
experimentalCaches: true,
});
const module = new Module('/root/foo.js');
require('../../JSTransformer').prototype.transform.mockReturnValue({
sha1: 'abcdefabcdefabcdefabcdefabcdefabcdefabcd',

View File

@ -3578,45 +3578,6 @@ describe('traverseDependencies', function() {
});
});
describe('getModuleForPath()', () => {
let DependencyGraph;
let dependencyGraph;
beforeEach(async () => {
setMockFileSystem({
root: {
'index.js': ``,
imgs: {
'a.png': '',
},
},
});
DependencyGraph = require('../../node-haste/DependencyGraph');
dependencyGraph = await DependencyGraph.load(
{
...defaults,
projectRoots: ['/root'],
},
false /* since we're mocking the filesystem, we cannot use watchman */,
);
});
afterEach(() => {
dependencyGraph.end();
});
it('returns correctly a JS module', async () => {
const module = dependencyGraph.getModuleForPath('/root/index.js');
expect(module.path).toBe('/root/index.js');
});
it('returns correctly an asset module', async () => {
const module = dependencyGraph.getModuleForPath('/root/imgs/a.png');
expect(module.path).toBe('/root/imgs/a.png');
});
});
/**
* When running a test on the dependency graph, watch mode is enabled by
* default, so we must end the watcher to ensure the test does not hang up

View File

@ -113,13 +113,8 @@ async function getResolveDependencyFn(
): Promise<(from: string, to: string) => string> {
const dependencyGraph = await bundler.getDependencyGraph();
return (from: string, to: string) => {
return dependencyGraph.resolveDependency(
dependencyGraph.getModuleForPath(from),
to,
platform,
).path;
};
return (from: string, to: string) =>
dependencyGraph.resolveDependency(from, to, platform);
}
module.exports = {

View File

@ -184,14 +184,9 @@ class DependencyGraph extends EventEmitter {
_createModuleCache() {
const {_opts} = this;
return new ModuleCache(
{
return new ModuleCache({
getClosestPackage: this._getClosestPackage.bind(this),
hasteImplModulePath: _opts.hasteImplModulePath,
roots: _opts.projectRoots,
},
_opts.platforms,
);
});
}
getSha1(filename: string): string {
@ -212,24 +207,16 @@ class DependencyGraph extends EventEmitter {
this._haste.end();
}
getModuleForPath(entryFile: string): Module {
return this._moduleCache.getModule(entryFile);
}
resolveDependency(
fromModule: Module,
toModuleName: string,
platform: ?string,
): Module {
resolveDependency(from: string, to: string, platform: ?string): string {
const req = new ResolutionRequest({
moduleResolver: this._moduleResolver,
entryPath: fromModule.path,
entryPath: from,
helpers: this._helpers,
platform: platform || null,
moduleCache: this._moduleCache,
});
return req.resolveDependency(fromModule, toModuleName);
return req.resolveDependency(this._moduleCache.getModule(from), to).path;
}
_doesFileExist = (filePath: string): boolean => {

View File

@ -13,29 +13,19 @@
const isAbsolutePath = require('absolute-path');
import type ModuleCache from './ModuleCache';
import type {LocalPath} from './lib/toLocalPath';
export type ConstructorArgs = {
file: string,
localPath: LocalPath,
moduleCache: ModuleCache,
};
class Module {
localPath: LocalPath;
path: string;
_moduleCache: ModuleCache;
_sourceCode: ?string;
constructor({file, localPath, moduleCache}: ConstructorArgs) {
constructor(file: string, moduleCache: ModuleCache) {
if (!isAbsolutePath(file)) {
throw new Error('Expected file to be absolute path but got ' + file);
}
this.localPath = localPath;
this.path = file;
this._moduleCache = moduleCache;
}

View File

@ -13,52 +13,28 @@
const Module = require('./Module');
const Package = require('./Package');
const toLocalPath = require('./lib/toLocalPath');
type GetClosestPackageFn = (filePath: string) => ?string;
type Options = {|
hasteImplModulePath?: string,
getClosestPackage: GetClosestPackageFn,
roots: $ReadOnlyArray<string>,
|};
class ModuleCache {
_getClosestPackage: GetClosestPackageFn;
_moduleCache: {[filePath: string]: Module, __proto__: null};
_packageCache: {[filePath: string]: Package, __proto__: null};
_packageModuleMap: WeakMap<Module, string>;
_platforms: Set<string>;
_roots: $ReadOnlyArray<string>;
_opts: Options;
constructor(options: Options, platforms: Set<string>) {
const {getClosestPackage, roots} = options;
this._opts = options;
this._getClosestPackage = getClosestPackage;
constructor(options: {getClosestPackage: GetClosestPackageFn}) {
this._getClosestPackage = options.getClosestPackage;
this._moduleCache = Object.create(null);
this._packageCache = Object.create(null);
this._packageModuleMap = new WeakMap();
this._platforms = platforms;
this._roots = roots;
}
getModule(filePath: string) {
if (!this._moduleCache[filePath]) {
this._moduleCache[filePath] = new Module({
file: filePath,
localPath: toLocalPath(this._roots, filePath),
moduleCache: this,
options: this._getModuleOptions(),
});
this._moduleCache[filePath] = new Module(filePath, this);
}
return this._moduleCache[filePath];
}
getAllModules() {
return this._moduleCache;
}
getPackage(filePath: string): Package {
if (!this._packageCache[filePath]) {
this._packageCache[filePath] = new Package({
@ -97,12 +73,6 @@ class ModuleCache {
delete this._packageCache[filePath];
}
}
_getModuleOptions() {
return {
hasteImplModulePath: this._opts.hasteImplModulePath,
};
}
}
module.exports = ModuleCache;

View File

@ -20,15 +20,10 @@ describe('Module', () => {
beforeEach(() => {
moduleCache = new ModuleCache({});
module = new Module({
file: '/root/to/file.js',
localPath: 'file.js',
moduleCache,
});
module = new Module('/root/to/file.js', moduleCache);
});
it('Returns the correct values for many properties and methods', () => {
expect(module.localPath).toBe('file.js');
expect(module.path).toBe('/root/to/file.js');
});
});