packager: HasteMap: @flow + fixes

Summary: Add Flow types, revealing a few problems, such as `isHaste` having the wrong return value in the "pseudo-mocks". But since the buck worker is in fact working, I guess these functions were never called... The point of typing this file is that I'm going to start aggressively pruning dead code in `node-haste` and hopefully, eventually, get rid of `Moduleish` and `Packageish`.

Reviewed By: davidaurelio

Differential Revision: D5052379

fbshipit-source-id: dab3f18f05fcf43fbbc48b589170b1cf367d6a48
This commit is contained in:
Jean Lauliac
2017-05-12 10:37:03 -07:00
committed by Facebook Github Bot
parent ffe18867f2
commit 4ae8e5e21a
7 changed files with 66 additions and 31 deletions
@@ -13,10 +13,10 @@
import type {CachedReadResult, ReadResult} from '../../node-haste/Module';
import type {TransformedCodeFile} from '../types.flow';
import type {ModuleCache} from './node-haste.flow';
import type ModuleCache from './ModuleCache';
module.exports = class Module {
hasteID: Promise<?string>;
hasteID: ?string;
moduleCache: ModuleCache;
name: Promise<string>;
path: string;
@@ -27,9 +27,9 @@ module.exports = class Module {
moduleCache: ModuleCache,
info: TransformedCodeFile,
) {
this.hasteID = Promise.resolve(info.hasteID);
this.hasteID = info.hasteID;
this.moduleCache = moduleCache;
this.name = this.hasteID.then(name => name || getName(path));
this.name = Promise.resolve(this.hasteID || getName(path));
this.path = path;
this.type = 'Module';
}
@@ -51,7 +51,7 @@ module.exports = class Module {
}
isHaste() {
return this.hasteID.then(Boolean);
return Boolean(this.hasteID);
}
hash() {
@@ -34,11 +34,11 @@ module.exports = class ModuleCache {
this.packages = new Map();
}
getAssetModule(path: string) {
getAssetModule(path: string): Module {
return this.getModule(path);
}
getModule(path: string) {
getModule(path: string): Module {
let m = this.modules.get(path);
if (!m) {
m = new Module(path, this, this.getTransformedFile(path));
@@ -47,7 +47,7 @@ module.exports = class ModuleCache {
return m;
}
getPackage(path: string) {
getPackage(path: string): Package {
let p = this.packages.get(path);
if (!p) {
p = new Package(path, this.getPackageData(path));
@@ -64,7 +64,7 @@ module.exports = class ModuleCache {
return pkg;
}
getPackageOf(filePath: string) {
getPackageOf(filePath: string): ?Package {
const candidate = this._getClosestPackage(filePath);
return candidate != null ? this.getPackage(candidate) : null;
}
@@ -49,12 +49,12 @@ module.exports = class Package {
return path.join(this.root, main);
}
getName() {
getName(): Promise<string> {
return Promise.resolve(nullthrows(this.data.name));
}
isHaste() {
return Promise.resolve(!!this.data.name);
isHaste(): boolean {
return !!this.data.name;
}
redirectRequire(name: string) {
@@ -31,6 +31,8 @@ const ResolutionRequest = require('../../node-haste/DependencyGraph/ResolutionRe
const defaults = require('../../../defaults');
import type {Moduleish, Packageish} from '../../node-haste/DependencyGraph/ResolutionRequest';
type ResolveOptions = {|
assetExts: Extensions,
extraNodeModules: {[id: string]: string},
@@ -45,7 +47,7 @@ const platforms = new Set(defaults.platforms);
* a jest-haste-map's ModuleMap instance. Eventually, though, we'll
* want to figure out how to reunify and get rid of `HasteMap`.
*/
function getFakeModuleMap(hasteMap: HasteMap) {
function getFakeModuleMap(hasteMap: HasteMap<Module, Packageish>) {
return {
getModule(name: string, platform: ?string): ?string {
const module = hasteMap.getModule(name, platform);
@@ -58,7 +60,7 @@ function getFakeModuleMap(hasteMap: HasteMap) {
};
}
const nullModule = {
const nullModule: Moduleish = {
path: '/',
getPackage() {},
hash() {
@@ -66,6 +68,8 @@ const nullModule = {
},
readCached() { throw new Error('not implemented'); },
readFresh() { return Promise.reject(new Error('not implemented')); },
isHaste() { throw new Error('not implemented'); },
getName() { throw new Error('not implemented'); },
};
exports.createResolveFn = function(options: ResolveOptions): ResolveFn {