diff --git a/lib/get-config.js b/lib/get-config.js index b21e775974..52c66582ec 100644 --- a/lib/get-config.js +++ b/lib/get-config.js @@ -362,47 +362,85 @@ function _determineConfigForSeverity(config) { } } -function statusForModule(type, config, options) { - let moduleId = options.moduleId; - let list = config[type]; - let configPath = options.configPath || ''; - if (!list) { - return false; +class ModuleStatusCache { + constructor(config, configPath) { + this.config = config; + this.configPath = configPath || ''; + this.cache = { + pending: {}, + ignore: {}, + }; } - for (const item of list) { - let fullPathModuleId = path.resolve(process.cwd(), moduleId); + lookupPending(moduleId) { + if (!moduleId || !this.config.pending) { + return false; + } + if (!this.cache.pendingLookup) { + this.cache.pendingLookup = this._extractPendingCache(); + } + if (!(moduleId in this.cache.pending)) { + const fullPathModuleId = path.resolve(process.cwd(), moduleId); + this.cache.pending[moduleId] = this.cache.pendingLookup[fullPathModuleId]; + } + return this.cache.pending[moduleId]; + } - if (typeof item === 'function' && item(moduleId)) { - return true; - } else if (typeof item === 'string') { - let fullPathItem = path.resolve(process.cwd(), path.dirname(configPath), item); - if (fullPathModuleId === fullPathItem) { - return true; - } - } else if (item.moduleId) { - let fullPathItem = path.resolve(process.cwd(), path.dirname(configPath), item.moduleId); - if (fullPathModuleId === fullPathItem) { - return item; - } + lookupIgnore(moduleId) { + if (!(moduleId in this.cache.ignore)) { + const ignores = this.config['ignore'] || []; + this.cache.ignore[moduleId] = ignores.find((match) => match(moduleId)); } + return Boolean(this.cache.ignore[moduleId]); } - return false; + _extractPendingCache() { + const list = this.config.pending; + const byFullModuleId = {}; + + if (!list) { + return byFullModuleId; + } + + for (const item of list) { + if (typeof item === 'string') { + const fullPath = this.resolveFullModuleId(item); + byFullModuleId[fullPath] = true; + } else if (item.moduleId) { + const fullPath = this.resolveFullModuleId(item.moduleId); + byFullModuleId[fullPath] = item; + } + } + + return byFullModuleId; + } + resolveFullModuleId(moduleId) { + if (!this._baseDirBasedOnConfigPath) { + this._baseDirBasedOnConfigPath = path.resolve(process.cwd(), path.dirname(this.configPath)); + } + return path.resolve(this._baseDirBasedOnConfigPath, moduleId); + } } + +let configModuleCacheMap = new WeakMap(); + /** * Returns the config in conjunction with overrides configuration. * @param {*} config * @param {*} filePath */ function getConfigForFile(config, options) { + if (!configModuleCacheMap.has(config)) { + configModuleCacheMap.set(config, new ModuleStatusCache(config, options.configPath)); + } + let moduleStatusCache = configModuleCacheMap.get(config); let filePath = options.filePath; let configuredRules = config.rules; let overrides = config.overrides; let fileConfig = Object.assign({}, config, { - pendingStatus: statusForModule('pending', config, options), - shouldIgnore: statusForModule('ignore', config, options), + pendingStatus: moduleStatusCache.lookupPending(options.moduleId), + shouldIgnore: moduleStatusCache.lookupIgnore(options.moduleId), }); if (filePath && overrides) {