-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61251e7
commit 63dc00a
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphql-eslint/eslint-plugin': minor | ||
--- | ||
|
||
NEW RULE: avoid-typename-prefix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { FieldDefinitionNode } from 'graphql'; | ||
import { GraphQLESTreeNode } from '../estree-parser'; | ||
import { GraphQLESLintRule, GraphQLESlintRuleContext } from '../types'; | ||
|
||
const AVOID_TYPENAME_PREFIX = 'AVOID_TYPENAME_PREFIX'; | ||
|
||
function checkNode( | ||
context: GraphQLESlintRuleContext<any>, | ||
typeName: string, | ||
fields: GraphQLESTreeNode<FieldDefinitionNode>[] | ||
) { | ||
const lowerTypeName = (typeName || '').toLowerCase(); | ||
|
||
for (const field of fields) { | ||
const fieldName = field.name.value || ''; | ||
|
||
if (fieldName && lowerTypeName && fieldName.toLowerCase().startsWith(lowerTypeName)) { | ||
context.report({ | ||
node: field.name, | ||
data: { | ||
fieldName, | ||
typeName, | ||
}, | ||
messageId: AVOID_TYPENAME_PREFIX, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
const rule: GraphQLESLintRule = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Enforces users to avoid using the type name in a field name while defining your schema.', | ||
recommended: true, | ||
url: 'https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/avoid-typename-prefix.md', | ||
requiresSiblings: false, | ||
requiresSchema: false, | ||
examples: [ | ||
{ | ||
title: 'Incorrect', | ||
code: /* GraphQL */ ` | ||
type User { | ||
userId: ID! | ||
} | ||
`, | ||
}, | ||
{ | ||
title: 'Correct', | ||
code: /* GraphQL */ ` | ||
type User { | ||
id: ID! | ||
} | ||
`, | ||
}, | ||
], | ||
}, | ||
messages: { | ||
[AVOID_TYPENAME_PREFIX]: `Field "{{ fieldName }}" starts with the name of the parent type "{{ typeName }}"`, | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
ObjectTypeDefinition(node) { | ||
checkNode(context, node.name.value, node.fields); | ||
}, | ||
ObjectTypeExtension(node) { | ||
checkNode(context, node.name.value, node.fields); | ||
}, | ||
InterfaceTypeDefinition(node) { | ||
checkNode(context, node.name.value, node.fields); | ||
}, | ||
InterfaceTypeExtension(node) { | ||
checkNode(context, node.name.value, node.fields); | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
export default rule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { GraphQLRuleTester } from '../src/testkit'; | ||
import rule from '../src/rules/avoid-typename-prefix'; | ||
|
||
const ruleTester = new GraphQLRuleTester(); | ||
|
||
ruleTester.runGraphQLTests('avoid-typename-prefix', rule, { | ||
valid: [ | ||
{ | ||
code: /* GraphQL */ ` | ||
type User { | ||
id: ID! | ||
} | ||
`, | ||
}, | ||
{ | ||
code: /* GraphQL */ ` | ||
interface Node { | ||
id: ID! | ||
} | ||
`, | ||
}, | ||
{ | ||
code: /* GraphQL */ ` | ||
type User { | ||
# eslint-disable-next-line | ||
userId: ID! | ||
} | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: /* GraphQL */ ` | ||
type User { | ||
userId: ID! | ||
} | ||
`, | ||
errors: [{ message: 'Field "userId" starts with the name of the parent type "User"' }], | ||
}, | ||
{ | ||
code: /* GraphQL */ ` | ||
type User { | ||
userId: ID! | ||
userName: String! | ||
} | ||
`, | ||
errors: [ | ||
{ message: 'Field "userId" starts with the name of the parent type "User"' }, | ||
{ message: 'Field "userName" starts with the name of the parent type "User"' }, | ||
], | ||
}, | ||
{ | ||
code: /* GraphQL */ ` | ||
interface Node { | ||
nodeId: ID! | ||
} | ||
`, | ||
errors: [{ message: 'Field "nodeId" starts with the name of the parent type "Node"' }], | ||
}, | ||
], | ||
}); |