From 4a3bf20fb7cb999ab2cf2260c2efbf213a886fd9 Mon Sep 17 00:00:00 2001 From: Rikki Schulte Date: Tue, 30 Nov 2021 19:49:18 +0100 Subject: [PATCH] remove filePathToUrl as well, fix tests --- .../src/GraphQLCache.ts | 3 +- .../src/MessageProcessor.ts | 11 ++-- .../src/__tests__/MessageProcessor-test.ts | 66 +++++++++++-------- 3 files changed, 42 insertions(+), 38 deletions(-) diff --git a/packages/graphql-language-service-server/src/GraphQLCache.ts b/packages/graphql-language-service-server/src/GraphQLCache.ts index d6b3ddee309..fc3fb2c25fb 100644 --- a/packages/graphql-language-service-server/src/GraphQLCache.ts +++ b/packages/graphql-language-service-server/src/GraphQLCache.ts @@ -31,7 +31,6 @@ import { parseDocument } from './parseDocument'; import stringToHash from './stringToHash'; import glob from 'glob'; import { LoadConfigOptions } from './types'; -import { pathToFileURL } from 'url'; import { URI } from 'vscode-uri'; // Maximum files to read when processing GraphQL files. @@ -366,7 +365,7 @@ export class GraphQLCache implements GraphQLCacheInterface { // the docs indicate that is what's there :shrug: const cacheEntry = globResult.statCache[filePath] as fs.Stats; return { - filePath: pathToFileURL(filePath).toString(), + filePath: URI.parse(filePath).toString(), mtime: Math.trunc(cacheEntry.mtime.getTime() / 1000), size: cacheEntry.size, }; diff --git a/packages/graphql-language-service-server/src/MessageProcessor.ts b/packages/graphql-language-service-server/src/MessageProcessor.ts index a2e237894b2..5c2356ef981 100644 --- a/packages/graphql-language-service-server/src/MessageProcessor.ts +++ b/packages/graphql-language-service-server/src/MessageProcessor.ts @@ -9,7 +9,6 @@ import mkdirp from 'mkdirp'; import { readFileSync, existsSync, writeFileSync, writeFile } from 'fs'; -import { pathToFileURL } from 'url'; import * as path from 'path'; import glob from 'fast-glob'; import { URI } from 'vscode-uri'; @@ -132,7 +131,7 @@ export class MessageProcessor { }; this._tmpDir = tmpDir || tmpdir(); this._tmpDirBase = path.join(this._tmpDir, 'graphql-language-service'); - this._tmpUriBase = pathToFileURL(this._tmpDirBase).toString(); + this._tmpUriBase = URI.parse(this._tmpDirBase).toString(); this._loadConfigOptions = loadConfigOptions; if ( loadConfigOptions.extensions && @@ -832,9 +831,7 @@ export class MessageProcessor { const isFileUri = existsSync(uri); let version = 1; if (isFileUri) { - const schemaUri = pathToFileURL( - path.join(project.dirpath, uri), - ).toString(); + const schemaUri = URI.parse(path.join(project.dirpath, uri)).toString(); const schemaDocument = this._getCachedDocument(schemaUri); if (schemaDocument) { @@ -860,7 +857,7 @@ export class MessageProcessor { projectTmpPath = path.join(projectTmpPath, appendPath); } if (prependWithProtocol) { - return pathToFileURL(path.resolve(projectTmpPath)).toString(); + return URI.parse(path.resolve(projectTmpPath)).toString(); } else { return path.resolve(projectTmpPath); } @@ -1015,7 +1012,7 @@ export class MessageProcessor { } // build full system URI path with protocol - const uri = pathToFileURL(filePath).toString(); + const uri = URI.parse(filePath).toString(); // i would use the already existing graphql-config AST, but there are a few reasons we can't yet const contents = this._parser(document.rawSDL, uri); diff --git a/packages/graphql-language-service-server/src/__tests__/MessageProcessor-test.ts b/packages/graphql-language-service-server/src/__tests__/MessageProcessor-test.ts index 2a6fbb767df..471a379aff4 100644 --- a/packages/graphql-language-service-server/src/__tests__/MessageProcessor-test.ts +++ b/packages/graphql-language-service-server/src/__tests__/MessageProcessor-test.ts @@ -315,6 +315,43 @@ describe('MessageProcessor', () => { await expect(result[0].uri).toEqual(`${queryPathUri}/test3.graphql`); }); + describe('handleDidOpenOrSaveNotification', () => { + const mockReadFileSync: jest.Mock = jest.requireMock('fs').readFileSync; + + beforeEach(() => { + mockReadFileSync.mockReturnValue(''); + messageProcessor._updateGraphQLConfig = jest.fn(); + }); + it('updates config for standard config filename changes', async () => { + await messageProcessor.handleDidOpenOrSaveNotification({ + textDocument: { + uri: `${pathToFileURL('.')}/.graphql.config.js`, + languageId: 'js', + version: 0, + text: '', + }, + }); + + expect(messageProcessor._updateGraphQLConfig).toHaveBeenCalled(); + }); + + it('updates config for custom config filename changes', async () => { + const customConfigName = 'custom-config-name.yml'; + messageProcessor._settings = { load: { fileName: customConfigName } }; + + await messageProcessor.handleDidOpenOrSaveNotification({ + textDocument: { + uri: `${pathToFileURL('.')}/${customConfigName}`, + languageId: 'js', + version: 0, + text: '', + }, + }); + + expect(messageProcessor._updateGraphQLConfig).toHaveBeenCalled(); + }); + }); + it('parseDocument finds queries in tagged templates', async () => { const text = ` // @flow @@ -506,34 +543,5 @@ export function Example(arg: string) {}`; expect(messageProcessor._updateGraphQLConfig).not.toHaveBeenCalled(); }); - - it('updates config for standard config filename changes', async () => { - await messageProcessor.handleWatchedFilesChangedNotification({ - changes: [ - { - uri: `${pathToFileURL('.')}/.graphql.config`, - type: FileChangeType.Changed, - }, - ], - }); - - expect(messageProcessor._updateGraphQLConfig).toHaveBeenCalled(); - }); - - it('updates config for custom config filename changes', async () => { - const customConfigName = 'custom-config-name.yml'; - messageProcessor._settings = { load: { fileName: customConfigName } }; - - await messageProcessor.handleWatchedFilesChangedNotification({ - changes: [ - { - uri: `${pathToFileURL('.')}/${customConfigName}`, - type: FileChangeType.Changed, - }, - ], - }); - - expect(messageProcessor._updateGraphQLConfig).toHaveBeenCalled(); - }); }); });