Skip to content

[unused-fields] Add ignore fields option #94

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/rule-unused-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ const utils = require('./utils');

const getGraphQLAST = utils.getGraphQLAST;

const DEFAULT_OPTIONS = {
ignoreFields: []
};

function getOptions(optionValue) {
if (optionValue) {
return {
ignoreFields: optionValue.ignoreFields || DEFAULT_OPTIONS.ignoreFields
};
}
return DEFAULT_OPTIONS;
}

function getGraphQLFieldNames(graphQLAst) {
const fieldNames = {};

Expand Down Expand Up @@ -82,6 +95,8 @@ function isPageInfoField(field) {
}

function rule(context) {
const options = getOptions(context.options[0]);

let currentMethod = [];
let foundMemberAccesses = {};
let templateLiterals = [];
Expand Down Expand Up @@ -138,7 +153,8 @@ function rule(context) {
!isPageInfoField(field) &&
// Do not warn for unused __typename which can be a workaround
// when only interested in existence of an object.
field !== '__typename'
field !== '__typename' &&
!options.ignoreFields.includes(field)
) {
context.report({
node: templateLiteral,
Expand Down
15 changes: 14 additions & 1 deletion test/unused-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ ruleTester.run('unused-fields', rules['unused-fields'], {
}
}
\`;
`
`,
{
code: `
graphql\`fragment Test on Page { id }\`;
`,
options: [{ignoreFields: ['id']}]
}
],
invalid: [
{
Expand All @@ -109,6 +115,13 @@ ruleTester.run('unused-fields', rules['unused-fields'], {
`,
errors: [unusedFieldsWarning('unused1'), unusedFieldsWarning('unused2')]
},
{
code: `
graphql\`fragment Test on Page { unused1, unused2 }\`;
`,
options: [{ignoreFields: ['unused1']}],
errors: [unusedFieldsWarning('unused2')]
},
{
code: `
const getByPath = require('getByPath');
Expand Down