Skip to content

Commit

Permalink
coverage for locateCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
acao committed May 19, 2024
1 parent 3d033a4 commit 798d782
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 36 deletions.
76 changes: 40 additions & 36 deletions packages/graphql-language-service-server/src/MessageProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -895,42 +895,13 @@ export class MessageProcessor {
}
}
if (locateCommand && result && result?.printedName) {
try {
const locateResult = locateCommand(
project.name,
result.printedName,
{
node: result.node,
type: result.type,
project,
},
);
if (typeof locateResult === 'string') {
const [uri, startLine = '1', endLine = '1'] =
locateResult.split(':');
return {
uri,
range: new Range(
new Position(parseInt(startLine, 10), 0),
new Position(parseInt(endLine, 10), 0),
),
};
}
return (
locateResult || {
uri: res.path,
range: defRange,
}
);
} catch (error) {
this._logger.error(
'There was an error executing user defined locateCommand\n\n' +
(error as Error).toString(),
);
return {
uri: res.path,
range: defRange,
};
const locateResult = this._getCustomLocateResult(
project,
result,
locateCommand,
);
if (locateResult) {
return locateResult;
}
}
return {
Expand All @@ -950,6 +921,39 @@ export class MessageProcessor {
);
return formatted;
}
_getCustomLocateResult(
project: GraphQLProjectConfig,
result: DefinitionQueryResponse,
locateCommand: LocateCommand,
) {
if (!result.printedName) {
return null;
}
try {
const locateResult = locateCommand(project.name, result.printedName, {
node: result.node,
type: result.type,
project,
});
if (typeof locateResult === 'string') {
const [uri, startLine = '1', endLine = '1'] = locateResult.split(':');
return {
uri,
range: new Range(
new Position(parseInt(startLine, 10), 0),
new Position(parseInt(endLine, 10), 0),
),
};
}
return locateResult;
} catch (error) {
this._logger.error(
'There was an error executing user defined locateCommand\n\n' +
(error as Error).toString(),
);
return null;
}
}

public async handleDocumentSymbolRequest(
params: DocumentSymbolParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,83 @@ describe('MessageProcessor', () => {
const result = await messageProcessor.handleDefinitionRequest(test);
await expect(result[0].uri).toEqual(`${queryPathUri}/test3.graphql`);
});

it('retrieves custom results from locateCommand', async () => {
jest.setTimeout(10000);
const validQuery = `
{
hero(episode: EMPIRE){
...testFragment
}
}
`;

const newDocument = {
textDocument: {
text: validQuery,
uri: `${queryPathUri}/test3.graphql`,
version: 1,
},
};
messageProcessor._getCachedDocument = (_uri: string) => ({
version: 1,
contents: [
{
query: validQuery,
range: new Range(new Position(0, 0), new Position(20, 4)),
},
],
});

await messageProcessor.handleDidOpenOrSaveNotification(newDocument);

const test = {
position: new Position(3, 15),
textDocument: newDocument.textDocument,
};
const result = await messageProcessor._languageService.getDefinition(
validQuery,
test.position,
test.textDocument.uri,
);
const project = messageProcessor._graphQLCache.getProjectForFile(
test.textDocument.uri,
)!;

const customResult = messageProcessor._getCustomLocateResult(
project,
{ definitions: result, printedName: 'example' },
() => 'hello',
);
expect(customResult.uri).toEqual(`hello`);

Check failure on line 461 in packages/graphql-language-service-server/src/__tests__/MessageProcessor.test.ts

View workflow job for this annotation

GitHub Actions / ESLint

Strings must use singlequote

const customResult2 = messageProcessor._getCustomLocateResult(
project,
{ definitions: result, printedName: 'example' },
() => 'hello:2:4',
);
expect(customResult2.uri).toEqual(`hello`);

Check failure on line 468 in packages/graphql-language-service-server/src/__tests__/MessageProcessor.test.ts

View workflow job for this annotation

GitHub Actions / ESLint

Strings must use singlequote
expect(customResult2.range.start.line).toEqual(2);
expect(customResult2.range.start.character).toEqual(0);
expect(customResult2.range.end.line).toEqual(4);

const customResult3 = messageProcessor._getCustomLocateResult(
project,
{ definitions: result, printedName: 'example' },
() => ({
uri: 'hello1',
range: {
start: { character: 2, line: 2 },
end: { character: 4, line: 4 },
},
}),
);
expect(customResult3.uri).toEqual(`hello1`);

Check failure on line 484 in packages/graphql-language-service-server/src/__tests__/MessageProcessor.test.ts

View workflow job for this annotation

GitHub Actions / ESLint

Strings must use singlequote
expect(customResult3.range.start.line).toEqual(2);
expect(customResult3.range.start.character).toEqual(2);
expect(customResult3.range.end.line).toEqual(4);
expect(customResult3.range.end.character).toEqual(4);
});
it('runs hover requests', async () => {
const validQuery = `
{
Expand Down

0 comments on commit 798d782

Please sign in to comment.