From 5e098a4a80a8e1cff4541ad34363ab2001fcda4a Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Mon, 23 Mar 2020 20:50:53 +0100 Subject: [PATCH] fix: make sure that custom parser is used if passed to process (#1438) --- .../src/GraphQLCache.ts | 13 ++++++++++--- .../src/MessageProcessor.ts | 9 +++++---- .../src/__tests__/GraphQLCache-test.ts | 10 +++++++--- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/graphql-language-service-server/src/GraphQLCache.ts b/packages/graphql-language-service-server/src/GraphQLCache.ts index 1354878823b..ee4a118d237 100644 --- a/packages/graphql-language-service-server/src/GraphQLCache.ts +++ b/packages/graphql-language-service-server/src/GraphQLCache.ts @@ -54,6 +54,7 @@ const { export async function getGraphQLCache( configDir: Uri, + parser: typeof parseDocument, extensions?: Array<(config: GraphQLConfig) => GraphQLConfig>, config?: GraphQLConfig, ): Promise { @@ -64,7 +65,7 @@ export async function getGraphQLCache( graphQLConfig = await extension(graphQLConfig); } } - return new GraphQLCache(configDir, graphQLConfig); + return new GraphQLCache(configDir, graphQLConfig, parser); } export class GraphQLCache implements GraphQLCacheInterface { @@ -75,8 +76,13 @@ export class GraphQLCache implements GraphQLCacheInterface { _typeExtensionMap: Map; _fragmentDefinitionsCache: Map>; _typeDefinitionsCache: Map>; + _parser: typeof parseDocument; - constructor(configDir: Uri, graphQLConfig: GraphQLConfig) { + constructor( + configDir: Uri, + graphQLConfig: GraphQLConfig, + parser: typeof parseDocument, + ) { this._configDir = configDir; this._graphQLConfig = graphQLConfig; this._graphQLFileListCache = new Map(); @@ -84,6 +90,7 @@ export class GraphQLCache implements GraphQLCacheInterface { this._fragmentDefinitionsCache = new Map(); this._typeDefinitionsCache = new Map(); this._typeExtensionMap = new Map(); + this._parser = parser; } getGraphQLConfig = (): GraphQLConfig => this._graphQLConfig; @@ -797,7 +804,7 @@ export class GraphQLCache implements GraphQLCacheInterface { let queries: CachedContent[] = []; if (content.trim().length !== 0) { try { - queries = parseDocument(content, filePath); + queries = this._parser(content, filePath); if (queries.length === 0) { // still resolve with an empty ast resolve({ diff --git a/packages/graphql-language-service-server/src/MessageProcessor.ts b/packages/graphql-language-service-server/src/MessageProcessor.ts index 0ca20a07a5d..be4f6551654 100644 --- a/packages/graphql-language-service-server/src/MessageProcessor.ts +++ b/packages/graphql-language-service-server/src/MessageProcessor.ts @@ -86,7 +86,7 @@ export class MessageProcessor { this._extensions = extensions; this._fileExtensions = fileExtensions; this._graphQLConfig = config; - this._parser = parser || parseDocument; + this._parser = parser ?? parseDocument; } async handleInitializeRequest( @@ -118,6 +118,7 @@ export class MessageProcessor { this._graphQLCache = await getGraphQLCache( rootPath, + this._parser, this._extensions, this._graphQLConfig, ); @@ -160,7 +161,7 @@ export class MessageProcessor { if ('text' in textDocument && textDocument.text) { // textDocument/didSave does not pass in the text content. // Only run the below function if text is passed in. - contents = parseDocument(textDocument.text, uri, this._fileExtensions); + contents = this._parser(textDocument.text, uri, this._fileExtensions); this._invalidateCache(textDocument, uri, contents); } else { @@ -230,7 +231,7 @@ export class MessageProcessor { // If it's a .js file, try parsing the contents to see if GraphQL queries // exist. If not found, delete from the cache. - const contents = parseDocument( + const contents = this._parser( contentChange.text, uri, this._fileExtensions, @@ -449,7 +450,7 @@ export class MessageProcessor { ) { const uri = change.uri; const text: string = readFileSync(new URL(uri).pathname).toString(); - const contents = parseDocument(text, uri, this._fileExtensions); + const contents = this._parser(text, uri, this._fileExtensions); this._updateFragmentDefinition(uri, contents); this._updateObjectTypeDefinition(uri, contents); diff --git a/packages/graphql-language-service-server/src/__tests__/GraphQLCache-test.ts b/packages/graphql-language-service-server/src/__tests__/GraphQLCache-test.ts index 54947769524..25d32e16e95 100644 --- a/packages/graphql-language-service-server/src/__tests__/GraphQLCache-test.ts +++ b/packages/graphql-language-service-server/src/__tests__/GraphQLCache-test.ts @@ -32,11 +32,11 @@ function wihtoutASTNode(definition: any) { describe('GraphQLCache', () => { const configDir = __dirname; let graphQLRC; - let cache = new GraphQLCache(configDir, graphQLRC); + let cache = new GraphQLCache(configDir, graphQLRC, parseDocument); beforeEach(async () => { graphQLRC = await loadConfig({ rootDir: configDir }); - cache = new GraphQLCache(configDir, graphQLRC); + cache = new GraphQLCache(configDir, graphQLRC, parseDocument); }); afterEach(() => { @@ -52,7 +52,11 @@ describe('GraphQLCache', () => { }; }; const extensions = [extension]; - const cacheWithExtensions = await getGraphQLCache(configDir, extensions); + const cacheWithExtensions = await getGraphQLCache( + configDir, + parseDocument, + extensions, + ); const config = cacheWithExtensions.getGraphQLConfig(); expect('extension' in config).toBe(true); expect((config as any).extension).toBe('extension-used');