Skip to content

Commit

Permalink
Revert "replace report.usedDeprecatedRules by report.results[].usedDe…
Browse files Browse the repository at this point in the history
…precatedRules"

This reverts commit f3cc32f.
  • Loading branch information
mysticatea committed Feb 22, 2020
1 parent f3cc32f commit 1d8beae
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 330 deletions.
142 changes: 65 additions & 77 deletions lib/cli-engine/cli-engine.js
Expand Up @@ -26,7 +26,7 @@ const ModuleResolver = require("../shared/relative-module-resolver");
const { Linter } = require("../linter");
const builtInRules = require("../rules");
const { CascadingConfigArrayFactory } = require("./cascading-config-array-factory");
const { IgnorePattern } = require("./config-array");
const { IgnorePattern, getUsedExtractedConfigs } = require("./config-array");
const { FileEnumerator } = require("./file-enumerator");
const hash = require("./hash");
const LintResultCache = require("./lint-result-cache");
Expand Down Expand Up @@ -76,13 +76,6 @@ const validFixTypes = new Set(["problem", "suggestion", "layout"]);
* @property {string} resolvePluginsRelativeTo The folder where plugins should be resolved from, defaulting to the CWD
*/

/**
* Information of deprecated rules.
* @typedef {Object} DeprecatedRuleInfo
* @property {string} ruleId The rule ID.
* @property {string[]} replacedBy The rule IDs that replace this deprecated rule.
*/

/**
* A linting result.
* @typedef {Object} LintResult
Expand All @@ -94,7 +87,13 @@ const validFixTypes = new Set(["problem", "suggestion", "layout"]);
* @property {number} fixableWarningCount Number of fixable warnings for the result.
* @property {string} [source] The source code of the file that was linted.
* @property {string} [output] The source code of the file that was linted, with as many fixes applied as possible.
* @property {DeprecatedRuleInfo[]} usedDeprecatedRules The list of used deprecated rules.
*/

/**
* Information of deprecated rules.
* @typedef {Object} DeprecatedRuleInfo
* @property {string} ruleId The rule ID.
* @property {string[]} replacedBy The rule IDs that replace this deprecated rule.
*/

/**
Expand All @@ -105,6 +104,7 @@ const validFixTypes = new Set(["problem", "suggestion", "layout"]);
* @property {number} warningCount Number of warnings for the result.
* @property {number} fixableErrorCount Number of fixable errors for the result.
* @property {number} fixableWarningCount Number of fixable warnings for the result.
* @property {DeprecatedRuleInfo[]} usedDeprecatedRules The list of used deprecated rules.
*/

/**
Expand Down Expand Up @@ -192,59 +192,6 @@ function calculateStatsPerRun(results) {
});
}

/**
* Collect used deprecated rules.
* @param {ExtractedConfig} config The config which were used.
* @param {Map<string, Rule>} pluginRules The rule definitions of loaded plugins.
* @returns {DeprecatedRuleInfo[]} Used deprecated rules.
*/
function getRuleDeprecationWarnings(config, pluginRules) {
const retv = [];

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

// Skip if it's not used.
if (!ConfigOps.getRuleSeverity(ruleConfig)) {
continue;
}
const rule = pluginRules.get(ruleId) || builtInRules.get(ruleId);

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

// This rule was used and deprecated.
retv.push({
ruleId,
replacedBy: rule.meta.replacedBy || []
});
}

return retv;
}

/**
* Merge used deprecated rules from all lint results.
* @param {LintResult[]} results The lint results.
* @returns {DeprecatedRuleInfo[]} Used deprecated rules.
*/
function mergeRuleDeprecationWarnings(results) {
const retv = [];
const ids = new Set();

for (const { usedDeprecatedRules } of results) {
for (const usedDeprecatedRule of usedDeprecatedRules) {
if (!ids.has(usedDeprecatedRule.ruleId)) {
ids.add(usedDeprecatedRule.ruleId);
retv.push(usedDeprecatedRule);
}
}
}

return retv;
}

