Skip to content

Commit d2d165d

Browse files
authored
Fix: only autofix in require-meta-schema rule when no options present (#184)
1 parent a864297 commit d2d165d

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

lib/rules/require-meta-schema.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ module.exports = {
5151
const requireSchemaPropertyWhenOptionless = !context.options[0] || context.options[0].requireSchemaPropertyWhenOptionless;
5252

5353
let hasEmptySchema = false;
54+
let isUsingOptions = false;
5455

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

6465
if (!schemaNode) {
65-
if (requireSchemaPropertyWhenOptionless) {
66-
context.report({
67-
node: metaNode,
68-
messageId: 'missing',
69-
fix (fixer) {
70-
return utils.insertProperty(fixer, metaNode, 'schema: []', sourceCode);
71-
},
72-
});
73-
}
7466
return;
7567
}
7668

@@ -109,6 +101,21 @@ module.exports = {
109101
}
110102
},
111103

104+
'Program:exit' () {
105+
if (!schemaNode && requireSchemaPropertyWhenOptionless) {
106+
context.report({
107+
node: metaNode,
108+
messageId: 'missing',
109+
fix (fixer) {
110+
if (isUsingOptions) {
111+
return null;
112+
}
113+
return utils.insertProperty(fixer, metaNode, 'schema: []', sourceCode);
114+
},
115+
});
116+
}
117+
},
118+
112119
MemberExpression (node) {
113120
// Check if `context.options` was used when no options were defined in `meta.schema`.
114121
if (
@@ -118,6 +125,7 @@ module.exports = {
118125
node.property.type === 'Identifier' &&
119126
node.property.name === 'options'
120127
) {
128+
isUsingOptions = true;
121129
context.report({ node: schemaNode || metaNode, messageId: 'foundOptionsUsage' });
122130
}
123131
},

tests/lib/rules/require-meta-schema.js

+14
Original file line numberDiff line numberDiff line change
@@ -238,5 +238,19 @@ schema: [] },
238238
options: [{ requireSchemaPropertyWhenOptionless: false }],
239239
errors: [{ messageId: 'foundOptionsUsage', type: 'ObjectExpression' }],
240240
},
241+
{
242+
// No schema, but using rule options, should have no autofix.
243+
code: `
244+
module.exports = {
245+
meta: {},
246+
create(context) { const options = context.options; }
247+
};
248+
`,
249+
output: null,
250+
errors: [
251+
{ messageId: 'foundOptionsUsage', type: 'ObjectExpression' },
252+
{ messageId: 'missing', type: 'ObjectExpression' },
253+
],
254+
},
241255
],
242256
});

0 commit comments

Comments
 (0)