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:
parent
ffe18867f2
commit
4ae8e5e21a
|
@ -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 {
|
||||
|
|
|
@ -5,7 +5,11 @@
|
|||
* 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 EventEmitter = require('events');
|
||||
|
@ -18,15 +22,37 @@ const GENERIC_PLATFORM = 'generic';
|
|||
const NATIVE_PLATFORM = 'native';
|
||||
const PACKAGE_JSON = path.sep + 'package.json';
|
||||
|
||||
class HasteMap extends EventEmitter {
|
||||
import type {Moduleish, Packageish, ModuleishCache} from './ResolutionRequest';
|
||||
import type DependencyGraphHelpers from './DependencyGraphHelpers';
|
||||
|
||||
type Options<TModule, TPackage> = {|
|
||||
extensions: Array<string>,
|
||||
files: Array<string>,
|
||||
helpers: DependencyGraphHelpers,
|
||||
moduleCache: ModuleishCache<TModule, TPackage>,
|
||||
platforms: Set<string>,
|
||||
preferNativePlatform: boolean,
|
||||
|};
|
||||
|
||||
class HasteMap<TModule: Moduleish, TPackage: Packageish> extends EventEmitter {
|
||||
|
||||
_extensions: Array<string>;
|
||||
_files: Array<string>;
|
||||
_helpers: DependencyGraphHelpers;
|
||||
_map: {};
|
||||
_moduleCache: ModuleishCache<TModule, TPackage>;
|
||||
_packages: {};
|
||||
_platforms: Set<string>;
|
||||
_preferNativePlatform: boolean;
|
||||
|
||||
constructor({
|
||||
extensions,
|
||||
files,
|
||||
moduleCache,
|
||||
preferNativePlatform,
|
||||
helpers,
|
||||
moduleCache,
|
||||
platforms,
|
||||
}) {
|
||||
preferNativePlatform,
|
||||
}: Options<TModule, TPackage>) {
|
||||
super();
|
||||
this._extensions = extensions;
|
||||
this._files = files;
|
||||
|
@ -35,8 +61,8 @@ class HasteMap extends EventEmitter {
|
|||
this._platforms = platforms;
|
||||
this._preferNativePlatform = preferNativePlatform;
|
||||
|
||||
this._processHastePackage = throat(1, this._processHastePackage.bind(this));
|
||||
this._processHasteModule = throat(1, this._processHasteModule.bind(this));
|
||||
(this: any)._processHastePackage = throat(1, this._processHastePackage.bind(this));
|
||||
(this: any)._processHasteModule = throat(1, this._processHasteModule.bind(this));
|
||||
}
|
||||
|
||||
build() {
|
||||
|
@ -60,7 +86,7 @@ class HasteMap extends EventEmitter {
|
|||
return this._files;
|
||||
}
|
||||
|
||||
processFileChange(type, absPath) {
|
||||
processFileChange(type: string, absPath: string) {
|
||||
return Promise.resolve().then(() => {
|
||||
/*eslint no-labels: 0 */
|
||||
let invalidated;
|
||||
|
@ -96,7 +122,7 @@ class HasteMap extends EventEmitter {
|
|||
});
|
||||
}
|
||||
|
||||
getModule(name, platform = null): Module {
|
||||
getModule(name: string, platform: ?string): ?TModule {
|
||||
const modulesMap = this._map[name];
|
||||
if (modulesMap == null) {
|
||||
return null;
|
||||
|
@ -117,11 +143,11 @@ class HasteMap extends EventEmitter {
|
|||
return module;
|
||||
}
|
||||
|
||||
getPackage(name): Package {
|
||||
getPackage(name: string): TPackage {
|
||||
return this._packages[name];
|
||||
}
|
||||
|
||||
_processHasteModule(file, previousName) {
|
||||
_processHasteModule(file: string, previousName: ?string) {
|
||||
const module = this._moduleCache.getModule(file);
|
||||
return Promise.resolve().then(() => {
|
||||
const isHaste = module.isHaste();
|
||||
|
@ -136,7 +162,7 @@ class HasteMap extends EventEmitter {
|
|||
});
|
||||
}
|
||||
|
||||
_processHastePackage(file, previousName) {
|
||||
_processHastePackage(file: string, previousName: ?string) {
|
||||
const p = this._moduleCache.getPackage(file);
|
||||
return Promise.resolve().then(() => {
|
||||
const isHaste = p.isHaste();
|
||||
|
@ -157,7 +183,7 @@ class HasteMap extends EventEmitter {
|
|||
});
|
||||
}
|
||||
|
||||
_updateHasteMap(name, mod) {
|
||||
_updateHasteMap(name: string, mod: TModule | TPackage) {
|
||||
let existingModule;
|
||||
|
||||
if (mod.type === 'Package') {
|
||||
|
|
|
@ -49,21 +49,26 @@ export type ModuleMap = {
|
|||
): ?string,
|
||||
};
|
||||
|
||||
type Packageish = {
|
||||
export type Packageish = {
|
||||
isHaste(): boolean,
|
||||
getName(): Promise<string>,
|
||||
path: string,
|
||||
redirectRequire(toModuleName: string): string | false,
|
||||
getMain(): string,
|
||||
+root: string,
|
||||
};
|
||||
|
||||
type Moduleish = {
|
||||
export type Moduleish = {
|
||||
+path: string,
|
||||
isHaste(): boolean,
|
||||
getName(): Promise<string>,
|
||||
getPackage(): ?Packageish,
|
||||
hash(): string,
|
||||
readCached(transformOptions: TransformWorkerOptions): CachedReadResult,
|
||||
readFresh(transformOptions: TransformWorkerOptions): Promise<ReadResult>,
|
||||
};
|
||||
|
||||
type ModuleishCache<TModule, TPackage> = {
|
||||
export type ModuleishCache<TModule, TPackage> = {
|
||||
getPackage(
|
||||
name: string,
|
||||
platform?: string,
|
||||
|
|
|
@ -59,8 +59,8 @@ class Package {
|
|||
return path.join(this.root, main);
|
||||
}
|
||||
|
||||
isHaste(): Promise<boolean> {
|
||||
return Promise.resolve().then(() => !!this.read().name);
|
||||
isHaste(): boolean {
|
||||
return !!this.read().name;
|
||||
}
|
||||
|
||||
getName(): Promise<string> {
|
||||
|
|
Loading…
Reference in New Issue