Skip to content

Commit

Permalink
feat: add suggestions for no-typename-prefix, no-root-type, `no-c…
Browse files Browse the repository at this point in the history
…ase-insensitive-enum-values-duplicates` rules (#968)
  • Loading branch information
dimaMachina committed Feb 19, 2022
1 parent a1c8464 commit db22ba5
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-bulldogs-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': minor
---

feat: add suggestions for `no-typename-prefix`, `no-root-type`, `no-case-insensitive-enum-values-duplicates` rules
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { GraphQLESLintRule } from '../types';
const rule: GraphQLESLintRule = {
meta: {
type: 'suggestion',
hasSuggestions: true,
docs: {
url: `https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/no-case-insensitive-enum-values-duplicates.md`,
category: 'Schema',
Expand Down Expand Up @@ -47,7 +48,13 @@ const rule: GraphQLESLintRule = {
const enumName = duplicate.name.value;
context.report({
node: duplicate.name,
message: `Case-insensitive enum values duplicates are not allowed! Found: "${enumName}"`,
message: `Case-insensitive enum values duplicates are not allowed! Found: \`${enumName}\`.`,
suggest: [
{
desc: `Remove \`${enumName}\` enum value`,
fix: fixer => fixer.remove(duplicate as any),
},
],
});
}
},
Expand Down
9 changes: 8 additions & 1 deletion packages/plugin/src/rules/no-root-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type NoRootTypeConfig = { disallow: typeof ROOT_TYPES };
const rule: GraphQLESLintRule<[NoRootTypeConfig]> = {
meta: {
type: 'suggestion',
hasSuggestions: true,
docs: {
category: 'Schema',
description: 'Disallow using root types `mutation` and/or `subscription`.',
Expand Down Expand Up @@ -84,7 +85,13 @@ const rule: GraphQLESLintRule<[NoRootTypeConfig]> = {
const typeName = node.value;
context.report({
node,
message: `Root type "${typeName}" is forbidden`,
message: `Root type \`${typeName}\` is forbidden.`,
suggest: [
{
desc: `Remove \`${typeName}\` type`,
fix: fixer => fixer.remove((node as any).parent),
},
],
});
},
};
Expand Down
8 changes: 8 additions & 0 deletions packages/plugin/src/rules/no-typename-prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const NO_TYPENAME_PREFIX = 'NO_TYPENAME_PREFIX';
const rule: GraphQLESLintRule = {
meta: {
type: 'suggestion',
hasSuggestions: true,
docs: {
category: 'Schema',
description: 'Enforces users to avoid using the type name in a field name while defining your schema.',
Expand Down Expand Up @@ -62,6 +63,13 @@ const rule: GraphQLESLintRule = {
},
messageId: NO_TYPENAME_PREFIX,
node: field.name,
suggest: [
{
desc: `Remove \`${fieldName.slice(0, typeName.length)}\` prefix`,
fix: fixer =>
fixer.replaceText(field.name as any, fieldName.replace(new RegExp(`^${typeName}`, 'i'), '')),
},
],
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/rules/no-unreachable-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const rule: GraphQLESLintRule = {
data: { typeName },
suggest: [
{
desc: `Remove ${typeName}`,
desc: `Remove \`${typeName}\``,
fix: fixer => fixer.remove(node as any),
},
],
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/rules/no-unused-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const rule: GraphQLESLintRule = {
data: { fieldName },
suggest: [
{
desc: `Remove "${fieldName}" field`,
desc: `Remove \`${fieldName}\` field`,
fix(fixer) {
const sourceCode = context.getSourceCode() as any;
const tokenBefore = sourceCode.getTokenBefore(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ exports[` 1`] = `
❌ Error

> 1 | enum A { TEST TesT }
| ^^^^ Case-insensitive enum values duplicates are not allowed! Found: "TesT"
| ^^^^ Case-insensitive enum values duplicates are not allowed! Found: \`TesT\`.

💡 Suggestion: Remove \`TesT\` enum value

1 | enum A { TEST }
`;

exports[` 2`] = `
❌ Error

> 1 | extend enum A { TEST TesT }
| ^^^^ Case-insensitive enum values duplicates are not allowed! Found: "TesT"
| ^^^^ Case-insensitive enum values duplicates are not allowed! Found: \`TesT\`.

💡 Suggestion: Remove \`TesT\` enum value

1 | extend enum A { TEST }
`;
24 changes: 20 additions & 4 deletions packages/plugin/tests/__snapshots__/no-root-type.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ exports[` 1`] = `
❌ Error

> 1 | type Mutation
| ^^^^^^^^ Root type "Mutation" is forbidden
| ^^^^^^^^ Root type \`Mutation\` is forbidden.

💡 Suggestion: Remove \`Mutation\` type

1 |
`;

exports[` 2`] = `
Expand All @@ -27,7 +31,11 @@ exports[` 2`] = `
❌ Error

> 1 | type Subscription
| ^^^^^^^^^^^^ Root type "Subscription" is forbidden
| ^^^^^^^^^^^^ Root type \`Subscription\` is forbidden.

💡 Suggestion: Remove \`Subscription\` type

1 |
`;

exports[` 3`] = `
Expand All @@ -42,7 +50,11 @@ exports[` 3`] = `
❌ Error

> 1 | extend type Mutation { foo: ID }
| ^^^^^^^^ Root type "Mutation" is forbidden
| ^^^^^^^^ Root type \`Mutation\` is forbidden.

💡 Suggestion: Remove \`Mutation\` type

1 |
`;

exports[` 4`] = `
Expand All @@ -57,5 +69,9 @@ exports[` 4`] = `
❌ Error

> 1 | type MyMutation
| ^^^^^^^^^^ Root type "MyMutation" is forbidden
| ^^^^^^^^^^ Root type \`MyMutation\` is forbidden.

💡 Suggestion: Remove \`MyMutation\` type

1 |
`;
26 changes: 26 additions & 0 deletions packages/plugin/tests/__snapshots__/no-typename-prefix.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ exports[` 1`] = `
> 2 | userId: ID!
| ^^^^^^ Field "userId" starts with the name of the parent type "User"
3 | }

💡 Suggestion: Remove \`user\` prefix

1 | type User {
2 | Id: ID!
3 | }
`;

exports[` 2`] = `
Expand All @@ -24,12 +30,26 @@ Code
| ^^^^^^ Field "userId" starts with the name of the parent type "User"
3 | userName: String!

💡 Suggestion: Remove \`user\` prefix

1 | type User {
2 | Id: ID!
3 | userName: String!
4 | }

❌ Error 2/2

2 | userId: ID!
> 3 | userName: String!
| ^^^^^^^^ Field "userName" starts with the name of the parent type "User"
4 | }

💡 Suggestion: Remove \`user\` prefix

1 | type User {
2 | userId: ID!
3 | Name: String!
4 | }
`;

exports[` 3`] = `
Expand All @@ -39,4 +59,10 @@ exports[` 3`] = `
> 2 | nodeId: ID!
| ^^^^^^ Field "nodeId" starts with the name of the parent type "Node"
3 | }

💡 Suggestion: Remove \`node\` prefix

1 | interface Node {
2 | Id: ID!
3 | }
`;
30 changes: 15 additions & 15 deletions packages/plugin/tests/__snapshots__/no-unreachable-types.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Code
| ^^^^ Type "Node" is unreachable
6 | id: ID!

💡 Suggestion: Remove Node
💡 Suggestion: Remove \`Node\`

1 | type Query {
2 | node(id: ID!): AnotherNode!
Expand Down Expand Up @@ -63,7 +63,7 @@ Code
| ^^^^ Type "User" is unreachable
14 | id: ID!

💡 Suggestion: Remove User
💡 Suggestion: Remove \`User\`

1 | type Query {
2 | node(id: ID!): AnotherNode!
Expand Down Expand Up @@ -92,7 +92,7 @@ Code
| ^^^^^^^^^ Type "SuperUser" is unreachable
19 | id: ID!

💡 Suggestion: Remove SuperUser
💡 Suggestion: Remove \`SuperUser\`

1 | type Query {
2 | node(id: ID!): AnotherNode!
Expand Down Expand Up @@ -154,7 +154,7 @@ Code
| ^^^^^^^^ Type "DateTime" is unreachable
3 |

💡 Suggestion: Remove DateTime
💡 Suggestion: Remove \`DateTime\`

1 | # ScalarTypeDefinition
2 |
Expand Down Expand Up @@ -193,7 +193,7 @@ Code
| ^^^^ Type "Role" is unreachable
6 | ADMIN

💡 Suggestion: Remove Role
💡 Suggestion: Remove \`Role\`

1 | # ScalarTypeDefinition
2 | scalar DateTime
Expand Down Expand Up @@ -229,7 +229,7 @@ Code
| ^^^^ Type "auth" is unreachable
12 |

💡 Suggestion: Remove auth
💡 Suggestion: Remove \`auth\`

1 | # ScalarTypeDefinition
2 | scalar DateTime
Expand Down Expand Up @@ -268,7 +268,7 @@ Code
| ^^^^^ Type "Union" is unreachable
15 |

💡 Suggestion: Remove Union
💡 Suggestion: Remove \`Union\`

1 | # ScalarTypeDefinition
2 | scalar DateTime
Expand Down Expand Up @@ -307,7 +307,7 @@ Code
| ^^^^^^^^^^^ Type "UsersFilter" is unreachable
18 | limit: Int

💡 Suggestion: Remove UsersFilter
💡 Suggestion: Remove \`UsersFilter\`

1 | # ScalarTypeDefinition
2 | scalar DateTime
Expand Down Expand Up @@ -344,7 +344,7 @@ Code
| ^^^^^^^ Type "Address" is unreachable
23 | city: String

💡 Suggestion: Remove Address
💡 Suggestion: Remove \`Address\`

1 | # ScalarTypeDefinition
2 | scalar DateTime
Expand Down Expand Up @@ -381,7 +381,7 @@ Code
| ^^^^ Type "User" is unreachable
28 | city: String

💡 Suggestion: Remove User
💡 Suggestion: Remove \`User\`

1 | # ScalarTypeDefinition
2 | scalar DateTime
Expand Down Expand Up @@ -435,7 +435,7 @@ exports[` 3`] = `
> 18 | scalar DateTime
| ^^^^^^^^ Type "DateTime" is unreachable

💡 Suggestion: Remove DateTime
💡 Suggestion: Remove \`DateTime\`

1 | interface User {
2 | id: String
Expand Down Expand Up @@ -487,7 +487,7 @@ Code
| ^^^^ Type "User" is unreachable
2 | id: String

💡 Suggestion: Remove User
💡 Suggestion: Remove \`User\`

1 |
2 |
Expand Down Expand Up @@ -515,7 +515,7 @@ Code
| ^^^^^^^^^ Type "SuperUser" is unreachable
10 | id: String

💡 Suggestion: Remove SuperUser
💡 Suggestion: Remove \`SuperUser\`

1 | interface User {
2 | id: String
Expand Down Expand Up @@ -543,7 +543,7 @@ Code
| ^^^^^^^^^ Type "SuperUser" is unreachable
15 | detail: String

💡 Suggestion: Remove SuperUser
💡 Suggestion: Remove \`SuperUser\`

1 | interface User {
2 | id: String
Expand Down Expand Up @@ -590,7 +590,7 @@ exports[` 5`] = `
> 20 | scalar DateTime
| ^^^^^^^^ Type "DateTime" is unreachable

💡 Suggestion: Remove DateTime
💡 Suggestion: Remove \`DateTime\`

1 | type Query {
2 | node(id: ID!): Node!
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/tests/__snapshots__/no-unused-fields.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports[` 1`] = `
| ^^^^^^^^^ Field "firstName" is unused
4 | }

💡 Suggestion: Remove "firstName" field
💡 Suggestion: Remove \`firstName\` field

1 | type User {
2 | id: ID!
Expand All @@ -29,7 +29,7 @@ exports[` 2`] = `
| ^^^^^^^^^^ Field "deleteUser" is unused
7 | }

💡 Suggestion: Remove "deleteUser" field
💡 Suggestion: Remove \`deleteUser\` field

1 | type Query {
2 | user(id: ID!): User
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ ruleTester.runGraphQLTests('no-case-insensitive-enum-values-duplicates', rule, {
invalid: [
{
code: 'enum A { TEST TesT }',
errors: [{ message: 'Case-insensitive enum values duplicates are not allowed! Found: "TesT"' }],
errors: [{ message: 'Case-insensitive enum values duplicates are not allowed! Found: `TesT`.' }],
},
{
code: 'extend enum A { TEST TesT }',
errors: [{ message: 'Case-insensitive enum values duplicates are not allowed! Found: "TesT"' }],
errors: [{ message: 'Case-insensitive enum values duplicates are not allowed! Found: `TesT`.' }],
},
],
});

0 comments on commit db22ba5

Please sign in to comment.