-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathrule-graphql-naming.js
183 lines (174 loc) · 5.25 KB
/
rule-graphql-naming.js
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
/**
* 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';
const utils = require('./utils');
const getGraphQLAST = utils.getGraphQLAST;
const getLoc = utils.getLoc;
const getModuleName = utils.getModuleName;
const getRange = utils.getRange;
const isGraphQLTag = utils.isGraphQLTag;
const isGraphQLDeprecatedTag = utils.isGraphQLDeprecatedTag;
const shouldLint = utils.shouldLint;
const CREATE_CONTAINER_FUNCTIONS = new Set([
'createFragmentContainer',
'createPaginationContainer',
'createRefetchContainer'
]);
function isCreateContainerCall(node) {
const callee = node.callee;
// prettier-ignore
return (
callee.type === 'Identifier' &&
CREATE_CONTAINER_FUNCTIONS.has(callee.name)
) || (
callee.kind === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
// Relay, relay, RelayCompat, etc.
/relay/i.test(callee.object.value) &&
callee.property.type === 'Identifier' &&
CREATE_CONTAINER_FUNCTIONS.has(callee.property.name)
);
}
function calleeToString(callee) {
if (callee.type) {
return callee.name;
}
if (
callee.kind === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
callee.property.type === 'Identifier'
) {
return callee.object.value + '.' + callee.property.name;
}
return null;
}
function validateTemplate(context, taggedTemplateExpression, keyName) {
const ast = getGraphQLAST(taggedTemplateExpression);
if (!ast) {
return;
}
const moduleName = getModuleName(context.getFilename());
ast.definitions.forEach(def => {
if (!def.name) {
// no name, covered by graphql-naming/TaggedTemplateExpression
return;
}
const definitionName = def.name.value;
if (def.kind === 'FragmentDefinition') {
if (keyName) {
const expectedName = moduleName + '_' + keyName;
if (definitionName !== expectedName) {
context.report({
loc: getLoc(context, taggedTemplateExpression, def.name),
message:
'Container fragment names must be `<ModuleName>_<propName>`. ' +
'Got `{{actual}}`, expected `{{expected}}`.',
data: {
actual: definitionName,
expected: expectedName
},
fix: fixer =>
fixer.replaceTextRange(
getRange(context, taggedTemplateExpression, def.name),
expectedName
)
});
}
}
}
});
}
module.exports = {
meta: {
fixable: 'code',
docs: {
description: 'Validates naming conventions of graphql tags'
}
},
create(context) {
if (!shouldLint(context)) {
return {};
}
return {
TaggedTemplateExpression(node) {
const ast = getGraphQLAST(node);
if (!ast) {
return;
}
ast.definitions.forEach(definition => {
switch (definition.kind) {
case 'OperationDefinition': {
const moduleName = getModuleName(context.getFilename());
const name = definition.name;
if (!name) {
return;
}
const operationName = name.value;
if (operationName.indexOf(moduleName) !== 0) {
context.report({
message:
'Operations should start with the module name. ' +
'Expected prefix `{{expected}}`, got `{{actual}}`.',
data: {
expected: moduleName,
actual: operationName
},
loc: getLoc(context, node, name)
});
}
break;
}
default:
}
});
},
CallExpression(node) {
if (!isCreateContainerCall(node)) {
return;
}
const fragments = node.arguments[1];
if (fragments.type === 'ObjectExpression') {
fragments.properties.forEach(property => {
if (
property.type === 'Property' &&
property.key.type === 'Identifier' &&
property.computed === false &&
property.value.type === 'TaggedTemplateExpression'
) {
if (
!isGraphQLTag(property.value.tag) &&
!isGraphQLDeprecatedTag(property.value.tag)
) {
context.report({
node: property.value.tag,
message:
'`{{callee}}` expects GraphQL to be tagged with ' +
'graphql`...`.',
data: {
callee: calleeToString(node.callee)
}
});
return;
}
validateTemplate(context, property.value, property.key.name);
} else {
context.report({
node: property,
message:
'`{{callee}}` expects fragment definitions to be ' +
'`key: graphql`.',
data: {
callee: calleeToString(node.callee)
}
});
}
});
}
}
};
}
};