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 { _getHasteName(): ?string {
if (this._hasteNameCache != null) { if (this._hasteNameCache == null) {
this._hasteNameCache = {hasteName: this._readHasteName()};
}
return this._hasteNameCache.hasteName; 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; const hasteImpl = this._options.hasteImpl;
if (hasteImpl === undefined || hasteImpl.enforceHasteNameMatches) { if (hasteImpl == null) {
const moduleDocBlock = this._readDocBlock(); return this._readHasteNameFromDocBlock();
const {providesModule} = moduleDocBlock;
this._hasteNameCache = {
hasteName: providesModule && !this._depGraphHelpers.isNodeModulesDir(this.path)
? /^\S+/.exec(providesModule)[0]
: undefined,
};
} }
if (hasteImpl !== undefined) {
const {enforceHasteNameMatches} = hasteImpl; const {enforceHasteNameMatches} = hasteImpl;
if (enforceHasteNameMatches) { if (enforceHasteNameMatches != null) {
/* $FlowFixMe: this rely on the above if being executed, that is fragile. const name = this._readHasteNameFromDocBlock();
* Rework the algo. */ enforceHasteNameMatches(this.path, name || undefined);
enforceHasteNameMatches(this.path, this._hasteNameCache.hasteName);
} }
this._hasteNameCache = {hasteName: hasteImpl.getHasteName(this.path)}; return 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 * We extract the Haste name from the `@providesModule` docbloc field. This is
// docblock comments, but doesn't want it to conflict with whitelisted @providesModule * not allowed for modules living in `node_modules`, except if they are
// modules, such as react-haste, fbjs-haste, or react-native or with non-dependency, * whitelisted.
// project-specific code that is using @providesModule. */
_readHasteNameFromDocBlock(): ?string {
const moduleDocBlock = this._readDocBlock(); const moduleDocBlock = this._readDocBlock();
const {providesModule} = moduleDocBlock; const {providesModule} = moduleDocBlock;
this._hasteNameCache = { if (providesModule && !this._depGraphHelpers.isNodeModulesDir(this.path)) {
hasteName: return /^\S+/.exec(providesModule)[0];
providesModule && !this._depGraphHelpers.isNodeModulesDir(this.path)
? /^\S+/.exec(providesModule)[0]
: undefined,
};
} }
return this._hasteNameCache.hasteName; return null;
} }
/** /**

View File

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