Skip to content

Commit 7608dea

Browse files
loren138ota-meshi
andauthored
New Rule vue/sort-keys (#997)
* sort keys * fix for eslint 6.7 * ignore children/grandchildren * more finegrained parent analysis * expand the test * Update docs/rules/sort-keys.md Co-Authored-By: Yosuke Ota <[email protected]> Co-authored-by: Yosuke Ota <[email protected]>
1 parent fe190dc commit 7608dea

File tree

7 files changed

+1573
-1
lines changed

7 files changed

+1573
-1
lines changed

.eslintignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/coverage
33
/node_modules
44
/tests/fixtures
5-
/tests/integrations/*/node_modules
5+
/tests/integrations/eslint-plugin-import
66

77
!.vuepress
88
/docs/.vuepress/dist

docs/rules/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ For example:
168168
| [vue/require-direct-export](./require-direct-export.md) | require the component to be directly exported | |
169169
| [vue/require-name-property](./require-name-property.md) | require a name property in Vue components | |
170170
| [vue/script-indent](./script-indent.md) | enforce consistent indentation in `<script>` | :wrench: |
171+
| [vue/sort-keys](./sort-keys.md) | enforce sort-keys within components after the top level details | |
171172
| [vue/space-infix-ops](./space-infix-ops.md) | require spacing around infix operators | :wrench: |
172173
| [vue/space-unary-ops](./space-unary-ops.md) | enforce consistent spacing before or after unary operators | :wrench: |
173174
| [vue/static-class-names-order](./static-class-names-order.md) | enforce static class names order | :wrench: |

docs/rules/sort-keys.md

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
pageClass: rule-details
3+
sidebarDepth: 0
4+
title: vue/sort-keys
5+
description: enforce sort-keys in a manner that is compatible with order-in-components
6+
---
7+
# vue/sort-keys
8+
> enforce sort-keys within components after the top level details
9+
10+
This rule is almost the same rule as core [sorts-keys] rule but it will not error on top component properties allowing that order to be enforced with `order-in-components`.
11+
12+
## Options
13+
14+
```json
15+
{
16+
"sort-keys": ["error", "asc", {
17+
"caseSensitive": true,
18+
"ignoreChildrenOf": ["model"],
19+
"ignoreGrandchildrenOf": ["computed", "directives", "inject", "props", "watch"],
20+
"minKeys": 2,
21+
"natural": false
22+
}]
23+
}
24+
```
25+
26+
The 1st option is `"asc"` or `"desc"`.
27+
28+
* `"asc"` (default) - enforce properties to be in ascending order.
29+
* `"desc"` - enforce properties to be in descending order.
30+
31+
The 2nd option is an object which has 5 properties.
32+
33+
* `caseSensitive` - if `true`, enforce properties to be in case-sensitive order. Default is `true`.
34+
* `ignoreChildrenOf` - an array of properties to ignore the children of. Default is `["model"]`
35+
* `ignoreGrandchildrenOf` - an array of properties to ignore the grandchildren sort order. Default is `["computed", "directives", "inject", "props", "watch"]`
36+
* `minKeys` - Specifies the minimum number of keys that an object should have in order for the object's unsorted keys to produce an error. Default is `2`, which means by default all objects with unsorted keys will result in lint errors.
37+
* `natural` - if `true`, enforce properties to be in natural order. Default is `false`. Natural Order compares strings containing combination of letters and numbers in the way a human being would sort. It basically sorts numerically, instead of sorting alphabetically. So the number 10 comes after the number 3 in Natural Sorting.
38+
39+
While using this rule, you may disable the normal `sort-keys` rule. This rule will apply to plain js files as well as Vue component scripts.
40+
41+
## :book: Rule Details
42+
43+
This rule forces many dictionary properties to be in alphabetical order while allowing `order-in-components`, property details,
44+
and other similar fields not to be in alphabetical order.
45+
46+
<eslint-code-block fix :rules="{'vue/sort-keys': ['error']}">
47+
48+
```vue
49+
<script>
50+
/* ✓ GOOD */
51+
export default {
52+
name: 'app',
53+
model: {
54+
prop: 'checked',
55+
event: 'change'
56+
},
57+
props: {
58+
propA: {
59+
type: String,
60+
default: 'abc',
61+
},
62+
propB: {
63+
type: String,
64+
default: 'abc',
65+
},
66+
},
67+
}
68+
</script>
69+
```
70+
71+
</eslint-code-block>
72+
73+
<eslint-code-block fix :rules="{'vue/sort-keys': ['error']}">
74+
75+
```vue
76+
<script>
77+
/* ✗ BAD */
78+
export default {
79+
name: 'app',
80+
model: {
81+
prop: 'checked',
82+
event: 'change'
83+
},
84+
props: {
85+
propB: {
86+
type: String,
87+
default: 'abc',
88+
},
89+
propA: {
90+
type: String,
91+
default: 'abc',
92+
},
93+
},
94+
}
95+
</script>
96+
```
97+
98+
</eslint-code-block>
99+
100+
## :books: Further reading
101+
102+
- [sorts-keys]
103+
104+
[sort-keys]: https://eslint.org/docs/rules/sort-keys
105+
106+
## :mag: Implementation
107+
108+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/sort-keys.js)
109+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/sort-keys.js)

lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ module.exports = {
7878
'return-in-computed-property': require('./rules/return-in-computed-property'),
7979
'script-indent': require('./rules/script-indent'),
8080
'singleline-html-element-content-newline': require('./rules/singleline-html-element-content-newline'),
81+
'sort-keys': require('./rules/sort-keys'),
8182
'space-infix-ops': require('./rules/space-infix-ops'),
8283
'space-unary-ops': require('./rules/space-unary-ops'),
8384
'static-class-names-order': require('./rules/static-class-names-order'),

0 commit comments

Comments
 (0)