Skip to content

Fix: Only autofix when no options present in require-meta-schema rule #184

New issue

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

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

Already on GitHub? # 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
26 changes: 17 additions & 9 deletions lib/rules/require-meta-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = {
const requireSchemaPropertyWhenOptionless = !context.options[0] || context.options[0].requireSchemaPropertyWhenOptionless;

let hasEmptySchema = false;
let isUsingOptions = false;

return {
Program (ast) {
Expand All @@ -62,15 +63,6 @@ module.exports = {
metaNode.properties.find(p => p.type === 'Property' && utils.getKeyName(p) === 'schema');

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

Expand Down Expand Up @@ -109,6 +101,21 @@ module.exports = {
}
},

'Program:exit' () {
if (!schemaNode && requireSchemaPropertyWhenOptionless) {
context.report({
node: metaNode,
messageId: 'missing',
fix (fixer) {
if (isUsingOptions) {
return null;
}
return utils.insertProperty(fixer, metaNode, 'schema: []', sourceCode);
},
});
}
},

MemberExpression (node) {
// Check if `context.options` was used when no options were defined in `meta.schema`.
if (
Expand All @@ -118,6 +125,7 @@ module.exports = {
node.property.type === 'Identifier' &&
node.property.name === 'options'
) {
isUsingOptions = true;
context.report({ node: schemaNode || metaNode, messageId: 'foundOptionsUsage' });
}
},
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/rules/require-meta-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,5 +238,19 @@ schema: [] },
options: [{ requireSchemaPropertyWhenOptionless: false }],
errors: [{ messageId: 'foundOptionsUsage', type: 'ObjectExpression' }],
},
{
// No schema, but using rule options, should have no autofix.
code: `
module.exports = {
meta: {},
create(context) { const options = context.options; }
};
`,
output: null,
errors: [
{ messageId: 'foundOptionsUsage', type: 'ObjectExpression' },
{ messageId: 'missing', type: 'ObjectExpression' },
],
},
],
});