Skip to content

New: Add requireSchemaPropertyWhenOptionless option to require-meta-schema rule #180

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/rules/require-meta-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ module.exports = {
};
```

## Options

This rule takes an optional object containing:

* `boolean` — `requireSchemaPropertyWhenOptionless` — Whether the rule should require the `meta.schema` property to be specified (with `schema: []`) for rules that have no options. Defaults to `true`.

## Further Reading

* [working-with-rules#options-schemas](https://eslint.org/docs/developer-guide/working-with-rules#options-schemas)
41 changes: 29 additions & 12 deletions lib/rules/require-meta-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@ module.exports = {
recommended: false, // TODO: enable it in a major release.
},
fixable: 'code',
schema: [],
schema: [
{
type: 'object',
properties: {
requireSchemaPropertyWhenOptionless: {
type: 'boolean',
default: true,
},
},
additionalProperties: false,
},
],
messages: {
foundOptionsUsage: '`meta.schema` has no schema defined but rule has options.',
missing: '`meta.schema` is required (use [] if rule has no schema).',
Expand All @@ -33,27 +44,33 @@ module.exports = {
}

let contextIdentifiers;
let hasEmptySchema = false;
const metaNode = info.meta;
let schemaNode;

// Options
const requireSchemaPropertyWhenOptionless = !context.options[0] || context.options[0].requireSchemaPropertyWhenOptionless;

let hasEmptySchema = false;

return {
Program (ast) {
contextIdentifiers = utils.getContextIdentifiers(context, ast);

const metaNode = info.meta;
schemaNode =
metaNode &&
metaNode.properties &&
metaNode.properties.find(p => p.type === 'Property' && utils.getKeyName(p) === 'schema');

if (!schemaNode) {
context.report({
node: metaNode,
messageId: 'missing',
fix (fixer) {
return utils.insertProperty(fixer, metaNode, 'schema: []', sourceCode);
},
});
if (requireSchemaPropertyWhenOptionless) {
context.report({
node: metaNode,
messageId: 'missing',
fix (fixer) {
return utils.insertProperty(fixer, metaNode, 'schema: []', sourceCode);
Copy link
Contributor

@aladdin-add aladdin-add Sep 23, 2021

Choose a reason for hiding this comment

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

is it likely to change the runtime behavior?

  • before fixing: skip validating
  • after fixing: no options allowed.

or it can be a suggestion?

Copy link
Member Author

Choose a reason for hiding this comment

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

This PR does not change the existing autofixing behavior. However, you just discovered a bug. The goal is that the rule should only autofix to schema: [] if no options are used. I can fix this autofixing bug as a follow-up PR once this PR is merged.

},
});
}
return;
}

Expand Down Expand Up @@ -95,13 +112,13 @@ module.exports = {
MemberExpression (node) {
// Check if `context.options` was used when no options were defined in `meta.schema`.
if (
hasEmptySchema &&
(hasEmptySchema || !schemaNode) &&
node.object.type === 'Identifier' &&
contextIdentifiers.has(node.object) &&
node.property.type === 'Identifier' &&
node.property.name === 'options'
) {
context.report({ node: schemaNode, messageId: 'foundOptionsUsage' });
context.report({ node: schemaNode || metaNode, messageId: 'foundOptionsUsage' });
}
},
};
Expand Down
66 changes: 66 additions & 0 deletions tests/lib/rules/require-meta-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ ruleTester.run('require-meta-schema', rule, {
create
};
`,

{
// requireSchemaPropertyWhenOptionless = false
code: `
module.exports = {
meta: {},
create(context) {}
};
`,
options: [{ requireSchemaPropertyWhenOptionless: false }],
},
],

invalid: [
Expand All @@ -103,6 +114,25 @@ schema: []
`,
errors: [{ messageId: 'missing', type: 'ObjectExpression' }],
},
{
// requireSchemaPropertyWhenOptionless = true.
code: `
module.exports = {
meta: {},
create(context) {}
};
`,
output: `
module.exports = {
meta: {
schema: []
},
create(context) {}
};
`,
options: [{ requireSchemaPropertyWhenOptionless: true }],
errors: [{ messageId: 'missing', type: 'ObjectExpression' }],
},
{
code: `
module.exports = {
Expand All @@ -129,6 +159,18 @@ schema: [] },
output: null,
errors: [{ messageId: 'wrongType', type: 'Literal' }],
},
{
// requireSchemaPropertyWhenOptionless = false.
code: `
module.exports = {
meta: { schema: null },
create(context) {}
};
`,
output: null,
options: [{ requireSchemaPropertyWhenOptionless: false }],
errors: [{ messageId: 'wrongType', type: 'Literal' }],
},
{
code: `
module.exports = {
Expand Down Expand Up @@ -172,5 +214,29 @@ schema: [] },
output: null,
errors: [{ messageId: 'foundOptionsUsage', type: 'Property' }],
},
{
// Empty schema (object), but using rule options, requireSchemaPropertyWhenOptionless = false.
code: `
module.exports = {
meta: { schema: {} },
create(context) { const options = context.options; }
};
`,
output: null,
options: [{ requireSchemaPropertyWhenOptionless: false }],
errors: [{ messageId: 'foundOptionsUsage', type: 'Property' }],
},
{
// No schema, but using rule options, requireSchemaPropertyWhenOptionless = false.
code: `
module.exports = {
meta: {},
create(context) { const options = context.options; }
};
`,
output: null,
options: [{ requireSchemaPropertyWhenOptionless: false }],
errors: [{ messageId: 'foundOptionsUsage', type: 'ObjectExpression' }],
},
],
});