Skip to content

Commit

Permalink
feat!: update ESLint to flat config (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
targos authored Jul 11, 2024
1 parent 3e90b3d commit 982b5e4
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 146 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ jobs:
# Documentation: https://github.com/zakodium/workflows#nodejs-ci
uses: zakodium/workflows/.github/workflows/nodejs.yml@nodejs-v1
with:
lint-eslint: false
lint-prettier: false
node-version-matrix: '[20]'
node-version-matrix: '[22]'
4 changes: 2 additions & 2 deletions .graphqlconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"schema": "./test/schema/**/*.graphql",
"documents": "./test/operations/**/*.graphql"
"schema": "./test/schema/**/*.{gql,graphql}",
"documents": "./test/operations/**/*.{gql,graphql}"
}
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CHANGELOG.md
52 changes: 31 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# eslint-config-graphql
# @zakodium/eslint-config-graphql

Shared ESLint config for frontend and backend projects using graphql
Shared ESLint config for frontend and backend projects using GraphQL

## Installation

```console
npx i -D @zakodium/eslint-config-graphql eslint
npm i -D @zakodium/eslint-config-graphql eslint
```

`graphql` is also a peer dependency and would usually be in the dependencies of your project
Expand All @@ -16,27 +16,37 @@ npm i graphql

## Usage

Create a `.eslintrc.yml` with the following contents:
Create a `eslint.config.mjs` with the following contents:

```yml
extends: zakodium-graphql
```
Create a `.graphqlrc` or `.graphqlconfig` file with your graphql configuration

Or alternatively, specify the options in the eslint config
```js
import graphql from '@zakodium/eslint-config-graphql';

```yml
extends: zakodium-graphql
overrides:
files:
- *.graphql
parserOptions:
schema: path/to/your/schema/**/*.graphql
operations path/to/your/operations/**/*.graphql
export default [
// You will probably extend other configs as well.
...graphql,
];
```

Create a `.graphqlrc` or `.graphqlconfig` file with your GraphQL configuration

Or alternatively, specify the options in the ESLint config:

```js
import graphql from '@zakodium/eslint-config-graphql';

