Skip to content

Commit

Permalink
add new rules to recommended configs, rename relay config to `schem…
Browse files Browse the repository at this point in the history
…a-relay` (#1794)

* add

* more

* more

* more

* more

* rename

* rename

* fix tests
  • Loading branch information
dimaMachina authored Aug 1, 2023
1 parent 6593482 commit 4079167
Show file tree
Hide file tree
Showing 157 changed files with 137 additions and 95 deletions.
17 changes: 17 additions & 0 deletions .changeset/dull-scissors-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'@graphql-eslint/eslint-plugin': major
---

- bring back `possible-type-extension` rule to `schema-recommended` config

- add `unique-operation-name` and `unique-fragment-name` rules to `operations-recommended` config

The concept of sibling operations provided by graphql-config's `documents` fields is based on
uniquely named operations and fragments, for omitting false-positive/negative cases when operations
and fragments are located in separate files. For this reason, these rules must be included in the
recommended config

- rename `relay` config to `schema-relay`

> To avoid confusing when users extend this config for executable definitions (operations and
> fragments)
4 changes: 2 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
ignorePatterns: ['examples', 'packages/plugin/tests/__snapshots__'],
ignorePatterns: ['examples', 'packages/plugin/__tests__/__snapshots__'],
extends: [
'@theguild',
'@theguild/eslint-config/json',
Expand Down Expand Up @@ -55,7 +55,7 @@ module.exports = {
},
},
{
files: ['**/tests/mocks/**/*.{ts,js}'],
files: ['**/__tests__/mocks/**/*.{ts,js}'],
rules: {
'@typescript-eslint/no-unused-vars': 'off',
},
Expand Down
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dist/
packages/plugin/tests/__snapshots__/
packages/plugin/__tests__/__snapshots__/
examples/prettier/invalid.graphql
examples/prettier/invalid.js
pnpm-lock.yaml
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Rules', () => {
});

it('should load all rules properly from `relay` config', async () => {
const eslint = getESLintWithConfig(configs.relay, { documents: '{ foo }' });
const eslint = getESLintWithConfig(configs['schema-relay'], { documents: '{ foo }' });
const results = await eslint.lintText('{ foo }', { filePath: 'foo.graphql' });
expect(results).toHaveLength(1);
});
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 2 additions & 6 deletions packages/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"node": ">=18"
},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
"./package.json": "./package.json",
".": {
Expand All @@ -27,7 +26,6 @@
}
}
},
"typings": "dist/esm/index.d.mts",
"keywords": [
"eslint",
"eslintplugin",
Expand All @@ -39,6 +37,7 @@
"test": "vitest"
},
"peerDependencies": {
"eslint": "^8",
"graphql": "^16"
},
"dependencies": {
Expand Down Expand Up @@ -68,8 +67,5 @@
"directory": "dist",
"access": "public"
},
"sideEffects": false,
"typescript": {
"definition": "dist/cjs/index.d.ts"
}
"sideEffects": false
}
7 changes: 4 additions & 3 deletions packages/plugin/src/configs/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import operationsAllConfig from './operations-all.js';
import operationsRecommendedConfig from './operations-recommended.js';
import relayConfig from './relay.js';
import schemaAllConfig from './schema-all.js';
import schemaRecommendedConfig from './schema-recommended.js';
import relayConfig from './schema-relay.js';
import { ConfigName } from '../types.js';

export const configs = {
'schema-recommended': schemaRecommendedConfig,
'schema-all': schemaAllConfig,
'schema-relay': relayConfig,
'operations-recommended': operationsRecommendedConfig,
'operations-all': operationsAllConfig,
relay: relayConfig,
};
} satisfies Record<ConfigName, unknown>;
File renamed without changes.
7 changes: 4 additions & 3 deletions packages/plugin/src/flat-configs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { configs } from './configs/index.js';
import { parseForESLint } from './parser.js';
import { ConfigName } from './types.js';

