Skip to content

Commit d5110cb

Browse files
committed
update.
1 parent 5ee1c38 commit d5110cb

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

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

+5-10
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,16 @@ module.exports = {
5757
message,
5858
data: { order: orderMsg.join(', ') },
5959
fix (fixer) {
60-
// reorder the properties and put in result array.
6160
const last = properties[properties.length - 1];
61+
// end loc to replace(including whitespaces).
62+
const finalEnd = sourceCode.getTokenAfter(last, token => token.value === '}' && token.type === 'Punctuator').range[0];
63+
// reorder the properties and put in result array.
6264
const result = [];
6365
for (let j = 0; j < keyNames.length; j++) {
6466
const insertedIndex = orderMsg.indexOf(keyNames[j]);
6567
const propertyCode = sourceCode.getText(properties[j]);
6668
const propertyStart = properties[j].range[1];
67-
68-
// last property didn't include trailing comma.
69-
const propertyEnd = j === properties.length - 1 ?
70-
sourceCode.getTokenAfter(last, token => token.value === '}' && token.type === 'Punctuator').range[0] :
71-
properties[j + 1].range[0];
69+
const propertyEnd = j < properties.length - 1 ? properties[j + 1].range[0] : finalEnd;
7270
let trailing = sourceCode.getText().slice(propertyStart, propertyEnd);
7371

7472
// for last property, should check & add trailling commas.
@@ -79,12 +77,9 @@ module.exports = {
7977
}
8078

8179
const start = properties[0].range[0];
82-
const end = sourceCode.getTokenAfter(last).value === ',' ?
83-
sourceCode.getTokenAfter(last, token => token.value === '}' && token.type === 'Punctuator').range[0] :
84-
last.range[1];
8580

8681
return fixer.replaceTextRange(
87-
[start, end],
82+
[start, finalEnd],
8883
result.join('')
8984
);
9085
},

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,18 @@ ruleTester.run('test-case-property-ordering', rule, {
119119
code: `
120120
new RuleTester().run('foo', bar, {
121121
valid: [
122-
{ options: ["baz"], parserOptions: "", code: "foo", errors: ["foo"], output: "", },
122+
{\ncode: "foo",\noutput: "",\nerrors: ["baz"],\nparserOptions: "",\n},
123123
]
124124
});
125125
`,
126126
output: `
127127
new RuleTester().run('foo', bar, {
128128
valid: [
129-
{ code: "foo", errors: ["foo"], output: "", options: ["baz"], parserOptions: "", },
129+
{\ncode: "foo",\noutput: "",\nparserOptions: "",\nerrors: ["baz"],\n},
130130
]
131131
});
132132
`,
133-
options: [['code', 'errors', 'output']],
134-
errors: [{ message: 'The properties of a test case should be placed in a consistent order: [code, errors, output, options, parserOptions].' }],
133+
errors: [{ message: 'The properties of a test case should be placed in a consistent order: [code, output, parserOptions, errors].' }],
135134
},
136135
],
137136
});

0 commit comments

Comments
 (0)