Skip to content

Commit 798d782

Browse files
committed
coverage for locateCommand
1 parent 3d033a4 commit 798d782

File tree

2 files changed

+117
-36
lines changed

2 files changed

+117
-36
lines changed

packages/graphql-language-service-server/src/MessageProcessor.ts

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -895,42 +895,13 @@ export class MessageProcessor {
895895
}
896896
}
897897
if (locateCommand && result && result?.printedName) {
898-
try {
899-
const locateResult = locateCommand(
900-
project.name,
901-
result.printedName,
902-
{
903-
node: result.node,
904-
type: result.type,
905-
project,
906-
},
907-
);
908-
if (typeof locateResult === 'string') {
909-
const [uri, startLine = '1', endLine = '1'] =
910-
locateResult.split(':');
911-
return {
912-
uri,
913-
range: new Range(
914-
new Position(parseInt(startLine, 10), 0),
915-
new Position(parseInt(endLine, 10), 0),
916-
),
917-
};
918-
}
919-
return (
920-
locateResult || {
921-
uri: res.path,
922-
range: defRange,
923-
}
924-
);
925-
} catch (error) {
926-
this._logger.error(
927-
'There was an error executing user defined locateCommand\n\n' +
928-
(error as Error).toString(),
929-
);
930-
return {
931-
uri: res.path,
932-
range: defRange,
933-
};
898+
const locateResult = this._getCustomLocateResult(
899+
project,
900+
result,
901+
locateCommand,
902+
);
903+
if (locateResult) {
904+
return locateResult;
934905
}
935906
}
936907
return {
@@ -950,6 +921,39 @@ export class MessageProcessor {
950921
);
951922
return formatted;
952923
}
924+
_getCustomLocateResult(
925+
project: GraphQLProjectConfig,
926+
result: DefinitionQueryResponse,
927+
locateCommand: LocateCommand,
928+
) {
929+
if (!result.printedName) {
930+
return null;
931+
}
932+
try {
933+
const locateResult = locateCommand(project.name, result.printedName, {
934+
node: result.node,
935+
type: result.type,
936+
project,
937+
});
938+
if (typeof locateResult === 'string') {
939+
const [uri, startLine = '1', endLine = '1'] = locateResult.split(':');
940+
return {
941+
uri,
942+
range: new Range(
943+
new Position(parseInt(startLine, 10), 0),
944+
new Position(parseInt(endLine, 10), 0),
945+
),
946+
};
947+
}
948+
return locateResult;
949+
} catch (error) {
950+
this._logger.error(
951+
'There was an error executing user defined locateCommand\n\n' +
952+
(error as Error).toString(),
953+
);
954+
return null;
955+
}
956+
}
953957

954958
public async handleDocumentSymbolRequest(
955959
params: DocumentSymbolParams,

packages/graphql-language-service-server/src/__tests__/MessageProcessor.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,83 @@ describe('MessageProcessor', () => {
410410
const result = await messageProcessor.handleDefinitionRequest(test);
411411
await expect(result[0].uri).toEqual(`${queryPathUri}/test3.graphql`);
412412
});
413+
414+
it('retrieves custom results from locateCommand', async () => {
415+
jest.setTimeout(10000);
416+
const validQuery = `
417+
{
418+
hero(episode: EMPIRE){
419+
...testFragment
420+
}
421+
}
422+
`;
423+
424+
const newDocument = {
425+
textDocument: {
426+
text: validQuery,
427+
uri: `${queryPathUri}/test3.graphql`,
428+
version: 1,
429+
},
430+
};
431+
messageProcessor._getCachedDocument = (_uri: string) => ({
432+
version: 1,
433+
contents: [
434+
{
435+
query: validQuery,
436+
range: new Range(new Position(0, 0), new Position(20, 4)),
437+
},
438+
],
439+
});
440+
441+
await messageProcessor.handleDidOpenOrSaveNotification(newDocument);
442+
443+
const test = {
444+
position: new Position(3, 15),
445+
textDocument: newDocument.textDocument,
446+
};
447+
const result = await messageProcessor._languageService.getDefinition(
448+
validQuery,
449+
test.position,
450+
test.textDocument.uri,
451+
);
452+
const project = messageProcessor._graphQLCache.getProjectForFile(
453+
test.textDocument.uri,
454+
)!;
455+
456+
const customResult = messageProcessor._getCustomLocateResult(
457+
project,
458+
{ definitions: result, printedName: 'example' },
459+
() => 'hello',
460+
);
461+
expect(customResult.uri).toEqual(`hello`);
462+
463+
const customResult2 = messageProcessor._getCustomLocateResult(
464+
project,
465+
{ definitions: result, printedName: 'example' },
466+
() => 'hello:2:4',
467+
);
468+
expect(customResult2.uri).toEqual(`hello`);
469+
expect(customResult2.range.start.line).toEqual(2);
470+
expect(customResult2.range.start.character).toEqual(0);
471+
expect(customResult2.range.end.line).toEqual(4);
472+
473+
const customResult3 = messageProcessor._getCustomLocateResult(
474+
project,
475+
{ definitions: result, printedName: 'example' },
476+
() => ({
477+
uri: 'hello1',
478+
range: {
479+
start: { character: 2, line: 2 },
480+
end: { character: 4, line: 4 },
481+
},
482+
}),
483+
);
484+
expect(customResult3.uri).toEqual(`hello1`);
485+
expect(customResult3.range.start.line).toEqual(2);
486+
expect(customResult3.range.start.character).toEqual(2);
487+
expect(customResult3.range.end.line).toEqual(4);
488+
expect(customResult3.range.end.character).toEqual(4);
489+
});
413490
it('runs hover requests', async () => {
414491
const validQuery = `
415492
{

0 commit comments

Comments
 (0)