From b54fd91f7753d66d6f4523c292fe235bae08fa76 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Fri, 4 Nov 2016 10:42:41 -0700 Subject: [PATCH] RM node-haste DependencyGraph: @flow Summary: In addition to adding flow in that file, I also had to fix `Module.js` and others to make everything compatible together. Reviewed By: davidaurelio Differential Revision: D4118829 fbshipit-source-id: 4f6dbc515741c38817cc4c9757e981fabb03915a --- .../src/node-haste/Cache/index.js | 21 ++-- .../react-packager/src/node-haste/Module.js | 18 +--- .../src/node-haste/ModuleCache.js | 4 +- .../react-packager/src/node-haste/Package.js | 8 +- .../react-packager/src/node-haste/fastfs.js | 9 +- .../react-packager/src/node-haste/index.js | 96 ++++++++++++++++++- 6 files changed, 111 insertions(+), 45 deletions(-) diff --git a/packager/react-packager/src/node-haste/Cache/index.js b/packager/react-packager/src/node-haste/Cache/index.js index 12782f619c..a72b009c22 100644 --- a/packager/react-packager/src/node-haste/Cache/index.js +++ b/packager/react-packager/src/node-haste/Cache/index.js @@ -67,21 +67,24 @@ class Cache { return path.join(tmpdir, hash.digest('hex')); } - get( + get( filepath: string, field: string, - loaderCb: (filepath: string) => Promise, - ): Promise { + loaderCb: (filepath: string) => Promise, + ): Promise { if (!isAbsolutePath(filepath)) { throw new Error('Use absolute paths'); } return this.has(filepath, field) - ? this._data[filepath].data[field] + /* $FlowFixMe: this class is unsound as a whole because it uses + * untyped storage where in fact each "field" has a particular type. + * We cannot express this using Flow. */ + ? (this._data[filepath].data[field]: Promise) : this.set(filepath, field, loaderCb(filepath)); } - invalidate(filepath: string, field: string) { + invalidate(filepath: string, field: ?string) { if (this.has(filepath, field)) { if (field == null) { delete this._data[filepath]; @@ -95,16 +98,16 @@ class Cache { return this._persistCache(); } - has(filepath: string, field: string) { + has(filepath: string, field: ?string) { return Object.prototype.hasOwnProperty.call(this._data, filepath) && (field == null || Object.prototype.hasOwnProperty.call(this._data[filepath].data, field)); } - set( + set( filepath: string, field: string, - loaderPromise: Promise, - ): Promise { + loaderPromise: Promise, + ): Promise { let record = this._data[filepath]; if (!record) { // $FlowFixMe: temporarily invalid record. diff --git a/packager/react-packager/src/node-haste/Module.js b/packager/react-packager/src/node-haste/Module.js index e8ca46343d..dabb17a426 100644 --- a/packager/react-packager/src/node-haste/Module.js +++ b/packager/react-packager/src/node-haste/Module.js @@ -18,7 +18,9 @@ const isAbsolutePath = require('absolute-path'); const jsonStableStringify = require('json-stable-stringify'); const path = require('path'); +import type Cache from './Cache'; import type ModuleCache from './ModuleCache'; +import type FastFs from './fastfs'; export type Extractor = (sourceCode: string) => {deps: {sync: Array}}; type TransformedCode = { @@ -37,23 +39,7 @@ export type TransformCode = ( dependencyOffsets?: Array, map?: string, }>; -export type Cache = { - get( - filePath: string, - key: string, - loadFunc: () => Promise, - ): Promise, - invalidate(filePath: string): void, -}; export type Options = {cacheTransformResults?: boolean}; -export type FastFs = { - readFile: (filePath: string) => Promise, - closest: (innerFilePath: string, fileName: string) => string, - on: ( - event: 'change', - onChange: (type: string, filePath: string, root: string) => void, - ) => FastFs, -}; export type DepGraphHelpers = {isNodeModulesDir: (filePath: string) => boolean}; export type ConstructorArgs = { diff --git a/packager/react-packager/src/node-haste/ModuleCache.js b/packager/react-packager/src/node-haste/ModuleCache.js index 4377ac84fc..c1d61d2211 100644 --- a/packager/react-packager/src/node-haste/ModuleCache.js +++ b/packager/react-packager/src/node-haste/ModuleCache.js @@ -18,14 +18,14 @@ const Polyfill = require('./Polyfill'); const path = require('path'); +import type Cache from './Cache'; import type { - Cache, DepGraphHelpers, - FastFs, Extractor, TransformCode, Options as ModuleOptions, } from './Module'; +import type FastFs from './fastfs'; class ModuleCache { diff --git a/packager/react-packager/src/node-haste/Package.js b/packager/react-packager/src/node-haste/Package.js index 68baf06068..3e4ffa1dc5 100644 --- a/packager/react-packager/src/node-haste/Package.js +++ b/packager/react-packager/src/node-haste/Package.js @@ -14,10 +14,8 @@ const isAbsolutePath = require('absolute-path'); const path = require('path'); -import type { - Cache, - FastFs, -} from './Module'; +import type Cache from './Cache'; +import type FastFs from './fastfs'; class Package { @@ -74,7 +72,7 @@ class Package { ); } - getName() { + getName(): Promise { return this._cache.get(this.path, 'package-name', () => this.read().then(json => json.name) ); diff --git a/packager/react-packager/src/node-haste/fastfs.js b/packager/react-packager/src/node-haste/fastfs.js index 47ac3322ce..e8e8220b5c 100644 --- a/packager/react-packager/src/node-haste/fastfs.js +++ b/packager/react-packager/src/node-haste/fastfs.js @@ -18,11 +18,6 @@ const {EventEmitter} = require('events'); const NOT_FOUND_IN_ROOTS = 'NotFoundInRootsError'; -interface Activity { - startEvent(title: string, m: mixed, opts: mixed): mixed, - endEvent(activity: mixed): void, -} - interface FileWatcher { on(event: 'all', handler: (type: string, filePath: string, rootPath: string, fstat: fs.Stats) => void): void, } @@ -41,16 +36,14 @@ class Fastfs extends EventEmitter { _ignore: (filePath: string) => boolean; _roots: Array; _fastPaths: {[filePath: string]: File}; - _activity: mixed; constructor( name: string, roots: Array, fileWatcher: FileWatcher, files: Array, - {ignore, activity}: { + {ignore}: { ignore: (filePath: string) => boolean, - activity: Activity, }, ) { super(); diff --git a/packager/react-packager/src/node-haste/index.js b/packager/react-packager/src/node-haste/index.js index 68b9527468..1eb53652b2 100644 --- a/packager/react-packager/src/node-haste/index.js +++ b/packager/react-packager/src/node-haste/index.js @@ -5,7 +5,10 @@ * 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 */ + 'use strict'; const Cache = require('./Cache'); @@ -31,6 +34,12 @@ const path = require('path'); const replacePatterns = require('./lib/replacePatterns'); const util = require('util'); +import type { + TransformCode, + Options as ModuleOptions, + Extractor, +} from './Module'; + const ERROR_BUILDING_DEP_GRAPH = 'DependencyGraphError'; const { @@ -41,6 +50,39 @@ const { } = require('../Logger'); class DependencyGraph { + + _opts: { + roots: Array, + ignoreFilePath: (filePath: string) => boolean, + fileWatcher: FileWatcher, + assetRoots_DEPRECATED: Array, + assetExts: Array, + providesModuleNodeModules: mixed, + platforms: Set, + preferNativePlatform: boolean, + extensions: Array, + mocksPattern: mixed, + extractRequires: Extractor, + transformCode: TransformCode, + shouldThrowOnUnresolvedErrors: () => boolean, + enableAssetMap: boolean, + moduleOptions: ModuleOptions, + extraNodeModules: mixed, + useWatchman: boolean, + maxWorkers: number, + resetCache: boolean, + }; + _cache: Cache; + _assetDependencies: mixed; + _helpers: DependencyGraphHelpers; + _fastfs: Fastfs; + _moduleCache: ModuleCache; + _hasteMap: HasteMap; + _deprecatedAssetMap: DeprecatedAssetMap; + _hasteMapError: ?Error; + + _loading: ?Promise; + constructor({ roots, ignoreFilePath, @@ -64,6 +106,28 @@ class DependencyGraph { useWatchman, maxWorkers, resetCache, + }: { + roots: Array, + ignoreFilePath: (filePath: string) => boolean, + fileWatcher: FileWatcher, + assetRoots_DEPRECATED: Array, + assetExts: Array, + providesModuleNodeModules: mixed, + platforms: mixed, + preferNativePlatform: boolean, + cache: Cache, + extensions: Array, + mocksPattern: mixed, + extractRequires: Extractor, + transformCode: TransformCode, + shouldThrowOnUnresolvedErrors: () => boolean, + enableAssetMap: boolean, + assetDependencies: mixed, + moduleOptions: ?ModuleOptions, + extraNodeModules: mixed, + useWatchman: boolean, + maxWorkers: number, + resetCache: boolean, }) { this._opts = { roots, @@ -187,6 +251,7 @@ class DependencyGraph { const error = new Error( `Failed to build DependencyGraph: ${err.message}` ); + /* $FlowFixMe: monkey-patching */ error.type = ERROR_BUILDING_DEP_GRAPH; error.stack = err.stack; throw error; @@ -201,7 +266,7 @@ class DependencyGraph { * Returns a promise with the direct dependencies the module associated to * the given entryPath has. */ - getShallowDependencies(entryPath, transformOptions) { + getShallowDependencies(entryPath: string, transformOptions: mixed) { return this._moduleCache .getModule(entryPath) .getDependencies(transformOptions); @@ -214,7 +279,7 @@ class DependencyGraph { /** * Returns the module object for the given path. */ - getModuleForPath(entryFile) { + getModuleForPath(entryFile: string) { return this._moduleCache.getModule(entryFile); } @@ -228,6 +293,12 @@ class DependencyGraph { transformOptions, onProgress, recursive = true, + }: { + entryPath: string, + platform: string, + transformOptions: mixed, + onProgress: () => void, + recursive: boolean, }) { return this.load().then(() => { platform = this._getRequestPlatform(entryPath, platform); @@ -258,11 +329,11 @@ class DependencyGraph { }); } - matchFilesByPattern(pattern) { + matchFilesByPattern(pattern: RegExp) { return this.load().then(() => this._fastfs.matchFilesByPattern(pattern)); } - _getRequestPlatform(entryPath, platform) { + _getRequestPlatform(entryPath: string, platform: string) { if (platform == null) { platform = getPlatformExtension(entryPath, this._opts.platforms); } else if (!this._opts.platforms.has(platform)) { @@ -322,16 +393,30 @@ class DependencyGraph { } return this._loading; }; + /* $FlowFixMe: there is a risk this happen before we assign that + * variable in the load() function. */ this._loading = this._loading.then(resolve, resolve); } - createPolyfill(options) { + createPolyfill(options: {file: string}) { return this._moduleCache.createPolyfill(options); } getHasteMap() { return this._hasteMap; } + + static Cache; + static Fastfs; + static FileWatcher; + static Module; + static Polyfill; + static extractRequires; + static getAssetDataFromName; + static getPlatformExtension; + static replacePatterns; + static getInverseDependencies; + } Object.assign(DependencyGraph, { @@ -348,6 +433,7 @@ Object.assign(DependencyGraph, { }); function NotFoundError() { + /* $FlowFixMe: monkey-patching */ Error.call(this); Error.captureStackTrace(this, this.constructor); var msg = util.format.apply(util, arguments);