From 5c109a337b2554a8c4260f0b0a446745fe6c7ab6 Mon Sep 17 00:00:00 2001 From: cpojer Date: Thu, 12 Nov 2015 14:45:23 -0800 Subject: [PATCH] Get rid of all the hasOwnProperty checks in HasteModuleLoader. --- src/HasteModuleLoader/HasteModuleLoader.js | 42 +++++++++++----------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/HasteModuleLoader/HasteModuleLoader.js b/src/HasteModuleLoader/HasteModuleLoader.js index 8f2dcc8d51b4..24c1accb118d 100644 --- a/src/HasteModuleLoader/HasteModuleLoader.js +++ b/src/HasteModuleLoader/HasteModuleLoader.js @@ -38,22 +38,20 @@ const isFile = file => { return stat.isFile() || stat.isFIFO(); }; -const hasOwnProperty = Object.prototype.hasOwnProperty; - let _configUnmockListRegExpCache = null; class Loader { constructor(config, environment) { this._config = config; - this._coverageCollectors = {}; + this._coverageCollectors = Object.create(null); this._currentlyExecutingModulePath = ''; this._environment = environment; - this._explicitShouldMock = {}; - this._explicitlySetMocks = {}; + this._explicitShouldMock = Object.create(null); + this._explicitlySetMocks = Object.create(null); this._isCurrentlyExecutingManualMock = null; - this._mockMetaDataCache = {}; + this._mockMetaDataCache = Object.create(null); this._shouldAutoMock = true; - this._configShouldMockModuleNames = {}; + this._configShouldMockModuleNames = Object.create(null); this._extensions = config.moduleFileExtensions.map(ext => '.' + ext); this._resolver = HasteResolver.get( @@ -127,7 +125,7 @@ class Loader { (onlyCollectFrom && onlyCollectFrom[filename] === true); if (shouldCollectCoverage) { - if (!hasOwnProperty.call(this._coverageCollectors, filename)) { + if (!this._coverageCollectors[filename]) { this._coverageCollectors[filename] = new this._CoverageCollector(moduleContent, filename); } @@ -172,7 +170,7 @@ class Loader { _generateMock(currPath, moduleName) { const modulePath = this._resolveModuleName(currPath, moduleName); - if (!hasOwnProperty.call(this._mockMetaDataCache, modulePath)) { + if (!this._mockMetaDataCache[modulePath]) { // This allows us to handle circular dependencies while generating an // automock this._mockMetaDataCache[modulePath] = moduleMocker.getMetadata({}); @@ -187,8 +185,8 @@ class Loader { // mocked has calls into side-effectful APIs on another module. const origMockRegistry = this._mockRegistry; const origModuleRegistry = this._moduleRegistry; - this._mockRegistry = {}; - this._moduleRegistry = {}; + this._mockRegistry = Object.create(null); + this._moduleRegistry = Object.create(null); const moduleExports = this.requireModule(currPath, moduleName); @@ -324,14 +322,14 @@ class Loader { */ _shouldMock(currPath, moduleName) { const moduleID = this._getNormalizedModuleID(currPath, moduleName); - if (hasOwnProperty.call(this._explicitShouldMock, moduleID)) { + if (moduleID in this._explicitShouldMock) { return this._explicitShouldMock[moduleID]; } else if (resolve.isCore(moduleName)) { return false; } else if (this._shouldAutoMock) { // See if the module is specified in the config as a module that should // never be mocked - if (hasOwnProperty.call(this._configShouldMockModuleNames, moduleName)) { + if (this._configShouldMockModuleNames[moduleName]) { return this._configShouldMockModuleNames[moduleName]; } else if (this._unmockListRegExps.length > 0) { this._configShouldMockModuleNames[moduleName] = true; @@ -412,7 +410,7 @@ class Loader { ); } - const coverageInfo = {}; + const coverageInfo = Object.create(null); for (const filePath in this._coverageCollectors) { coverageInfo[filePath] = this._coverageCollectors[filePath].extractRuntimeCoverageInfo(); @@ -429,7 +427,7 @@ class Loader { } return ( - hasOwnProperty.call(this._coverageCollectors, filePath) + this._coverageCollectors[filePath] ? this._coverageCollectors[filePath].extractRuntimeCoverageInfo() : null ); @@ -474,7 +472,7 @@ class Loader { // TODO if (this._reverseDependencyMap == null) { - const reverseDepMap = this._reverseDependencyMap = {}; + const reverseDepMap = this._reverseDependencyMap = Object.create(null); const allResources = []; Object.keys(allResources).forEach(resourceID => { const resource = allResources[resourceID]; @@ -488,8 +486,8 @@ class Loader { const dependencyPaths = _getDependencyPathsFromResource(resource); for (let i = 0; i < dependencyPaths.length; i++) { const requiredModulePath = dependencyPaths[i]; - if (!hasOwnProperty.call(reverseDepMap, requiredModulePath)) { - reverseDepMap[requiredModulePath] = {}; + if (!reverseDepMap[requiredModulePath]) { + reverseDepMap[requiredModulePath] = Object.create(null); } reverseDepMap[requiredModulePath][resource.path] = true; } @@ -506,7 +504,7 @@ class Loader { requireMock(currPath, moduleName) { const moduleID = this._getNormalizedModuleID(currPath, moduleName); - if (hasOwnProperty.call(this._explicitlySetMocks, moduleID)) { + if (this._explicitlySetMocks[moduleID]) { return this._explicitlySetMocks[moduleID]; } @@ -544,7 +542,7 @@ class Loader { } } - if (hasOwnProperty.call(this._mockRegistry, modulePath)) { + if (this._mockRegistry[modulePath]) { return this._mockRegistry[modulePath]; } @@ -746,8 +744,8 @@ class Loader { } resetModuleRegistry() { - this._mockRegistry = {}; - this._moduleRegistry = {}; + this._mockRegistry = Object.create(null); + this._moduleRegistry = Object.create(null); if (this._environment && this._environment.global) { var envGlobal = this._environment.global;