-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathunused-fields.js
More file actions
196 lines (190 loc) · 4.61 KB
/
Copy pathunused-fields.js
File metadata and controls
196 lines (190 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var eslint = require('eslint');
const rules = require('..').rules;
const RuleTester = eslint.RuleTester;
const ruleTester = new RuleTester({
parser: 'babel-eslint',
parserOptions: {ecmaVersion: 6, sourceType: 'module'}
});
function unusedFieldsWarning(field) {
return (
`This queries for the field \`${field}\` but this file does ` +
'not seem to use it directly. If a different file needs this ' +
'information that file should export a fragment and colocate ' +
'the query for the data with the usage.\n' +
'If only interested in the existence of a record, __typename ' +
'can be used without this warning.'
);
}
ruleTester.run('unused-fields', rules['unused-fields'], {
valid: [
`
graphql\`fragment foo on Page { name2 }\`;
props.page.name;
foo.name2;
`,
'graphql`fragment foo on Page { __typename }`;',
// Syntax error is ignored by this rule
`graphql\`fragment Test { name2 }\`;`,
`
graphql\`fragment Test on InternalTask {
owner: task_owner {
name: full_name
}
}\`;
node.owner.name;
`,
'graphql`fragment Test on Page { ...Other_x }`;',
`
const {
normal,
aliased: v1,
[computed]: x,
nested: { v2 },
...rest
} = foo;
`,
'graphql`mutation { page_unlike(data: $input) }`',
'String.raw`foo bar`',
// Facebook naming of page info fields
`
graphql\`
fragment foo on Page {
page_info {
has_next_page
has_previous_page
end_cursor
start_cursor
}
}
\`;
`,
// OSS naming of page info fields
`
graphql\`
fragment foo on Page {
pageInfo {
hasNextPage
hasPreviousPage
endCursor
startCursor
}
}
\`;
`,
{
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: [
{
code: `
graphql\`
fragment Test on Page {
name
name2
}
\`;
props.page.name;
`,
errors: [
{
message: unusedFieldsWarning('name2'),
line: 5
}
]
},
{
code: `
graphql\`fragment Test on Page { unused1, unused2 }\`;
`,
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')]
},
{
code: `
graphql\`fragment Test on Page { unused1, unused2 }\`;
`,
options: [{ignoreFields: ['unused1']}],
errors: [unusedFieldsWarning('unused2')]
},
{
code: `
const getByPath = require('getByPath');
graphql\`fragment Test on Page { unused1, used1, used2 }\`;
alert(getByPath(obj, ['foo', 'used1', 'used2']))
`,
errors: [unusedFieldsWarning('unused1')]
},
{
code: `
graphql\`fragment Test on Page { unused1, used1, used2 }\`;
obj?.foo?.used1?.used2;
`,
errors: [unusedFieldsWarning('unused1')]
},
{
code: `
const dotAccess = require('dotAccess');
graphql\`fragment Test on Page { unused1, used1, used2 }\`;
alert(dotAccess(obj, 'foo.used1.used2'))
`,
errors: [unusedFieldsWarning('unused1')]
},
{
code: `
graphql\`fragment Test on Page {
unused1
unused2
used1
used2
used3
used4
}\`;
var { used1: unused1, used2: {used3} } = node;
function test({used4}) {
return x;
}
`,
errors: [
{
message: unusedFieldsWarning('unused1'),
line: 3
},
{
message: unusedFieldsWarning('unused2'),
line: 4
}
]
}
]
});