/**
* Processes an source code using ESLint.
* @param {Object} config The config object.
Expand Down Expand Up @@ -318,11 +265,6 @@ function verifyText({
result.source = text;
}

result.usedDeprecatedRules = getRuleDeprecationWarnings(
config.extractConfig(filePathToVerify),
config.pluginRules
);

return result;
}

Expand Down Expand Up @@ -382,6 +324,50 @@ function getRule(ruleId, configArrays) {
return builtInRules.get(ruleId) || null;
}

/**
* Collect used deprecated rules.
* @param {ConfigArray[]} usedConfigArrays The config arrays which were used.
* @returns {IterableIterator<DeprecatedRuleInfo>} Used deprecated rules.
*/
function *iterateRuleDeprecationWarnings(usedConfigArrays) {
const processedRuleIds = new Set();

// Flatten used configs.
/** @type {ExtractedConfig[]} */
const configs = [].concat(
...usedConfigArrays.map(getUsedExtractedConfigs)
);

// Traverse rule configs.
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 (!ConfigOps.getRuleSeverity(ruleConfig)) {
continue;
}
const rule = getRule(ruleId, usedConfigArrays);

// 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 || []
};
}
}
}

/**
* Checks if the given message is an error message.
* @param {LintMessage} message The message to check.
Expand Down Expand Up @@ -845,15 +831,16 @@ class CLIEngine {
lintResultCache.reconcile();
}

// Collect used deprecated rules.
const usedDeprecatedRules = Array.from(
iterateRuleDeprecationWarnings(lastConfigArrays)
);

debug(`Linting complete in: ${Date.now() - startTime}ms`);
return {
results,
...calculateStatsPerRun(results),

get usedDeprecatedRules() {
emitDeprecationWarning("executeOnFiles()", "ESLINT_LEGACY_USED_DEPRECATED_RULES");
return mergeRuleDeprecationWarnings(results);
}
usedDeprecatedRules
};
}

Expand Down Expand Up @@ -915,15 +902,16 @@ class CLIEngine {
}));
}

// Collect used deprecated rules.
const usedDeprecatedRules = Array.from(
iterateRuleDeprecationWarnings(lastConfigArrays)
);

debug(`Linting complete in: ${Date.now() - startTime}ms`);
return {
results,
...calculateStatsPerRun(results),

get usedDeprecatedRules() {
emitDeprecationWarning("executeOnText()", "ESLINT_LEGACY_USED_DEPRECATED_RULES");
return mergeRuleDeprecationWarnings(results);
}
usedDeprecatedRules
};
}

Expand Down
8 changes: 1 addition & 7 deletions lib/shared/deprecation-warnings.js
Expand Up @@ -37,13 +37,7 @@ const deprecationWarningMessages = {
"ESLint may use plugins that have the same name but different " +
"implementations in each target file. This method will be confused in " +
"such a case. " +
"Please use 'metadata.getRuleMeta(ruleId, filePath)' method instead.",
ESLINT_LEGACY_USED_DEPRECATED_RULES:
"'report.usedDeprecatedRules' property in lint report has been deprecated. " +
"ESLint may use plugins that have the same name but different " +
"implementations in each target file. This method will be confused in " +
"such a case. " +
"Please use the 'usedDeprecatedRules' property of each result instead."
"Please use 'metadata.getRuleMeta(ruleId, filePath)' method instead."
};

/**
Expand Down
3 changes: 1 addition & 2 deletions tests/bin/eslint.js
Expand Up @@ -90,8 +90,7 @@ describe("bin/eslint.js", () => {
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
output: "var foo = bar;\n",
usedDeprecatedRules: []
output: "var foo = bar;\n"
}
]);

Expand Down

0 comments on commit 1d8beae

Please sign in to comment.