cascading-config-array-factory.js 17 KB

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