Skip to content

Commit 2285dbd

Browse files
authored
Add option to jsonc/sort-keys rule that allows to specify order of properties with array. (#70)
* Add option to `jsonc/sort-keys` rule that allows to specify order of properties with array. * update * update doc
1 parent 5d3a676 commit 2285dbd

File tree

5 files changed

+489
-49
lines changed

5 files changed

+489
-49
lines changed

docs/rules/key-name-casing.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ This rule enforces a naming convention to property key names.
3535

3636
## :wrench: Options
3737

38-
Nothing.
39-
4038
```json5
4139
{
4240
"jsonc/key-name-casing": ["error",

docs/rules/sort-keys.md

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ since: "v0.1.0"
1313

1414
## :book: Rule Details
1515

16-
This rule checks all property definitions of object expressions and verifies that all variables are sorted alphabetically.
16+
This rule checks all property definitions of object and verifies that all properties are sorted alphabetically or specified order.
1717

1818
<eslint-code-block fix>
1919

@@ -34,7 +34,82 @@ This rule checks all property definitions of object expressions and verifies tha
3434

3535
## :wrench: Options
3636

37-
Same as [sort-keys] rule option. See [here](https://eslint.org/docs/rules/sort-keys#options) for details.
37+
```json5
38+
{
39+
"jsonc/sort-keys": ["error",
40+
// For example, a definition for package.json
41+
{
42+
"pathPattern": "^$", // Hits the root properties
43+
"order": [
44+
"name",
45+
"version",
46+
"private",
47+
"publishConfig"
48+
// ...
49+
]
50+
},
51+
{
52+
"pathPattern": "^(?:dev|peer|optional|bundled)?[Dd]ependencies$",
53+
"order": { "type": "asc" }
54+
}
55+
// ...
56+
]
57+
}
58+
```
59+
60+
```json5
61+
{
62+
"jsonc/sort-keys": ["error",
63+
// For example, a definition for JSON Schema
64+
{
65+
"pathPattern": ".*", // Hits the all properties
66+
"hasProperties": ["type"],
67+
"order": [
68+
"type",
69+
"properties",
70+
"items",
71+
"required",
72+
"minItems",
73+
"additionalProperties",
74+
"additionalItems"
75+
// ...
76+
]
77+
}
78+
// ...
79+
]
80+
}
81+
```
82+
83+
The option receives multiple objects with the following properties:
84+
85+
- `pathPattern` (Required) ... Defines the regular expression pattern of paths to which you want to enforce the order. If you want to apply to the top level, define `"^$"`.
86+
- `hasProperties` ... Defines an array of property names. Checks only objects that have the defined properties.
87+
- `order` (Required) ... Defines how to enforce the order. You can use an object or an array.
88+
- Array ... Defines an array of properties to enforce the order.
89+
- Object ... The object has the following properties:
90+
- `type`:
91+
- `"asc"` ... Enforce properties to be in ascending order. This is default.
92+
- `"desc"` ... Enforce properties to be in descending order.
93+
- `caseSensitive` ... If `true`, enforce properties to be in case-sensitive order. Default is `true`.
94+
- `natural` ... If `true`, enforce properties to be in natural order. Default is `false`.
95+
- `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.
96+
97+
You can also define options in the same format as the [sort-keys] rule.
98+
99+
```json5
100+
{
101+
"jsonc/sort-keys": ["error",
102+
"asc",
103+
{
104+
"caseSensitive": true,
105+
"natural": false,
106+
"minKeys": 2
107+
}
108+
]
109+
}
110+
```
111+
112+
See [here](https://eslint.org/docs/rules/sort-keys#options) for details.
38113

39114
## :couple: Related rules
40115

0 commit comments

Comments
 (0)