Skip to content

Commit

Permalink
Improve comments and simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvan Mably committed Feb 26, 2017
1 parent fbfb2e3 commit 72266f7
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 49 deletions.
30 changes: 19 additions & 11 deletions lib/config.js
Expand Up @@ -33,7 +33,7 @@ const SUBCONFIG_SEP = ":";
//------------------------------------------------------------------------------

/**
* Determine if rules were explicitly passed in as options.
* Determines if any rules were explicitly passed in as options.
* @param {Object} options The options used to create our configuration.
* @returns {boolean} True if rules were passed in as options, false otherwise.
* @private
Expand Down Expand Up @@ -128,7 +128,7 @@ class Config {
}

/**
* Get personal config object from user's home directory
* Gets the personal config object from user's home directory.
* @returns {Object} the personal config object (null if there is no personal config)
* @private
*/
Expand All @@ -151,8 +151,8 @@ class Config {
}

/**
* Build a config hierarchy including the base config (conf/eslint.json), the
* environments config (conf/environments.js) and eventually the user config.
* Builds a hierarchy of config objects, including the base config, all local configs from the directory tree,
* and a config file specified on the command line, if applicable.
* @param {string} directory a file in whose directory we start looking for a local config
* @returns {Object[]} The config objects
* @private
Expand Down Expand Up @@ -183,9 +183,10 @@ class Config {
}

/**
* Get the local config hierarchy for a given directory.
* @param {string} directory The directory to start looking in for a local config file.
* @returns {Object[]} The shallow local config objects, or an empty array if there are no local config.
* Gets a list of config objects extracted from local config files that apply to the current directory, in
* descending order, beginning with the config that is highest in the directory tree.
* @param {string} directory The directory to start looking in for local config files.
* @returns {Object[]} The shallow local config objects, or an empty array if there are no local configs.
* @private
*/
getLocalConfigHierarchy(directory) {
Expand Down Expand Up @@ -267,7 +268,11 @@ class Config {
}

/**
* Get the vector of applicable configs from the hierarchy for a given file (glob matching occurs here).
* Gets the vector of applicable configs from the hierarchy for a given file, including any overrides that apply to
* the specified file path. A vector is an array of config file paths, each optionally followed by one or more
* numbers that correspond to the indices of nested config blocks within the config file's overrides section whose
* glob patterns match the specified file path; e.g., the vector ['/home/john/project/app/.eslintrc', 0, 2] would
* indicate that the main .eslintrc file and its first and third override blocks apply to the current file.
* @param {string} filePath The file path for which to build the hierarchy and config vector.
* @returns {Array<number|string>} array of config file paths or nested override indices
* @private
Expand Down Expand Up @@ -298,7 +303,7 @@ class Config {
}

/**
* Find local config files from directory and parent directories.
* Finds local config files from the specified directory and its parent directories.
* @param {string} directory The directory to start searching from.
* @returns {string[]} The paths of local config files found.
* @private
Expand All @@ -312,8 +317,11 @@ class Config {
}

/**
* Build a config object merging the base config (conf/eslint.json), the
* environments config (conf/environments.js) and eventually the user config.
* Builds the authoritative config object for the specified file path by merging the hierarchy of config objects
* that apply to the current file, including the base config (conf/eslint-recommended), the user's personal config
* from their homedir, all local configs from the directory tree, any specific config file passed on the command
* line, any configuration overrides set directly on the command line, and finally the environment configs
* (conf/environments).
* @param {string} filePath a file in whose directory we start looking for a local config
* @returns {Object} config object
*/
Expand Down
68 changes: 64 additions & 4 deletions lib/config/config-cache.js
Expand Up @@ -41,25 +41,57 @@ class ConfigCache {
this.mergedCache = new Map();
}

/**
* Empties all the subcaches.
* @returns {void}
* @private
*/
reset() {
this.filePathCache.clear();
this.localHierarchyCache.clear();
this.mergedVectorCache.clear();
this.mergedCache.clear();
}

getConfig(filePath) {
return this.filePathCache.get(filePath);
/**
* Gets a config object from the cache for the specified config file path.
* @param {string} configFilePath the absolute path to the config file
* @returns {Object|null} config object, if found in the cache, otherwise null
* @private
*/
getConfig(configFilePath) {
return this.filePathCache.get(configFilePath);
}

setConfig(filePath, config) {
this.filePathCache.set(filePath, config);
/**
* Sets a config object in the cache for the specified config file path.
* @param {string} configFilePath the absolute path to the config file
* @param {Object} config the config object to add to the cache
* @returns {void}
* @private
*/
setConfig(configFilePath, config) {
this.filePathCache.set(configFilePath, config);
}

/**
* Gets a list of hierarchy-local config objects that apply to the specified directory.
* @param {string} directory the path to the directory
* @returns {Object[]|null} a list of config objects, if found in the cache, otherwise null
* @private
*/
getHierarchyLocalConfigs(directory) {
return this.localHierarchyCache.get(directory);
}

/**
* For each of the supplied parent directories, sets the list of config objects for that directory to the
* appropriate subset of the supplied parent config objects.
* @param {string[]} parentDirectories a list of parent directories to add to the config cache
* @param {Object[]} parentConfigs a list of config objects that apply to the lowest directory in parentDirectories
* @returns {void}
* @private
*/
setHierarchyLocalConfigs(parentDirectories, parentConfigs) {
parentDirectories.forEach((localConfigDirectory, i) => {
const directoryParentConfigs = parentConfigs.slice(0, parentConfigs.length - i);
Expand All @@ -68,18 +100,46 @@ class ConfigCache {
});
}

/**
* Gets a merged config object corresponding to the supplied vector.
* @param {Array<number|string>} vector the vector to find a merged config for
* @returns {Object|null} a merged config object, if found in the cache, otherwise null
* @private
*/
getMergedVectorConfig(vector) {
return this.mergedVectorCache.get(hash(vector));
}

/**
* Sets a merged config object in the cache for the supplied vector.
* @param {Array<number|string>} vector the vector to save a merged config for
* @param {Object} config the merged config object to add to the cache
* @returns {void}
* @private
*/
setMergedVectorConfig(vector, config) {
this.mergedVectorCache.set(hash(vector), config);
}

/**
* Gets a merged config object corresponding to the supplied vector, including configuration options from outside
* the vector.
* @param {Array<number|string>} vector the vector to find a merged config for
* @returns {Object|null} a merged config object, if found in the cache, otherwise null
* @private
*/
getMergedConfig(vector) {
return this.mergedCache.get(hash(vector));
}

/**
* Sets a merged config object in the cache for the supplied vector, including configuration options from outside
* the vector.
* @param {Array<number|string>} vector the vector to save a merged config for
* @param {Object} config the merged config object to add to the cache
* @returns {void}
* @private
*/
setMergedConfig(vector, config) {
this.mergedCache.set(hash(vector), config);
}
Expand Down
45 changes: 13 additions & 32 deletions lib/config/config-file.js
Expand Up @@ -46,16 +46,6 @@ function sortByKey(a, b) {
return a.key > b.key ? 1 : -1;
}

/**
* Check if item is an javascript object
* @param {*} item object to check for
* @returns {boolean} True if its an object
* @private
*/
function isObject(item) {
return typeof item === "object" && !Array.isArray(item) && item !== null;
}

//------------------------------------------------------------------------------
// Private
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -569,35 +559,26 @@ function loadObject(configObject) {
}

/**
* Loads and parses a JSON config object from the config cache, falling back to the disk if the file is not cached.
* @param {string|Object} configToLoad the path to the JSON config file or the config object itself.
* Loads a config object from the config cache based on its filename, falling back to the disk if the file is not yet
* cached.
* @param {string} filePath the path to the config file
* @returns {Object} the parsed config object (empty object if there was a parse error)
* @private
*/
function loadCached(configToLoad) {
let config = {},
cachedConfig,
filePath = "";
function loadCached(filePath) {
const cachedConfig = configCache.getConfig(filePath);
let config = {};

if (isObject(configToLoad)) {
return loadObject(configToLoad);
if (cachedConfig) {
return cachedConfig;
}

if (typeof configToLoad === "string") {
filePath = configToLoad;
cachedConfig = configCache.getConfig(filePath);
config = load(filePath);

if (cachedConfig) {
return cachedConfig;
}

config = load(filePath);

if (config) {
config.filePath = filePath;
config.baseDirectory = path.dirname(filePath);
configCache.setConfig(filePath, config);
}
if (config) {
config.filePath = filePath;
config.baseDirectory = path.dirname(filePath);
configCache.setConfig(filePath, config);
}

return config;
Expand Down
6 changes: 4 additions & 2 deletions lib/config/config-ops.js
Expand Up @@ -275,7 +275,9 @@ module.exports = {
},

/**
* Merges all configurations for a given config vector
* Merges all configurations in a given config vector. A vector is an array of config file paths, each optionally
* followed by one or more numbers that correspond to the indices of nested config blocks within the config file's
* overrides section. All config data is assumed to be cached.
* @param {Array<number|string>} vector array of config file paths or relative override indices
* @returns {Object} config object
*/
Expand Down Expand Up @@ -341,7 +343,7 @@ module.exports = {
},

/**
* Checks that the specified file path matches all of the supplied glob patterns
* Checks that the specified file path matches all of the supplied glob patterns.
* @param {string} filePath The file path to test patterns against
* @param {string|string[]} patterns One or more glob patterns, all of which will be matched against the file path
* @returns {boolean} True if all the supplied patterns match the file path, false otherwise
Expand Down

0 comments on commit 72266f7

Please sign in to comment.