@shopify/hydrogen-codegen 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,383 +0,0 @@
1
- import {
2
- isIdentifier,
3
- isImportDefaultSpecifier,
4
- isImportSpecifier,
5
- isTemplateLiteral,
6
- isTSAsExpression,
7
- isTSTypeReference,
8
- isVariableDeclarator,
9
- } from '@babel/types';
10
- import {asArray} from '@graphql-tools/utils';
11
- import {freeText} from './utils.js';
12
- const defaults = {
13
- modules: [
14
- {
15
- name: 'graphql-tag',
16
- },
17
- {
18
- name: 'graphql-tag.macro',
19
- },
20
- {
21
- name: '@apollo/client',
22
- identifier: 'gql',
23
- },
24
- {
25
- name: '@apollo/client/core',
26
- identifier: 'gql',
27
- },
28
- {
29
- name: 'apollo-angular',
30
- identifier: 'gql',
31
- },
32
- {
33
- name: 'gatsby',
34
- identifier: 'graphql',
35
- },
36
- {
37
- name: 'apollo-server-express',
38
- identifier: 'gql',
39
- },
40
- {
41
- name: 'apollo-server',
42
- identifier: 'gql',
43
- },
44
- {
45
- name: 'react-relay',
46
- identifier: 'graphql',
47
- },
48
- {
49
- name: 'react-relay/hooks',
50
- identifier: 'graphql',
51
- },
52
- {
53
- name: 'relay-runtime',
54
- identifier: 'graphql',
55
- },
56
- {
57
- name: 'babel-plugin-relay/macro',
58
- identifier: 'graphql',
59
- },
60
- {
61
- name: 'apollo-boost',
62
- identifier: 'gql',
63
- },
64
- {
65
- name: 'apollo-server-koa',
66
- identifier: 'gql',
67
- },
68
- {
69
- name: 'apollo-server-hapi',
70
- identifier: 'gql',
71
- },
72
- {
73
- name: 'apollo-server-fastify',
74
- identifier: 'gql',
75
- },
76
- {
77
- name: ' apollo-server-lambda',
78
- identifier: 'gql',
79
- },
80
- {
81
- name: 'apollo-server-micro',
82
- identifier: 'gql',
83
- },
84
- {
85
- name: 'apollo-server-azure-functions',
86
- identifier: 'gql',
87
- },
88
- {
89
- name: 'apollo-server-cloud-functions',
90
- identifier: 'gql',
91
- },
92
- {
93
- name: 'apollo-server-cloudflare',
94
- identifier: 'gql',
95
- },
96
- {
97
- name: 'graphql.macro',
98
- identifier: 'gql',
99
- },
100
- {
101
- name: '@urql/core',
102
- identifier: 'gql',
103
- },
104
- {
105
- name: 'urql',
106
- identifier: 'gql',
107
- },
108
- {
109
- name: '@urql/preact',
110
- identifier: 'gql',
111
- },
112
- {
113
- name: '@urql/svelte',
114
- identifier: 'gql',
115
- },
116
- {
117
- name: '@urql/vue',
118
- identifier: 'gql',
119
- },
120
- ],
121
- gqlMagicComment: 'graphql',
122
- globalGqlIdentifierName: ['gql', 'graphql'],
123
- };
124
- function defaultPluckStringFromFile(code, {start, end}, options = {}) {
125
- return freeText(
126
- code
127
- // Slice quotes
128
- .slice(start + 1, end - 1)
129
- // Erase string interpolations as we gonna export everything as a single
130
- // string anyway
131
- .replace(/\$\{[^}]*\}/g, '')
132
- .split('\\`')
133
- .join('`'),
134
- options.skipIndent,
135
- );
136
- }
137
- function defaultIsGqlTemplateLiteral(node, options) {
138
- const leadingComments = node.leadingComments;
139
- if (!leadingComments) {
140
- return;
141
- }
142
- if (!leadingComments.length) {
143
- return;
144
- }
145
- const leadingComment = leadingComments[leadingComments.length - 1];
146
- const leadingCommentValue = leadingComment.value.trim().toLowerCase();
147
- if (leadingCommentValue === options.gqlMagicComment) {
148
- return true;
149
- }
150
- return false;
151
- }
152
- export default (code, out, options = {}) => {
153
- // Apply defaults to options
154
- let {
155
- modules = [],
156
- globalGqlIdentifierName,
157
- gqlMagicComment,
158
- skipIndent,
159
- isGqlTemplateLiteral = defaultIsGqlTemplateLiteral,
160
- pluckStringFromFile = defaultPluckStringFromFile,
161
- } = {
162
- ...defaults,
163
- ...options,
164
- };
165
- // Prevent case related potential errors
166
- gqlMagicComment = gqlMagicComment.toLowerCase();
167
- // normalize `name` and `identifier` values
168
- modules = modules.map((mod) => {
169
- return {
170
- name: mod.name,
171
- identifier: mod.identifier && mod.identifier.toLowerCase(),
172
- };
173
- });
174
- globalGqlIdentifierName = asArray(globalGqlIdentifierName).map((s) =>
175
- s.toLowerCase(),
176
- );
177
- const hooksOptions = {
178
- skipIndent,
179
- gqlMagicComment,
180
- modules,
181
- globalGqlIdentifierName,
182
- };
183
- // Keep imported identifiers
184
- // import gql from 'graphql-tag' -> gql
185
- // import { graphql } from 'gatsby' -> graphql
186
- // Will result with ['gql', 'graphql']
187
- const definedIdentifierNames = [];
188
- const alreadyProcessedOperationsCache = new Set();
189
- // Will accumulate all template literals
190
- const gqlTemplateLiterals = [];
191
- // Check if package is registered
192
- function isValidPackage(name) {
193
- return modules.some(
194
- (pkg) =>
195
- pkg.name && name && pkg.name.toLowerCase() === name.toLowerCase(),
196
- );
197
- }
198
- // Check if identifier is defined and imported from registered packages
199
- function isValidIdentifier(name) {
200
- return (
201
- definedIdentifierNames.some((id) => id === name) ||
202
- globalGqlIdentifierName.includes(name)
203
- );
204
- }
205
- const addTemplateLiteralToResult = (content) => {
206
- const cacheKey = `end/${content.end}/start/${content.start}/${content.content}`;
207
- if (alreadyProcessedOperationsCache.has(cacheKey)) {
208
- return;
209
- }
210
- alreadyProcessedOperationsCache.add(cacheKey);
211
- gqlTemplateLiterals.push(content);
212
- };
213
- // Push all template literals leaded by graphql magic comment
214
- // e.g. /* GraphQL */ `query myQuery {}` -> query myQuery {}
215
- const pluckMagicTemplateLiteral = (node, takeExpression = false) => {
216
- if (!isGqlTemplateLiteral(node, hooksOptions)) {
217
- return;
218
- }
219
- const nodeToUse = takeExpression ? node.expression : node;
220
- const gqlTemplateLiteral = pluckStringFromFile(
221
- code,
222
- nodeToUse,
223
- hooksOptions,
224
- );
225
- if (gqlTemplateLiteral) {
226
- addTemplateLiteralToResult({
227
- content: gqlTemplateLiteral,
228
- loc: node.loc,
229
- end: node.end,
230
- start: node.start,
231
- });
232
- }
233
- };
234
- const visitor = {
235
- CallExpression: {
236
- enter(path) {
237
- // Find the identifier name used from graphql-tag, commonJS
238
- // e.g. import gql from 'graphql-tag' -> gql
239
- const arg0 = path.node.arguments[0];
240
- if (
241
- 'name' in path.node.callee &&
242
- path.node.callee.name === 'require' &&
243
- 'value' in arg0 &&
244
- typeof arg0.value === 'string' &&
245
- isValidPackage(arg0.value)
246
- ) {
247
- if (!isVariableDeclarator(path.parent)) {
248
- return;
249
- }
250
- if (!isIdentifier(path.parent.id)) {
251
- return;
252
- }
253
- definedIdentifierNames.push(path.parent.id.name);
254
- return;
255
- }
256
- // Checks to see if a node represents a typescript '<expression> as const' expression
257
- function isTSAsConstExpression(node) {
258
- return (
259
- isTSAsExpression(node) &&
260
- isTSTypeReference(node.typeAnnotation) &&
261
- isIdentifier(node.typeAnnotation.typeName) &&
262
- node.typeAnnotation.typeName.name === 'const'
263
- );
264
- }
265
- // Extract template literal from as const expression if applicable
266
- // e.g. gql(`query myQuery {}` as const)
267
- const unwrappedExpression = isTSAsConstExpression(arg0)
268
- ? arg0.expression
269
- : arg0;
270
- // Push strings template literals to gql calls
271
- // e.g. gql(`query myQuery {}`) -> query myQuery {}
272
- if (
273
- isIdentifier(path.node.callee) &&
274
- isValidIdentifier(path.node.callee.name) &&
275
- isTemplateLiteral(unwrappedExpression)
276
- ) {
277
- const {start, end, loc} = unwrappedExpression;
278
- if (start != null && end != null && start != null && loc != null) {
279
- const gqlTemplateLiteral = pluckStringFromFile(
280
- code,
281
- unwrappedExpression,
282
- hooksOptions,
283
- );
284
- // If the entire template was made out of interpolations it should be an empty
285
- // string by now and thus should be ignored
286
- if (gqlTemplateLiteral) {
287
- addTemplateLiteralToResult({
288
- content: gqlTemplateLiteral,
289
- loc,
290
- end,
291
- start,
292
- });
293
- }
294
- }
295
- }
296
- },
297
- },
298
- ImportDeclaration: {
299
- enter(path) {
300
- // Find the identifier name used from graphql-tag, es6
301
- // e.g. import gql from 'graphql-tag' -> gql
302
- if (!isValidPackage(path.node.source.value)) {
303
- return;
304
- }
305
- const moduleNode = modules.find(
306
- (pkg) =>
307
- pkg.name.toLowerCase() === path.node.source.value.toLowerCase(),
308
- );
309
- if (moduleNode == null) {
310
- return;
311
- }
312
- const gqlImportSpecifier = path.node.specifiers.find(
313
- (importSpecifier) => {
314
- // When it's a default import and registered package has no named identifier
315
- if (
316
- isImportDefaultSpecifier(importSpecifier) &&
317
- !moduleNode.identifier
318
- ) {
319
- return true;
320
- }
321
- // When it's a named import that matches registered package's identifier
322
- if (
323
- isImportSpecifier(importSpecifier) &&
324
- 'name' in importSpecifier.imported &&
325
- importSpecifier.imported.name === moduleNode.identifier
326
- ) {
327
- return true;
328
- }
329
- return false;
330
- },
331
- );
332
- if (!gqlImportSpecifier) {
333
- return;
334
- }
335
- definedIdentifierNames.push(gqlImportSpecifier.local.name);
336
- },
337
- },
338
- ExpressionStatement: {
339
- exit(path) {
340
- // Push all template literals leaded by graphql magic comment
341
- // e.g. /* GraphQL */ `query myQuery {}` -> query myQuery {}
342
- if (!isTemplateLiteral(path.node.expression)) {
343
- return;
344
- }
345
- pluckMagicTemplateLiteral(path.node, true);
346
- },
347
- },
348
- TemplateLiteral: {
349
- exit(path) {
350
- pluckMagicTemplateLiteral(path.node);
351
- },
352
- },
353
- TaggedTemplateExpression: {
354
- exit(path) {
355
- // Push all template literals provided to the found identifier name
356
- // e.g. gql `query myQuery {}` -> query myQuery {}
357
- if (
358
- !isIdentifier(path.node.tag) ||
359
- !isValidIdentifier(path.node.tag.name)
360
- ) {
361
- return;
362
- }
363
- const gqlTemplateLiteral = pluckStringFromFile(
364
- code,
365
- path.node.quasi,
366
- hooksOptions,
367
- );
368
- if (gqlTemplateLiteral) {
369
- addTemplateLiteralToResult({
370
- content: gqlTemplateLiteral,
371
- end: path.node.quasi.end,
372
- start: path.node.quasi.start,
373
- loc: path.node.quasi.loc,
374
- });
375
- }
376
- },
377
- },
378
- exit() {
379
- out.returnValue = gqlTemplateLiterals;
380
- },
381
- };
382
- return visitor;
383
- };