Skip to content

Commit f05d2a7

Browse files
authored
Merge pull request #112 from relayjs/add-eslint-disable-support
Add eslint disable support
2 parents 0d7a960 + d26e588 commit f05d2a7

6 files changed

+60
-5
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ Add `plugin:relay/recommended` or `plugin:relay/strict` in `extends`:
4242
}
4343
```
4444

45+
### Suppressing rules within graphql tags
46+
47+
The following rules support suppression within graphql tags:
48+
49+
- relay/unused-fields
50+
- relay/must-colocate-fragment-spreads
51+
52+
Supported rules can be suppressed by adding `# eslint-disable-next-line relay/name-of-rule` to the preceding line:
53+
54+
```js
55+
graphql`fragment foo on Page {
56+
# eslint-disable-next-line relay/must-colocate-fragment-spreads
57+
...unused1
58+
}`
59+
```
60+
61+
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.
62+
4563
## Contribute
4664

4765
We actively welcome pull requests, learn how to [contribute](./CONTRIBUTING.md).

src/rule-must-colocate-fragment-spreads.js

+8
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
const {visit} = require('graphql');
6161
const utils = require('./utils');
6262

63+
const ESLINT_DISABLE_COMMENT =
64+
' eslint-disable-next-line relay/must-colocate-fragment-spreads';
65+
6366
function getGraphQLFragmentSpreads(graphQLAst) {
6467
const fragmentSpreads = {};
6568
visit(graphQLAst, {
@@ -89,6 +92,11 @@ function getGraphQLFragmentSpreads(graphQLAst) {
8992
}
9093
}
9194
}
95+
if (
96+
utils.hasPrecedingEslintDisableComment(node, ESLINT_DISABLE_COMMENT)
97+
) {
98+
return;
99+
}
92100
fragmentSpreads[node.name.value] = node;
93101
}
94102
});

src/rule-unused-fields.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,31 @@
77

88
'use strict';
99

10-
const utils = require('./utils');
10+
const {
11+
getGraphQLAST,
12+
getLoc,
13+
hasPrecedingEslintDisableComment
14+
} = require('./utils');
1115

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

1418
function getGraphQLFieldNames(graphQLAst) {
1519
const fieldNames = {};
1620

1721
function walkAST(node, ignoreLevel) {
1822
if (node.kind === 'Field' && !ignoreLevel) {
23+
if (hasPrecedingEslintDisableComment(node, ESLINT_DISABLE_COMMENT)) {
24+
return;
25+
}
1926
const nameNode = node.alias || node.name;
2027
fieldNames[nameNode.value] = nameNode;
2128
}
2229
if (node.kind === 'OperationDefinition') {
23-
if (node.operation === 'mutation' || node.operation === 'subscription') {
30+
if (
31+
node.operation === 'mutation' ||
32+
node.operation === 'subscription' ||
33+
hasPrecedingEslintDisableComment(node, ESLINT_DISABLE_COMMENT)
34+
) {
2435
return;
2536
}
2637
// Ignore fields that are direct children of query as used in mutation
@@ -142,7 +153,7 @@ function rule(context) {
142153
) {
143154
context.report({
144155
node: templateLiteral,
145-
loc: utils.getLoc(context, templateLiteral, queriedFields[field]),
156+
loc: getLoc(context, templateLiteral, queriedFields[field]),
146157
message:
147158
`This queries for the field \`${field}\` but this file does ` +
148159
'not seem to use it directly. If a different file needs this ' +

src/utils.js

+6
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,19 @@ function shouldLint(context) {
112112
return /graphql|relay/i.test(context.getSourceCode().text);
113113
}
114114

115+
function hasPrecedingEslintDisableComment(node, commentText) {
116+
const prevNode = node.loc.startToken.prev;
117+
return prevNode.kind === 'Comment' && prevNode.value.startsWith(commentText);
118+
}
119+
115120
module.exports = {
116121
isGraphQLTemplate: isGraphQLTemplate,
117122
getGraphQLAST: getGraphQLAST,
118123
getLoc: getLoc,
119124
getLocFromIndex: getLocFromIndex,
120125
getModuleName: getModuleName,
121126
getRange: getRange,
127+
hasPrecedingEslintDisableComment: hasPrecedingEslintDisableComment,
122128
isGraphQLTag: isGraphQLTag,
123129
isGraphQLDeprecatedTag: isGraphQLDeprecatedTag,
124130
shouldLint: shouldLint

test/must-colocate-fragment-spreads.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,13 @@ ruleTester.run(
100100
const getOperation = (reference) => {\
101101
return import(reference);\
102102
};\
103-
'
103+
',
104+
`
105+
graphql\`fragment foo on Page {
106+
# eslint-disable-next-line relay/must-colocate-fragment-spreads
107+
...unused1
108+
}\`;
109+
`
104110
],
105111
invalid: [
106112
{

test/unused-fields.js

+6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ ruleTester.run('unused-fields', rules['unused-fields'], {
8383
}
8484
}
8585
\`;
86+
`,
87+
`
88+
graphql\`fragment foo on Page {
89+
# eslint-disable-next-line relay/unused-fields
90+
name
91+
}\`;
8692
`
8793
],
8894
invalid: [

0 commit comments

Comments
 (0)