Skip to content

Commit 2ae6bc6

Browse files
committed
add sortSpreads option (#7)
* add sortSpreads option * 0.3.0
1 parent 6649aa2 commit 2ae6bc6

File tree

5 files changed

+95
-9
lines changed

5 files changed

+95
-9
lines changed

Diff for: README.md

+40
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ export default defineComponent({
6363
components: { Foo, Bar, Baz },
6464
});
6565

66+
// spreads must be grouped at the top
67+
export default defineComponent({
68+
components: { Bar, Baz, Foo, ...others },
69+
});
70+
6671
// not only in Vue-specific context
6772
const myObject = {
6873
components: { Foo, Bar, Baz },
@@ -76,8 +81,43 @@ export default defineComponent({
7681
components: { Bar, Baz, Foo },
7782
});
7883

84+
// spreads must be grouped at the top
85+
export default defineComponent({
86+
components: { ...others, Bar, Baz, Foo },
87+
});
88+
7989
// not only in Vue-specific context
8090
const myObject = {
8191
components: { Bar, Baz, Foo },
8292
};
8393
```
94+
95+
### Options
96+
97+
This rule accepts a configuration object:
98+
99+
```js
100+
{
101+
"@jay-es/vue-sort-components/vue-sort-components": ["error", { sortSpreads: false }]
102+
}
103+
```
104+
105+
- `sortSpreads` - if `true`, enforce spread properties to be sorted. Default is `false`.
106+
107+
#### sortSpreads
108+
109+
Examples of **incorrect** code for the `{ sortSpreads: true }` option:
110+
111+
```js
112+
export default defineComponent({
113+
components: { ...others2, ...others1, Bar, Baz },
114+
});
115+
```
116+
117+
Examples of **correct** code for the `{ sortSpreads: true }` option:
118+
119+
```js
120+
export default defineComponent({
121+
components: { ...others1, ...others2, Bar, Baz },
122+
});
123+
```

Diff for: package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jay-es/eslint-plugin-vue-sort-components",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "A plugin for ESLint to keep order of component names",
55
"main": "lib/index.js",
66
"files": [

Diff for: src/rules/vue-sort-components.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ const getArgName = (arg: Expression): string =>
1010

1111
const compareNodes = (
1212
a: Property | SpreadElement,
13-
b: Property | SpreadElement
13+
b: Property | SpreadElement,
14+
sortSpreads: boolean
1415
): -1 | 0 | 1 => {
1516
if (a.type === "Property" && b.type === "Property") {
1617
return naturalCompare(getKeyName(a), getKeyName(b));
1718
}
1819

1920
if (a.type === "SpreadElement" && b.type === "SpreadElement") {
20-
return naturalCompare(getArgName(a.argument), getArgName(b.argument));
21+
return sortSpreads
22+
? naturalCompare(getArgName(a.argument), getArgName(b.argument))
23+
: 0;
2124
}
2225

2326
return a.type === "SpreadElement" ? -1 : 1;
@@ -27,11 +30,22 @@ export const sortComponentsRule: Rule.RuleModule = {
2730
meta: {
2831
type: "layout",
2932
fixable: "code",
33+
schema: [
34+
{
35+
type: "object",
36+
properties: {
37+
sortSpreads: { type: "boolean" },
38+
},
39+
additionalProperties: false,
40+
},
41+
],
3042
messages: {
3143
sortComponents: "Component names must be sorted.",
3244
},
3345
},
3446
create(context) {
47+
const sortSpreads: boolean = context.options[0]?.sortSpreads ?? false;
48+
3549
return {
3650
Property(node) {
3751
const { value } = node;
@@ -43,7 +57,9 @@ export const sortComponentsRule: Rule.RuleModule = {
4357
}
4458

4559
const { properties } = value;
46-
const sorted = [...properties].sort(compareNodes);
60+
const sorted = [...properties].sort((a, b) =>
61+
compareNodes(a, b, sortSpreads)
62+
);
4763
const sameOrder = properties.every((v, i) => v === sorted[i]);
4864

4965
if (sameOrder) {

Diff for: tests/rules/vue-sort-components.spec.ts

+33-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,20 @@ ruleTester.run("vue-sort-components", sortComponentsRule, {
3030
code: "const obj = { components: { ...others, bar, baz, foo } }",
3131
parserOptions: { ecmaVersion: 2018 },
3232
},
33-
// not components
33+
34+
// options.sortSpreads
35+
{
36+
code: "const obj = { components: { ...others2, ...others1, bar, baz, foo } }",
37+
parserOptions: { ecmaVersion: 2018 },
38+
},
39+
{
40+
options: [{ sortSpreads: true }],
41+
code: "const obj = { components: { ...others1, ...others2, bar, baz, foo } }",
42+
parserOptions: { ecmaVersion: 2018 },
43+
},
44+
45+
// not applied
46+
// non-component
3447
{
3548
code: "const obj = { nested: { foo, bar } }",
3649
parserOptions: { ecmaVersion: 6 },
@@ -65,8 +78,25 @@ ruleTester.run("vue-sort-components", sortComponentsRule, {
6578
},
6679
// spread
6780
{
68-
code: "const obj = { components: { bar, ...others, foo, ...others2 } }",
69-
output: "const obj = { components: { ...others, ...others2, bar, foo } }",
81+
code: "const obj = { components: { bar, ...others, foo } }",
82+
output: "const obj = { components: { ...others, bar, foo } }",
83+
parserOptions: { ecmaVersion: 2018 },
84+
errors: [{ messageId: "sortComponents" }],
85+
},
86+
87+
// options.sortSpreads
88+
{
89+
code: "const obj = { components: { bar, ...others2, foo, ...others1 } }",
90+
output:
91+
"const obj = { components: { ...others2, ...others1, bar, foo } }",
92+
parserOptions: { ecmaVersion: 2018 },
93+
errors: [{ messageId: "sortComponents" }],
94+
},
95+
{
96+
options: [{ sortSpreads: true }],
97+
code: "const obj = { components: { bar, ...others2, foo, ...others1 } }",
98+
output:
99+
"const obj = { components: { ...others1, ...others2, bar, foo } }",
70100
parserOptions: { ecmaVersion: 2018 },
71101
errors: [{ messageId: "sortComponents" }],
72102
},

0 commit comments

Comments
 (0)