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 an ignoredFields option #122

Open
wants to merge 1 commit 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
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,42 @@ The following rules support suppression within graphql tags:
Supported rules can be suppressed by adding `# eslint-disable-next-line relay/name-of-rule` to the preceding line:

```js
graphql`fragment foo on Page {
# eslint-disable-next-line relay/must-colocate-fragment-spreads
...unused1
}`
graphql`
fragment foo on Page {
# eslint-disable-next-line relay/must-colocate-fragment-spreads
...unused1
}
`;
```

Note that only the `eslint-disable-next-line` form of suppression works. `eslint-disable-line` doesn't currently work until graphql-js provides support for [parsing Comment nodes](https://github.com/graphql/graphql-js/issues/2241) in their AST.

## Ignoring fields with `unused-fields`

Sometimes you might have fields that are accessed by library code (outside the current file), for example:

```js
import getNodes from 'some/lib';

graphql`
fragment foo on Page {
edges { # not directly accessed by this file
node { # not directly accessed by this file
name
}
}
}
`;

getNodes(foo).map(node => node.name);
```

You can ignore the `unused-fields` rule for these fields by providing the `ignoredFields` option.

```js
'relay/unused-fields': ['warn', {ignoredFields: ['edges', 'node']}],
```

## Contribute

We actively welcome pull requests, learn how to [contribute](./CONTRIBUTING.md).
Expand Down
29 changes: 28 additions & 1 deletion src/rule-unused-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,17 @@ function isPageInfoField(field) {
}
}

function getOptions(context) {
return (context.options || [])[0] || {};
}

function isIgnoredField(field, {ignoredFields = []}) {
return ignoredFields.includes(field);
}

function rule(context) {
const options = getOptions(context);

let currentMethod = [];
let foundMemberAccesses = {};
let templateLiterals = [];
Expand Down Expand Up @@ -147,6 +157,7 @@ function rule(context) {
if (
!foundMemberAccesses[field] &&
!isPageInfoField(field) &&
!isIgnoredField(field, options) &&
// Do not warn for unused __typename which can be a workaround
// when only interested in existence of an object.
field !== '__typename'
Expand Down Expand Up @@ -205,4 +216,20 @@ function rule(context) {
};
}

module.exports = rule;
module.exports = module.exports = {
meta: {
schema: [
{
type: 'object',
properties: {
ignoredFields: {
type: 'array',
items: {type: 'string'}
}
},
additionalProperties: false
}
]
},
create: rule
};
20 changes: 19 additions & 1 deletion test/unused-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,16 @@ ruleTester.run('unused-fields', rules['unused-fields'], {
# eslint-disable-next-line relay/unused-fields
name
}\`;
`
`,
{
code: `
graphql\`fragment foo on Page {
edges { node { name } }
}\`;
getCollectionNodes(foo)[0]?.name;
`,
options: [{ignoredFields: ['node', 'edges']}]
}
],
invalid: [
{
Expand Down Expand Up @@ -163,6 +172,15 @@ ruleTester.run('unused-fields', rules['unused-fields'], {
line: 4
}
]
},
{
code: `
graphql\`fragment foo on Page {
edges { node { name } }
}\`;
getCollectionNodes(foo)[0]?.name;
`,
errors: [unusedFieldsWarning('edges'), unusedFieldsWarning('node')]
}
]
});