UNPKG

4.78 kBJavaScriptView Raw
1/**
2 * @fileoverview Responsible for caching config files
3 * @author Sylvan Mably
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Helpers
10//------------------------------------------------------------------------------
11
12/**
13 * Get a string hash for a config vector
14 * @param {Array<Object>} vector config vector to hash
15 * @returns {string} hash of the vector values
16 * @private
17 */
18function hash(vector) {
19 return JSON.stringify(vector);
20}
21
22//------------------------------------------------------------------------------
23// API
24//------------------------------------------------------------------------------
25
26/**
27 * Configuration caching class
28 */
29module.exports = class ConfigCache {
30
31 constructor() {
32 this.configFullNameCache = new Map();
33 this.localHierarchyCache = new Map();
34 this.mergedVectorCache = new Map();
35 this.mergedCache = new Map();
36 }
37
38 /**
39 * Gets a config object from the cache for the specified config file path.
40 * @param {string} configFullName the name of the configuration as used in the eslint config(e.g. 'plugin:node/recommended'),
41 * or the absolute path to a config file. This should uniquely identify a config.
42 * @returns {Object|null} config object, if found in the cache, otherwise null
43 * @private
44 */
45 getConfig(configFullName) {
46 return this.configFullNameCache.get(configFullName);
47 }
48
49 /**
50 * Sets a config object in the cache for the specified config file path.
51 * @param {string} configFullName the name of the configuration as used in the eslint config(e.g. 'plugin:node/recommended'),
52 * or the absolute path to a config file. This should uniquely identify a config.
53 * @param {Object} config the config object to add to the cache
54 * @returns {void}
55 * @private
56 */
57 setConfig(configFullName, config) {
58 this.configFullNameCache.set(configFullName, config);
59 }
60
61 /**
62 * Gets a list of hierarchy-local config objects that apply to the specified directory.
63 * @param {string} directory the path to the directory
64 * @returns {Object[]|null} a list of config objects, if found in the cache, otherwise null
65 * @private
66 */
67 getHierarchyLocalConfigs(directory) {
68 return this.localHierarchyCache.get(directory);
69 }
70
71 /**
72 * For each of the supplied parent directories, sets the list of config objects for that directory to the
73 * appropriate subset of the supplied parent config objects.
74 * @param {string[]} parentDirectories a list of parent directories to add to the config cache
75 * @param {Object[]} parentConfigs a list of config objects that apply to the lowest directory in parentDirectories
76 * @returns {void}
77 * @private
78 */
79 setHierarchyLocalConfigs(parentDirectories, parentConfigs) {
80 parentDirectories.forEach((localConfigDirectory, i) => {
81 const directoryParentConfigs = parentConfigs.slice(0, parentConfigs.length - i);
82
83 this.localHierarchyCache.set(localConfigDirectory, directoryParentConfigs);
84 });
85 }
86
87 /**
88 * Gets a merged config object corresponding to the supplied vector.
89 * @param {Array<Object>} vector the vector to find a merged config for
90 * @returns {Object|null} a merged config object, if found in the cache, otherwise null
91 * @private
92 */
93 getMergedVectorConfig(vector) {
94 return this.mergedVectorCache.get(hash(vector));
95 }
96
97 /**
98 * Sets a merged config object in the cache for the supplied vector.
99 * @param {Array<Object>} vector the vector to save a merged config for
100 * @param {Object} config the merged config object to add to the cache
101 * @returns {void}
102 * @private
103 */
104 setMergedVectorConfig(vector, config) {
105 this.mergedVectorCache.set(hash(vector), config);
106 }
107
108 /**
109 * Gets a merged config object corresponding to the supplied vector, including configuration options from outside
110 * the vector.
111 * @param {Array<Object>} vector the vector to find a merged config for
112 * @returns {Object|null} a merged config object, if found in the cache, otherwise null
113 * @private
114 */
115 getMergedConfig(vector) {
116 return this.mergedCache.get(hash(vector));
117 }
118
119 /**
120 * Sets a merged config object in the cache for the supplied vector, including configuration options from outside
121 * the vector.
122 * @param {Array<Object>} vector the vector to save a merged config for
123 * @param {Object} config the merged config object to add to the cache
124 * @returns {void}
125 * @private
126 */
127 setMergedConfig(vector, config) {
128 this.mergedCache.set(hash(vector), config);
129 }
130};