Skip to content

Commit afc1514

Browse files
authored
Fix: Change autofix to suggestion in require-meta-schema rule (#185)
* fix: change autofix to suggestion in require-meta-schema rule * fix for eslint 6 not allowing bailing out of suggestion
1 parent d2d165d commit afc1514

File tree

4 files changed

+61
-27
lines changed

4 files changed

+61
-27
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Name | ✔️ | 🛠 | 💡 | Description
6767
[require-meta-docs-url](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/require-meta-docs-url.md) | | 🛠 | | require rules to implement a `meta.docs.url` property
6868
[require-meta-fixable](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/require-meta-fixable.md) | ✔️ | | | require rules to implement a `meta.fixable` property
6969
[require-meta-has-suggestions](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/require-meta-has-suggestions.md) | | 🛠 | | require suggestable rules to implement a `meta.hasSuggestions` property
70-
[require-meta-schema](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/require-meta-schema.md) | | 🛠 | | require rules to implement a `meta.schema` property
70+
[require-meta-schema](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/require-meta-schema.md) | | | 💡 | require rules to implement a `meta.schema` property
7171
[require-meta-type](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/require-meta-type.md) | | | | require rules to implement a `meta.type` property
7272
[test-case-property-ordering](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/test-case-property-ordering.md) | | 🛠 | | require the properties of a test case to be placed in a consistent order
7373
[test-case-shorthand-strings](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/test-case-shorthand-strings.md) | | 🛠 | | enforce consistent usage of shorthand strings for test cases with no options

docs/rules/require-meta-schema.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Require rules to implement a `meta.schema` property (require-meta-schema)
22

3-
⚒️ The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#--fix) can automatically fix some of the problems reported by this rule.
3+
💡 Some problems reported by this rule are manually fixable by editor [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
44

55
Defining a schema for each rule allows eslint to validate that configuration options are passed correctly. Even when there are no options for a rule, a schema should still be defined (as an empty array) so that eslint can validate that no data is mistakenly passed to the rule.
66

lib/rules/require-meta-schema.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ module.exports = {
1515
category: 'Rules',
1616
recommended: false, // TODO: enable it in a major release.
1717
},
18-
fixable: 'code',
1918
schema: [
2019
{
2120
type: 'object',
@@ -29,10 +28,12 @@ module.exports = {
2928
},
3029
],
3130
messages: {
31+
addEmptySchema: 'Add empty schema indicating the rule has no options.',
3232
foundOptionsUsage: '`meta.schema` has no schema defined but rule has options.',
3333
missing: '`meta.schema` is required (use [] if rule has no schema).',
3434
wrongType: '`meta.schema` should be an array or object (use [] if rule has no schema).',
3535
},
36+
hasSuggestions: true,
3637
},
3738

3839
create (context) {
@@ -106,12 +107,14 @@ module.exports = {
106107
context.report({
107108
node: metaNode,
108109
messageId: 'missing',
109-
fix (fixer) {
110-
if (isUsingOptions) {
111-
return null;
112-
}
113-
return utils.insertProperty(fixer, metaNode, 'schema: []', sourceCode);
114-
},
110+
suggest: isUsingOptions ? [] : [
111+
{
112+
messageId: 'addEmptySchema',
113+
fix (fixer) {
114+
return utils.insertProperty(fixer, metaNode, 'schema: []', sourceCode);
115+
},
116+
},
117+
],
115118
});
116119
}
117120
},

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

+49-18
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,24 @@ ruleTester.run('require-meta-schema', rule, {
104104
create(context) {}
105105
};
106106
`,
107-
output: `
107+
output: null,
108+
errors: [
109+
{
110+
messageId: 'missing',
111+
type: 'ObjectExpression',
112+
suggestions: [
113+
{
114+
messageId: 'addEmptySchema',
115+
output: `
108116
module.exports = {
109117
meta: {
110118
schema: []
111119
},
112120
create(context) {}
113121
};
114122
`,
115-
errors: [{ messageId: 'missing', type: 'ObjectExpression' }],
123+
}],
124+
}],
116125
},
117126
{
118127
// requireSchemaPropertyWhenOptionless = true.
@@ -122,16 +131,27 @@ schema: []
122131
create(context) {}
123132
};
124133
`,
125-
output: `
134+
output: null,
135+
options: [{ requireSchemaPropertyWhenOptionless: true }],
136+
errors: [
137+
{
138+
messageId: 'missing',
139+
type: 'ObjectExpression',
140+
suggestions: [
141+
{
142+
messageId: 'addEmptySchema',
143+
output: `
126144
module.exports = {
127145
meta: {
128146
schema: []
129147
},
130148
create(context) {}
131149
};
132150
`,
133-
options: [{ requireSchemaPropertyWhenOptionless: true }],
134-
errors: [{ messageId: 'missing', type: 'ObjectExpression' }],
151+
},
152+
],
153+
},
154+
],
135155
},
136156
{
137157
code: `
@@ -140,14 +160,25 @@ schema: []
140160
create(context) {}
141161
};
142162
`,
143-
output: `
163+
output: null,
164+
errors: [
165+
{
166+
messageId: 'missing',
167+
type: 'ObjectExpression',
168+
suggestions: [
169+
{
170+
messageId: 'addEmptySchema',
171+
output: `
144172
module.exports = {
145173
meta: { type: 'problem',
146174
schema: [] },
147175
create(context) {}
148176
};
149177
`,
150-
errors: [{ messageId: 'missing', type: 'ObjectExpression' }],
178+
},
179+
],
180+
},
181+
],
151182
},
152183
{
153184
code: `
@@ -157,7 +188,7 @@ schema: [] },
157188
};
158189
`,
159190
output: null,
160-
errors: [{ messageId: 'wrongType', type: 'Literal' }],
191+
errors: [{ messageId: 'wrongType', type: 'Literal', suggestions: [] }],
161192
},
162193
{
163194
// requireSchemaPropertyWhenOptionless = false.
@@ -169,7 +200,7 @@ schema: [] },
169200
`,
170201
output: null,
171202
options: [{ requireSchemaPropertyWhenOptionless: false }],
172-
errors: [{ messageId: 'wrongType', type: 'Literal' }],
203+
errors: [{ messageId: 'wrongType', type: 'Literal', suggestions: [] }],
173204
},
174205
{
175206
code: `
@@ -179,7 +210,7 @@ schema: [] },
179210
};
180211
`,
181212
output: null,
182-
errors: [{ messageId: 'wrongType', type: 'Identifier' }],
213+
errors: [{ messageId: 'wrongType', type: 'Identifier', suggestions: [] }],
183214
},
184215
{
185216
code: `
@@ -190,7 +221,7 @@ schema: [] },
190221
};
191222
`,
192223
output: null,
193-
errors: [{ messageId: 'wrongType', type: 'Literal' }],
224+
errors: [{ messageId: 'wrongType', type: 'Literal', suggestions: [] }],
194225
},
195226
{
196227
// Empty schema (array), but using rule options.
@@ -201,7 +232,7 @@ schema: [] },
201232
};
202233
`,
203234
output: null,
204-
errors: [{ messageId: 'foundOptionsUsage', type: 'Property' }],
235+
errors: [{ messageId: 'foundOptionsUsage', type: 'Property', suggestions: [] }],
205236
},
206237
{
207238
// Empty schema (object), but using rule options.
@@ -212,7 +243,7 @@ schema: [] },
212243
};
213244
`,
214245
output: null,
215-
errors: [{ messageId: 'foundOptionsUsage', type: 'Property' }],
246+
errors: [{ messageId: 'foundOptionsUsage', type: 'Property', suggestions: [] }],
216247
},
217248
{
218249
// Empty schema (object), but using rule options, requireSchemaPropertyWhenOptionless = false.
@@ -224,7 +255,7 @@ schema: [] },
224255
`,
225256
output: null,
226257
options: [{ requireSchemaPropertyWhenOptionless: false }],
227-
errors: [{ messageId: 'foundOptionsUsage', type: 'Property' }],
258+
errors: [{ messageId: 'foundOptionsUsage', type: 'Property', suggestions: [] }],
228259
},
229260
{
230261
// No schema, but using rule options, requireSchemaPropertyWhenOptionless = false.
@@ -236,10 +267,10 @@ schema: [] },
236267
`,
237268
output: null,
238269
options: [{ requireSchemaPropertyWhenOptionless: false }],
239-
errors: [{ messageId: 'foundOptionsUsage', type: 'ObjectExpression' }],
270+
errors: [{ messageId: 'foundOptionsUsage', type: 'ObjectExpression', suggestions: [] }],
240271
},
241272
{
242-
// No schema, but using rule options, should have no autofix.
273+
// No schema, but using rule options, should have no suggestions.
243274
code: `
244275
module.exports = {
245276
meta: {},
@@ -248,8 +279,8 @@ schema: [] },
248279
`,
249280
output: null,
250281
errors: [
251-
{ messageId: 'foundOptionsUsage', type: 'ObjectExpression' },
252-
{ messageId: 'missing', type: 'ObjectExpression' },
282+
{ messageId: 'foundOptionsUsage', type: 'ObjectExpression', suggestions: [] },
283+
{ messageId: 'missing', type: 'ObjectExpression', suggestions: [] },
253284
],
254285
},
255286
],

0 commit comments

Comments
 (0)