Skip to content
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

[unused-fields] Add option to check leave fields only #95

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 29 additions & 4 deletions src/rule-unused-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,30 @@ const utils = require('./utils');

const getGraphQLAST = utils.getGraphQLAST;

function getGraphQLFieldNames(graphQLAst) {
const DEFAULT_OPTIONS = {
ignoreFields: [],
leavesOnly: false
};

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

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

function walkAST(node, ignoreLevel) {
if (node.kind === 'Field' && !ignoreLevel) {
if (
node.kind === 'Field' &&
!ignoreLevel &&
(!leavesOnly || node.selectionSet == null)
) {
const nameNode = node.alias || node.name;
fieldNames[nameNode.value] = nameNode;
}
Expand Down Expand Up @@ -82,6 +101,8 @@ function isPageInfoField(field) {
}

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

let currentMethod = [];
let foundMemberAccesses = {};
let templateLiterals = [];
Expand Down Expand Up @@ -131,14 +152,18 @@ function rule(context) {
return;
}

const queriedFields = getGraphQLFieldNames(graphQLAst);
const queriedFields = getGraphQLFieldNames(
graphQLAst,
options.leavesOnly
);
for (const field in queriedFields) {
if (
!foundMemberAccesses[field] &&
!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
36 changes: 35 additions & 1 deletion test/unused-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,21 @@ ruleTester.run('unused-fields', rules['unused-fields'], {
}
}
\`;
`
`,
{
code: `
graphql\`fragment Test on Page { id }\`;
`,
options: [{ignoreFields: ['id']}]
},
{
code: `
graphql\`fragment Test on Page { connection { edges { node { id } } } }\`;
const nodes = useConnectionArray(props.connection);
node.id;
`,
options: [{leavesOnly: true}]
}
],
invalid: [
{
Expand All @@ -109,6 +123,26 @@ ruleTester.run('unused-fields', rules['unused-fields'], {
`,
errors: [unusedFieldsWarning('unused1'), unusedFieldsWarning('unused2')]
},
{
code: `
graphql\`fragment Test on Page { unused1 { unused2 } }\`;
`,
errors: [unusedFieldsWarning('unused1'), unusedFieldsWarning('unused2')]
},
{
code: `
graphql\`fragment Test on Page { unused1 { unused2 } }\`;
`,
options: [{leavesOnly: true}],
errors: [unusedFieldsWarning('unused2')]
},
Comment on lines +132 to +138
Copy link

@AugustoCalaca AugustoCalaca Dec 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it works well if we use options, for some reason, as [{leavesOnly: true}, ignoreFields: ['unsed2']]?
I think that can add a new test

{
  code: `
    graphql\`fragment Test on Page { unused1 { unused2 } }\`;
  `,
  options: [{leavesOnly: true}, ignoreFields: ['unsed2']],
},

{
code: `
graphql\`fragment Test on Page { unused1, unused2 }\`;
`,
options: [{ignoreFields: ['unused1']}],
errors: [unusedFieldsWarning('unused2')]
},
{
code: `
const getByPath = require('getByPath');
Expand Down