autoconfig.js 12 KB

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