-
-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ignore config to no-unused-fields
rule
#2281
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@graphql-eslint/eslint-plugin": major | ||
--- | ||
|
||
Add ignore config to `no-unused-fields` rule |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,39 @@ | ||
import { GraphQLSchema, TypeInfo, visit, visitWithTypeInfo } from 'graphql'; | ||
import { FieldDefinitionNode, GraphQLSchema, TypeInfo, visit, visitWithTypeInfo } from 'graphql'; | ||
import { FromSchema } from 'json-schema-to-ts'; | ||
import { GraphQLESTreeNode } from '../estree-converter/types.js'; | ||
import { SiblingOperations } from '../siblings.js'; | ||
import { GraphQLESLintRule } from '../types.js'; | ||
import { GraphQLESLintRule, GraphQLESLintRuleListener } from '../types.js'; | ||
import { requireGraphQLSchemaFromContext, requireSiblingsOperations } from '../utils.js'; | ||
|
||
const RULE_ID = 'no-unused-fields'; | ||
|
||
const schema = { | ||
type: 'array', | ||
maxItems: 1, | ||
items: { | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
ignoredFieldSelectors: { | ||
type: 'array', | ||
uniqueItems: true, | ||
minItems: 1, | ||
description: [ | ||
'Fields that will be ignored and are allowed to be unused.', | ||
'', | ||
'> These fields are defined by ESLint [`selectors`](https://eslint.org/docs/developer-guide/selectors). Paste or drop code into the editor in [ASTExplorer](https://astexplorer.net) and inspect the generated AST to compose your selector.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
].join('\n'), | ||
items: { | ||
type: 'string', | ||
pattern: '^FieldDefinition(.+)$', | ||
}, | ||
}, | ||
}, | ||
}, | ||
} as const; | ||
|
||
export type RuleOptions = FromSchema<typeof schema>; | ||
|
||
type UsedFields = Record<string, Set<string>>; | ||
|
||
let usedFieldsCache: UsedFields; | ||
|
@@ -41,7 +70,7 @@ function getUsedFields(schema: GraphQLSchema, operations: SiblingOperations): Us | |
return usedFieldsCache; | ||
} | ||
|
||
export const rule: GraphQLESLintRule = { | ||
export const rule: GraphQLESLintRule<RuleOptions> = { | ||
meta: { | ||
messages: { | ||
[RULE_ID]: 'Field "{{fieldName}}" is unused', | ||
|
@@ -96,45 +125,74 @@ export const rule: GraphQLESLintRule = { | |
} | ||
`, | ||
}, | ||
{ | ||
title: 'Correct (ignoring fields)', | ||
usage: [{ ignoredFieldSelectors: ['FieldDefinition[name.value=lastName]'] }], | ||
code: /* GraphQL */ ` | ||
type User { | ||
id: ID! | ||
firstName: String | ||
lastName: String | ||
} | ||
|
||
type Query { | ||
me: User | ||
} | ||
|
||
query { | ||
me { | ||
id | ||
firstName | ||
} | ||
} | ||
`, | ||
}, | ||
], | ||
}, | ||
type: 'suggestion', | ||
schema: [], | ||
schema, | ||
hasSuggestions: true, | ||
}, | ||
create(context) { | ||
const schema = requireGraphQLSchemaFromContext(RULE_ID, context); | ||
const siblingsOperations = requireSiblingsOperations(RULE_ID, context); | ||
const usedFields = getUsedFields(schema, siblingsOperations); | ||
const { ignoredFieldSelectors } = context.options[0] || {}; | ||
const selector = (ignoredFieldSelectors || []).reduce( | ||
(acc, selector) => `${acc}:not(${selector})`, | ||
'FieldDefinition', | ||
); | ||
|
||
return { | ||
FieldDefinition(node) { | ||
const fieldName = node.name.value; | ||
const parentTypeName = node.parent.name.value; | ||
const isUsed = usedFields[parentTypeName]?.has(fieldName); | ||
|
||
if (isUsed) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node: node.name, | ||
messageId: RULE_ID, | ||
data: { fieldName }, | ||
suggest: [ | ||
{ | ||
desc: `Remove \`${fieldName}\` field`, | ||
fix(fixer) { | ||
const sourceCode = context.getSourceCode() as any; | ||
const tokenBefore = sourceCode.getTokenBefore(node); | ||
const tokenAfter = sourceCode.getTokenAfter(node); | ||
const isEmptyType = tokenBefore.type === '{' && tokenAfter.type === '}'; | ||
return fixer.remove((isEmptyType ? node.parent : node) as any); | ||
}, | ||
const listener: GraphQLESLintRuleListener = (node: GraphQLESTreeNode<FieldDefinitionNode>) => { | ||
const fieldName = node.name.value; | ||
const parentTypeName = node.parent.name.value; | ||
const isUsed = usedFields[parentTypeName]?.has(fieldName); | ||
|
||
if (isUsed) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node: node.name, | ||
messageId: RULE_ID, | ||
data: { fieldName }, | ||
suggest: [ | ||
{ | ||
desc: `Remove \`${fieldName}\` field`, | ||
fix(fixer) { | ||
const sourceCode = context.getSourceCode() as any; | ||
const tokenBefore = sourceCode.getTokenBefore(node); | ||
const tokenAfter = sourceCode.getTokenAfter(node); | ||
const isEmptyType = tokenBefore.type === '{' && tokenAfter.type === '}'; | ||
return fixer.remove((isEmptyType ? node.parent : node) as any); | ||
}, | ||
], | ||
}); | ||
}, | ||
}, | ||
], | ||
}); | ||
}; | ||
|
||
return { | ||
[selector]: listener, | ||
}; | ||
}, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few other names I considered here were:
allowedUnusedFields
allowedUnusedFieldSelectors
ignoredFields
I think
ignoredFieldSelectors
avoids some of the oxymoronic qualities ofallowedUnused*
and provides useful context that this configuration uses ESLint selectors, but am open to other options.