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

Configure Feed

Select the types of activity you want to include in your feed.

fix(lsp): surface unused fragment definition leaves (#299)

authored by

Jovi De Croock and committed by
GitHub
19551c57 4ca851ce

+27 -5
+5
.changeset/wise-coins-look.md
··· 1 + --- 2 + '@0no-co/graphqlsp': patch 3 + --- 4 + 5 + Correctly identify unused fields on a fragment-definition, these have no parent to group by so we display them as unused leaves
+1
packages/example-tada/src/Pokemon.tsx
··· 3 3 export const Fields = { Pokemon: graphql(` 4 4 fragment Pok on Pokemon { 5 5 resistant 6 + types 6 7 }`) 7 8 } 8 9
+21 -5
packages/graphqlsp/src/fieldUsage.ts
··· 495 495 const unused = allPaths.filter(x => !allAccess.includes(x)); 496 496 const aggregatedUnusedFields = new Set<string>(); 497 497 const unusedChildren: { [key: string]: Set<string> } = {}; 498 + const unusedFragmentLeaf = new Set<string>(); 498 499 unused.forEach(unusedField => { 499 500 const split = unusedField.split('.'); 500 501 split.pop(); 501 502 const parentField = split.join('.'); 502 503 const loc = fieldToLoc.get(parentField); 503 - if (!loc) return; 504 504 505 - aggregatedUnusedFields.add(parentField); 506 - if (unusedChildren[parentField]) { 507 - unusedChildren[parentField].add(unusedField); 505 + if (loc) { 506 + aggregatedUnusedFields.add(parentField); 507 + if (unusedChildren[parentField]) { 508 + unusedChildren[parentField].add(unusedField); 509 + } else { 510 + unusedChildren[parentField] = new Set([unusedField]); 511 + } 508 512 } else { 509 - unusedChildren[parentField] = new Set([unusedField]); 513 + unusedFragmentLeaf.add(unusedField); 510 514 } 511 515 }); 512 516 ··· 522 526 messageText: `Field(s) ${[...unusedFields] 523 527 .map(x => `'${x}'`) 524 528 .join(', ')} are not used.`, 529 + }); 530 + }); 531 + 532 + unusedFragmentLeaf.forEach(field => { 533 + const loc = fieldToLoc.get(field)!; 534 + diagnostics.push({ 535 + file: source, 536 + length: loc.length, 537 + start: node.getStart() + loc.start + 1, 538 + category: ts.DiagnosticCategory.Warning, 539 + code: UNUSED_FIELD_CODE, 540 + messageText: `Field ${field} is not used.`, 525 541 }); 526 542 }); 527 543 });