Skip to content

Commit 2b1eee1

Browse files
committed
auto-fix
1 parent cb1e1fc commit 2b1eee1

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "mocha tests/",
8-
"test:watch": "mocha --watch tests/"
8+
"test:watch": "mocha --watch --reporter min tests/"
99
},
1010
"repository": {
1111
"type": "git",

rules/vue-sort-components.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// @ts-check
22
"use strict";
33

4+
const naturalCompare = require("natural-compare");
5+
46
/**
57
* @param {import('estree').Property} node
68
* @returns {string}
@@ -12,6 +14,7 @@ const getKeyName = (node) =>
1214
module.exports = {
1315
meta: {
1416
type: "layout",
17+
fixable: "code",
1518
messages: {
1619
sortComponents: "Component names must be sorted.",
1720
},
@@ -27,21 +30,27 @@ module.exports = {
2730
return;
2831
}
2932

30-
const keys = value.properties
31-
.filter(
32-
/** @returns {node is import('estree').Property} */
33-
(node) => node.type === "Property"
34-
)
35-
.map((node) => getKeyName(node));
36-
37-
const sortedKeys = [...keys].sort();
38-
const sameOrder = sortedKeys.every((v, i) => v === keys[i]);
33+
const properties = value.properties.filter(
34+
/** @returns {node is import('estree').Property} */
35+
(node) => node.type === "Property"
36+
);
37+
const sorted = [...properties].sort((a, b) =>
38+
naturalCompare(getKeyName(a), getKeyName(b))
39+
);
40+
const sameOrder = properties.every((v, i) => v === sorted[i]);
3941

4042
if (sameOrder) return;
4143

4244
context.report({
4345
node,
4446
messageId: "sortComponents",
47+
fix(fixer) {
48+
const sourceCode = context.getSourceCode();
49+
const sortedCodes = sorted.map((node) => sourceCode.getText(node));
50+
return properties.map((node, i) =>
51+
fixer.replaceText(node, sortedCodes[i])
52+
);
53+
},
4554
});
4655
},
4756
};

tests/vue-sort-components.spec.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,56 @@ const ruleTester = new RuleTester();
88

99
ruleTester.run("vue-sort-components", rule, {
1010
valid: [
11+
// single
1112
{
1213
code: "const obj = { components: { foo } }",
1314
parserOptions: { ecmaVersion: 6 },
1415
},
16+
// multiple
1517
{
1618
code: "const obj = { components: { bar, baz, foo } }",
1719
parserOptions: { ecmaVersion: 6 },
1820
},
21+
// multiple, not shorthand
22+
{
23+
code: "const obj = { components: { bar: 0, baz: 1, foo: 2 } }",
24+
parserOptions: { ecmaVersion: 6 },
25+
},
26+
// nested
1927
{
2028
code: "const obj = { nested: { components: { bar, baz, foo } } }",
2129
parserOptions: { ecmaVersion: 6 },
2230
},
31+
// not components
2332
{
24-
code: "const obj = { foo, bar }",
33+
code: "const obj = { nested: { foo, bar } }",
2534
parserOptions: { ecmaVersion: 6 },
2635
},
36+
// array
2737
{
2838
code: "const obj = { components: [ foo, bar, baz ] }",
2939
parserOptions: { ecmaVersion: 6 },
3040
},
3141
],
3242
invalid: [
43+
// multiple
3344
{
3445
code: "const obj = { components: { foo, bar, baz } }",
46+
output: "const obj = { components: { bar, baz, foo } }",
47+
parserOptions: { ecmaVersion: 6 },
48+
errors: [{ messageId: "sortComponents" }],
49+
},
50+
// multiple, not shorthand
51+
{
52+
code: "const obj = { components: { foo: 0, bar: 1, baz: 2 } }",
53+
output: "const obj = { components: { bar: 1, baz: 2, foo: 0 } }",
3554
parserOptions: { ecmaVersion: 6 },
3655
errors: [{ messageId: "sortComponents" }],
3756
},
57+
// nested
3858
{
3959
code: "const obj = { nested: { components: { foo, bar, baz } } }",
60+
output: "const obj = { nested: { components: { bar, baz, foo } } }",
4061
parserOptions: { ecmaVersion: 6 },
4162
errors: [{ messageId: "sortComponents" }],
4263
},

0 commit comments

Comments
 (0)