Skip to content

Add eslint disable support #112

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

Merged
merged 4 commits into from
Feb 5, 2021
Merged
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: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ Add `plugin:relay/recommended` or `plugin:relay/strict` in `extends`:
}
```

### Suppressing rules within graphql tags

The following rules support suppression within graphql tags:

- relay/unused-fields
- relay/must-colocate-fragment-spreads

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
}`
```

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.

## Contribute

We actively welcome pull requests, learn how to [contribute](./CONTRIBUTING.md).
Expand Down
8 changes: 8 additions & 0 deletions src/rule-must-colocate-fragment-spreads.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
const {visit} = require('graphql');
const utils = require('./utils');

const ESLINT_DISABLE_COMMENT =
' eslint-disable-next-line relay/must-colocate-fragment-spreads';

function getGraphQLFragmentSpreads(graphQLAst) {
const fragmentSpreads = {};
visit(graphQLAst, {
Expand Down Expand Up @@ -89,6 +92,11 @@ function getGraphQLFragmentSpreads(graphQLAst) {
}
}
}
if (
utils.hasPrecedingEslintDisableComment(node, ESLINT_DISABLE_COMMENT)
) {
return;
}
fragmentSpreads[node.name.value] = node;
}
});
Expand Down
19 changes: 15 additions & 4 deletions src/rule-unused-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,31 @@

'use strict';

const utils = require('./utils');
const {
getGraphQLAST,
getLoc,
hasPrecedingEslintDisableComment
} = require('./utils');

const getGraphQLAST = utils.getGraphQLAST;
const ESLINT_DISABLE_COMMENT = ' eslint-disable-next-line relay/unused-fields';

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

function walkAST(node, ignoreLevel) {
if (node.kind === 'Field' && !ignoreLevel) {
if (hasPrecedingEslintDisableComment(node, ESLINT_DISABLE_COMMENT)) {
return;
}
const nameNode = node.alias || node.name;
fieldNames[nameNode.value] = nameNode;
}
if (node.kind === 'OperationDefinition') {
if (node.operation === 'mutation' || node.operation === 'subscription') {
if (
node.operation === 'mutation' ||
node.operation === 'subscription' ||
hasPrecedingEslintDisableComment(node, ESLINT_DISABLE_COMMENT)
) {
return;
}
// Ignore fields that are direct children of query as used in mutation
Expand Down Expand Up @@ -142,7 +153,7 @@ function rule(context) {
) {
context.report({
node: templateLiteral,
loc: utils.getLoc(context, templateLiteral, queriedFields[field]),
loc: getLoc(context, templateLiteral, queriedFields[field]),
message:
`This queries for the field \`${field}\` but this file does ` +
'not seem to use it directly. If a different file needs this ' +
Expand Down
6 changes: 6 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,19 @@ function shouldLint(context) {
return /graphql|relay/i.test(context.getSourceCode().text);
}

function hasPrecedingEslintDisableComment(node, commentText) {
const prevNode = node.loc.startToken.prev;
return prevNode.kind === 'Comment' && prevNode.value.startsWith(commentText);
}

module.exports = {
isGraphQLTemplate: isGraphQLTemplate,
getGraphQLAST: getGraphQLAST,
getLoc: getLoc,
getLocFromIndex: getLocFromIndex,
getModuleName: getModuleName,
getRange: getRange,
hasPrecedingEslintDisableComment: hasPrecedingEslintDisableComment,
isGraphQLTag: isGraphQLTag,
isGraphQLDeprecatedTag: isGraphQLDeprecatedTag,
shouldLint: shouldLint
Expand Down
8 changes: 7 additions & 1 deletion test/must-colocate-fragment-spreads.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ ruleTester.run(
const getOperation = (reference) => {\
return import(reference);\
};\
'
',
`
graphql\`fragment foo on Page {
# eslint-disable-next-line relay/must-colocate-fragment-spreads
...unused1
}\`;
`
],
invalid: [
{
Expand Down
6 changes: 6 additions & 0 deletions test/unused-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ ruleTester.run('unused-fields', rules['unused-fields'], {
}
}
\`;
`,
`
graphql\`fragment foo on Page {
# eslint-disable-next-line relay/unused-fields
name
}\`;
`
],
invalid: [
Expand Down