flat-config-array.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * @fileoverview Flat Config Array
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //-----------------------------------------------------------------------------
  7. // Requirements
  8. //-----------------------------------------------------------------------------
  9. const { ConfigArray, ConfigArraySymbol } = require("@eslint/config-array");
  10. const { flatConfigSchema } = require("./flat-config-schema");
  11. const { defaultConfig } = require("./default-config");
  12. const { Config } = require("./config");
  13. //-----------------------------------------------------------------------------
  14. // Helpers
  15. //-----------------------------------------------------------------------------
  16. /**
  17. * Fields that are considered metadata and not part of the config object.
  18. */
  19. const META_FIELDS = new Set(["name"]);
  20. /**
  21. * Wraps a config error with details about where the error occurred.
  22. * @param {Error} error The original error.
  23. * @param {number} originalLength The original length of the config array.
  24. * @param {number} baseLength The length of the base config.
  25. * @returns {TypeError} The new error with details.
  26. */
  27. function wrapConfigErrorWithDetails(error, originalLength, baseLength) {
  28. let location = "user-defined";
  29. let configIndex = error.index;
  30. /*
  31. * A config array is set up in this order:
  32. * 1. Base config
  33. * 2. Original configs
  34. * 3. User-defined configs
  35. * 4. CLI-defined configs
  36. *
  37. * So we need to adjust the index to account for the base config.
  38. *
  39. * - If the index is less than the base length, it's in the base config
  40. * (as specified by `baseConfig` argument to `FlatConfigArray` constructor).
  41. * - If the index is greater than the base length but less than the original
  42. * length + base length, it's in the original config. The original config
  43. * is passed to the `FlatConfigArray` constructor as the first argument.
  44. * - Otherwise, it's in the user-defined config, which is loaded from the
  45. * config file and merged with any command-line options.
  46. */
  47. if (error.index < baseLength) {
  48. location = "base";
  49. } else if (error.index < originalLength + baseLength) {
  50. location = "original";
  51. configIndex = error.index - baseLength;
  52. } else {
  53. configIndex = error.index - originalLength - baseLength;
  54. }
  55. return new TypeError(
  56. `${error.message.slice(0, -1)} at ${location} index ${configIndex}.`,
  57. { cause: error }
  58. );
  59. }
  60. const originalBaseConfig = Symbol("originalBaseConfig");
  61. const originalLength = Symbol("originalLength");
  62. const baseLength = Symbol("baseLength");
  63. //-----------------------------------------------------------------------------
  64. // Exports
  65. //-----------------------------------------------------------------------------
  66. /**
  67. * Represents an array containing configuration information for ESLint.
  68. */
  69. class FlatConfigArray extends ConfigArray {
  70. /**
  71. * Creates a new instance.
  72. * @param {*[]} configs An array of configuration information.
  73. * @param {{basePath: string, shouldIgnore: boolean, baseConfig: FlatConfig}} options The options
  74. * to use for the config array instance.
  75. */
  76. constructor(configs, {
  77. basePath,
  78. shouldIgnore = true,
  79. baseConfig = defaultConfig
  80. } = {}) {
  81. super(configs, {
  82. basePath,
  83. schema: flatConfigSchema
  84. });
  85. /**
  86. * The original length of the array before any modifications.
  87. * @type {number}
  88. */
  89. this[originalLength] = this.length;
  90. if (baseConfig[Symbol.iterator]) {
  91. this.unshift(...baseConfig);
  92. } else {
  93. this.unshift(baseConfig);
  94. }
  95. /**
  96. * The length of the array after applying the base config.
  97. * @type {number}
  98. */
  99. this[baseLength] = this.length - this[originalLength];
  100. /**
  101. * The base config used to build the config array.
  102. * @type {Array<FlatConfig>}
  103. */
  104. this[originalBaseConfig] = baseConfig;
  105. Object.defineProperty(this, originalBaseConfig, { writable: false });
  106. /**
  107. * Determines if `ignores` fields should be honored.
  108. * If true, then all `ignores` fields are honored.
  109. * if false, then only `ignores` fields in the baseConfig are honored.
  110. * @type {boolean}
  111. */
  112. this.shouldIgnore = shouldIgnore;
  113. Object.defineProperty(this, "shouldIgnore", { writable: false });
  114. }
  115. /**
  116. * Normalizes the array by calling the superclass method and catching/rethrowing
  117. * any ConfigError exceptions with additional details.
  118. * @param {any} [context] The context to use to normalize the array.
  119. * @returns {Promise<FlatConfigArray>} A promise that resolves when the array is normalized.
  120. */
  121. normalize(context) {
  122. return super.normalize(context)
  123. .catch(error => {
  124. if (error.name === "ConfigError") {
  125. throw wrapConfigErrorWithDetails(error, this[originalLength], this[baseLength]);
  126. }
  127. throw error;
  128. });
  129. }
  130. /**
  131. * Normalizes the array by calling the superclass method and catching/rethrowing
  132. * any ConfigError exceptions with additional details.
  133. * @param {any} [context] The context to use to normalize the array.
  134. * @returns {FlatConfigArray} The current instance.
  135. * @throws {TypeError} If the config is invalid.
  136. */
  137. normalizeSync(context) {
  138. try {
  139. return super.normalizeSync(context);
  140. } catch (error) {
  141. if (error.name === "ConfigError") {
  142. throw wrapConfigErrorWithDetails(error, this[originalLength], this[baseLength]);
  143. }
  144. throw error;
  145. }
  146. }
  147. /* eslint-disable class-methods-use-this -- Desired as instance method */
  148. /**
  149. * Replaces a config with another config to allow us to put strings
  150. * in the config array that will be replaced by objects before
  151. * normalization.
  152. * @param {Object} config The config to preprocess.
  153. * @returns {Object} The preprocessed config.
  154. */
  155. [ConfigArraySymbol.preprocessConfig](config) {
  156. /*
  157. * If a config object has `ignores` and no other non-meta fields, then it's an object
  158. * for global ignores. If `shouldIgnore` is false, that object shouldn't apply,
  159. * so we'll remove its `ignores`.
  160. */
  161. if (
  162. !this.shouldIgnore &&
  163. !this[originalBaseConfig].includes(config) &&
  164. config.ignores &&
  165. Object.keys(config).filter(key => !META_FIELDS.has(key)).length === 1
  166. ) {
  167. /* eslint-disable-next-line no-unused-vars -- need to strip off other keys */
  168. const { ignores, ...otherKeys } = config;
  169. return otherKeys;
  170. }
  171. return config;
  172. }
  173. /**
  174. * Finalizes the config by replacing plugin references with their objects
  175. * and validating rule option schemas.
  176. * @param {Object} config The config to finalize.
  177. * @returns {Object} The finalized config.
  178. * @throws {TypeError} If the config is invalid.
  179. */
  180. [ConfigArraySymbol.finalizeConfig](config) {
  181. return new Config(config);
  182. }
  183. /* eslint-enable class-methods-use-this -- Desired as instance method */
  184. }
  185. exports.FlatConfigArray = FlatConfigArray;