Skip to content

Commit

Permalink
🎉 add new rule alphabetize (#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitri POSTOLOV committed Oct 20, 2021
1 parent 12ee95a commit c7a8b33
Show file tree
Hide file tree
Showing 26 changed files with 800 additions and 127 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-squids-burn.md
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': minor
---

add new rule `alphabetize`
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -10,6 +10,7 @@ module.exports = {
'no-prototype-builtins': 'off',
'no-restricted-globals': ['error', { name: 'isNaN', message: 'Use Number.isNaN instead' }],
'no-useless-constructor': 'off',
'object-shorthand': ['error', 'always'],
'no-unused-vars': 'off', // disable base rule as it can report incorrect errors
'@typescript-eslint/no-unused-vars': ['warn', { args: 'none' }],
'@typescript-eslint/no-use-before-define': 'off',
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Expand Up @@ -11,6 +11,7 @@ Each rule has emojis denoting:
<!-- prettier-ignore-start -->
Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|Description|🚀&nbsp;/&nbsp;🔮|🔧|✅
-|-|:-:|-|-
[alphabetize](rules/alphabetize.md)|Enforce arrange in alphabetical order for type fields, enum values, input object fields, operation selections and more.|🚀||
[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.|🚀||
[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.|🚀||
[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.|🚀||
Expand Down
140 changes: 140 additions & 0 deletions docs/rules/alphabetize.md
@@ -0,0 +1,140 @@
# `alphabetize`

- Category: `Best Practices`
- Rule name: `@graphql-eslint/alphabetize`
- Requires GraphQL Schema: `false` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema)
- Requires GraphQL Operations: `false` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations)

Enforce arrange in alphabetical order for type fields, enum values, input object fields, operation selections and more.

## Usage Examples

### Incorrect

```graphql
# eslint @graphql-eslint/alphabetize: ['error', { fields: ['ObjectTypeDefinition'] }]

type User {
password: String
firstName: String! # should be before "password"
age: Int # should be before "firstName"
lastName: String!
}
```

### Correct

```graphql
# eslint @graphql-eslint/alphabetize: ['error', { fields: ['ObjectTypeDefinition'] }]

type User {
age: Int
firstName: String!
lastName: String!
password: String
}
```

### Incorrect

```graphql
# eslint @graphql-eslint/alphabetize: ['error', { values: ['EnumTypeDefinition'] }]

enum Role {
SUPER_ADMIN
ADMIN # should be before "SUPER_ADMIN"
USER
GOD # should be before "USER"
}
```

### Correct

```graphql
# eslint @graphql-eslint/alphabetize: ['error', { values: ['EnumTypeDefinition'] }]

enum Role {
ADMIN
GOD
SUPER_ADMIN
USER
}
```

### Incorrect

```graphql
# eslint @graphql-eslint/alphabetize: ['error', { selections: ['OperationDefinition'] }]

query {
me {
firstName
lastName
email # should be before "lastName"
}
}
```

### Correct

```graphql
# eslint @graphql-eslint/alphabetize: ['error', { selections: ['OperationDefinition'] }]

query {
me {
email
firstName
lastName
}
}
```

## Config Schema

The schema defines the following properties:

### `fields` (array)

Fields of `type`, `interface`, and `input`.

The elements of the array must contain the following properties:

- `ObjectTypeDefinition`
- `InterfaceTypeDefinition`
- `InputObjectTypeDefinition`

### `values` (array)

Values of `enum`.

The elements of the array must contain the following properties:

- `EnumTypeDefinition`

### `selections` (array)

Selections of operations (`query`, `mutation` and `subscription`) and `fragment`.

The elements of the array must contain the following properties:

- `OperationDefinition`
- `FragmentDefinition`

### `variables` (array)

Variables of operations (`query`, `mutation` and `subscription`).

The elements of the array must contain the following properties:

- `OperationDefinition`

### `arguments` (array)

Arguments of fields and directives.

The elements of the array must contain the following properties:

- `FieldDefinition`
- `Field`
- `DirectiveDefinition`
- `Directive`
10 changes: 3 additions & 7 deletions docs/rules/avoid-operation-name-prefix.md
Expand Up @@ -31,17 +31,13 @@ query userDetails {

## Config Schema

### (array)
The schema defines the following properties:

The schema defines an array with all elements of the type `object`.

The array object has the following properties:

#### `caseSensitive` (boolean)
### `caseSensitive` (boolean)

Default: `false`

#### `keywords` (array, required)
### `keywords` (array, required)

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

Expand Down
12 changes: 4 additions & 8 deletions docs/rules/description-style.md
Expand Up @@ -16,7 +16,7 @@ Require all comments to follow the same style (either block or inline).

""" Description """
type someTypeName {
...
# ...
}
```

Expand All @@ -27,19 +27,15 @@ type someTypeName {

" Description "
type someTypeName {
...
# ...
}
```

## Config Schema

### (array)
The schema defines the following properties:

The schema defines an array with all elements of the type `object`.

The array object has the following properties:

#### `style` (string, enum)
### `style` (string, enum)

This element must be one of the following enum values:

Expand Down
14 changes: 5 additions & 9 deletions docs/rules/input-name.md
Expand Up @@ -42,31 +42,27 @@ type Mutation {

## Config Schema

### (array)
The schema defines the following properties:

The schema defines an array with all elements of the type `object`.

The array object has the following properties:

#### `checkInputType` (boolean)
### `checkInputType` (boolean)

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

Default: `false`

#### `caseSensitiveInputType` (boolean)
### `caseSensitiveInputType` (boolean)

Allow for case discrepancies in the input type name

Default: `true`

#### `checkQueries` (boolean)
### `checkQueries` (boolean)

Apply the rule to Queries

Default: `false`

#### `checkMutations` (boolean)
### `checkMutations` (boolean)

Apply the rule to Mutations

Expand Down
16 changes: 6 additions & 10 deletions docs/rules/match-document-filename.md
Expand Up @@ -86,41 +86,37 @@ query UserById {

## Config Schema

### (array)
The schema defines the following properties:

The schema defines an array with all elements of the type `object`.

The array object has the following properties:

#### `fileExtension` (string, enum)
### `fileExtension` (string, enum)

This element must be one of the following enum values:

* `.gql`
* `.graphql`

#### `query`
### `query`

The object must be one of the following types:

* `asString`
* `asObject`

#### `mutation`
### `mutation`

The object must be one of the following types:

* `asString`
* `asObject`

#### `subscription`
### `subscription`

The object must be one of the following types:

* `asString`
* `asObject`

#### `fragment`
### `fragment`

The object must be one of the following types:

Expand Down

0 comments on commit c7a8b33

Please sign in to comment.