Blame view
node_modules/eslint/lib/config.js
10.7 KB
f7563de62
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
/** * @fileoverview Responsible for loading config files * @author Seth McLaughlin */ "use strict"; //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ const path = require("path"), ConfigOps = require("./config/config-ops"), ConfigFile = require("./config/config-file"), Plugins = require("./config/plugins"), FileFinder = require("./file-finder"), userHome = require("user-home"), isResolvable = require("is-resolvable"), pathIsInside = require("path-is-inside"); const debug = require("debug")("eslint:config"); //------------------------------------------------------------------------------ // Constants //------------------------------------------------------------------------------ const PERSONAL_CONFIG_DIR = userHome || null; //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ /** * 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; } /** * Load and parse a JSON config object from a file. * @param {string|Object} configToLoad the path to the JSON config file or the config object itself. * @returns {Object} the parsed config object (empty object if there was a parse error) * @private */ function loadConfig(configToLoad) { let config = {}, filePath = ""; if (configToLoad) { if (isObject(configToLoad)) { config = configToLoad; if (config.extends) { config = ConfigFile.applyExtends(config, filePath); } } else { filePath = configToLoad; config = ConfigFile.load(filePath); } } return config; } /** * Get personal config object from ~/.eslintrc. * @returns {Object} the personal config object (null if there is no personal config) * @private */ function getPersonalConfig() { let config; if (PERSONAL_CONFIG_DIR) { const filename = ConfigFile.getFilenameForDirectory(PERSONAL_CONFIG_DIR); if (filename) { debug("Using personal config"); config = loadConfig(filename); } } return config || null; } /** * Determine if 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. */ function hasRules(options) { return options.rules && Object.keys(options.rules).length > 0; } /** * Get a local config object. * @param {Object} thisConfig A Config object. * @param {string} directory The directory to start looking in for a local config file. * @returns {Object} The local config object, or an empty object if there is no local config. */ function getLocalConfig(thisConfig, directory) { const localConfigFiles = thisConfig.findLocalConfigFiles(directory), numFiles = localConfigFiles.length, projectConfigPath = ConfigFile.getFilenameForDirectory(thisConfig.options.cwd); let found, config = {}, rootPath; for (let i = 0; i < numFiles; i++) { const localConfigFile = localConfigFiles[i]; // Don't consider the personal config file in the home directory, // except if the home directory is the same as the current working directory if (path.dirname(localConfigFile) === PERSONAL_CONFIG_DIR && localConfigFile !== projectConfigPath) { continue; } // If root flag is set, don't consider file if it is above root if (rootPath && !pathIsInside(path.dirname(localConfigFile), rootPath)) { continue; } debug(`Loading ${localConfigFile}`); const localConfig = loadConfig(localConfigFile); // Don't consider a local config file found if the config is null if (!localConfig) { continue; } // Check for root flag if (localConfig.root === true) { rootPath = path.dirname(localConfigFile); } found = true; debug(`Using ${localConfigFile}`); config = ConfigOps.merge(localConfig, config); } if (!found && !thisConfig.useSpecificConfig) { /* * - Is there a personal config in the user's home directory? If so, * merge that with the passed-in config. * - Otherwise, if no rules were manually passed in, throw and error. * - Note: This function is not called if useEslintrc is false. */ const personalConfig = getPersonalConfig(); if (personalConfig) { config = ConfigOps.merge(config, personalConfig); } else if (!hasRules(thisConfig.options) && !thisConfig.options.baseConfig) { // No config file, no manual configuration, and no rules, so error. const noConfigError = new Error("No ESLint configuration found."); noConfigError.messageTemplate = "no-config-found"; noConfigError.messageData = { directory, filesExamined: localConfigFiles }; throw noConfigError; } } return config; } //------------------------------------------------------------------------------ // API //------------------------------------------------------------------------------ /** * Configuration class */ class Config { /** * Config options * @param {Object} options Options to be passed in */ constructor(options) { options = options || {}; this.ignore = options.ignore; this.ignorePath = options.ignorePath; this.cache = {}; this.parser = options.parser; this.parserOptions = options.parserOptions || {}; this.baseConfig = options.baseConfig ? loadConfig(options.baseConfig) : { rules: {} }; this.useEslintrc = (options.useEslintrc !== false); this.env = (options.envs || []).reduce((envs, name) => { envs[ name ] = true; return envs; }, {}); /* * Handle declared globals. * For global variable foo, handle "foo:false" and "foo:true" to set * whether global is writable. * If user declares "foo", convert to "foo:false". */ this.globals = (options.globals || []).reduce((globals, def) => { const parts = def.split(":"); globals[parts[0]] = (parts.length > 1 && parts[1] === "true"); return globals; }, {}); const useConfig = options.configFile; this.options = options; if (useConfig) { debug(`Using command line config ${useConfig}`); if (isResolvable(useConfig) || isResolvable(`eslint-config-${useConfig}`) || useConfig.charAt(0) === "@") { this.useSpecificConfig = loadConfig(useConfig); } else { this.useSpecificConfig = loadConfig(path.resolve(this.options.cwd, useConfig)); } } } /** * Build a config object merging the base config (conf/eslint-recommended), * the environments config (conf/environments.js) and eventually the user * config. * @param {string} filePath a file in whose directory we start looking for a local config * @returns {Object} config object */ getConfig(filePath) { const directory = filePath ? path.dirname(filePath) : this.options.cwd; let config, userConfig; debug(`Constructing config for ${filePath ? filePath : "text"}`); config = this.cache[directory]; if (config) { debug("Using config from cache"); return config; } // Step 1: Determine user-specified config from .eslintrc.* and package.json files if (this.useEslintrc) { debug("Using .eslintrc and package.json files"); userConfig = getLocalConfig(this, directory); } else { debug("Not using .eslintrc or package.json files"); userConfig = {}; } // Step 2: Create a copy of the baseConfig config = ConfigOps.merge({}, this.baseConfig); // Step 3: Merge in the user-specified configuration from .eslintrc and package.json config = ConfigOps.merge(config, userConfig); // Step 4: Merge in command line config file if (this.useSpecificConfig) { debug("Merging command line config file"); config = ConfigOps.merge(config, this.useSpecificConfig); } // Step 5: Merge in command line environments debug("Merging command line environment settings"); config = ConfigOps.merge(config, { env: this.env }); // Step 6: Merge in command line rules if (this.options.rules) { debug("Merging command line rules"); config = ConfigOps.merge(config, { rules: this.options.rules }); } // Step 7: Merge in command line globals config = ConfigOps.merge(config, { globals: this.globals }); // Only override parser if it is passed explicitly through the command line or if it's not // defined yet (because the final object will at least have the parser key) if (this.parser || !config.parser) { config = ConfigOps.merge(config, { parser: this.parser }); } if (this.parserOptions) { config = ConfigOps.merge(config, { parserOptions: this.parserOptions }); } // Step 8: Merge in command line plugins if (this.options.plugins) { debug("Merging command line plugins"); Plugins.loadAll(this.options.plugins); config = ConfigOps.merge(config, { plugins: this.options.plugins }); } // Step 9: Apply environments to the config if present if (config.env) { config = ConfigOps.applyEnvironments(config); } this.cache[directory] = config; return config; } /** * Find local config files from directory and parent directories. * @param {string} directory The directory to start searching from. * @returns {string[]} The paths of local config files found. */ findLocalConfigFiles(directory) { if (!this.localConfigFinder) { this.localConfigFinder = new FileFinder(ConfigFile.CONFIG_FILES, this.options.cwd); } return this.localConfigFinder.findAllInDirectoryAndParents(directory); } } module.exports = Config; |