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)

Changed files
+34
.changeset
packages
graphqlsp
+5
.changeset/wide-cities-grin.md
···
+
---
+
'@0no-co/graphqlsp': minor
+
---
+
+
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
···
if (allFields.find(x => x.startsWith(joined + '.'))) {
pathParts.push(foundRef.text);
}
+
+
// When we encounter an external function where we use this identifier
+
// we'll mark all of the sub-fields as used as we consider this a bail
+
// scenario.
+
if (ts.isCallExpression(foundRef.parent)) {
+
const callExpression = foundRef.parent;
+
return callExpression.arguments.flatMap(arg => {
+
let parts = [...pathParts];
+
let reference = arg;
+
+
while (ts.isPropertyAccessExpression(reference)) {
+
const joined = [...parts, reference.name.text].join('.');
+
if (allFields.find(x => x.startsWith(joined + '.'))) {
+
parts.push(reference.name.text);
+
}
+
reference = reference.expression;
+
}
+
+
if (ts.isIdentifier(reference)) {
+
const joined = [...parts, reference.getText()].join('.');
+
if (allFields.find(x => x.startsWith(joined + '.'))) {
+
parts.push(reference.getText());
+
}
+
}
+
+
const joined = parts.join('.');
+
return allFields.filter(x => x.startsWith(joined + '.'));
+
});
+
}
} else if (
ts.isPropertyAccessExpression(foundRef) &&
foundRef.name.text === 'at' &&