config.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /**
  2. * @fileoverview Responsible for loading config files
  3. * @author Seth McLaughlin
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const path = require("path"),
  10. os = require("os"),
  11. ConfigOps = require("./config/config-ops"),
  12. ConfigFile = require("./config/config-file"),
  13. ConfigCache = require("./config/config-cache"),
  14. Plugins = require("./config/plugins"),
  15. FileFinder = require("./util/file-finder");
  16. const debug = require("debug")("eslint:config");
  17. //------------------------------------------------------------------------------
  18. // Constants
  19. //------------------------------------------------------------------------------
  20. const PERSONAL_CONFIG_DIR = os.homedir();
  21. const SUBCONFIG_SEP = ":";
  22. //------------------------------------------------------------------------------
  23. // Helpers
  24. //------------------------------------------------------------------------------
  25. /**
  26. * Determines if any rules were explicitly passed in as options.
  27. * @param {Object} options The options used to create our configuration.
  28. * @returns {boolean} True if rules were passed in as options, false otherwise.
  29. * @private
  30. */
  31. function hasRules(options) {
  32. return options.rules && Object.keys(options.rules).length > 0;
  33. }
  34. /**
  35. * Determines if a module is can be resolved.
  36. * @param {string} moduleId The ID (name) of the module
  37. * @returns {boolean} True if it is resolvable; False otherwise.
  38. */
  39. function isResolvable(moduleId) {
  40. try {
  41. require.resolve(moduleId);
  42. return true;
  43. } catch (err) {
  44. return false;
  45. }
  46. }
  47. //------------------------------------------------------------------------------
  48. // API
  49. //------------------------------------------------------------------------------
  50. /**
  51. * Configuration class
  52. */
  53. class Config {
  54. /**
  55. * @param {Object} providedOptions Options to be passed in
  56. * @param {Linter} linterContext Linter instance object
  57. */
  58. constructor(providedOptions, linterContext) {
  59. const options = providedOptions || {};
  60. this.linterContext = linterContext;
  61. this.plugins = new Plugins(linterContext.environments, linterContext.defineRule.bind(linterContext));
  62. this.options = options;
  63. this.ignore = options.ignore;
  64. this.ignorePath = options.ignorePath;
  65. this.parser = options.parser;
  66. this.parserOptions = options.parserOptions || {};
  67. this.configCache = new ConfigCache();
  68. this.baseConfig = options.baseConfig
  69. ? ConfigOps.merge({}, ConfigFile.loadObject(options.baseConfig, this))
  70. : { rules: {} };
  71. this.baseConfig.filePath = "";
  72. this.baseConfig.baseDirectory = this.options.cwd;
  73. this.configCache.setConfig(this.baseConfig.filePath, this.baseConfig);
  74. this.configCache.setMergedVectorConfig(this.baseConfig.filePath, this.baseConfig);
  75. this.useEslintrc = (options.useEslintrc !== false);
  76. this.env = (options.envs || []).reduce((envs, name) => {
  77. envs[name] = true;
  78. return envs;
  79. }, {});
  80. /*
  81. * Handle declared globals.
  82. * For global variable foo, handle "foo:false" and "foo:true" to set
  83. * whether global is writable.
  84. * If user declares "foo", convert to "foo:false".
  85. */
  86. this.globals = (options.globals || []).reduce((globals, def) => {
  87. const parts = def.split(SUBCONFIG_SEP);
  88. globals[parts[0]] = (parts.length > 1 && parts[1] === "true");
  89. return globals;
  90. }, {});
  91. this.loadSpecificConfig(options.configFile);
  92. // Empty values in configs don't merge properly
  93. const cliConfigOptions = {
  94. env: this.env,
  95. rules: this.options.rules,
  96. globals: this.globals,
  97. parserOptions: this.parserOptions,
  98. plugins: this.options.plugins
  99. };
  100. this.cliConfig = {};
  101. Object.keys(cliConfigOptions).forEach(configKey => {
  102. const value = cliConfigOptions[configKey];
  103. if (value) {
  104. this.cliConfig[configKey] = value;
  105. }
  106. });
  107. }
  108. /**
  109. * Loads the config options from a config specified on the command line.
  110. * @param {string} [config] A shareable named config or path to a config file.
  111. * @returns {void}
  112. */
  113. loadSpecificConfig(config) {
  114. if (config) {
  115. debug(`Using command line config ${config}`);
  116. const isNamedConfig =
  117. isResolvable(config) ||
  118. isResolvable(`eslint-config-${config}`) ||
  119. config.charAt(0) === "@";
  120. this.specificConfig = ConfigFile.load(
  121. isNamedConfig ? config : path.resolve(this.options.cwd, config),
  122. this
  123. );
  124. }
  125. }
  126. /**
  127. * Gets the personal config object from user's home directory.
  128. * @returns {Object} the personal config object (null if there is no personal config)
  129. * @private
  130. */
  131. getPersonalConfig() {
  132. if (typeof this.personalConfig === "undefined") {
  133. let config;
  134. const filename = ConfigFile.getFilenameForDirectory(PERSONAL_CONFIG_DIR);
  135. if (filename) {
  136. debug("Using personal config");
  137. config = ConfigFile.load(filename, this);
  138. }
  139. this.personalConfig = config || null;
  140. }
  141. return this.personalConfig;
  142. }
  143. /**
  144. * Builds a hierarchy of config objects, including the base config, all local configs from the directory tree,
  145. * and a config file specified on the command line, if applicable.
  146. * @param {string} directory a file in whose directory we start looking for a local config
  147. * @returns {Object[]} The config objects, in ascending order of precedence
  148. * @private
  149. */
  150. getConfigHierarchy(directory) {
  151. debug(`Constructing config file hierarchy for ${directory}`);
  152. // Step 1: Always include baseConfig
  153. let configs = [this.baseConfig];
  154. // Step 2: Add user-specified config from .eslintrc.* and package.json files
  155. if (this.useEslintrc) {
  156. debug("Using .eslintrc and package.json files");
  157. configs = configs.concat(this.getLocalConfigHierarchy(directory));
  158. } else {
  159. debug("Not using .eslintrc or package.json files");
  160. }
  161. // Step 3: Merge in command line config file
  162. if (this.specificConfig) {
  163. debug("Using command line config file");
  164. configs.push(this.specificConfig);
  165. }
  166. return configs;
  167. }
  168. /**
  169. * Gets a list of config objects extracted from local config files that apply to the current directory, in
  170. * descending order, beginning with the config that is highest in the directory tree.
  171. * @param {string} directory The directory to start looking in for local config files.
  172. * @returns {Object[]} The shallow local config objects, in ascending order of precedence (closest to the current
  173. * directory at the end), or an empty array if there are no local configs.
  174. * @private
  175. */
  176. getLocalConfigHierarchy(directory) {
  177. const localConfigFiles = this.findLocalConfigFiles(directory),
  178. projectConfigPath = ConfigFile.getFilenameForDirectory(this.options.cwd),
  179. searched = [],
  180. configs = [];
  181. for (const localConfigFile of localConfigFiles) {
  182. const localConfigDirectory = path.dirname(localConfigFile);
  183. const localConfigHierarchyCache = this.configCache.getHierarchyLocalConfigs(localConfigDirectory);
  184. if (localConfigHierarchyCache) {
  185. const localConfigHierarchy = localConfigHierarchyCache.concat(configs);
  186. this.configCache.setHierarchyLocalConfigs(searched, localConfigHierarchy);
  187. return localConfigHierarchy;
  188. }
  189. /*
  190. * Don't consider the personal config file in the home directory,
  191. * except if the home directory is the same as the current working directory
  192. */
  193. if (localConfigDirectory === PERSONAL_CONFIG_DIR && localConfigFile !== projectConfigPath) {
  194. continue;
  195. }
  196. debug(`Loading ${localConfigFile}`);
  197. const localConfig = ConfigFile.load(localConfigFile, this);
  198. // Ignore empty config files
  199. if (!localConfig) {
  200. continue;
  201. }
  202. debug(`Using ${localConfigFile}`);
  203. configs.unshift(localConfig);
  204. searched.push(localConfigDirectory);
  205. // Stop traversing if a config is found with the root flag set
  206. if (localConfig.root) {
  207. break;
  208. }
  209. }
  210. if (!configs.length && !this.specificConfig) {
  211. // Fall back on the personal config from ~/.eslintrc
  212. debug("Using personal config file");
  213. const personalConfig = this.getPersonalConfig();
  214. if (personalConfig) {
  215. configs.unshift(personalConfig);
  216. } else if (!hasRules(this.options) && !this.options.baseConfig) {
  217. // No config file, no manual configuration, and no rules, so error.
  218. const noConfigError = new Error("No ESLint configuration found.");
  219. noConfigError.messageTemplate = "no-config-found";
  220. noConfigError.messageData = {
  221. directory,
  222. filesExamined: localConfigFiles
  223. };
  224. throw noConfigError;
  225. }
  226. }
  227. // Set the caches for the parent directories
  228. this.configCache.setHierarchyLocalConfigs(searched, configs);
  229. return configs;
  230. }
  231. /**
  232. * Gets the vector of applicable configs and subconfigs from the hierarchy for a given file. A vector is an array of
  233. * entries, each of which in an object specifying a config file path and an array of override indices corresponding
  234. * to entries in the config file's overrides section whose glob patterns match the specified file path; e.g., the
  235. * vector entry { configFile: '/home/john/app/.eslintrc', matchingOverrides: [0, 2] } would indicate that the main
  236. * project .eslintrc file and its first and third override blocks apply to the current file.
  237. * @param {string} filePath The file path for which to build the hierarchy and config vector.
  238. * @returns {Array<Object>} config vector applicable to the specified path
  239. * @private
  240. */
  241. getConfigVector(filePath) {
  242. const directory = filePath ? path.dirname(filePath) : this.options.cwd;
  243. return this.getConfigHierarchy(directory).map(config => {
  244. const vectorEntry = {
  245. filePath: config.filePath,
  246. matchingOverrides: []
  247. };
  248. if (config.overrides) {
  249. const relativePath = path.relative(config.baseDirectory, filePath || directory);
  250. config.overrides.forEach((override, i) => {
  251. if (ConfigOps.pathMatchesGlobs(relativePath, override.files, override.excludedFiles)) {
  252. vectorEntry.matchingOverrides.push(i);
  253. }
  254. });
  255. }
  256. return vectorEntry;
  257. });
  258. }
  259. /**
  260. * Finds local config files from the specified directory and its parent directories.
  261. * @param {string} directory The directory to start searching from.
  262. * @returns {GeneratorFunction} The paths of local config files found.
  263. */
  264. findLocalConfigFiles(directory) {
  265. if (!this.localConfigFinder) {
  266. this.localConfigFinder = new FileFinder(ConfigFile.CONFIG_FILES, this.options.cwd);
  267. }
  268. return this.localConfigFinder.findAllInDirectoryAndParents(directory);
  269. }
  270. /**
  271. * Builds the authoritative config object for the specified file path by merging the hierarchy of config objects
  272. * that apply to the current file, including the base config (conf/eslint-recommended), the user's personal config
  273. * from their homedir, all local configs from the directory tree, any specific config file passed on the command
  274. * line, any configuration overrides set directly on the command line, and finally the environment configs
  275. * (conf/environments).
  276. * @param {string} filePath a file in whose directory we start looking for a local config
  277. * @returns {Object} config object
  278. */
  279. getConfig(filePath) {
  280. const vector = this.getConfigVector(filePath);
  281. let config = this.configCache.getMergedConfig(vector);
  282. if (config) {
  283. debug("Using config from cache");
  284. return config;
  285. }
  286. // Step 1: Merge in the filesystem configurations (base, local, and personal)
  287. config = ConfigOps.getConfigFromVector(vector, this.configCache);
  288. // Step 2: Merge in command line configurations
  289. config = ConfigOps.merge(config, this.cliConfig);
  290. if (this.cliConfig.plugins) {
  291. this.plugins.loadAll(this.cliConfig.plugins);
  292. }
  293. /*
  294. * Step 3: Override parser only if it is passed explicitly through the command line
  295. * or if it's not defined yet (because the final object will at least have the parser key)
  296. */
  297. if (this.parser || !config.parser) {
  298. config = ConfigOps.merge(config, { parser: this.parser });
  299. }
  300. // Step 4: Apply environments to the config
  301. config = ConfigOps.applyEnvironments(config, this.linterContext.environments);
  302. this.configCache.setMergedConfig(vector, config);
  303. return config;
  304. }
  305. }
  306. module.exports = Config;