Skip to content

Commit c7a8b33

Browse files
author
Dimitri POSTOLOV
authored
🎉 add new rule alphabetize (#646)
1 parent 12ee95a commit c7a8b33

26 files changed

+800
-127
lines changed

.changeset/ten-squids-burn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': minor
3+
---
4+
5+
add new rule `alphabetize`

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010
'no-prototype-builtins': 'off',
1111
'no-restricted-globals': ['error', { name: 'isNaN', message: 'Use Number.isNaN instead' }],
1212
'no-useless-constructor': 'off',
13+
'object-shorthand': ['error', 'always'],
1314
'no-unused-vars': 'off', // disable base rule as it can report incorrect errors
1415
'@typescript-eslint/no-unused-vars': ['warn', { args: 'none' }],
1516
'@typescript-eslint/no-use-before-define': 'off',

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Each rule has emojis denoting:
1111
<!-- prettier-ignore-start -->
1212
Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|Description|🚀&nbsp;/&nbsp;🔮|🔧|✅
1313
-|-|:-:|-|-
14+
[alphabetize](rules/alphabetize.md)|Enforce arrange in alphabetical order for type fields, enum values, input object fields, operation selections and more.|🚀||
1415
[avoid-duplicate-fields](rules/avoid-duplicate-fields.md)|Checks for duplicate fields in selection set, variables in operation definition, or in arguments set of a field.|🚀||
1516
[avoid-operation-name-prefix](rules/avoid-operation-name-prefix.md)|Enforce/avoid operation name prefix, useful if you wish to avoid prefix in your root fields, or avoid using REST terminology in your schema.|🚀||
1617
[avoid-scalar-result-type-on-mutation](rules/avoid-scalar-result-type-on-mutation.md)|Avoid scalar result type on mutation type to make sure to return a valid state.|🚀||

docs/rules/alphabetize.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# `alphabetize`
2+
3+
- Category: `Best Practices`
4+
- Rule name: `@graphql-eslint/alphabetize`
5+
- Requires GraphQL Schema: `false` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema)
6+
- Requires GraphQL Operations: `false` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations)
7+
8+
Enforce arrange in alphabetical order for type fields, enum values, input object fields, operation selections and more.
9+
10+
## Usage Examples
11+
12+
### Incorrect
13+
14+
```graphql
15+
# eslint @graphql-eslint/alphabetize: ['error', { fields: ['ObjectTypeDefinition'] }]
16+
17+
type User {
18+
password: String
19+
firstName: String! # should be before "password"
20+
age: Int # should be before "firstName"
21+
lastName: String!
22+
}
23+
```
24+
25+
### Correct
26+
27+
```graphql
28+
# eslint @graphql-eslint/alphabetize: ['error', { fields: ['ObjectTypeDefinition'] }]
29+
30+
type User {
31+
age: Int
32+
firstName: String!
33+
lastName: String!
34+
password: String
35+
}
36+
```
37+
38+
### Incorrect
39+
40+
```graphql
41+
# eslint @graphql-eslint/alphabetize: ['error', { values: ['EnumTypeDefinition'] }]
42+
43+
enum Role {
44+
SUPER_ADMIN
45+
ADMIN # should be before "SUPER_ADMIN"
46+
USER
47+
GOD # should be before "USER"
48+
}
49+
```
50+
51+
### Correct
52+
53+
```graphql
54+
# eslint @graphql-eslint/alphabetize: ['error', { values: ['EnumTypeDefinition'] }]
55+
56+
enum Role {
57+
ADMIN
58+
GOD
59+
SUPER_ADMIN
60+
USER
61+
}
62+
```
63+
64+
### Incorrect
65+
66+
```graphql
67+
# eslint @graphql-eslint/alphabetize: ['error', { selections: ['OperationDefinition'] }]
68+
69+
query {
70+
me {
71+
firstName
72+
lastName
73+
email # should be before "lastName"
74+
}
75+
}
76+
```
77+
78+
### Correct
79+
80+
```graphql
81+
# eslint @graphql-eslint/alphabetize: ['error', { selections: ['OperationDefinition'] }]
82+
83+
query {
84+
me {
85+
email
86+
firstName
87+
lastName
88+
}
89+
}
90+
```
91+
92+
## Config Schema
93+
94+
The schema defines the following properties:
95+
96+
### `fields` (array)
97+
98+
Fields of `type`, `interface`, and `input`.
99+
100+
The elements of the array must contain the following properties:
101+
102+
- `ObjectTypeDefinition`
103+
- `InterfaceTypeDefinition`
104+
- `InputObjectTypeDefinition`
105+
106+
### `values` (array)
107+
108+
Values of `enum`.
109+
110+
The elements of the array must contain the following properties:
111+
112+
- `EnumTypeDefinition`
113+
114+
### `selections` (array)
115+
116+
Selections of operations (`query`, `mutation` and `subscription`) and `fragment`.
117+
118+
The elements of the array must contain the following properties:
119+
120+
- `OperationDefinition`
121+
- `FragmentDefinition`
122+
123+
### `variables` (array)
124+
125+
Variables of operations (`query`, `mutation` and `subscription`).
126+
127+
The elements of the array must contain the following properties:
128+
129+
- `OperationDefinition`
130+
131+
### `arguments` (array)
132+
133+
Arguments of fields and directives.
134+
135+
The elements of the array must contain the following properties:
136+
137+
- `FieldDefinition`
138+
- `Field`
139+
- `DirectiveDefinition`
140+
- `Directive`