const languageOptions = {
parser: { parseForESLint },
Expand All @@ -17,9 +18,9 @@ export const flatConfigs = {
languageOptions,
rules: configs['operations-recommended'].rules,
},
relay: {
'schema-relay': {
languageOptions,
rules: configs.relay.rules,
rules: configs['schema-relay'].rules,
},
'schema-all': {
languageOptions,
Expand All @@ -32,4 +33,4 @@ export const flatConfigs = {
languageOptions,
rules: configs['schema-recommended'].rules,
},
};
} satisfies Record<ConfigName, unknown>;
3 changes: 1 addition & 2 deletions packages/plugin/src/rules/graphql-js-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,7 @@ export const GRAPHQL_JS_VALIDATIONS: Record<string, GraphQLESLintRule> = Object.
{
category: 'Schema',
description: 'A type extension is only valid if the type is defined and has the same kind.',
// TODO: add in graphql-eslint v4
recommended: false,
recommended: true,
requiresSchema: true,
isDisabledForAllConfig: true,
},
Expand Down
1 change: 1 addition & 0 deletions packages/plugin/src/rules/unique-fragment-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const rule: GraphQLESLintRule = {
description: 'Enforce unique fragment names across your project.',
url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`,
requiresSiblings: true,
recommended: true,
examples: [
{
title: 'Incorrect',
Expand Down
1 change: 1 addition & 0 deletions packages/plugin/src/rules/unique-operation-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const rule: GraphQLESLintRule = {
description: 'Enforce unique operation names across your project.',
url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`,
requiresSiblings: true,
recommended: true,
examples: [
{
title: 'Incorrect',
Expand Down
7 changes: 7 additions & 0 deletions packages/plugin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,10 @@ export type OmitRecursively<T extends object, K extends PropertyKey> = Omit<
{ [P in keyof T]: OmitDistributive<T[P], K> },
K
>;

export type ConfigName =
| 'operations-all'
| 'operations-recommended'
| 'schema-all'
| 'schema-recommended'
| 'schema-relay';
2 changes: 1 addition & 1 deletion packages/plugin/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default defineConfig({
test: {
globals: true,
resolveSnapshotPath: testPath =>
testPath.replace('tests/', 'tests/__snapshots__/').replace(/\.ts$/, '.md'),
testPath.replace('__tests__/', '__tests__/__snapshots__/').replace(/\.ts$/, '.md'),
setupFiles: ['./serializer.ts'],
alias: {
'@graphql-eslint/eslint-plugin': 'src/index.ts',
Expand Down
2 changes: 1 addition & 1 deletion packages/rule-tester/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function applyFix(code: string, { range, text }: Rule.Fix): string {

export class RuleTester<ParserOptions> extends ESLintRuleTester {
fromMockFile(path: string): string {
return readFileSync(resolve(__dirname, `../../plugin/tests/mocks/${path}`), 'utf-8');
return readFileSync(resolve(__dirname, `../../plugin/__tests__/mocks/${path}`), 'utf-8');
}

// @ts-expect-error -- fix later
Expand Down
2 changes: 1 addition & 1 deletion scripts/create-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const rule: GraphQLESLintRule = {

await writeFile(RULE_PATH, RULE_CONTENT.trimStart());

const TEST_PATH = join(CWD, `packages/plugin/tests/${ruleId}.spec.ts`);
const TEST_PATH = join(CWD, `packages/plugin/__tests__/${ruleId}.spec.ts`);
const TEST_CONTENT = `
import { GraphQLRuleTester, ParserOptions } from '../src';
import { rule } from '../src/rules/${ruleId}';
Expand Down
5 changes: 3 additions & 2 deletions scripts/generate-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ async function generateDocs(): Promise<void> {
} else {
blocks.push(
`- [Rule source](https://github.com/B2o5T/graphql-eslint/tree/master/packages/plugin/src/rules/${ruleName}.ts)`,
`- [Test source](https://github.com/B2o5T/graphql-eslint/tree/master/packages/plugin/tests/${ruleName}.spec.ts)`,
`- [Test source](https://github.com/B2o5T/graphql-eslint/tree/master/packages/plugin/__tests__/${ruleName}.spec.ts)`,
);
}
return {
Expand Down Expand Up @@ -194,7 +194,8 @@ async function generateDocs(): Promise<void> {
`- ${Icon.FIXABLE} if some problems reported by the rule are automatically fixable by the \`--fix\` [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) option`,
`- ${Icon.HAS_SUGGESTIONS} if some problems reported by the rule are manually fixable by editor [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions)`,
BR,
// '<!-- 🚨 IMPORTANT! Do not manually modify this table. Run: `yarn generate:docs` -->',
'<!-- 🚨 IMPORTANT! Do not manually modify this table. Run: `yarn generate:docs` -->',
'<!-- prettier-ignore -->',
printMarkdownTable(
[
`Name${NBSP.repeat(20)}`,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
// ensure that nobody can accidentally use this config for a build
"noEmit": true
},
"include": ["scripts/*.ts", "packages/plugin/tests/**/*.ts", "vite.config.ts", "serializer.ts"],
"include": ["scripts/*.ts", "packages/plugin/__tests__/**/*.ts", "vite.config.ts", "serializer.ts"],
"exclude": []
}
13 changes: 10 additions & 3 deletions website/src/components/play-page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReactElement, useRef } from 'react';
import { flatConfigs, rules } from '@graphql-eslint/eslint-plugin';
import { flatConfigs, rules, ConfigName } from '@graphql-eslint/eslint-plugin';
import graphqlESLintPkgJson from '@graphql-eslint/eslint-plugin/package.json';
import { asArray } from '@graphql-tools/utils';
import { clsx } from 'clsx';
Expand All @@ -9,8 +9,15 @@ import { StringParam, useQueryParam, withDefault } from 'use-query-params';
import { GraphQLEditor } from './graphql-editor';
import { Select } from './select';

const schemaConfigs = ['schema-recommended', 'schema-all', 'relay'] as const;
const operationsConfigs = ['operations-recommended', 'operations-all'] as const;
const schemaConfigs: ReadonlyArray<ConfigName> = [
'schema-recommended',
'schema-all',
'schema-relay',
] as const;
const operationsConfigs: ReadonlyArray<ConfigName> = [
'operations-recommended',
'operations-all',
] as const;

const schemaRulesOptions = Object.entries(rules)
.filter(([, rule]) => asArray(rule.meta.docs!.category).includes('Schema'))
Expand Down
2 changes: 1 addition & 1 deletion website/src/pages/docs/configs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Callout } from '@theguild/components'
|[`schema-all`](https://github.com/B2o5T/graphql-eslint/tree/master/packages/plugin/src/configs/schema-all.ts)|enables all rules for schema (SDL) development, except for those that require `parserOptions.operations` option|
|[`operations-recommended`](https://github.com/B2o5T/graphql-eslint/tree/master/packages/plugin/src/configs/operations-recommended.ts) |enables recommended rules for consuming GraphQL (operations) development|
|[`operations-all`](https://github.com/B2o5T/graphql-eslint/tree/master/packages/plugin/src/configs/operations-all.ts)|enables all rules for consuming GraphQL (operations) development|
|[`relay`](https://github.com/B2o5T/graphql-eslint/tree/master/packages/plugin/src/configs/relay.ts)|enables rules from Relay specification for schema (SDL) development|
|[`schema-relay`](https://github.com/B2o5T/graphql-eslint/tree/master/packages/plugin/src/configs/schema-relay.ts)|enables rules from Relay specification for schema (SDL) development|
{/* prettier-ignore-end */}

<Callout type="warning">
Expand Down
Loading

0 comments on commit 4079167

Please sign in to comment.