cascading-config-array-factory.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /**
  2. * @fileoverview `CascadingConfigArrayFactory` class.
  3. *
  4. * `CascadingConfigArrayFactory` class has a responsibility:
  5. *
  6. * 1. Handles cascading of config files.
  7. *
  8. * It provides two methods:
  9. *
  10. * - `getConfigArrayForFile(filePath)`
  11. * Get the corresponded configuration of a given file. This method doesn't
  12. * throw even if the given file didn't exist.
  13. * - `clearCache()`
  14. * Clear the internal cache. You have to call this method when
  15. * `additionalPluginPool` was updated if `baseConfig` or `cliConfig` depends
  16. * on the additional plugins. (`CLIEngine#addPlugin()` method calls this.)
  17. *
  18. * @author Toru Nagashima <https://github.com/mysticatea>
  19. */
  20. "use strict";
  21. //------------------------------------------------------------------------------
  22. // Requirements
  23. //------------------------------------------------------------------------------
  24. const os = require("os");
  25. const path = require("path");
  26. const { validateConfigArray } = require("../shared/config-validator");
  27. const { ConfigArrayFactory } = require("./config-array-factory");
  28. const { ConfigArray, ConfigDependency, IgnorePattern } = require("./config-array");
  29. const loadRules = require("./load-rules");
  30. const debug = require("debug")("eslint:cascading-config-array-factory");
  31. //------------------------------------------------------------------------------
  32. // Helpers
  33. //------------------------------------------------------------------------------
  34. // Define types for VSCode IntelliSense.
  35. /** @typedef {import("../shared/types").ConfigData} ConfigData */
  36. /** @typedef {import("../shared/types").Parser} Parser */
  37. /** @typedef {import("../shared/types").Plugin} Plugin */
  38. /** @typedef {ReturnType<ConfigArrayFactory["create"]>} ConfigArray */
  39. /**
  40. * @typedef {Object} CascadingConfigArrayFactoryOptions
  41. * @property {Map<string,Plugin>} [additionalPluginPool] The map for additional plugins.
  42. * @property {ConfigData} [baseConfig] The config by `baseConfig` option.
  43. * @property {ConfigData} [cliConfig] The config by CLI options (`--env`, `--global`, `--ignore-pattern`, `--parser`, `--parser-options`, `--plugin`, and `--rule`). CLI options overwrite the setting in config files.
  44. * @property {string} [cwd] The base directory to start lookup.
  45. * @property {string} [ignorePath] The path to the alternative file of `.eslintignore`.
  46. * @property {string[]} [rulePaths] The value of `--rulesdir` option.
  47. * @property {string} [specificConfigPath] The value of `--config` option.
  48. * @property {boolean} [useEslintrc] if `false` then it doesn't load config files.
  49. */
  50. /**
  51. * @typedef {Object} CascadingConfigArrayFactoryInternalSlots
  52. * @property {ConfigArray} baseConfigArray The config array of `baseConfig` option.
  53. * @property {ConfigData} baseConfigData The config data of `baseConfig` option. This is used to reset `baseConfigArray`.
  54. * @property {ConfigArray} cliConfigArray The config array of CLI options.
  55. * @property {ConfigData} cliConfigData The config data of CLI options. This is used to reset `cliConfigArray`.
  56. * @property {ConfigArrayFactory} configArrayFactory The factory for config arrays.
  57. * @property {Map<string, ConfigArray>} configCache The cache from directory paths to config arrays.
  58. * @property {string} cwd The base directory to start lookup.
  59. * @property {WeakMap<ConfigArray, ConfigArray>} finalizeCache The cache from config arrays to finalized config arrays.
  60. * @property {string} [ignorePath] The path to the alternative file of `.eslintignore`.
  61. * @property {string[]|null} rulePaths The value of `--rulesdir` option. This is used to reset `baseConfigArray`.
  62. * @property {string|null} specificConfigPath The value of `--config` option. This is used to reset `cliConfigArray`.
  63. * @property {boolean} useEslintrc if `false` then it doesn't load config files.
  64. */
  65. /** @type {WeakMap<CascadingConfigArrayFactory, CascadingConfigArrayFactoryInternalSlots>} */
  66. const internalSlotsMap = new WeakMap();
  67. /**
  68. * Create the config array from `baseConfig` and `rulePaths`.
  69. * @param {CascadingConfigArrayFactoryInternalSlots} slots The slots.
  70. * @returns {ConfigArray} The config array of the base configs.
  71. */
  72. function createBaseConfigArray({
  73. configArrayFactory,
  74. baseConfigData,
  75. rulePaths,
  76. cwd
  77. }) {
  78. const baseConfigArray = configArrayFactory.create(
  79. baseConfigData,
  80. { name: "BaseConfig" }
  81. );
  82. /*
  83. * Create the config array element for the default ignore patterns.
  84. * This element has `ignorePattern` property that ignores the default
  85. * patterns in the current working directory.
  86. */
  87. baseConfigArray.unshift(configArrayFactory.create(
  88. { ignorePatterns: IgnorePattern.DefaultPatterns },
  89. { name: "DefaultIgnorePattern" }
  90. )[0]);
  91. /*
  92. * Load rules `--rulesdir` option as a pseudo plugin.
  93. * Use a pseudo plugin to define rules of `--rulesdir`, so we can validate
  94. * the rule's options with only information in the config array.
  95. */
  96. if (rulePaths && rulePaths.length > 0) {
  97. baseConfigArray.push({
  98. name: "--rulesdir",
  99. filePath: "",
  100. plugins: {
  101. "": new ConfigDependency({
  102. definition: {
  103. rules: rulePaths.reduce(
  104. (map, rulesPath) => Object.assign(
  105. map,
  106. loadRules(rulesPath, cwd)
  107. ),
  108. {}
  109. )
  110. },
  111. filePath: "",
  112. id: "",
  113. importerName: "--rulesdir",
  114. importerPath: ""
  115. })
  116. }
  117. });
  118. }
  119. return baseConfigArray;
  120. }
  121. /**
  122. * Create the config array from CLI options.
  123. * @param {CascadingConfigArrayFactoryInternalSlots} slots The slots.
  124. * @returns {ConfigArray} The config array of the base configs.
  125. */
  126. function createCLIConfigArray({
  127. cliConfigData,
  128. configArrayFactory,
  129. ignorePath,
  130. specificConfigPath
  131. }) {
  132. const cliConfigArray = configArrayFactory.create(
  133. cliConfigData,
  134. { name: "CLIOptions" }
  135. );
  136. cliConfigArray.unshift(
  137. ...(ignorePath
  138. ? configArrayFactory.loadESLintIgnore(ignorePath)
  139. : configArrayFactory.loadDefaultESLintIgnore())
  140. );
  141. if (specificConfigPath) {
  142. cliConfigArray.unshift(
  143. ...configArrayFactory.loadFile(
  144. specificConfigPath,
  145. { name: "--config" }
  146. )
  147. );
  148. }
  149. return cliConfigArray;
  150. }
  151. /**
  152. * The error type when there are files matched by a glob, but all of them have been ignored.
  153. */
  154. class ConfigurationNotFoundError extends Error {
  155. // eslint-disable-next-line jsdoc/require-description
  156. /**
  157. * @param {string} directoryPath The directory path.
  158. */
  159. constructor(directoryPath) {
  160. super(`No ESLint configuration found in ${directoryPath}.`);
  161. this.messageTemplate = "no-config-found";
  162. this.messageData = { directoryPath };
  163. }
  164. }
  165. /**
  166. * This class provides the functionality that enumerates every file which is
  167. * matched by given glob patterns and that configuration.
  168. */
  169. class CascadingConfigArrayFactory {
  170. /**
  171. * Initialize this enumerator.
  172. * @param {CascadingConfigArrayFactoryOptions} options The options.
  173. */
  174. constructor({
  175. additionalPluginPool = new Map(),
  176. baseConfig: baseConfigData = null,
  177. cliConfig: cliConfigData = null,
  178. cwd = process.cwd(),
  179. ignorePath,
  180. resolvePluginsRelativeTo = cwd,
  181. rulePaths = [],
  182. specificConfigPath = null,
  183. useEslintrc = true
  184. } = {}) {
  185. const configArrayFactory = new ConfigArrayFactory({
  186. additionalPluginPool,
  187. cwd,
  188. resolvePluginsRelativeTo
  189. });
  190. internalSlotsMap.set(this, {
  191. baseConfigArray: createBaseConfigArray({
  192. baseConfigData,
  193. configArrayFactory,
  194. cwd,
  195. rulePaths
  196. }),
  197. baseConfigData,
  198. cliConfigArray: createCLIConfigArray({
  199. cliConfigData,
  200. configArrayFactory,
  201. ignorePath,
  202. specificConfigPath
  203. }),
  204. cliConfigData,
  205. configArrayFactory,
  206. configCache: new Map(),
  207. cwd,
  208. finalizeCache: new WeakMap(),
  209. ignorePath,
  210. rulePaths,
  211. specificConfigPath,
  212. useEslintrc
  213. });
  214. }
  215. /**
  216. * The path to the current working directory.
  217. * This is used by tests.
  218. * @type {string}
  219. */
  220. get cwd() {
  221. const { cwd } = internalSlotsMap.get(this);
  222. return cwd;
  223. }
  224. /**
  225. * Get the config array of a given file.
  226. * If `filePath` was not given, it returns the config which contains only
  227. * `baseConfigData` and `cliConfigData`.
  228. * @param {string} [filePath] The file path to a file.
  229. * @param {Object} [options] The options.
  230. * @param {boolean} [options.ignoreNotFoundError] If `true` then it doesn't throw `ConfigurationNotFoundError`.
  231. * @returns {ConfigArray} The config array of the file.
  232. */
  233. getConfigArrayForFile(filePath, { ignoreNotFoundError = false } = {}) {
  234. const {
  235. baseConfigArray,
  236. cliConfigArray,
  237. cwd
  238. } = internalSlotsMap.get(this);
  239. if (!filePath) {
  240. return new ConfigArray(...baseConfigArray, ...cliConfigArray);
  241. }
  242. const directoryPath = path.dirname(path.resolve(cwd, filePath));
  243. debug(`Load config files for ${directoryPath}.`);
  244. return this._finalizeConfigArray(
  245. this._loadConfigInAncestors(directoryPath),
  246. directoryPath,
  247. ignoreNotFoundError
  248. );
  249. }
  250. /**
  251. * Clear config cache.
  252. * @returns {void}
  253. */
  254. clearCache() {
  255. const slots = internalSlotsMap.get(this);
  256. slots.baseConfigArray = createBaseConfigArray(slots);
  257. slots.cliConfigArray = createCLIConfigArray(slots);
  258. slots.configCache.clear();
  259. }
  260. /**
  261. * Load and normalize config files from the ancestor directories.
  262. * @param {string} directoryPath The path to a leaf directory.
  263. * @returns {ConfigArray} The loaded config.
  264. * @private
  265. */
  266. _loadConfigInAncestors(directoryPath) {
  267. const {
  268. baseConfigArray,
  269. configArrayFactory,
  270. configCache,
  271. cwd,
  272. useEslintrc
  273. } = internalSlotsMap.get(this);
  274. if (!useEslintrc) {
  275. return baseConfigArray;
  276. }
  277. let configArray = configCache.get(directoryPath);
  278. // Hit cache.
  279. if (configArray) {
  280. debug(`Cache hit: ${directoryPath}.`);
  281. return configArray;
  282. }
  283. debug(`No cache found: ${directoryPath}.`);
  284. const homePath = os.homedir();
  285. // Consider this is root.
  286. if (directoryPath === homePath && cwd !== homePath) {
  287. debug("Stop traversing because of considered root.");
  288. return this._cacheConfig(directoryPath, baseConfigArray);
  289. }
  290. // Load the config on this directory.
  291. try {
  292. configArray = configArrayFactory.loadInDirectory(directoryPath);
  293. } catch (error) {
  294. /* istanbul ignore next */
  295. if (error.code === "EACCES") {
  296. debug("Stop traversing because of 'EACCES' error.");
  297. return this._cacheConfig(directoryPath, baseConfigArray);
  298. }
  299. throw error;
  300. }
  301. if (configArray.length > 0 && configArray.isRoot()) {
  302. debug("Stop traversing because of 'root:true'.");
  303. configArray.unshift(...baseConfigArray);
  304. return this._cacheConfig(directoryPath, configArray);
  305. }
  306. // Load from the ancestors and merge it.
  307. const parentPath = path.dirname(directoryPath);
  308. const parentConfigArray = parentPath && parentPath !== directoryPath
  309. ? this._loadConfigInAncestors(parentPath)
  310. : baseConfigArray;
  311. if (configArray.length > 0) {
  312. configArray.unshift(...parentConfigArray);
  313. } else {
  314. configArray = parentConfigArray;
  315. }
  316. // Cache and return.
  317. return this._cacheConfig(directoryPath, configArray);
  318. }
  319. /**
  320. * Freeze and cache a given config.
  321. * @param {string} directoryPath The path to a directory as a cache key.
  322. * @param {ConfigArray} configArray The config array as a cache value.
  323. * @returns {ConfigArray} The `configArray` (frozen).
  324. */
  325. _cacheConfig(directoryPath, configArray) {
  326. const { configCache } = internalSlotsMap.get(this);
  327. Object.freeze(configArray);
  328. configCache.set(directoryPath, configArray);
  329. return configArray;
  330. }
  331. /**
  332. * Finalize a given config array.
  333. * Concatenate `--config` and other CLI options.
  334. * @param {ConfigArray} configArray The parent config array.
  335. * @param {string} directoryPath The path to the leaf directory to find config files.
  336. * @param {boolean} ignoreNotFoundError If `true` then it doesn't throw `ConfigurationNotFoundError`.
  337. * @returns {ConfigArray} The loaded config.
  338. * @private
  339. */
  340. _finalizeConfigArray(configArray, directoryPath, ignoreNotFoundError) {
  341. const {
  342. cliConfigArray,
  343. configArrayFactory,
  344. finalizeCache,
  345. useEslintrc
  346. } = internalSlotsMap.get(this);
  347. let finalConfigArray = finalizeCache.get(configArray);
  348. if (!finalConfigArray) {
  349. finalConfigArray = configArray;
  350. // Load the personal config if there are no regular config files.
  351. if (
  352. useEslintrc &&
  353. configArray.every(c => !c.filePath) &&
  354. cliConfigArray.every(c => !c.filePath) // `--config` option can be a file.
  355. ) {
  356. debug("Loading the config file of the home directory.");
  357. finalConfigArray = configArrayFactory.loadInDirectory(
  358. os.homedir(),
  359. { name: "PersonalConfig", parent: finalConfigArray }
  360. );
  361. }
  362. // Apply CLI options.
  363. if (cliConfigArray.length > 0) {
  364. finalConfigArray = finalConfigArray.concat(cliConfigArray);
  365. }
  366. // Validate rule settings and environments.
  367. validateConfigArray(finalConfigArray);
  368. // Cache it.
  369. Object.freeze(finalConfigArray);
  370. finalizeCache.set(configArray, finalConfigArray);
  371. debug(
  372. "Configuration was determined: %o on %s",
  373. finalConfigArray,
  374. directoryPath
  375. );
  376. }
  377. // At least one element (the default ignore patterns) exists.
  378. if (!ignoreNotFoundError && useEslintrc && finalConfigArray.length <= 1) {
  379. throw new ConfigurationNotFoundError(directoryPath);
  380. }
  381. return finalConfigArray;
  382. }
  383. }
  384. //------------------------------------------------------------------------------
  385. // Public Interface
  386. //------------------------------------------------------------------------------
  387. module.exports = { CascadingConfigArrayFactory };