config.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. * @fileoverview The `Config` class
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //-----------------------------------------------------------------------------
  7. // Requirements
  8. //-----------------------------------------------------------------------------
  9. const { RuleValidator } = require("./rule-validator");
  10. const { flatConfigSchema, hasMethod } = require("./flat-config-schema");
  11. const { ObjectSchema } = require("@eslint/config-array");
  12. //-----------------------------------------------------------------------------
  13. // Helpers
  14. //-----------------------------------------------------------------------------
  15. const ruleValidator = new RuleValidator();
  16. const severities = new Map([
  17. [0, 0],
  18. [1, 1],
  19. [2, 2],
  20. ["off", 0],
  21. ["warn", 1],
  22. ["error", 2]
  23. ]);
  24. /**
  25. * Splits a plugin identifier in the form a/b/c into two parts: a/b and c.
  26. * @param {string} identifier The identifier to parse.
  27. * @returns {{objectName: string, pluginName: string}} The parts of the plugin
  28. * name.
  29. */
  30. function splitPluginIdentifier(identifier) {
  31. const parts = identifier.split("/");
  32. return {
  33. objectName: parts.pop(),
  34. pluginName: parts.join("/")
  35. };
  36. }
  37. /**
  38. * Returns the name of an object in the config by reading its `meta` key.
  39. * @param {Object} object The object to check.
  40. * @returns {string?} The name of the object if found or `null` if there
  41. * is no name.
  42. */
  43. function getObjectId(object) {
  44. // first check old-style name
  45. let name = object.name;
  46. if (!name) {
  47. if (!object.meta) {
  48. return null;
  49. }
  50. name = object.meta.name;
  51. if (!name) {
  52. return null;
  53. }
  54. }
  55. // now check for old-style version
  56. let version = object.version;
  57. if (!version) {
  58. version = object.meta && object.meta.version;
  59. }
  60. // if there's a version then append that
  61. if (version) {
  62. return `${name}@${version}`;
  63. }
  64. return name;
  65. }
  66. /**
  67. * Converts a languageOptions object to a JSON representation.
  68. * @param {Record<string, any>} languageOptions The options to create a JSON
  69. * representation of.
  70. * @param {string} objectKey The key of the object being converted.
  71. * @returns {Record<string, any>} The JSON representation of the languageOptions.
  72. * @throws {TypeError} If a function is found in the languageOptions.
  73. */
  74. function languageOptionsToJSON(languageOptions, objectKey = "languageOptions") {
  75. const result = {};
  76. for (const [key, value] of Object.entries(languageOptions)) {
  77. if (value) {
  78. if (typeof value === "object") {
  79. const name = getObjectId(value);
  80. if (name && hasMethod(value)) {
  81. result[key] = name;
  82. } else {
  83. result[key] = languageOptionsToJSON(value, key);
  84. }
  85. continue;
  86. }
  87. if (typeof value === "function") {
  88. throw new TypeError(`Cannot serialize key "${key}" in ${objectKey}: Function values are not supported.`);
  89. }
  90. }
  91. result[key] = value;
  92. }
  93. return result;
  94. }
  95. /**
  96. * Normalizes the rules configuration. Ensure that each rule config is
  97. * an array and that the severity is a number. This function modifies the
  98. * rulesConfig.
  99. * @param {Record<string, any>} rulesConfig The rules configuration to normalize.
  100. * @returns {void}
  101. */
  102. function normalizeRulesConfig(rulesConfig) {
  103. for (const [ruleId, ruleConfig] of Object.entries(rulesConfig)) {
  104. // ensure rule config is an array
  105. if (!Array.isArray(ruleConfig)) {
  106. rulesConfig[ruleId] = [ruleConfig];
  107. }
  108. // normalize severity
  109. rulesConfig[ruleId][0] = severities.get(rulesConfig[ruleId][0]);
  110. }
  111. }
  112. //-----------------------------------------------------------------------------
  113. // Exports
  114. //-----------------------------------------------------------------------------
  115. /**
  116. * Represents a normalized configuration object.
  117. */
  118. class Config {
  119. /**
  120. * The name to use for the language when serializing to JSON.
  121. * @type {string|undefined}
  122. */
  123. #languageName;
  124. /**
  125. * The name to use for the processor when serializing to JSON.
  126. * @type {string|undefined}
  127. */
  128. #processorName;
  129. /**
  130. * Creates a new instance.
  131. * @param {Object} config The configuration object.
  132. */
  133. constructor(config) {
  134. const { plugins, language, languageOptions, processor, ...otherKeys } = config;
  135. // Validate config object
  136. const schema = new ObjectSchema(flatConfigSchema);
  137. schema.validate(config);
  138. // first, copy all the other keys over
  139. Object.assign(this, otherKeys);
  140. // ensure that a language is specified
  141. if (!language) {
  142. throw new TypeError("Key 'language' is required.");
  143. }
  144. // copy the rest over
  145. this.plugins = plugins;
  146. this.language = language;
  147. // Check language value
  148. const { pluginName: languagePluginName, objectName: localLanguageName } = splitPluginIdentifier(language);
  149. this.#languageName = language;
  150. if (!plugins || !plugins[languagePluginName] || !plugins[languagePluginName].languages || !plugins[languagePluginName].languages[localLanguageName]) {
  151. throw new TypeError(`Key "language": Could not find "${localLanguageName}" in plugin "${languagePluginName}".`);
  152. }
  153. this.language = plugins[languagePluginName].languages[localLanguageName];
  154. if (this.language.defaultLanguageOptions ?? languageOptions) {
  155. this.languageOptions = flatConfigSchema.languageOptions.merge(
  156. this.language.defaultLanguageOptions,
  157. languageOptions
  158. );
  159. } else {
  160. this.languageOptions = {};
  161. }
  162. // Validate language options
  163. try {
  164. this.language.validateLanguageOptions(this.languageOptions);
  165. } catch (error) {
  166. throw new TypeError(`Key "languageOptions": ${error.message}`, { cause: error });
  167. }
  168. // Check processor value
  169. if (processor) {
  170. this.processor = processor;
  171. if (typeof processor === "string") {
  172. const { pluginName, objectName: localProcessorName } = splitPluginIdentifier(processor);
  173. this.#processorName = processor;
  174. if (!plugins || !plugins[pluginName] || !plugins[pluginName].processors || !plugins[pluginName].processors[localProcessorName]) {
  175. throw new TypeError(`Key "processor": Could not find "${localProcessorName}" in plugin "${pluginName}".`);
  176. }
  177. this.processor = plugins[pluginName].processors[localProcessorName];
  178. } else if (typeof processor === "object") {
  179. this.#processorName = getObjectId(processor);
  180. this.processor = processor;
  181. } else {
  182. throw new TypeError("Key 'processor' must be a string or an object.");
  183. }
  184. }
  185. // Process the rules
  186. if (this.rules) {
  187. normalizeRulesConfig(this.rules);
  188. ruleValidator.validate(this);
  189. }
  190. }
  191. /**
  192. * Converts the configuration to a JSON representation.
  193. * @returns {Record<string, any>} The JSON representation of the configuration.
  194. * @throws {Error} If the configuration cannot be serialized.
  195. */
  196. toJSON() {
  197. if (this.processor && !this.#processorName) {
  198. throw new Error("Could not serialize processor object (missing 'meta' object).");
  199. }
  200. if (!this.#languageName) {
  201. throw new Error("Could not serialize language object (missing 'meta' object).");
  202. }
  203. return {
  204. ...this,
  205. plugins: Object.entries(this.plugins).map(([namespace, plugin]) => {
  206. const pluginId = getObjectId(plugin);
  207. if (!pluginId) {
  208. return namespace;
  209. }
  210. return `${namespace}:${pluginId}`;
  211. }),
  212. language: this.#languageName,
  213. languageOptions: languageOptionsToJSON(this.languageOptions),
  214. processor: this.#processorName
  215. };
  216. }
  217. }
  218. module.exports = { Config };