123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- "use strict";
- const equal = require("fast-deep-equal"),
- recConfig = require("../../conf/eslint-recommended"),
- ConfigOps = require("@eslint/eslintrc/lib/shared/config-ops"),
- { Linter } = require("../linter"),
- configRule = require("./config-rule");
- const debug = require("debug")("eslint:autoconfig");
- const linter = new Linter();
- const MAX_CONFIG_COMBINATIONS = 17,
- RECOMMENDED_CONFIG_NAME = "eslint:recommended";
- function makeRegistryItems(rulesConfig) {
- return Object.keys(rulesConfig).reduce((accumulator, ruleId) => {
- accumulator[ruleId] = rulesConfig[ruleId].map(config => ({
- config,
- specificity: config.length || 1,
- errorCount: void 0
- }));
- return accumulator;
- }, {});
- }
- class Registry {
-
-
- constructor(rulesConfig) {
- this.rules = (rulesConfig) ? makeRegistryItems(rulesConfig) : {};
- }
-
- populateFromCoreRules() {
- const rulesConfig = configRule.createCoreRuleConfigs( true);
- this.rules = makeRegistryItems(rulesConfig);
- }
-
- buildRuleSets() {
- let idx = 0;
- const ruleIds = Object.keys(this.rules),
- ruleSets = [];
-
- const addRuleToRuleSet = function(rule) {
-
- const hasFewCombos = (this.rules[rule].length <= MAX_CONFIG_COMBINATIONS);
- if (this.rules[rule][idx] && (hasFewCombos || this.rules[rule][idx].specificity <= 2)) {
-
- if (!hasFewCombos && typeof this.rules[rule][idx].config[1] === "object") {
- return;
- }
- ruleSets[idx] = ruleSets[idx] || {};
- ruleSets[idx][rule] = this.rules[rule][idx].config;
-
- this.rules[rule][idx].errorCount = 0;
- }
- }.bind(this);
- while (ruleSets.length === idx) {
- ruleIds.forEach(addRuleToRuleSet);
- idx += 1;
- }
- return ruleSets;
- }
-
- stripFailingConfigs() {
- const ruleIds = Object.keys(this.rules),
- newRegistry = new Registry();
- newRegistry.rules = Object.assign({}, this.rules);
- ruleIds.forEach(ruleId => {
- const errorFreeItems = newRegistry.rules[ruleId].filter(registryItem => (registryItem.errorCount === 0));
- if (errorFreeItems.length > 0) {
- newRegistry.rules[ruleId] = errorFreeItems;
- } else {
- delete newRegistry.rules[ruleId];
- }
- });
- return newRegistry;
- }
-
- stripExtraConfigs() {
- const ruleIds = Object.keys(this.rules),
- newRegistry = new Registry();
- newRegistry.rules = Object.assign({}, this.rules);
- ruleIds.forEach(ruleId => {
- newRegistry.rules[ruleId] = newRegistry.rules[ruleId].filter(registryItem => (typeof registryItem.errorCount !== "undefined"));
- });
- return newRegistry;
- }
-
- getFailingRulesRegistry() {
- const ruleIds = Object.keys(this.rules),
- failingRegistry = new Registry();
- ruleIds.forEach(ruleId => {
- const failingConfigs = this.rules[ruleId].filter(registryItem => (registryItem.errorCount > 0));
- if (failingConfigs && failingConfigs.length === this.rules[ruleId].length) {
- failingRegistry.rules[ruleId] = failingConfigs;
- }
- });
- return failingRegistry;
- }
-
- createConfig() {
- const ruleIds = Object.keys(this.rules),
- config = { rules: {} };
- ruleIds.forEach(ruleId => {
- if (this.rules[ruleId].length === 1) {
- config.rules[ruleId] = this.rules[ruleId][0].config;
- }
- });
- return config;
- }
-
- filterBySpecificity(specificity) {
- const ruleIds = Object.keys(this.rules),
- newRegistry = new Registry();
- newRegistry.rules = Object.assign({}, this.rules);
- ruleIds.forEach(ruleId => {
- newRegistry.rules[ruleId] = this.rules[ruleId].filter(registryItem => (registryItem.specificity === specificity));
- });
- return newRegistry;
- }
-
- lintSourceCode(sourceCodes, config, cb) {
- let lintedRegistry = new Registry();
- lintedRegistry.rules = Object.assign({}, this.rules);
- const ruleSets = lintedRegistry.buildRuleSets();
- lintedRegistry = lintedRegistry.stripExtraConfigs();
- debug("Linting with all possible rule combinations");
- const filenames = Object.keys(sourceCodes);
- const totalFilesLinting = filenames.length * ruleSets.length;
- filenames.forEach(filename => {
- debug(`Linting file: ${filename}`);
- let ruleSetIdx = 0;
- ruleSets.forEach(ruleSet => {
- const lintConfig = Object.assign({}, config, { rules: ruleSet });
- const lintResults = linter.verify(sourceCodes[filename], lintConfig);
- lintResults.forEach(result => {
-
- if (
- lintedRegistry.rules[result.ruleId] &&
- lintedRegistry.rules[result.ruleId][ruleSetIdx]
- ) {
- lintedRegistry.rules[result.ruleId][ruleSetIdx].errorCount += 1;
- }
- });
- ruleSetIdx += 1;
- if (cb) {
- cb(totalFilesLinting);
- }
- });
-
- sourceCodes[filename] = null;
- });
- return lintedRegistry;
- }
- }
- function extendFromRecommended(config) {
- const newConfig = Object.assign({}, config);
- ConfigOps.normalizeToStrings(newConfig);
- const recRules = Object.keys(recConfig.rules).filter(ruleId => ConfigOps.isErrorSeverity(recConfig.rules[ruleId]));
- recRules.forEach(ruleId => {
- if (equal(recConfig.rules[ruleId], newConfig.rules[ruleId])) {
- delete newConfig.rules[ruleId];
- }
- });
- newConfig.extends.unshift(RECOMMENDED_CONFIG_NAME);
- return newConfig;
- }
- module.exports = {
- Registry,
- extendFromRecommended
- };
|