packager: Module.js: fix Haste name code

Summary: The original code wasn't very readable. Better with this change, I think.

Reviewed By: davidaurelio

Differential Revision: D4851335

fbshipit-source-id: 210309d4b727aff58bea48d0ab324256234cd941
This commit is contained in:
Jean Lauliac 2017-04-11 08:05:20 -07:00 committed by Facebook Github Bot
parent 056a829136
commit 5790a4b57f
2 changed files with 39 additions and 36 deletions

View File

@ -210,44 +210,46 @@ class Module {
}
_getHasteName(): ?string {
if (this._hasteNameCache != null) {
if (this._hasteNameCache == null) {
this._hasteNameCache = {hasteName: this._readHasteName()};
}
return this._hasteNameCache.hasteName;
}
/**
* If a custom Haste implementation is provided, then we use it to determine
* the actual Haste name instead of "@providesModule".
* `enforceHasteNameMatches` has been added to that it is easier to
* transition from a system using "@providesModule" to a system using another
* custom system, by throwing if inconsistencies are detected. For example,
* we could verify that the file's basename (ex. "bar/foo.js") is the same as
* the "@providesModule" name (ex. "foo").
*/
_readHasteName(): ?string {
const hasteImpl = this._options.hasteImpl;
if (hasteImpl === undefined || hasteImpl.enforceHasteNameMatches) {
const moduleDocBlock = this._readDocBlock();
const {providesModule} = moduleDocBlock;
this._hasteNameCache = {
hasteName: providesModule && !this._depGraphHelpers.isNodeModulesDir(this.path)
? /^\S+/.exec(providesModule)[0]
: undefined,
};
if (hasteImpl == null) {
return this._readHasteNameFromDocBlock();
}
if (hasteImpl !== undefined) {
const {enforceHasteNameMatches} = hasteImpl;
if (enforceHasteNameMatches) {
/* $FlowFixMe: this rely on the above if being executed, that is fragile.
* Rework the algo. */
enforceHasteNameMatches(this.path, this._hasteNameCache.hasteName);
if (enforceHasteNameMatches != null) {
const name = this._readHasteNameFromDocBlock();
enforceHasteNameMatches(this.path, name || undefined);
}
this._hasteNameCache = {hasteName: hasteImpl.getHasteName(this.path)};
} else {
// 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.
return hasteImpl.getHasteName(this.path);
}
/**
* We extract the Haste name from the `@providesModule` docbloc field. This is
* not allowed for modules living in `node_modules`, except if they are
* whitelisted.
*/
_readHasteNameFromDocBlock(): ?string {
const moduleDocBlock = this._readDocBlock();
const {providesModule} = moduleDocBlock;
this._hasteNameCache = {
hasteName:
providesModule && !this._depGraphHelpers.isNodeModulesDir(this.path)
? /^\S+/.exec(providesModule)[0]
: undefined,
};
if (providesModule && !this._depGraphHelpers.isNodeModulesDir(this.path)) {
return /^\S+/.exec(providesModule)[0];
}
return this._hasteNameCache.hasteName;
return null;
}
/**

View File

@ -315,6 +315,7 @@ describe('Module', () => {
arbitrary: 'arbitrary',
dependencies: ['foo', 'bar'],
dependencyOffsets: [12, 764],
id: null,
map: {version: 3},
subObject: {foo: 'bar'},
};