export default [
// You will probably extend other configs as well.
...graphql,
{
files: ['**/*.{gql,graphql}'],
languageOptions: {
parserOptions: {
schema: 'path/to/your/schema/**/*.{gql,graphql}',
operations: 'path/to/your/operations/**/*.{gql,graphql}',
},
},
},
];
```

You can then customize the config for your project by changing rules in this file.


5 changes: 5 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import cheminfo from 'eslint-config-cheminfo';

import graphql from './index.js';

export default [...cheminfo, ...graphql];
188 changes: 96 additions & 92 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,104 @@
module.exports = {
overrides: [
{
files: ['*.graphql', '*.gql'],
parser: '@graphql-eslint/eslint-plugin',
plugins: ['@graphql-eslint'],
rules: {
'@graphql-eslint/alphabetize': 0,
// Enforce all comments to be block comments
'@graphql-eslint/description-style': 0,
'@graphql-eslint/executable-definitions': 0,
'@graphql-eslint/fields-on-correct-type': 2,
'@graphql-eslint/fragments-on-composite-type': 2,
// This rule is deactivated because it is not compatible with
// naming input types PascalCase but mutations camelCase
// https://github.com/dotansimha/graphql-eslint/blob/a2b71f6be17ff57614f57b3648ae2256cc834ea9/docs/rules/input-name.md
'@graphql-eslint/input-name': 0,
'@graphql-eslint/known-argument-names': 2,
'@graphql-eslint/known-directives': 2,
'@graphql-eslint/known-fragment-names': 2,
'@graphql-eslint/known-type-names': 2,
'@graphql-eslint/lone-anonymous-operation': 2,
'@graphql-eslint/lone-schema-definition': 2,
'@graphql-eslint/match-document-filename': 0,
'@graphql-eslint/naming-convention': [
'error',
{
ObjectTypeDefinition: 'PascalCase',
FieldDefinition: 'camelCase',
InputObjectTypeDefinition: 'PascalCase',
InputValueDefinition: 'camelCase',
EnumTypeDefinition: 'PascalCase',
EnumValueDefinition: 'UPPER_CASE',
InterfaceTypeDefinition: 'PascalCase',
UnionTypeDefinition: 'PascalCase',
ScalarTypeDefinition: 'PascalCase',
OperationDefinition: 'PascalCase',
Argument: 'camelCase',
FragmentDefinition: 'camelCase',
allowLeadingUnderscore: true,
allowTrailingUnderscore: false,
},
],
import * as graphqlEslint from '@graphql-eslint/eslint-plugin';

'@graphql-eslint/no-anonymous-operations': 2,
'@graphql-eslint/no-case-insensitive-enum-values-duplicates': 2,
'@graphql-eslint/no-deprecated': 1,
'@graphql-eslint/no-duplicate-fields': 2,
'@graphql-eslint/no-hashtag-description': 2,
'@graphql-eslint/no-fragment-cycles': 2,
'@graphql-eslint/no-root-type': 0,
// What errors does this rule prevent?
'@graphql-eslint/no-scalar-result-type-on-mutation': 0,
'@graphql-eslint/no-typename-prefix': 2,
'@graphql-eslint/no-undefined-variables': 2,
'@graphql-eslint/no-unreachable-types': 2,
'@graphql-eslint/no-unused-fields': 1,
'@graphql-eslint/no-unused-fragments': 2,
'@graphql-eslint/no-unused-variables': 2,
export default [
{
files: ['**/*.{gql,graphql}'],
plugins: {
'@graphql-eslint': graphqlEslint,
},
languageOptions: {
parser: graphqlEslint,
},
rules: {
'@graphql-eslint/alphabetize': 'off',
// Enforce all comments to be block comments
'@graphql-eslint/description-style': 'off',
'@graphql-eslint/executable-definitions': 'off',
'@graphql-eslint/fields-on-correct-type': 'error',
'@graphql-eslint/fragments-on-composite-type': 'error',
// This rule is deactivated because it is not compatible with
// naming input types PascalCase but mutations camelCase
// https://github.com/dotansimha/graphql-eslint/blob/a2b71f6be17ff57614f57b3648ae2256cc834ea9/docs/rules/input-name.md
'@graphql-eslint/input-name': 'off',
'@graphql-eslint/known-argument-names': 'error',
'@graphql-eslint/known-directives': 'error',
'@graphql-eslint/known-fragment-names': 'error',
'@graphql-eslint/known-type-names': 'error',
'@graphql-eslint/lone-anonymous-operation': 'error',
'@graphql-eslint/lone-schema-definition': 'error',
'@graphql-eslint/match-document-filename': 'off',
'@graphql-eslint/naming-convention': [
'error',
{
ObjectTypeDefinition: 'PascalCase',
FieldDefinition: 'camelCase',
InputObjectTypeDefinition: 'PascalCase',
InputValueDefinition: 'camelCase',
EnumTypeDefinition: 'PascalCase',
EnumValueDefinition: 'UPPER_CASE',
InterfaceTypeDefinition: 'PascalCase',
UnionTypeDefinition: 'PascalCase',
ScalarTypeDefinition: 'PascalCase',
OperationDefinition: 'PascalCase',
Argument: 'camelCase',
FragmentDefinition: 'camelCase',
allowLeadingUnderscore: true,
allowTrailingUnderscore: false,
},
],

'@graphql-eslint/no-anonymous-operations': 'error',
'@graphql-eslint/no-case-insensitive-enum-values-duplicates': 'error',
'@graphql-eslint/no-deprecated': 'warn',
'@graphql-eslint/no-duplicate-fields': 'error',
'@graphql-eslint/no-hashtag-description': 'error',
'@graphql-eslint/no-fragment-cycles': 'error',
'@graphql-eslint/no-root-type': 'off',
// What errors does this rule prevent?
'@graphql-eslint/no-scalar-result-type-on-mutation': 'off',
'@graphql-eslint/no-typename-prefix': 'error',
'@graphql-eslint/no-undefined-variables': 'error',
'@graphql-eslint/no-unreachable-types': 'error',
'@graphql-eslint/no-unused-fields': 'warn',
'@graphql-eslint/no-unused-fragments': 'error',
'@graphql-eslint/no-unused-variables': 'error',

'@graphql-eslint/overlapping-fields-can-be-merged': 2,
'@graphql-eslint/possible-fragment-spread': 2,
'@graphql-eslint/possible-type-extension': 2,
'@graphql-eslint/provided-required-arguments': 2,
'@graphql-eslint/overlapping-fields-can-be-merged': 'error',
'@graphql-eslint/possible-fragment-spread': 'error',
'@graphql-eslint/possible-type-extension': 'error',
'@graphql-eslint/provided-required-arguments': 'error',

'@graphql-eslint/relay-arguments': 0,
'@graphql-eslint/relay-connection-types ': 0,
'@graphql-eslint/relay-edge-types': 0,
'@graphql-eslint/relay-page-info': 0,
'@graphql-eslint/relay-arguments': 'off',
'@graphql-eslint/relay-connection-types ': 'off',
'@graphql-eslint/relay-edge-types': 'off',
'@graphql-eslint/relay-page-info': 'off',

'@graphql-eslint/require-deprecation-date': 0,
'@graphql-eslint/require-deprecation-reason': 2,
'@graphql-eslint/require-description': 0,
'@graphql-eslint/require-field-of-type-query-in-mutation-result': 0,
'@graphql-eslint/require-id-when-available': 2,
'@graphql-eslint/require-deprecation-date': 'off',
'@graphql-eslint/require-deprecation-reason': 'error',
'@graphql-eslint/require-description': 'off',
'@graphql-eslint/require-field-of-type-query-in-mutation-result': 'off',
'@graphql-eslint/require-id-when-available': 'error',

'@graphql-eslint/scalar-leafs': 2,
'@graphql-eslint/selection-set-depth': 0,
'@graphql-eslint/strict-id-in-types': 0,
'@graphql-eslint/scalar-leafs': 'error',
'@graphql-eslint/selection-set-depth': 'off',
'@graphql-eslint/strict-id-in-types': 'off',

'@graphql-eslint/unique-argument-names': 2,
'@graphql-eslint/unique-directive-names': 2,
'@graphql-eslint/unique-directive-names-per-location': 2,
'@graphql-eslint/unique-enum-value-names': 2,
'@graphql-eslint/unique-field-definition-names': 2,
'@graphql-eslint/unique-fragment-name': 2,
'@graphql-eslint/unique-input-field-names': 2,
'@graphql-eslint/unique-operation-name': 2,
'@graphql-eslint/unique-operation-types': 2,
'@graphql-eslint/unique-type-names': 2,
'@graphql-eslint/unique-variable-names': 2,
'@graphql-eslint/unique-argument-names': 'error',
'@graphql-eslint/unique-directive-names': 'error',
'@graphql-eslint/unique-directive-names-per-location': 'error',
'@graphql-eslint/unique-enum-value-names': 'error',
'@graphql-eslint/unique-field-definition-names': 'error',
'@graphql-eslint/unique-fragment-name': 'error',
'@graphql-eslint/unique-input-field-names': 'error',
'@graphql-eslint/unique-operation-name': 'error',
'@graphql-eslint/unique-operation-types': 'error',
'@graphql-eslint/unique-type-names': 'error',
'@graphql-eslint/unique-variable-names': 'error',

'@graphql-eslint/value-literals-of-correct-type': 2,
'@graphql-eslint/variables-are-input-types': 2,
'@graphql-eslint/variables-in-allowed-position': 2,
strict: 0,
},
'@graphql-eslint/value-literals-of-correct-type': 'error',
'@graphql-eslint/variables-are-input-types': 'error',
'@graphql-eslint/variables-in-allowed-position': 'error',
strict: 'off',
},
],
};
},
];
25 changes: 17 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,31 @@
"name": "@zakodium/eslint-config-graphql",
"version": "5.1.0",
"description": "ESLint config for GraphQL files",
"main": "index.js",
"type": "module",
"exports": {
".": "./index.js"
},
"license": "MIT",
"repository": "https://github.com/zakodium/eslint-config-graphql",
"scripts": {
"test": "npm run test-only",
"test-only": "node test/test.mjs"
"eslint": "eslint .",
"eslint-fix": "npm run eslint -- --fix",
"prettier": "prettier --check .",
"prettier-write": "prettier --write .",
"test": "npm run test-only && npm run eslint && npm run prettier",
"test-only": "node test/test.js"
},
"dependencies": {
"@graphql-eslint/eslint-plugin": "^3.20.1"
},
"peerDependencies": {
"@graphql-eslint/eslint-plugin": "^3.20.1",
"eslint": "^8.55.0",
"eslint": "^8.57.0",
"graphql": "^16.8.1"
},
"devDependencies": {
"@graphql-eslint/eslint-plugin": "3.20.1",
"eslint": "8.55.0",
"eslint": "8.57.0",
"eslint-config-cheminfo": "^11.0.3",
"graphql": "16.8.1",
"prettier": "^3.1.0"
"prettier": "^3.3.2"
}
}
File renamed without changes.
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import assert from 'node:assert';

import { loadESLint } from 'eslint';

const ESLint = await loadESLint({ useFlatConfig: true });
const eslint = new ESLint();
const formatter = await eslint.loadFormatter('stylish');

const result = await eslint.lintFiles(['test/**/*.{gql,graphql}']);

assert.strictEqual(result.length, 3, '3 graphql files should be linted');

const errorCount = result.reduce(
(prev, current) => prev + current.errorCount,
0,
);
assert.strictEqual(
errorCount,
0,
`graphql files should not have any errors: ${formatter.format(result)}`,
);
20 changes: 0 additions & 20 deletions test/test.mjs

This file was deleted.

0 comments on commit 982b5e4

Please sign in to comment.