Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rule test deprecation warnings: fn-style rules/missing schema #15761

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/rule-tester/rule-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,10 @@ class RuleTester {
].concat(scenarioErrors).join("\n"));
}

if (typeof rule === "function") {
// eslint-disable-next-line no-console -- needed for temporary deprecation warning before this is converted into an assert
console.warn("DEPRECATION WARNING: This rule is using the deprecated function-style format and will stop working in ESLint v9. Please convert to object-style rules: https://eslint.org/docs/developer-guide/working-with-rules This lint rule can assist with the conversion: https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/main/docs/rules/prefer-object-rule.md");
}

linter.defineRule(ruleName, Object.assign({}, rule, {

Expand Down Expand Up @@ -578,6 +582,10 @@ class RuleTester {

if (hasOwnProperty(item, "options")) {
assert(Array.isArray(item.options), "options must be an array");
if (!rule.schema) {
Comment on lines 583 to +585
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we shouldn't log the warning if item.options is an empty array, as that's basically the same as if no options had been passed.

// eslint-disable-next-line no-console -- needed for temporary deprecation warning before this is converted into an assert
console.warn("DEPRECATION WARNING: This test case specifies `options` but the rule is missing `meta.schema` and will stop working in ESLint v9. Please add `meta.schema`: https://eslint.org/docs/developer-guide/working-with-rules#options-schemas This lint rule can assist with the conversion: https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/main/docs/rules/require-meta-schema.md");
Comment on lines +586 to +587
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes that the warning message will be logged right after the test title, but that doesn't have to be the case.

For example, if you run mocha with -R progress, it would look like this:

[▬▬..........................................................................]DEPRECATION WARNING: This test case specifies `options` but the rule is missing `meta.schema` and will stop working in ESLint v9. Please add `meta.schema`: https://eslint.org/docs/developer-guide/working-with-rules#options-schemas This lint rule can assist with the conversion: https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/main/docs/rules/require-meta-schema.md
  [▬▬▬.........................................................................]DEPRECATION WARNING: This test case specifies `options` but the rule is missing `meta.schema` and will stop working in ESLint v9. Please add `meta.schema`: https://eslint.org/docs/developer-guide/working-with-rules#options-schemas This lint rule can assist with the conversion: https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/main/docs/rules/require-meta-schema.md
DEPRECATION WARNING: This test case specifies `options` but the rule is missing `meta.schema` and will stop working in ESLint v9. Please add `meta.schema`:
https://eslint.org/docs/developer-guide/working-with-rules#options-schemas This lint rule can assist with the conversion: https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/main/docs/rules/require-meta-schema.md
  [▬▬▬▬........................................................................]DEPRECATION WARNING: This test case specifies `options` but the rule is missing `meta.schema` and will stop working in ESLint v9. Please add `meta.schema`: https://eslint.org/docs/developer-guide/working-with-rules#options-schemas This lint rule can assist with the conversion: https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/main/docs/rules/require-meta-schema.md
  [▬▬▬▬▬.......................................................................]DEPRECATION WARNING: This test case specifies `options` but the rule is missing `meta.schema` and will stop working in ESLint v9. Please add `meta.schema`: https://eslint.org/docs/developer-guide/working-with-rules#options-schemas This lint rule can assist with the conversion: https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/main/docs/rules/require-meta-schema.md
DEPRECATION WARNING: This test case specifies `options` but the rule is missing `meta.schema` and will stop working in ESLint v9. Please add `meta.schema`:
https://eslint.org/docs/developer-guide/working-with-rules#options-schemas This lint rule can assist with the conversion: https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/main/docs/rules/require-meta-schema.md
...

The problem is that there's no info about which rule is the cause of the warnings. We could add ruleName to the message. Also, it might be good to log the warning only once per rule (not for each test case that has options)?

}
config.rules[ruleName] = [1].concat(item.options);
} else {
config.rules[ruleName] = 1;
Expand Down
58 changes: 58 additions & 0 deletions tests/lib/rule-tester/rule-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -2295,6 +2295,64 @@ describe("RuleTester", () => {
});
});

describe("deprecations", () => {
it("should output a deprecation warning when using a function-style rule", () => {

/**
* Legacy-format rule (a function instead of an object with `create` method).
* @param {RuleContext} context The ESLint rule context object.
* @returns {Object} Listeners.
*/
function functionStyleRule(context) {
return {
Program(node) {
context.report({ node, message: "bad" });
}
};
}

const spy = sinon.spy(console, "warn");

ruleTester.run("functionStyleRule", functionStyleRule, {
valid: [],
invalid: [
{ code: "var foo = bar;", errors: 1 }
]
});

assert.strictEqual(spy.callCount, 1, "calls `console.warn` once");
assert.deepStrictEqual(spy.getCall(0).args, ["DEPRECATION WARNING: This rule is using the deprecated function-style format and will stop working in ESLint v9. Please convert to object-style rules: https://eslint.org/docs/developer-guide/working-with-rules This lint rule can assist with the conversion: https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/main/docs/rules/prefer-object-rule.md"]);

spy.restore();
});

it("should output a deprecation warning when using options with a schema-less rule", () => {
const ruleWithNoOptions = {
create(context) {
return {
Program(node) {
context.report({ node, message: "bad" });
}
};
}
};

const spy = sinon.spy(console, "warn");

ruleTester.run("ruleWithNoOptions", ruleWithNoOptions, {
valid: [],
invalid: [
{ code: "var foo = bar;", options: [{ foo: true }], errors: 1 }
]
});

assert.strictEqual(spy.callCount, 1, "calls `console.warn` once");
assert.deepStrictEqual(spy.getCall(0).args, ["DEPRECATION WARNING: This test case specifies `options` but the rule is missing `meta.schema` and will stop working in ESLint v9. Please add `meta.schema`: https://eslint.org/docs/developer-guide/working-with-rules#options-schemas This lint rule can assist with the conversion: https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/main/docs/rules/require-meta-schema.md"]);

spy.restore();
});
});

/**
* Asserts that a particular value will be emitted from an EventEmitter.
* @param {EventEmitter} emitter The emitter that should emit a value
Expand Down