diff --git a/README.md b/README.md index bff41615..20a1a083 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ Name | ✔️ | 🛠 | Description [prefer-placeholders](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/prefer-placeholders.md) | | | Disallows template literals as report messages [report-message-format](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/report-message-format.md) | | | Enforces a consistent format for report messages [require-meta-fixable](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/require-meta-fixable.md) | ✔️ | | Requires a `meta.fixable` property for fixable rules -[test-case-property-ordering](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/test-case-property-ordering.md) | | | Requires the properties of a test case to be placed in a consistent order. +[test-case-property-ordering](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/test-case-property-ordering.md) | | 🛠 | Requires the properties of a test case to be placed in a consistent order. [test-case-shorthand-strings](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/test-case-shorthand-strings.md) | | 🛠 | Enforces consistent usage of shorthand strings for test cases with no options ## Supported Presets diff --git a/docs/rules/test-case-property-ordering.md b/docs/rules/test-case-property-ordering.md index 9b012e05..7172b6bb 100644 --- a/docs/rules/test-case-property-ordering.md +++ b/docs/rules/test-case-property-ordering.md @@ -1,5 +1,7 @@ # enforce ordering of keys in test cases (test-case-property-ordering) +(fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixes problems reported by this rule. + This rule enforces that the properties of RuleTester test cases are arranged in a consistent order. ## Rule Details diff --git a/lib/rules/test-case-property-ordering.js b/lib/rules/test-case-property-ordering.js index 91547352..ce2092de 100644 --- a/lib/rules/test-case-property-ordering.js +++ b/lib/rules/test-case-property-ordering.js @@ -18,7 +18,7 @@ module.exports = { category: 'Tests', recommended: false, }, - fixable: null, // or "code" or "whitespace" + fixable: 'code', schema: [{ type: 'array', elements: { type: 'string' }, @@ -31,29 +31,37 @@ module.exports = { // ---------------------------------------------------------------------- const message = 'The properties of a test case should be placed in a consistent order: [{{order}}].'; const order = context.options[0] || ['code', 'output', 'options', 'parserOptions', 'errors']; + const sourceCode = context.getSourceCode(); return { Program (ast) { utils.getTestInfo(context, ast).forEach(testRun => { [testRun.valid, testRun.invalid].forEach(tests => { (tests || []).forEach(test => { - const keys = (test.properties || []).map(utils.getKeyName); + const properties = test.properties || []; + const keyNames = properties.map(utils.getKeyName); - for (let i = 0, lastChecked; i < keys.length; i++) { - const current = order.indexOf(keys[i]); + for (let i = 0, lastChecked; i < keyNames.length; i++) { + const current = order.indexOf(keyNames[i]); // current < lastChecked to catch unordered; // and lastChecked === -1 to catch extra properties before. if (current > -1 && (current < lastChecked || lastChecked === -1)) { - let orderMsg = order.filter(item => keys.indexOf(item) > -1); + let orderMsg = order.filter(item => keyNames.indexOf(item) > -1); orderMsg = orderMsg.concat( - lastChecked === -1 ? keys.filter(item => order.indexOf(item) === -1) : [] + lastChecked === -1 ? keyNames.filter(item => order.indexOf(item) === -1) : [] ); context.report({ - node: test.properties[i], + node: properties[i], message, data: { order: orderMsg.join(', ') }, + fix (fixer) { + return orderMsg.map((key, index) => { + const propertyToInsert = test.properties[keyNames.indexOf(key)]; + return fixer.replaceText(test.properties[index], sourceCode.getText(propertyToInsert)); + }); + }, }); } lastChecked = current; diff --git a/tests/lib/rules/test-case-property-ordering.js b/tests/lib/rules/test-case-property-ordering.js index 4b414a51..3efa1a7b 100644 --- a/tests/lib/rules/test-case-property-ordering.js +++ b/tests/lib/rules/test-case-property-ordering.js @@ -22,23 +22,14 @@ ruleTester.run('test-case-property-ordering', rule, { ` new RuleTester().run('foo', bar, { valid: [ - { - code: "foo", - output: "bar", - options: ["baz"], - }, + { code: "foo", output: "bar", options: ["baz"], }, ] }); `, ` new RuleTester().run('foo', bar, { valid: [ - { - code: "foo", - output: "bar", - options: ["baz"], - env: { es6: true }, - }, + { code: "foo",output: "bar",options: ["baz"],env: { es6: true }, }, ] }); `, @@ -46,11 +37,7 @@ ruleTester.run('test-case-property-ordering', rule, { code: ` new RuleTester().run('foo', bar, { valid: [ - { - code: "foo", - options: ["baz"], - output: "bar", - }, + { code: "foo", options: ["baz"], output: "bar", }, ] }); `, @@ -63,11 +50,14 @@ ruleTester.run('test-case-property-ordering', rule, { code: ` new RuleTester().run('foo', bar, { valid: [ - { - code: "foo", - options: ["baz"], - output: "bar", - }, + { code: "foo", options: ["baz"], output: "bar", }, + ] + }); + `, + output: ` + new RuleTester().run('foo', bar, { + valid: [ + { code: "foo", output: "bar", options: ["baz"], }, ] }); `, @@ -75,16 +65,18 @@ ruleTester.run('test-case-property-ordering', rule, { }, { code: ` - new RuleTester().run('foo', bar, { - valid: [ - { - env: { es6: true }, - code: "foo", - output: "bar", - options: ["baz"], - }, - ] - }); + new RuleTester().run('foo', bar, { + valid: [ + { env: { es6: true }, code: "foo", output: "bar", options: ["baz"], }, + ] + }); + `, + output: ` + new RuleTester().run('foo', bar, { + valid: [ + { code: "foo", output: "bar", options: ["baz"], env: { es6: true }, }, + ] + }); `, errors: [{ message: 'The properties of a test case should be placed in a consistent order: [code, output, options, env].' }], }, @@ -92,12 +84,14 @@ ruleTester.run('test-case-property-ordering', rule, { code: ` new RuleTester().run('foo', bar, { valid: [ - { - code: "foo", - env: { es6: true }, - output: "bar", - options: ["baz"], - }, + { code: "foo", env: { es6: true }, output: "bar", options: ["baz"], }, + ] + }); + `, + output: ` + new RuleTester().run('foo', bar, { + valid: [ + { code: "foo", output: "bar", options: ["baz"], env: { es6: true }, }, ] }); `, @@ -107,11 +101,14 @@ ruleTester.run('test-case-property-ordering', rule, { code: ` new RuleTester().run('foo', bar, { valid: [ - { - code: "foo", - output: "bar", - options: ["baz"], - }, + { code: "foo", output: "bar", options: ["baz"], }, + ] + }); + `, + output: ` + new RuleTester().run('foo', bar, { + valid: [ + { code: "foo", options: ["baz"], output: "bar", }, ] }); `, @@ -122,18 +119,18 @@ ruleTester.run('test-case-property-ordering', rule, { code: ` new RuleTester().run('foo', bar, { valid: [ - { - options: ["baz"], - parserOptions: "", - code: "foo", - errors: ["foo"], - output: "", - }, + {\ncode: "foo",\noutput: "",\nerrors: ["baz"],\nparserOptions: "",\n}, + ] + }); + `, + output: ` + new RuleTester().run('foo', bar, { + valid: [ + {\ncode: "foo",\noutput: "",\nparserOptions: "",\nerrors: ["baz"],\n}, ] }); `, - options: [['code', 'errors', 'output']], - errors: [{ message: 'The properties of a test case should be placed in a consistent order: [code, errors, output, options, parserOptions].' }], + errors: [{ message: 'The properties of a test case should be placed in a consistent order: [code, output, parserOptions, errors].' }], }, ], });