Skip to content

Commit

Permalink
Fix: add deprecation warning to Node API output (eslint#7443)
Browse files Browse the repository at this point in the history
  • Loading branch information
calling committed Oct 10, 2018
1 parent 95c4cb1 commit 02d3dc3
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 66 deletions.
29 changes: 28 additions & 1 deletion lib/cli-engine.js
Expand Up @@ -272,6 +272,30 @@ function createIgnoreResult(filePath, baseDir) {
};
}

/**
* Produces rule warnings (i.e. deprecation) from configured rules
* @param {Object} rules - Rules configured
* @param {Map} loadedRules - Map of loaded rules
* @returns {Object} Contains rule warnings
* @private
*/
function createRuleWarnings(rules, loadedRules) {
const ruleWarnings = { usedDeprecatedRules: [] };

if (!rules) {
return ruleWarnings;
}

Object.keys(rules).forEach(name => {
const loadedRule = loadedRules.get(name);

if (loadedRule.meta && loadedRule.meta.deprecated) {
ruleWarnings.usedDeprecatedRules.push({ ruleId: name, replacedBy: loadedRule.meta.docs.replacedBy });
}
});

return ruleWarnings;
}

/**
* Checks if the given message is an error message.
Expand Down Expand Up @@ -555,14 +579,17 @@ class CLIEngine {

const stats = calculateStatsPerRun(results);

const ruleWarnings = createRuleWarnings(this.options.rules, this.getRules());

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

return {
results,
errorCount: stats.errorCount,
warningCount: stats.warningCount,
fixableErrorCount: stats.fixableErrorCount,
fixableWarningCount: stats.fixableWarningCount
fixableWarningCount: stats.fixableWarningCount,
usedDeprecatedRules: ruleWarnings.usedDeprecatedRules
};
}

Expand Down
155 changes: 90 additions & 65 deletions tests/lib/cli-engine.js
Expand Up @@ -1375,6 +1375,33 @@ describe("CLIEngine", () => {
assert.strictEqual(report.results[0].messages.length, 0);
});

it("should warn when deprecated rules are configured", () => {
engine = new CLIEngine({
cwd: originalDir,
configFile: ".eslintrc.js",
rules: { "indent-legacy": 1 }
});

const report = engine.executeOnFiles(["lib/cli*.js"]);

assert.deepStrictEqual(
report.usedDeprecatedRules,
[{ ruleId: "indent-legacy", replacedBy: ["indent"] }]
);
});

it("should not warn when deprecated rules are not configured", () => {
engine = new CLIEngine({
cwd: originalDir,
configFile: ".eslintrc.js",
rules: { indent: 1 }
});

const report = engine.executeOnFiles(["lib/cli*.js"]);

assert.deepStrictEqual(report.usedDeprecatedRules, []);
});

describe("Fix Mode", () => {

it("should return fixed text on multiple files when in fix mode", () => {
Expand Down Expand Up @@ -1407,70 +1434,68 @@ describe("CLIEngine", () => {
const report = engine.executeOnFiles([path.resolve(fixtureDir, `${fixtureDir}/fixmode`)]);

report.results.forEach(convertCRLF);
assert.deepStrictEqual(report, {
results: [
{
filePath: fs.realpathSync(path.resolve(fixtureDir, "fixmode/multipass.js")),
messages: [],
errorCount: 0,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
output: "true ? \"yes\" : \"no\";\n"
},
{
filePath: fs.realpathSync(path.resolve(fixtureDir, "fixmode/ok.js")),
messages: [],
errorCount: 0,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0
},
{
filePath: fs.realpathSync(path.resolve(fixtureDir, "fixmode/quotes-semi-eqeqeq.js")),
messages: [
{
column: 9,
line: 2,
message: "Expected '===' and instead saw '=='.",
messageId: "unexpected",
nodeType: "BinaryExpression",
ruleId: "eqeqeq",
severity: 2
}
],
errorCount: 1,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
output: "var msg = \"hi\";\nif (msg == \"hi\") {\n\n}\n"
},
{
filePath: fs.realpathSync(path.resolve(fixtureDir, "fixmode/quotes.js")),
messages: [
{
column: 18,
line: 1,
endColumn: 21,
endLine: 1,
message: "'foo' is not defined.",
nodeType: "Identifier",
ruleId: "no-undef",
severity: 2
}
],
errorCount: 1,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
output: "var msg = \"hi\" + foo;\n"
}
],
errorCount: 2,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0
});
assert.deepStrictEqual(report.results, [
{
filePath: fs.realpathSync(path.resolve(fixtureDir, "fixmode/multipass.js")),
messages: [],
errorCount: 0,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
output: "true ? \"yes\" : \"no\";\n"
},
{
filePath: fs.realpathSync(path.resolve(fixtureDir, "fixmode/ok.js")),
messages: [],
errorCount: 0,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0
},
{
filePath: fs.realpathSync(path.resolve(fixtureDir, "fixmode/quotes-semi-eqeqeq.js")),
messages: [
{
column: 9,
line: 2,
message: "Expected '===' and instead saw '=='.",
messageId: "unexpected",
nodeType: "BinaryExpression",
ruleId: "eqeqeq",
severity: 2
}
],
errorCount: 1,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
output: "var msg = \"hi\";\nif (msg == \"hi\") {\n\n}\n"
},
{
filePath: fs.realpathSync(path.resolve(fixtureDir, "fixmode/quotes.js")),
messages: [
{
column: 18,
line: 1,
endColumn: 21,
endLine: 1,
message: "'foo' is not defined.",
nodeType: "Identifier",
ruleId: "no-undef",
severity: 2
}
],
errorCount: 1,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
output: "var msg = \"hi\" + foo;\n"
}
]);
assert.strictEqual(report.errorCount, 2);
assert.strictEqual(report.warningCount, 0);
assert.strictEqual(report.fixableErrorCount, 0);
assert.strictEqual(report.fixableWarningCount, 0);
});

it("should run autofix even if files are cached without autofix results", () => {
Expand Down Expand Up @@ -2117,7 +2142,7 @@ describe("CLIEngine", () => {
assert.deepStrictEqual(result, cachedResult, "the result is the same regardless of using cache or not");

// assert the file was not processed because the cache was used
assert.isFalse(spy.called, "the file was not loaded because it used the cache");
assert.isFalse(spy.calledWith(file), "the file was not loaded because it used the cache");
});

it("should remember the files from a previous run and do not operate on then if not changed", () => {
Expand Down

0 comments on commit 02d3dc3

Please sign in to comment.