Reviewed By: davidaurelio

Differential Revision: D4507868

fbshipit-source-id: 8b845bf8a484f494af1900eaf36454a8f3f78a9f
This commit is contained in:
Bhuwan Khattar 2017-02-06 08:16:08 -08:00 коммит произвёл Facebook Github Bot
Родитель 4e96b3aec5
Коммит d8694a258f
1 изменённых файлов: 31 добавлений и 27 удалений

Просмотреть файл

@ -76,7 +76,8 @@ class Module {
_reporter: Reporter; _reporter: Reporter;
_globalCache: ?GlobalTransformCache; _globalCache: ?GlobalTransformCache;
_docBlock: Promise<{id?: string, moduleDocBlock: {[key: string]: mixed}}>; _docBlock: Promise<{[key: string]: string}>;
_hasteName: Promise<string | void>;
_readSourceCodePromise: Promise<string>; _readSourceCodePromise: Promise<string>;
_readPromises: Map<string, Promise<ReadResult>>; _readPromises: Map<string, Promise<ReadResult>>;
@ -118,7 +119,7 @@ class Module {
return this._cache.get( return this._cache.get(
this.path, this.path,
'isHaste', 'isHaste',
() => this._readDocBlock().then(({id}) => !!id) () => this._getHasteName().then(name => !!name),
); );
} }
@ -134,9 +135,9 @@ class Module {
return this._cache.get( return this._cache.get(
this.path, this.path,
'name', 'name',
() => this._readDocBlock().then(({id}) => { () => this._getHasteName().then(name => {
if (id) { if (name !== undefined) {
return id; return name;
} }
const p = this.getPackage(); const p = this.getPackage();
@ -147,12 +148,12 @@ class Module {
} }
return p.getName() return p.getName()
.then(name => { .then(packageName => {
if (!name) { if (!packageName) {
return this.path; return this.path;
} }
return joinPath(name, relativePath(p.root, this.path)).replace(/\\/g, '/'); return joinPath(packageName, relativePath(p.root, this.path)).replace(/\\/g, '/');
}); });
}) })
); );
@ -176,23 +177,6 @@ class Module {
this._readPromises.clear(); this._readPromises.clear();
} }
_parseDocBlock(docBlock) {
// Extract an id for the module if it's using @providesModule syntax
// and if it's NOT in node_modules (and not a whitelisted node_module).
// This handles the case where a project may have a dep that has @providesModule
// docblock comments, but doesn't want it to conflict with whitelisted @providesModule
// modules, such as react-haste, fbjs-haste, or react-native or with non-dependency,
// project-specific code that is using @providesModule.
const moduleDocBlock = docblock.parseAsObject(docBlock);
const providesModule = moduleDocBlock.providesModule;
const id =
providesModule && !this._depGraphHelpers.isNodeModulesDir(this.path)
? /^\S+/.exec(providesModule)[0]
: undefined;
return {id, moduleDocBlock};
}
_readSourceCode() { _readSourceCode() {
if (!this._readSourceCodePromise) { if (!this._readSourceCodePromise) {
this._readSourceCodePromise = new Promise( this._readSourceCodePromise = new Promise(
@ -205,11 +189,30 @@ class Module {
_readDocBlock() { _readDocBlock() {
if (!this._docBlock) { if (!this._docBlock) {
this._docBlock = this._readSourceCode() this._docBlock = this._readSourceCode()
.then(docBlock => this._parseDocBlock(docBlock)); .then(source => docblock.parseAsObject(source));
} }
return this._docBlock; return this._docBlock;
} }
_getHasteName() {
if (!this._hasteName) {
// Extract an id for the module if it's using @providesModule syntax
// and if it's NOT in node_modules (and not a whitelisted node_module).
// This handles the case where a project may have a dep that has @providesModule
// docblock comments, but doesn't want it to conflict with whitelisted @providesModule
// modules, such as react-haste, fbjs-haste, or react-native or with non-dependency,
// project-specific code that is using @providesModule.
this._hasteName = this._readDocBlock().then(moduleDocBlock => {
const {providesModule} = moduleDocBlock;
return providesModule
&& !this._depGraphHelpers.isNodeModulesDir(this.path)
? /^\S+/.exec(providesModule)[0]
: undefined;
});
}
return this._hasteName;
}
/** /**
* To what we read from the cache or worker, we need to add id and source. * To what we read from the cache or worker, we need to add id and source.
*/ */
@ -313,7 +316,8 @@ class Module {
const freshPromise = Promise.all([ const freshPromise = Promise.all([
this._readSourceCode(), this._readSourceCode(),
this._readDocBlock(), this._readDocBlock(),
]).then(([sourceCode, {id, moduleDocBlock}]) => { this._getHasteName(),
]).then(([sourceCode, moduleDocBlock, id]) => {
// Ignore requires in JSON files or generated code. An example of this // Ignore requires in JSON files or generated code. An example of this
// is prebuilt files like the SourceMap library. // is prebuilt files like the SourceMap library.
const extern = this.isJSON() || 'extern' in moduleDocBlock; const extern = this.isJSON() || 'extern' in moduleDocBlock;