Skip to content

Commit 7eb3fe7

Browse files
committed
Update: add autofixing to test-case-property-ordering. (fixes eslint-community#31)
1 parent 04d6a09 commit 7eb3fe7

File tree

2 files changed

+75
-16
lines changed

2 files changed

+75
-16
lines changed

lib/rules/test-case-property-ordering.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
category: 'Tests',
1919
recommended: false,
2020
},
21-
fixable: null, // or "code" or "whitespace"
21+
fixable: 'code',
2222
schema: [{
2323
type: 'array',
2424
elements: { type: 'string' },
@@ -31,29 +31,43 @@ module.exports = {
3131
// ----------------------------------------------------------------------
3232
const message = 'The properties of a test case should be placed in a consistent order: [{{order}}].';
3333
const order = context.options[0] || ['code', 'output', 'options', 'parserOptions', 'errors'];
34+
const sourceCode = context.getSourceCode();
3435

3536
return {
3637
Program (ast) {
3738
utils.getTestInfo(context, ast).forEach(testRun => {
3839
[testRun.valid, testRun.invalid].forEach(tests => {
3940
(tests || []).forEach(test => {
40-
const keys = (test.properties || []).map(utils.getKeyName);
41+
const keys = test.properties || [];
42+
const keyNames = keys.map(utils.getKeyName);
4143

42-
for (let i = 0, lastChecked; i < keys.length; i++) {
43-
const current = order.indexOf(keys[i]);
44+
for (let i = 0, lastChecked; i < keyNames.length; i++) {
45+
const current = order.indexOf(keyNames[i]);
4446

4547
// current < lastChecked to catch unordered;
4648
// and lastChecked === -1 to catch extra properties before.
4749
if (current > -1 && (current < lastChecked || lastChecked === -1)) {
48-
let orderMsg = order.filter(item => keys.indexOf(item) > -1);
50+
let orderMsg = order.filter(item => keyNames.indexOf(item) > -1);
4951
orderMsg = orderMsg.concat(
50-
lastChecked === -1 ? keys.filter(item => order.indexOf(item) === -1) : []
52+
lastChecked === -1 ? keyNames.filter(item => order.indexOf(item) === -1) : []
5153
);
5254

5355
context.report({
5456
node: test.properties[i],
5557
message,
5658
data: { order: orderMsg.join(', ') },
59+
fix (fixer) {
60+
// reorder the properties and put in result array.
61+
const result = [];
62+
for (let j = 0; j < keyNames.length; j++) {
63+
const insertedIndex = orderMsg.indexOf(keyNames[j]);
64+
result[insertedIndex] = sourceCode.getText(keys[j]);
65+
}
66+
return fixer.replaceTextRange(
67+
[test.properties[0].range[0], test.properties[test.properties.length - 1].range[1]],
68+
result.join(', ')
69+
);
70+
},
5771
});
5872
}
5973
lastChecked = current;

tests/lib/rules/test-case-property-ordering.js

+55-10
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,38 @@ ruleTester.run('test-case-property-ordering', rule, {
7171
]
7272
});
7373
`,
74+
output: `
75+
new RuleTester().run('foo', bar, {
76+
valid: [
77+
{
78+
code: "foo", output: "bar", options: ["baz"],
79+
},
80+
]
81+
});
82+
`,
7483
errors: [{ message: 'The properties of a test case should be placed in a consistent order: [code, output, options].' }],
7584
},
7685
{
7786
code: `
78-
new RuleTester().run('foo', bar, {
79-
valid: [
80-
{
81-
env: { es6: true },
82-
code: "foo",
83-
output: "bar",
84-
options: ["baz"],
85-
},
86-
]
87-
});
87+
new RuleTester().run('foo', bar, {
88+
valid: [
89+
{
90+
env: { es6: true },
91+
code: "foo",
92+
output: "bar",
93+
options: ["baz"],
94+
},
95+
]
96+
});
97+
`,
98+
output: `
99+
new RuleTester().run('foo', bar, {
100+
valid: [
101+
{
102+
code: "foo", output: "bar", options: ["baz"], env: { es6: true },
103+
},
104+
]
105+
});
88106
`,
89107
errors: [{ message: 'The properties of a test case should be placed in a consistent order: [code, output, options, env].' }],
90108
},
@@ -101,6 +119,15 @@ ruleTester.run('test-case-property-ordering', rule, {
101119
]
102120
});
103121
`,
122+
output: `
123+
new RuleTester().run('foo', bar, {
124+
valid: [
125+
{
126+
code: "foo", output: "bar", options: ["baz"], env: { es6: true },
127+
},
128+
]
129+
});
130+
`,
104131
errors: [{ message: 'The properties of a test case should be placed in a consistent order: [code, output, options, env].' }],
105132
},
106133
{
@@ -115,6 +142,15 @@ ruleTester.run('test-case-property-ordering', rule, {
115142
]
116143
});
117144
`,
145+
output: `
146+
new RuleTester().run('foo', bar, {
147+
valid: [
148+
{
149+
code: "foo", options: ["baz"], output: "bar",
150+
},
151+
]
152+
});
153+
`,
118154
options: [['code', 'errors', 'options', 'output', 'parserOptions']],
119155
errors: [{ message: 'The properties of a test case should be placed in a consistent order: [code, options, output].' }],
120156
},
@@ -132,6 +168,15 @@ ruleTester.run('test-case-property-ordering', rule, {
132168
]
133169
});
134170
`,
171+
output: `
172+
new RuleTester().run('foo', bar, {
173+
valid: [
174+
{
175+
code: "foo", errors: ["foo"], output: "", options: ["baz"], parserOptions: "",
176+
},
177+
]
178+
});
179+
`,
135180
options: [['code', 'errors', 'output']],
136181
errors: [{ message: 'The properties of a test case should be placed in a consistent order: [code, errors, output, options, parserOptions].' }],
137182
},

0 commit comments

Comments
 (0)