Skip to content

Commit

Permalink
refactor: remove unnecessary code in flat-eslint.js (#17308)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Jun 25, 2023
1 parent 1866e1d commit 9718a97
Showing 1 changed file with 2 additions and 104 deletions.
106 changes: 2 additions & 104 deletions lib/eslint/flat-eslint.js
Expand Up @@ -132,33 +132,6 @@ function calculateStatsPerFile(messages) {
return stat;
}

/**
* It will calculate the error and warning count for collection of results from all files
* @param {LintResult[]} results Collection of messages from all the files
* @returns {Object} Contains the stats
* @private
*/
function calculateStatsPerRun(results) {
const stat = {
errorCount: 0,
fatalErrorCount: 0,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0
};

for (let i = 0; i < results.length; i++) {
const result = results[i];

stat.errorCount += result.errorCount;
stat.fatalErrorCount += result.fatalErrorCount;
stat.warningCount += result.warningCount;
stat.fixableErrorCount += result.fixableErrorCount;
stat.fixableWarningCount += result.fixableWarningCount;
}
return stat;
}

/**
* Create rulesMeta object.
* @param {Map<string,Rule>} rules a map of rules from which to generate the object.
Expand Down Expand Up @@ -558,43 +531,6 @@ function shouldMessageBeFixed(message, config, fixTypes) {
return Boolean(rule && rule.meta && fixTypes.has(rule.meta.type));
}

/**
* Collect used deprecated rules.
* @param {Array<FlatConfig>} configs The configs to evaluate.
* @returns {IterableIterator<DeprecatedRuleInfo>} Used deprecated rules.
*/
function *iterateRuleDeprecationWarnings(configs) {
const processedRuleIds = new Set();

for (const config of configs) {
for (const [ruleId, ruleConfig] of Object.entries(config.rules)) {

// Skip if it was processed.
if (processedRuleIds.has(ruleId)) {
continue;
}
processedRuleIds.add(ruleId);

// Skip if it's not used.
if (!getRuleSeverity(ruleConfig)) {
continue;
}
const rule = getRuleFromConfig(ruleId, config);

// Skip if it's not deprecated.
if (!(rule && rule.meta && rule.meta.deprecated)) {
continue;
}

// This rule was used and deprecated.
yield {
ruleId,
replacedBy: rule.meta.replacedBy || []
};
}
}
}

/**
* Creates an error to be thrown when an array of results passed to `getRulesMetaForResults` was not created by the current engine.
* @returns {TypeError} An error object.
Expand Down Expand Up @@ -819,7 +755,6 @@ class FlatESLint {
errorOnUnmatchedPattern
} = eslintOptions;
const startTime = Date.now();
const usedConfigs = [];
const fixTypesSet = fixTypes ? new Set(fixTypes) : null;

// Delete cache file; should this be done here?
Expand Down Expand Up @@ -877,15 +812,6 @@ class FlatESLint {
return void 0;
}

/*
* Store used configs for:
* - this method uses to collect used deprecated rules.
* - `--fix-type` option uses to get the loaded rule's meta data.
*/
if (!usedConfigs.includes(config)) {
usedConfigs.push(config);
}

// Skip if there is cached result.
if (lintResultCache) {
const cachedResult =
Expand Down Expand Up @@ -954,22 +880,10 @@ class FlatESLint {
lintResultCache.reconcile();
}

let usedDeprecatedRules;
const finalResults = results.filter(result => !!result);

return processLintReport(this, {
results: finalResults,
...calculateStatsPerRun(finalResults),

// Initialize it lazily because CLI and `ESLint` API don't use it.
get usedDeprecatedRules() {
if (!usedDeprecatedRules) {
usedDeprecatedRules = Array.from(
iterateRuleDeprecationWarnings(usedConfigs)
);
}
return usedDeprecatedRules;
}
results: finalResults
});
}

Expand Down Expand Up @@ -1031,7 +945,6 @@ class FlatESLint {
const results = [];
const startTime = Date.now();
const resolvedFilename = path.resolve(cwd, filePath || "__placeholder__.js");
let config;

// Clear the last used config arrays.
if (resolvedFilename && await this.isPathIgnored(resolvedFilename)) {
Expand All @@ -1040,9 +953,6 @@ class FlatESLint {
}
} else {

// TODO: Needed?
config = configs.getConfig(resolvedFilename);

// Do lint.
results.push(verifyText({
text: code,
Expand All @@ -1057,21 +967,9 @@ class FlatESLint {
}

debug(`Linting complete in: ${Date.now() - startTime}ms`);
let usedDeprecatedRules;

return processLintReport(this, {
results,
...calculateStatsPerRun(results),

// Initialize it lazily because CLI and `ESLint` API don't use it.
get usedDeprecatedRules() {
if (!usedDeprecatedRules) {
usedDeprecatedRules = Array.from(
iterateRuleDeprecationWarnings(config)
);
}
return usedDeprecatedRules;
}
results
});

}
Expand Down

0 comments on commit 9718a97

Please sign in to comment.