-
Notifications
You must be signed in to change notification settings - Fork 108
Add ignore config to no-unused-fields
rule
#2281
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
Changes from 1 commit
6a861cf
0a2deaa
3d1e09b
e34b8bc
01a480b
c7ca0ea
a691206
d959c55
b5fdec6
c382384
4615d3b
bb4a55f
7763871
0169da2
6443b2a
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 |
---|---|---|
@@ -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.', | ||
|
||
].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, | ||
}; | ||
}, | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.
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.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.
I prefer
ignoredFieldSelectors
too