docs/rules/avoid-operation-name-prefix.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,13 @@ query userDetails {
3131

3232
## Config Schema
3333

34-
### (array)
34+
The schema defines the following properties:
3535

36-
The schema defines an array with all elements of the type `object`.
37-
38-
The array object has the following properties:
39-
40-
#### `caseSensitive` (boolean)
36+
### `caseSensitive` (boolean)
4137

4238
Default: `false`
4339

44-
#### `keywords` (array, required)
40+
### `keywords` (array, required)
4541

4642
The object is an array with all elements of the type `string`.
4743

docs/rules/description-style.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Require all comments to follow the same style (either block or inline).
1616

1717
""" Description """
1818
type someTypeName {
19-
...
19+
# ...
2020
}
2121
```
2222

@@ -27,19 +27,15 @@ type someTypeName {
2727

2828
" Description "
2929
type someTypeName {
30-
...
30+
# ...
3131
}
3232
```
3333

3434
## Config Schema
3535

36-
### (array)
36+
The schema defines the following properties:
3737

38-
The schema defines an array with all elements of the type `object`.
39-
40-
The array object has the following properties:
41-
42-
#### `style` (string, enum)
38+
### `style` (string, enum)
4339

4440
This element must be one of the following enum values:
4541

docs/rules/input-name.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,27 @@ type Mutation {
4242

4343
## Config Schema
4444

45-
### (array)
45+
The schema defines the following properties:
4646

47-
The schema defines an array with all elements of the type `object`.
48-
49-
The array object has the following properties:
50-
51-
#### `checkInputType` (boolean)
47+
### `checkInputType` (boolean)
5248

5349
Check that the input type name follows the convention <mutationName>Input
5450

5551
Default: `false`
5652

57-
#### `caseSensitiveInputType` (boolean)
53+
### `caseSensitiveInputType` (boolean)
5854

5955
Allow for case discrepancies in the input type name
6056

6157
Default: `true`
6258

63-
#### `checkQueries` (boolean)
59+
### `checkQueries` (boolean)
6460

6561
Apply the rule to Queries
6662

6763
Default: `false`
6864

69-
#### `checkMutations` (boolean)
65+
### `checkMutations` (boolean)
7066

7167
Apply the rule to Mutations
7268

docs/rules/match-document-filename.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,41 +86,37 @@ query UserById {
8686

8787
## Config Schema
8888

89-
### (array)
89+
The schema defines the following properties:
9090

91-
The schema defines an array with all elements of the type `object`.
92-
93-
The array object has the following properties:
94-
95-
#### `fileExtension` (string, enum)
91+
### `fileExtension` (string, enum)
9692

9793
This element must be one of the following enum values:
9894

9995
* `.gql`
10096
* `.graphql`
10197

102-
#### `query`
98+
### `query`
10399

104100
The object must be one of the following types:
105101

106102
* `asString`
107103
* `asObject`
108104

109-
#### `mutation`
105+
### `mutation`
110106

111107
The object must be one of the following types:
112108

113109
* `asString`
114110
* `asObject`
115111

116-
#### `subscription`
112+
### `subscription`
117113

118114
The object must be one of the following types:
119115

120116
* `asString`
121117
* `asObject`
122118

123-
#### `fragment`
119+
### `fragment`
124120

125121
The object must be one of the following types:
126122

0 commit comments

Comments
 (0)