autoconfig.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**
  2. * @fileoverview Used for creating a suggested configuration based on project code.
  3. * @author Ian VanSchooten
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const equal = require("fast-deep-equal"),
  10. recConfig = require("../../conf/eslint-recommended"),
  11. ConfigOps = require("@eslint/eslintrc/lib/shared/config-ops"),
  12. { Linter } = require("../linter"),
  13. configRule = require("./config-rule");
  14. const debug = require("debug")("eslint:autoconfig");
  15. const linter = new Linter();
  16. //------------------------------------------------------------------------------
  17. // Data
  18. //------------------------------------------------------------------------------
  19. const MAX_CONFIG_COMBINATIONS = 17, // 16 combinations + 1 for severity only
  20. RECOMMENDED_CONFIG_NAME = "eslint:recommended";
  21. //------------------------------------------------------------------------------
  22. // Private
  23. //------------------------------------------------------------------------------
  24. /**
  25. * Information about a rule configuration, in the context of a Registry.
  26. * @typedef {Object} registryItem
  27. * @param {ruleConfig} config A valid configuration for the rule
  28. * @param {number} specificity The number of elements in the ruleConfig array
  29. * @param {number} errorCount The number of errors encountered when linting with the config
  30. */
  31. /**
  32. * This callback is used to measure execution status in a progress bar
  33. * @callback progressCallback
  34. * @param {number} The total number of times the callback will be called.
  35. */
  36. /**
  37. * Create registryItems for rules
  38. * @param {rulesConfig} rulesConfig Hash of rule names and arrays of ruleConfig items
  39. * @returns {Object} registryItems for each rule in provided rulesConfig
  40. */
  41. function makeRegistryItems(rulesConfig) {
  42. return Object.keys(rulesConfig).reduce((accumulator, ruleId) => {
  43. accumulator[ruleId] = rulesConfig[ruleId].map(config => ({
  44. config,
  45. specificity: config.length || 1,
  46. errorCount: void 0
  47. }));
  48. return accumulator;
  49. }, {});
  50. }
  51. /**
  52. * Creates an object in which to store rule configs and error counts
  53. *
  54. * Unless a rulesConfig is provided at construction, the registry will not contain
  55. * any rules, only methods. This will be useful for building up registries manually.
  56. *
  57. * Registry class
  58. */
  59. class Registry {
  60. // eslint-disable-next-line jsdoc/require-description
  61. /**
  62. * @param {rulesConfig} [rulesConfig] Hash of rule names and arrays of possible configurations
  63. */
  64. constructor(rulesConfig) {
  65. this.rules = (rulesConfig) ? makeRegistryItems(rulesConfig) : {};
  66. }
  67. /**
  68. * Populate the registry with core rule configs.
  69. *
  70. * It will set the registry's `rule` property to an object having rule names
  71. * as keys and an array of registryItems as values.
  72. * @returns {void}
  73. */
  74. populateFromCoreRules() {
  75. const rulesConfig = configRule.createCoreRuleConfigs(/* noDeprecated = */ true);
  76. this.rules = makeRegistryItems(rulesConfig);
  77. }
  78. /**
  79. * Creates sets of rule configurations which can be used for linting
  80. * and initializes registry errors to zero for those configurations (side effect).
  81. *
  82. * This combines as many rules together as possible, such that the first sets
  83. * in the array will have the highest number of rules configured, and later sets
  84. * will have fewer and fewer, as not all rules have the same number of possible
  85. * configurations.
  86. *
  87. * The length of the returned array will be <= MAX_CONFIG_COMBINATIONS.
  88. * @returns {Object[]} "rules" configurations to use for linting
  89. */
  90. buildRuleSets() {
  91. let idx = 0;
  92. const ruleIds = Object.keys(this.rules),
  93. ruleSets = [];
  94. /**
  95. * Add a rule configuration from the registry to the ruleSets
  96. *
  97. * This is broken out into its own function so that it doesn't need to be
  98. * created inside of the while loop.
  99. * @param {string} rule The ruleId to add.
  100. * @returns {void}
  101. */
  102. const addRuleToRuleSet = function(rule) {
  103. /*
  104. * This check ensures that there is a rule configuration and that
  105. * it has fewer than the max combinations allowed.
  106. * If it has too many configs, we will only use the most basic of
  107. * the possible configurations.
  108. */
  109. const hasFewCombos = (this.rules[rule].length <= MAX_CONFIG_COMBINATIONS);
  110. if (this.rules[rule][idx] && (hasFewCombos || this.rules[rule][idx].specificity <= 2)) {
  111. /*
  112. * If the rule has too many possible combinations, only take
  113. * simple ones, avoiding objects.
  114. */
  115. if (!hasFewCombos && typeof this.rules[rule][idx].config[1] === "object") {
  116. return;
  117. }
  118. ruleSets[idx] = ruleSets[idx] || {};
  119. ruleSets[idx][rule] = this.rules[rule][idx].config;
  120. /*
  121. * Initialize errorCount to zero, since this is a config which
  122. * will be linted.
  123. */
  124. this.rules[rule][idx].errorCount = 0;
  125. }
  126. }.bind(this);
  127. while (ruleSets.length === idx) {
  128. ruleIds.forEach(addRuleToRuleSet);
  129. idx += 1;
  130. }
  131. return ruleSets;
  132. }
  133. /**
  134. * Remove all items from the registry with a non-zero number of errors
  135. *
  136. * Note: this also removes rule configurations which were not linted
  137. * (meaning, they have an undefined errorCount).
  138. * @returns {void}
  139. */
  140. stripFailingConfigs() {
  141. const ruleIds = Object.keys(this.rules),
  142. newRegistry = new Registry();
  143. newRegistry.rules = Object.assign({}, this.rules);
  144. ruleIds.forEach(ruleId => {
  145. const errorFreeItems = newRegistry.rules[ruleId].filter(registryItem => (registryItem.errorCount === 0));
  146. if (errorFreeItems.length > 0) {
  147. newRegistry.rules[ruleId] = errorFreeItems;
  148. } else {
  149. delete newRegistry.rules[ruleId];
  150. }
  151. });
  152. return newRegistry;
  153. }
  154. /**
  155. * Removes rule configurations which were not included in a ruleSet
  156. * @returns {void}
  157. */
  158. stripExtraConfigs() {
  159. const ruleIds = Object.keys(this.rules),
  160. newRegistry = new Registry();
  161. newRegistry.rules = Object.assign({}, this.rules);
  162. ruleIds.forEach(ruleId => {
  163. newRegistry.rules[ruleId] = newRegistry.rules[ruleId].filter(registryItem => (typeof registryItem.errorCount !== "undefined"));
  164. });
  165. return newRegistry;
  166. }
  167. /**
  168. * Creates a registry of rules which had no error-free configs.
  169. * The new registry is intended to be analyzed to determine whether its rules
  170. * should be disabled or set to warning.
  171. * @returns {Registry} A registry of failing rules.
  172. */
  173. getFailingRulesRegistry() {
  174. const ruleIds = Object.keys(this.rules),
  175. failingRegistry = new Registry();
  176. ruleIds.forEach(ruleId => {
  177. const failingConfigs = this.rules[ruleId].filter(registryItem => (registryItem.errorCount > 0));
  178. if (failingConfigs && failingConfigs.length === this.rules[ruleId].length) {
  179. failingRegistry.rules[ruleId] = failingConfigs;
  180. }
  181. });
  182. return failingRegistry;
  183. }
  184. /**
  185. * Create an eslint config for any rules which only have one configuration
  186. * in the registry.
  187. * @returns {Object} An eslint config with rules section populated
  188. */
  189. createConfig() {
  190. const ruleIds = Object.keys(this.rules),
  191. config = { rules: {} };
  192. ruleIds.forEach(ruleId => {
  193. if (this.rules[ruleId].length === 1) {
  194. config.rules[ruleId] = this.rules[ruleId][0].config;
  195. }
  196. });
  197. return config;
  198. }
  199. /**
  200. * Return a cloned registry containing only configs with a desired specificity
  201. * @param {number} specificity Only keep configs with this specificity
  202. * @returns {Registry} A registry of rules
  203. */
  204. filterBySpecificity(specificity) {
  205. const ruleIds = Object.keys(this.rules),
  206. newRegistry = new Registry();
  207. newRegistry.rules = Object.assign({}, this.rules);
  208. ruleIds.forEach(ruleId => {
  209. newRegistry.rules[ruleId] = this.rules[ruleId].filter(registryItem => (registryItem.specificity === specificity));
  210. });
  211. return newRegistry;
  212. }
  213. /**
  214. * Lint SourceCodes against all configurations in the registry, and record results
  215. * @param {Object[]} sourceCodes SourceCode objects for each filename
  216. * @param {Object} config ESLint config object
  217. * @param {progressCallback} [cb] Optional callback for reporting execution status
  218. * @returns {Registry} New registry with errorCount populated
  219. */
  220. lintSourceCode(sourceCodes, config, cb) {
  221. let lintedRegistry = new Registry();
  222. lintedRegistry.rules = Object.assign({}, this.rules);
  223. const ruleSets = lintedRegistry.buildRuleSets();
  224. lintedRegistry = lintedRegistry.stripExtraConfigs();
  225. debug("Linting with all possible rule combinations");
  226. const filenames = Object.keys(sourceCodes);
  227. const totalFilesLinting = filenames.length * ruleSets.length;
  228. filenames.forEach(filename => {
  229. debug(`Linting file: ${filename}`);
  230. let ruleSetIdx = 0;
  231. ruleSets.forEach(ruleSet => {
  232. const lintConfig = Object.assign({}, config, { rules: ruleSet });
  233. const lintResults = linter.verify(sourceCodes[filename], lintConfig);
  234. lintResults.forEach(result => {
  235. /*
  236. * It is possible that the error is from a configuration comment
  237. * in a linted file, in which case there may not be a config
  238. * set in this ruleSetIdx.
  239. * (https://github.com/eslint/eslint/issues/5992)
  240. * (https://github.com/eslint/eslint/issues/7860)
  241. */
  242. if (
  243. lintedRegistry.rules[result.ruleId] &&
  244. lintedRegistry.rules[result.ruleId][ruleSetIdx]
  245. ) {
  246. lintedRegistry.rules[result.ruleId][ruleSetIdx].errorCount += 1;
  247. }
  248. });
  249. ruleSetIdx += 1;
  250. if (cb) {
  251. cb(totalFilesLinting); // eslint-disable-line node/callback-return
  252. }
  253. });
  254. // Deallocate for GC
  255. sourceCodes[filename] = null;
  256. });
  257. return lintedRegistry;
  258. }
  259. }
  260. /**
  261. * Extract rule configuration into eslint:recommended where possible.
  262. *
  263. * This will return a new config with `["extends": [ ..., "eslint:recommended"]` and
  264. * only the rules which have configurations different from the recommended config.
  265. * @param {Object} config config object
  266. * @returns {Object} config object using `"extends": ["eslint:recommended"]`
  267. */
  268. function extendFromRecommended(config) {
  269. const newConfig = Object.assign({}, config);
  270. ConfigOps.normalizeToStrings(newConfig);
  271. const recRules = Object.keys(recConfig.rules).filter(ruleId => ConfigOps.isErrorSeverity(recConfig.rules[ruleId]));
  272. recRules.forEach(ruleId => {
  273. if (equal(recConfig.rules[ruleId], newConfig.rules[ruleId])) {
  274. delete newConfig.rules[ruleId];
  275. }
  276. });
  277. newConfig.extends.unshift(RECOMMENDED_CONFIG_NAME);
  278. return newConfig;
  279. }
  280. //------------------------------------------------------------------------------
  281. // Public Interface
  282. //------------------------------------------------------------------------------
  283. module.exports = {
  284. Registry,
  285. extendFromRecommended
  286. };