Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

autoconfig.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. * @param {Object} registry The autoconfig registry
  91. * @returns {Object[]} "rules" configurations to use for linting
  92. */
  93. buildRuleSets() {
  94. let idx = 0;
  95. const ruleIds = Object.keys(this.rules),
  96. ruleSets = [];
  97. /**
  98. * Add a rule configuration from the registry to the ruleSets
  99. *
  100. * This is broken out into its own function so that it doesn't need to be
  101. * created inside of the while loop.
  102. *
  103. * @param {string} rule The ruleId to add.
  104. * @returns {void}
  105. */
  106. const addRuleToRuleSet = function(rule) {
  107. /*
  108. * This check ensures that there is a rule configuration and that
  109. * it has fewer than the max combinations allowed.
  110. * If it has too many configs, we will only use the most basic of
  111. * the possible configurations.
  112. */
  113. const hasFewCombos = (this.rules[rule].length <= MAX_CONFIG_COMBINATIONS);
  114. if (this.rules[rule][idx] && (hasFewCombos || this.rules[rule][idx].specificity <= 2)) {
  115. /*
  116. * If the rule has too many possible combinations, only take
  117. * simple ones, avoiding objects.
  118. */
  119. if (!hasFewCombos && typeof this.rules[rule][idx].config[1] === "object") {
  120. return;
  121. }
  122. ruleSets[idx] = ruleSets[idx] || {};
  123. ruleSets[idx][rule] = this.rules[rule][idx].config;
  124. /*
  125. * Initialize errorCount to zero, since this is a config which
  126. * will be linted.
  127. */
  128. this.rules[rule][idx].errorCount = 0;
  129. }
  130. }.bind(this);
  131. while (ruleSets.length === idx) {
  132. ruleIds.forEach(addRuleToRuleSet);
  133. idx += 1;
  134. }
  135. return ruleSets;
  136. }
  137. /**
  138. * Remove all items from the registry with a non-zero number of errors
  139. *
  140. * Note: this also removes rule configurations which were not linted
  141. * (meaning, they have an undefined errorCount).
  142. *
  143. * @returns {void}
  144. */
  145. stripFailingConfigs() {
  146. const ruleIds = Object.keys(this.rules),
  147. newRegistry = new Registry();
  148. newRegistry.rules = Object.assign({}, this.rules);
  149. ruleIds.forEach(ruleId => {
  150. const errorFreeItems = newRegistry.rules[ruleId].filter(registryItem => (registryItem.errorCount === 0));
  151. if (errorFreeItems.length > 0) {
  152. newRegistry.rules[ruleId] = errorFreeItems;
  153. } else {
  154. delete newRegistry.rules[ruleId];
  155. }
  156. });
  157. return newRegistry;
  158. }
  159. /**
  160. * Removes rule configurations which were not included in a ruleSet
  161. *
  162. * @returns {void}
  163. */
  164. stripExtraConfigs() {
  165. const ruleIds = Object.keys(this.rules),
  166. newRegistry = new Registry();
  167. newRegistry.rules = Object.assign({}, this.rules);
  168. ruleIds.forEach(ruleId => {
  169. newRegistry.rules[ruleId] = newRegistry.rules[ruleId].filter(registryItem => (typeof registryItem.errorCount !== "undefined"));
  170. });
  171. return newRegistry;
  172. }
  173. /**
  174. * Creates a registry of rules which had no error-free configs.
  175. * The new registry is intended to be analyzed to determine whether its rules
  176. * should be disabled or set to warning.
  177. *
  178. * @returns {Registry} A registry of failing rules.
  179. */
  180. getFailingRulesRegistry() {
  181. const ruleIds = Object.keys(this.rules),
  182. failingRegistry = new Registry();
  183. ruleIds.forEach(ruleId => {
  184. const failingConfigs = this.rules[ruleId].filter(registryItem => (registryItem.errorCount > 0));
  185. if (failingConfigs && failingConfigs.length === this.rules[ruleId].length) {
  186. failingRegistry.rules[ruleId] = failingConfigs;
  187. }
  188. });
  189. return failingRegistry;
  190. }
  191. /**
  192. * Create an eslint config for any rules which only have one configuration
  193. * in the registry.
  194. *
  195. * @returns {Object} An eslint config with rules section populated
  196. */
  197. createConfig() {
  198. const ruleIds = Object.keys(this.rules),
  199. config = { rules: {} };
  200. ruleIds.forEach(ruleId => {
  201. if (this.rules[ruleId].length === 1) {
  202. config.rules[ruleId] = this.rules[ruleId][0].config;
  203. }
  204. });
  205. return config;
  206. }
  207. /**
  208. * Return a cloned registry containing only configs with a desired specificity
  209. *
  210. * @param {number} specificity Only keep configs with this specificity
  211. * @returns {Registry} A registry of rules
  212. */
  213. filterBySpecificity(specificity) {
  214. const ruleIds = Object.keys(this.rules),
  215. newRegistry = new Registry();
  216. newRegistry.rules = Object.assign({}, this.rules);
  217. ruleIds.forEach(ruleId => {
  218. newRegistry.rules[ruleId] = this.rules[ruleId].filter(registryItem => (registryItem.specificity === specificity));
  219. });
  220. return newRegistry;
  221. }
  222. /**
  223. * Lint SourceCodes against all configurations in the registry, and record results
  224. *
  225. * @param {Object[]} sourceCodes SourceCode objects for each filename
  226. * @param {Object} config ESLint config object
  227. * @param {progressCallback} [cb] Optional callback for reporting execution status
  228. * @returns {Registry} New registry with errorCount populated
  229. */
  230. lintSourceCode(sourceCodes, config, cb) {
  231. let lintedRegistry = new Registry();
  232. lintedRegistry.rules = Object.assign({}, this.rules);
  233. const ruleSets = lintedRegistry.buildRuleSets();
  234. lintedRegistry = lintedRegistry.stripExtraConfigs();
  235. debug("Linting with all possible rule combinations");
  236. const filenames = Object.keys(sourceCodes);
  237. const totalFilesLinting = filenames.length * ruleSets.length;
  238. filenames.forEach(filename => {
  239. debug(`Linting file: ${filename}`);
  240. let ruleSetIdx = 0;
  241. ruleSets.forEach(ruleSet => {
  242. const lintConfig = Object.assign({}, config, { rules: ruleSet });
  243. const lintResults = linter.verify(sourceCodes[filename], lintConfig);
  244. lintResults.forEach(result => {
  245. /*
  246. * It is possible that the error is from a configuration comment
  247. * in a linted file, in which case there may not be a config
  248. * set in this ruleSetIdx.
  249. * (https://github.com/eslint/eslint/issues/5992)
  250. * (https://github.com/eslint/eslint/issues/7860)
  251. */
  252. if (
  253. lintedRegistry.rules[result.ruleId] &&
  254. lintedRegistry.rules[result.ruleId][ruleSetIdx]
  255. ) {
  256. lintedRegistry.rules[result.ruleId][ruleSetIdx].errorCount += 1;
  257. }
  258. });
  259. ruleSetIdx += 1;
  260. if (cb) {
  261. cb(totalFilesLinting); // eslint-disable-line callback-return
  262. }
  263. });
  264. // Deallocate for GC
  265. sourceCodes[filename] = null;
  266. });
  267. return lintedRegistry;
  268. }
  269. }
  270. /**
  271. * Extract rule configuration into eslint:recommended where possible.
  272. *
  273. * This will return a new config with `"extends": "eslint:recommended"` and
  274. * only the rules which have configurations different from the recommended config.
  275. *
  276. * @param {Object} config config object
  277. * @returns {Object} config object using `"extends": "eslint:recommended"`
  278. */
  279. function extendFromRecommended(config) {
  280. const newConfig = Object.assign({}, config);
  281. ConfigOps.normalizeToStrings(newConfig);
  282. const recRules = Object.keys(recConfig.rules).filter(ruleId => ConfigOps.isErrorSeverity(recConfig.rules[ruleId]));
  283. recRules.forEach(ruleId => {
  284. if (lodash.isEqual(recConfig.rules[ruleId], newConfig.rules[ruleId])) {
  285. delete newConfig.rules[ruleId];
  286. }
  287. });
  288. newConfig.extends = RECOMMENDED_CONFIG_NAME;
  289. return newConfig;
  290. }
  291. //------------------------------------------------------------------------------
  292. // Public Interface
  293. //------------------------------------------------------------------------------
  294. module.exports = {
  295. Registry,
  296. extendFromRecommended
  297. };