Mirror: TypeScript LSP plugin that finds GraphQL documents in your code and provides diagnostics, auto-complete and hover-information.

Add bail case for call expressions (#374)

authored by

Jovi De Croock and committed by
GitHub
f6a7d305 7de8f3f2

+34
+5
.changeset/wide-cities-grin.md
··· 1 + --- 2 + '@0no-co/graphqlsp': minor 3 + --- 4 + 5 + Improves field-usage tracking, we bail when the identifier is passed into a function, this bail is intended so we don't have to traverse the whole codebase tracing down usage.
+29
packages/graphqlsp/src/fieldUsage.ts
··· 283 283 if (allFields.find(x => x.startsWith(joined + '.'))) { 284 284 pathParts.push(foundRef.text); 285 285 } 286 + 287 + // When we encounter an external function where we use this identifier 288 + // we'll mark all of the sub-fields as used as we consider this a bail 289 + // scenario. 290 + if (ts.isCallExpression(foundRef.parent)) { 291 + const callExpression = foundRef.parent; 292 + return callExpression.arguments.flatMap(arg => { 293 + let parts = [...pathParts]; 294 + let reference = arg; 295 + 296 + while (ts.isPropertyAccessExpression(reference)) { 297 + const joined = [...parts, reference.name.text].join('.'); 298 + if (allFields.find(x => x.startsWith(joined + '.'))) { 299 + parts.push(reference.name.text); 300 + } 301 + reference = reference.expression; 302 + } 303 + 304 + if (ts.isIdentifier(reference)) { 305 + const joined = [...parts, reference.getText()].join('.'); 306 + if (allFields.find(x => x.startsWith(joined + '.'))) { 307 + parts.push(reference.getText()); 308 + } 309 + } 310 + 311 + const joined = parts.join('.'); 312 + return allFields.filter(x => x.startsWith(joined + '.')); 313 + }); 314 + } 286 315 } else if ( 287 316 ts.isPropertyAccessExpression(foundRef) && 288 317 foundRef.name.text === 'at' &&