-
Notifications
You must be signed in to change notification settings - Fork 74
/
relayOperationGenericsRule.js
278 lines (251 loc) · 7.35 KB
/
relayOperationGenericsRule.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// @ts-check
const Lint = require("tslint")
const ts = require("typescript")
const { BREAK, parse, visit } = require("graphql")
class Rule extends Lint.Rules.AbstractRule {
/**
* @param {ts.SourceFile} sourceFile
*/
apply(sourceFile) {
return this.applyWithWalker(
new RelayOperationGenericsWalker(sourceFile, this.getOptions())
)
}
}
class RelayOperationGenericsWalker extends Lint.RuleWalker {
constructor(sourceFile, options) {
super(sourceFile, options)
this._imports = []
}
/**
* @return {ts.ImportDeclaration[]}
*/
getImports() {
return this._imports
}
/**
*
* @param {ts.ImportDeclaration} node
*/
visitImportDeclaration(node) {
this._imports.push(node)
super.visitImportDeclaration(node)
}
/**
* @param {ts.JsxSelfClosingElement} node
*/
visitJsxSelfClosingElement(node) {
// TODO: So many hoops to jump through without TS :/
/** @type {any} */
let any
if (node.tagName.getText() === "QueryRenderer") {
for (const property of node.attributes.properties) {
if (
property.kind === ts.SyntaxKind.JsxAttribute &&
property.name.getText() === "query"
) {
const initializer = property.initializer
if (initializer.kind === ts.SyntaxKind.JsxExpression) {
this.visitOperationConfiguration(
node,
initializer.expression,
node.tagName
)
} else {
this.addFailureAtNode(
initializer,
"expected a graphql`…` tagged-template expression"
)
}
break
}
}
}
super.visitJsxSelfClosingElement(node)
}
/**
* @param {ts.CallExpression} node
*/
visitCallExpression(node) {
// TODO: So many hoops to jump through without TS :/
/** @type {any} */
let any
any = node.expression
/** @type {ts.Identifier} */
const functionName = any
if (functionName.text === "commitMutation") {
any = node.arguments[1]
/** @type {undefined | ts.ObjectLiteralExpression} */
const config = any
if (config && config.kind === ts.SyntaxKind.ObjectLiteralExpression) {
// any = this.visitOperationConfiguration(node, config, functionName)
for (const property of config.properties) {
if (property.name.getText() === "mutation") {
if (property.kind === ts.SyntaxKind.PropertyAssignment) {
/** @type {ts.PropertyAssignment} */
const assignment = property
this.visitOperationConfiguration(
node,
assignment.initializer,
functionName
)
} else {
// TODO: Need to expand parsing if we want to support e.g.
// short-hand property assignment.
this.addFailureAtNode(
property,
"use traditional assignment for mutation query"
)
}
break
}
}
}
}
super.visitCallExpression(node)
}
/**
*
* @param {ts.CallExpression | ts.JsxSelfClosingElement} node
* @param {ts.Expression} expression
* @param {*} functionOrTagName
*/
visitOperationConfiguration(node, expression, functionOrTagName) {
// TODO: So many hoops to jump through without TS :/
/** @type {any} */
let any
any = expression
/** @type {ts.TaggedTemplateExpression} */
const taggedTemplate = any
if (
taggedTemplate.kind === ts.SyntaxKind.TaggedTemplateExpression &&
taggedTemplate.tag.getText() === "graphql"
) {
any = node.typeArguments && node.typeArguments[0]
/** @type {ts.TypeReferenceNode | undefined} */
const typeArgument = any
if (!typeArgument) {
const operationName = getOperationName(taggedTemplate)
const fixes = this.createFixes(
functionOrTagName.getEnd(),
0,
`<${operationName}>`,
operationName
)
this.addFailureAtNode(
functionOrTagName,
"missing operation type parameter",
fixes
)
} else {
const operationName = getOperationName(taggedTemplate)
if (
typeArgument.kind !== ts.SyntaxKind.TypeReference ||
typeArgument.typeName.getText() !== operationName
) {
const fixes = this.createFixes(
typeArgument.getStart(),
typeArgument.getWidth(),
operationName,
operationName
)
this.addFailureAtNode(
typeArgument,
`expected operation type parameter to be \`${operationName}\``,
fixes
)
}
}
} else {
this.addFailureAtNode(
taggedTemplate,
"expected a graphql`…` tagged-template"
)
}
}
/**
* @param {number} start
* @param {number} width
* @param {string} replacement
* @param {string} operationName
* @returns {Lint.Replacement[]}
*/
createFixes(start, width, replacement, operationName) {
const fixes = [new Lint.Replacement(start, width, replacement)]
if (!this.hasImportForOperation(operationName)) {
fixes.push(this.importDeclarationFixForOperation(operationName))
}
return fixes
}
/**
* @param {string} operationName
*/
importPathForOperation(operationName) {
const options = this.getOptions()[0] || {
artifactDirectory: "__generated__",
makeRelative: false,
}
if (options.makeRelative) {
throw new Error(
"[relayOperationGenericsRule] Making import declarations relative is not implemented yet."
)
}
return `${options.artifactDirectory}/${operationName}.graphql`
}
importDeclarationFixForOperation(operationName) {
const path = this.importPathForOperation(operationName)
const importDeclaration = `import { ${operationName} } from "${path}"\n`
const imports = this.getImports()
const lastImport = imports[imports.length - 1]
let start = 0
if (lastImport) {
start = lastImport.getEnd() + 1
}
return new Lint.Replacement(start, 0, importDeclaration)
}
/**
* @param {string} operationName
*/
hasImportForOperation(operationName) {
// TODO: So many hoops to jump through without TS :/
/** @type {any} */
let any
const importPath = this.importPathForOperation(operationName)
return this.getImports().some(node => {
any = node.moduleSpecifier
/** @type {ts.StringLiteral} */
const path = any
if (path.text === importPath && node.importClause) {
any = node.importClause.namedBindings
/** @type {ts.NamedImports} */
const namedBindings = any
if (namedBindings) {
return namedBindings.elements.some(
element => element.name.getText() === operationName
)
}
}
return false
})
}
}
/**
*
* @param {ts.TaggedTemplateExpression} taggedTemplate
* @return {string | null}
*/
function getOperationName(taggedTemplate) {
const template = taggedTemplate.template.getFullText()
// Strip backticks
const source = template.substring(1, template.length - 1)
const ast = parse(source)
let queryName = null
visit(ast, {
OperationDefinition(node) {
queryName = node.name.value
return BREAK
},
})
return queryName
}
module.exports = { Rule }