/
findGraphQLTags.js
195 lines (182 loc) · 5.02 KB
/
findGraphQLTags.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
/**
* Copyright (c) 2019 GraphQL Contributors
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import { Position, Range } from 'graphql-language-service-utils';
import { parse } from '@babel/parser';
// Attempt to be as inclusive as possible of source text.
const PARSER_OPTIONS = {
allowImportExportEverywhere: true,
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true,
sourceType: 'module',
plugins: [
'flow',
'jsx',
'doExpressions',
'objectRestSpread',
['decorators', { decoratorsBeforeExport: false }],
'classProperties',
'classPrivateProperties',
'classPrivateMethods',
'exportDefaultFrom',
'exportNamespaceFrom',
'asyncGenerators',
'functionBind',
'functionSent',
'dynamicImport',
'numericSeparator',
'optionalChaining',
'importMeta',
'bigInt',
'optionalCatchBinding',
'throwExpressions',
['pipelineOperator', { proposal: 'minimal' }],
'nullishCoalescingOperator',
],
strictMode: false,
};
export function findGraphQLTags(
text: string
): Array<{ tag: string, template: string, range: Range }> {
const result = [];
const ast = parse(text, PARSER_OPTIONS);
const visitors = {
CallExpression: node => {
const callee = node.callee;
if (
!(
(callee.type === 'Identifier' &&
CREATE_CONTAINER_FUNCTIONS[callee.name]) ||
(callee.kind === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
callee.object.value === 'Relay' &&
callee.property.type === 'Identifier' &&
CREATE_CONTAINER_FUNCTIONS[callee.property.name])
)
) {
traverse(node, visitors);
return;
}
const fragments = node.arguments[1];
if (fragments.type === 'ObjectExpression') {
fragments.properties.forEach(property => {
const tagName = getGraphQLTagName(property.value.tag);
const template = getGraphQLText(property.value.quasi);
if (tagName) {
const loc = property.value.loc;
const range = new Range(
new Position(loc.start.line - 1, loc.start.column),
new Position(loc.end.line - 1, loc.end.column)
);
result.push({
tag: tagName,
template,
range,
});
}
});
} else {
const tagName = getGraphQLTagName(fragments.tag);
const template = getGraphQLText(fragments.quasi);
if (tagName) {
const loc = fragments.loc;
const range = new Range(
new Position(loc.start.line - 1, loc.start.column),
new Position(loc.end.line - 1, loc.end.column)
);
result.push({
tag: tagName,
template,
range,
});
}
}
// Visit remaining arguments
for (let ii = 2; ii < node.arguments.length; ii++) {
visit(node.arguments[ii], visitors);
}
},
TaggedTemplateExpression: node => {
const tagName = getGraphQLTagName(node.tag);
if (tagName) {
const loc = node.quasi.quasis[0].loc;
const range = new Range(
new Position(loc.start.line - 1, loc.start.column),
new Position(loc.end.line - 1, loc.end.column)
);
result.push({
tag: tagName,
template: node.quasi.quasis[0].value.raw,
range,
});
}
},
};
visit(ast, visitors);
return result;
}
const CREATE_CONTAINER_FUNCTIONS = {
createFragmentContainer: true,
createPaginationContainer: true,
createRefetchContainer: true,
};
const IDENTIFIERS = { graphql: true, gql: true };
const IGNORED_KEYS = {
comments: true,
end: true,
leadingComments: true,
loc: true,
name: true,
start: true,
trailingComments: true,
type: true,
};
function getGraphQLTagName(tag) {
if (tag.type === 'Identifier' && IDENTIFIERS.hasOwnProperty(tag.name)) {
return tag.name;
} else if (
tag.type === 'MemberExpression' &&
tag.object.type === 'Identifier' &&
tag.object.name === 'graphql' &&
tag.property.type === 'Identifier' &&
tag.property.name === 'experimental'
) {
return 'graphql.experimental';
}
return null;
}
function getGraphQLText(quasi) {
const quasis = quasi.quasis;
return quasis[0].value.raw;
}
function visit(node, visitors) {
const fn = visitors[node.type];
if (fn != null) {
fn(node);
return;
}
traverse(node, visitors);
}
function traverse(node, visitors) {
for (const key in node) {
if (IGNORED_KEYS[key]) {
continue;
}
const prop = node[key];
if (prop && typeof prop === 'object' && typeof prop.type === 'string') {
visit(prop, visitors);
} else if (Array.isArray(prop)) {
prop.forEach(item => {
if (item && typeof item === 'object' && typeof item.type === 'string') {
visit(item, visitors);
}
});
}
}
}