Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lucky-friends-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@0no-co/graphqlsp': patch
---

Detect fragment usage in `maskFragments` calls to prevent false positive unused fragment warnings
10 changes: 10 additions & 0 deletions packages/graphqlsp/src/ast/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,13 @@ export const getSchemaName = (
}
return null;
};

/** Checks if node is a maskFragments() call */
export const isMaskFragmentsCall = (
node: ts.Node
): node is ts.CallExpression => {
if (!ts.isCallExpression(node)) return false;
if (!ts.isIdentifier(node.expression)) return false;
// Only checks function name, not whether it's from gql.tada
return node.expression.escapedText === 'maskFragments';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, we only check whether maskFragments is being used by looking at the function name.
I’d like to improve this if there’s a better approach, but I haven’t found a good solution yet.

};
15 changes: 15 additions & 0 deletions packages/graphqlsp/src/ast/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,21 @@ export function findAllImports(
return sourceFile.statements.filter(ts.isImportDeclaration);
}

export function findAllMaskFragmentsCalls(
sourceFile: ts.SourceFile
): Array<ts.CallExpression> {
const result: Array<ts.CallExpression> = [];

function find(node: ts.Node): void {
if (checks.isMaskFragmentsCall(node)) {
result.push(node);
}
ts.forEachChild(node, find);
}
find(sourceFile);
return result;
}

export function bubbleUpTemplate(node: ts.Node): ts.Node {
while (
ts.isNoSubstitutionTemplateLiteral(node) ||
Expand Down
19 changes: 19 additions & 0 deletions packages/graphqlsp/src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
findAllCallExpressions,
findAllPersistedCallExpressions,
findAllTaggedTemplateNodes,
findAllMaskFragmentsCalls,
getSource,
unrollFragment,
} from './ast';
Expand Down Expand Up @@ -292,6 +293,7 @@ export function getGraphQLDiagnostics(

if (isCallExpression && shouldCheckForColocatedFragments) {
const moduleSpecifierToFragments = getColocatedFragmentNames(source, info);
const typeChecker = info.languageService.getProgram()?.getTypeChecker();

const usedFragments = new Set();
nodes.forEach(({ node }) => {
Expand All @@ -307,6 +309,23 @@ export function getGraphQLDiagnostics(
} catch (e) {}
});

// check for maskFragments() calls
const maskFragmentsCalls = findAllMaskFragmentsCalls(source);
maskFragmentsCalls.forEach(call => {
const firstArg = call.arguments[0];
if (!firstArg) return;

// Handle array of fragments: maskFragments([Fragment1, Fragment2], data)
if (ts.isArrayLiteralExpression(firstArg)) {
firstArg.elements.forEach(element => {
if (ts.isIdentifier(element)) {
const fragmentDefs = unrollFragment(element, info, typeChecker);
fragmentDefs.forEach(def => usedFragments.add(def.name.value));
}
});
}
});

Object.keys(moduleSpecifierToFragments).forEach(moduleSpecifier => {
const {
fragments: fragmentNames,
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/fixture-project-tada/fixtures/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export const graphql = initGraphQLTada<{
}>();

export type { FragmentOf, ResultOf, VariablesOf } from 'gql.tada';
export { readFragment } from 'gql.tada';
export { readFragment, maskFragments } from 'gql.tada';
7 changes: 7 additions & 0 deletions test/e2e/fixture-project-tada/fixtures/used-fragment-mask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { maskFragments } from './graphql';
import { Pokemon, PokemonFields } from './fragment';

const data = { id: '1', name: 'Pikachu', fleeRate: 0.1 };
const x = maskFragments([PokemonFields], data);

console.log(Pokemon);
2 changes: 1 addition & 1 deletion test/e2e/fixture-project-tada/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export const graphql = initGraphQLTada<{
}>();

export type { FragmentOf, ResultOf, VariablesOf } from 'gql.tada';
export { readFragment } from 'gql.tada';
export { readFragment, maskFragments } from 'gql.tada';
44 changes: 44 additions & 0 deletions test/e2e/tada.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ describe('Fragment + operations', () => {
const outfileCombo = path.join(projectPath, 'simple.ts');
const outfileTypeCondition = path.join(projectPath, 'type-condition.ts');
const outfileUnusedFragment = path.join(projectPath, 'unused-fragment.ts');
const outfileUsedFragmentMask = path.join(
projectPath,
'used-fragment-mask.ts'
);
const outfileCombinations = path.join(projectPath, 'fragment.ts');

let server: TSServer;
Expand All @@ -38,6 +42,11 @@ describe('Fragment + operations', () => {
fileContent: '// empty',
scriptKindName: 'TS',
} satisfies ts.server.protocol.OpenRequestArgs);
server.sendCommand('open', {
file: outfileUsedFragmentMask,
fileContent: '// empty',
scriptKindName: 'TS',
} satisfies ts.server.protocol.OpenRequestArgs);

server.sendCommand('updateOpen', {
openFiles: [
Expand Down Expand Up @@ -69,6 +78,13 @@ describe('Fragment + operations', () => {
'utf-8'
),
},
{
file: outfileUsedFragmentMask,
fileContent: fs.readFileSync(
path.join(projectPath, 'fixtures/used-fragment-mask.ts'),
'utf-8'
),
},
],
} satisfies ts.server.protocol.UpdateOpenRequestArgs);

Expand All @@ -88,11 +104,16 @@ describe('Fragment + operations', () => {
file: outfileUnusedFragment,
tmpfile: outfileUnusedFragment,
} satisfies ts.server.protocol.SavetoRequestArgs);
server.sendCommand('saveto', {
file: outfileUsedFragmentMask,
tmpfile: outfileUsedFragmentMask,
} satisfies ts.server.protocol.SavetoRequestArgs);
});

afterAll(() => {
try {
fs.unlinkSync(outfileUnusedFragment);
fs.unlinkSync(outfileUsedFragmentMask);
fs.unlinkSync(outfileCombinations);
fs.unlinkSync(outfileCombo);
fs.unlinkSync(outfileTypeCondition);
Expand Down Expand Up @@ -386,6 +407,29 @@ List out all Pokémon, optionally in pages`
`);
}, 30000);

it('should not warn about unused fragments when using maskFragments', async () => {
server.sendCommand('saveto', {
file: outfileUsedFragmentMask,
tmpfile: outfileUsedFragmentMask,
} satisfies ts.server.protocol.SavetoRequestArgs);

await server.waitForResponse(
e =>
e.type === 'event' &&
e.event === 'semanticDiag' &&
e.body?.file === outfileUsedFragmentMask
);

const res = server.responses.filter(
resp =>
resp.type === 'event' &&
resp.event === 'semanticDiag' &&
resp.body?.file === outfileUsedFragmentMask
);
// Should have no diagnostics about unused fragments since maskFragments uses them
expect(res[0].body.diagnostics).toMatchInlineSnapshot(`[]`);
}, 30000);

it('gives quick-info at start of word (#15)', async () => {
server.send({
seq: 11,
Expand Down