Module.js: add @flow

Reviewed By: cpojer

Differential Revision: D4095611

fbshipit-source-id: d87509153eab3d8dbcd65b8c6c6ffb3e5746685f
This commit is contained in:
Jean Lauliac 2016-10-31 04:27:32 -07:00 committed by Facebook Github Bot
parent be244f3a72
commit d8aab602b2
1 changed files with 69 additions and 6 deletions

View File

@ -5,7 +5,10 @@
* This source code is licensed under the BSD-style license found in the * 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 * 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. * of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/ */
'use strict'; 'use strict';
const crypto = require('crypto'); const crypto = require('crypto');
@ -15,8 +18,57 @@ const isAbsolutePath = require('absolute-path');
const jsonStableStringify = require('json-stable-stringify'); const jsonStableStringify = require('json-stable-stringify');
const path = require('path'); const path = require('path');
type Extractor = (sourceCode: string) => {deps: {sync: Array<string>}};
type TransformedCode = {
code?: string,
dependencies?: Array<string>,
dependencyOffsets?: Array<number>,
map?: string,
};
type TransformCode = (
module: Module,
sourceCode: string,
transformOptions: mixed,
) => Promise<{
code: string,
dependencies?: Array<string>,
dependencyOffsets?: Array<number>,
map?: string,
}>;
type Cache = {
get<T>(
filePath: string,
key: string,
loadFunc: () => Promise<T>,
): Promise<T>,
invalidate(filePath: string): void,
};
type Options = {cacheTransformResults?: boolean};
type ModuleCache = {
getPackageForModule(m: Module): {
getName(): Promise<string>,
root: string,
},
};
type FastFs = {readFile: (filePath: string) => Promise<string>};
type DepGraphHelpers = {isNodeModulesDir: (filePath: string) => boolean};
class Module { class Module {
path: string;
type: string;
_fastfs: FastFs;
_moduleCache: ModuleCache;
_cache: Cache;
_extractor: Extractor;
_transformCode: TransformCode;
_depGraphHelpers: DepGraphHelpers;
_options: Options;
_docBlock: Promise<{id?: string, moduleDocBlock: {[key: string]: mixed}}>;
_readPromise: Promise<string>;
constructor({ constructor({
file, file,
fastfs, fastfs,
@ -26,6 +78,15 @@ class Module {
transformCode, transformCode,
depGraphHelpers, depGraphHelpers,
options, options,
}: {
file: string,
fastfs: FastFs,
moduleCache: ModuleCache,
cache: Cache,
extractor: Extractor,
transformCode: TransformCode,
depGraphHelpers: DepGraphHelpers,
options: Options,
}) { }) {
if (!isAbsolutePath(file)) { if (!isAbsolutePath(file)) {
throw new Error('Expected file to be absolute path but got ' + file); throw new Error('Expected file to be absolute path but got ' + file);
@ -43,7 +104,7 @@ class Module {
this._options = options; this._options = options;
} }
isHaste() { isHaste(): Promise<boolean> {
return this._cache.get( return this._cache.get(
this.path, this.path,
'isHaste', 'isHaste',
@ -51,15 +112,15 @@ class Module {
); );
} }
getCode(transformOptions) { getCode(transformOptions: mixed) {
return this.read(transformOptions).then(({code}) => code); return this.read(transformOptions).then(({code}) => code);
} }
getMap(transformOptions) { getMap(transformOptions: mixed) {
return this.read(transformOptions).then(({map}) => map); return this.read(transformOptions).then(({map}) => map);
} }
getName() { getName(): Promise<string> {
return this._cache.get( return this._cache.get(
this.path, this.path,
'name', 'name',
@ -91,7 +152,7 @@ class Module {
return this._moduleCache.getPackageForModule(this); return this._moduleCache.getPackageForModule(this);
} }
getDependencies(transformOptions) { getDependencies(transformOptions: mixed) {
return this.read(transformOptions).then(({dependencies}) => dependencies); return this.read(transformOptions).then(({dependencies}) => dependencies);
} }
@ -130,7 +191,7 @@ class Module {
return this._docBlock; return this._docBlock;
} }
read(transformOptions) { read(transformOptions: mixed): Promise<TransformedCode> {
return this._cache.get( return this._cache.get(
this.path, this.path,
cacheKey('moduleData', transformOptions), cacheKey('moduleData', transformOptions),
@ -152,6 +213,8 @@ class Module {
return codePromise.then(result => { return codePromise.then(result => {
const { const {
code, code,
/* $FlowFixMe: I don't think it should complain as there's
a default value */
dependencies = extern ? [] : this._extractor(code).deps.sync, dependencies = extern ? [] : this._extractor(code).deps.sync,
} = result; } = result;
if (this._options && this._options.cacheTransformResults === false) { if (this._options && this._options.cacheTransformResults === false) {