|
| 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) |
0 commit comments