From 55441338a8854f379a8adec931f3c431794e2197 Mon Sep 17 00:00:00 2001 From: Rikki Schulte Date: Sun, 17 Mar 2024 10:10:11 +0100 Subject: [PATCH] fix: more cleanup --- .../package.json | 7 +-- .../src/GraphQLCache.ts | 12 ++--- .../src/MessageProcessor.ts | 48 ++++++------------- .../graphql-language-service/src/types.ts | 1 + yarn.lock | 41 ++++++++++------ 5 files changed, 53 insertions(+), 56 deletions(-) diff --git a/packages/graphql-language-service-server/package.json b/packages/graphql-language-service-server/package.json index 9cf6a8aa398..704fc323df5 100644 --- a/packages/graphql-language-service-server/package.json +++ b/packages/graphql-language-service-server/package.json @@ -42,6 +42,10 @@ "@babel/parser": "^7.23.6", "@babel/types": "^7.23.5", "@graphql-tools/code-file-loader": "8.0.3", + "@graphql-tools/graphql-tag-pluck": "^8.3.0", + "@graphql-tools/graphql-file-loader": "^8.0.1", + "@graphql-tools/url-loader": "^8.0.2", + "@graphql-tools/utils": "^10.1.2", "@vue/compiler-sfc": "^3.4.5", "astrojs-compiler-sync": "^0.3.5", "cosmiconfig-toml-loader": "^1.0.0", @@ -63,9 +67,6 @@ "vscode-uri": "^3.0.2" }, "devDependencies": { - "@graphql-tools/graphql-file-loader": "^8.0.1", - "@graphql-tools/url-loader": "^8.0.2", - "@graphql-tools/utils": "^10.1.2", "@types/glob": "^8.1.0", "@types/mkdirp": "^1.0.1", "@types/mock-fs": "^4.13.4", diff --git a/packages/graphql-language-service-server/src/GraphQLCache.ts b/packages/graphql-language-service-server/src/GraphQLCache.ts index f63f404ed14..d02da820712 100644 --- a/packages/graphql-language-service-server/src/GraphQLCache.ts +++ b/packages/graphql-language-service-server/src/GraphQLCache.ts @@ -802,12 +802,7 @@ export class GraphQLCache { let fsPath = doc.location; let filePath; const isNetwork = doc.location.startsWith('http'); - if (!isNetwork) { - try { - fsPath = resolve(rootDir, doc.location); - } catch {} - filePath = URI.file(fsPath).toString(); - } else { + if (isNetwork) { filePath = this._getTmpProjectPath( projectConfig, true, @@ -818,6 +813,11 @@ export class GraphQLCache { false, 'generated-schema.graphql', ); + } else { + try { + fsPath = resolve(rootDir, doc.location); + } catch {} + filePath = URI.file(fsPath).toString(); } const content = doc.document.loc?.source.body ?? ''; diff --git a/packages/graphql-language-service-server/src/MessageProcessor.ts b/packages/graphql-language-service-server/src/MessageProcessor.ts index 828b1445d46..fb81b68d07c 100644 --- a/packages/graphql-language-service-server/src/MessageProcessor.ts +++ b/packages/graphql-language-service-server/src/MessageProcessor.ts @@ -392,22 +392,16 @@ export class MessageProcessor { return { uri, diagnostics }; } try { - console.log('and here'); const project = this._graphQLCache.getProjectForFile(uri); - console.log('and here 1'); if (project) { - const text = 'text' in textDocument && textDocument.text; + // the disk is always valid here, so the textDocument.text isn't useful I don't think + // const text = 'text' in textDocument && textDocument.text; // for some reason if i try to tell to not parse empty files, it breaks :shrug: // i think this is because if the file change is empty, it doesn't get parsed // TODO: this could be related to a bug in how we are calling didOpenOrSave in our tests // that doesn't reflect the real runtime behavior - const { contents } = await this._parseAndCacheFile( - uri, - project, - // text as string, - ); - console.log('and here 2'); + const { contents } = await this._parseAndCacheFile(uri, project); if (project?.extensions?.languageService?.enableValidation !== false) { await Promise.all( contents.map(async ({ documentString, range }) => { @@ -487,7 +481,6 @@ export class MessageProcessor { if (project?.extensions?.languageService?.enableValidation !== false) { // Send the diagnostics onChange as well try { - console.log({ contents }); await Promise.all( contents.map(async ({ documentString, range }) => { const results = await this._languageService.getDiagnostics( @@ -595,21 +588,17 @@ export class MessageProcessor { // Treat the computed list always complete. const cachedDocument = this._getCachedDocument(textDocument.uri); - console.log({ cachedDocument, uri: textDocument.uri }); if (!cachedDocument) { return { items: [], isIncomplete: false }; } const found = cachedDocument.contents.find(content => { const currentRange = content.range; - console.log({ currentRange, position: toPosition(position) }); if (currentRange?.containsPosition(toPosition(position))) { return true; } }); - console.log({ found }); - // If there is no GraphQL query in this file, return an empty result. if (!found) { return { items: [], isIncomplete: false }; @@ -797,24 +786,17 @@ export class MessageProcessor { const { textDocument, position } = params; const project = this._graphQLCache.getProjectForFile(textDocument.uri); const cachedDocument = this._getCachedDocument(textDocument.uri); - console.log({ cachedDocument }); if (!cachedDocument) { return []; } const found = cachedDocument.contents.find(content => { - console.log(content.range, toPosition(position)); const currentRange = content?.range; - if ( - currentRange && - currentRange?.containsPosition(toPosition(position)) - ) { + if (currentRange?.containsPosition(toPosition(position))) { return true; } }); - console.log({ found }, 'definition'); - // If there is no GraphQL query in this file, return an empty result. if (!found) { return []; @@ -833,9 +815,7 @@ export class MessageProcessor { toPosition(position), textDocument.uri, ); - console.log({ result }); - } catch (err) { - console.error(err); + } catch { // these thrown errors end up getting fired before the service is initialized, so lets cool down on that } @@ -865,7 +845,6 @@ export class MessageProcessor { const vOffset = isEmbedded ? cachedDoc?.contents[0].range?.start.line ?? 0 : parentRange.start.line; - console.log({ defRange }); defRange.setStart( (defRange.start.line += vOffset), @@ -1333,11 +1312,15 @@ export class MessageProcessor { } private _getCachedDocument(uri: string): CachedDocumentType | null { - const fileCache = this._getDocumentCacheForFile(uri); - console.log(fileCache); - const cachedDocument = fileCache?.get(uri); - if (cachedDocument) { - return cachedDocument; + const project = this._graphQLCache.getProjectForFile(uri); + if (project) { + const cachedDocument = this._graphQLCache._getCachedDocument( + uri, + project, + ); + if (cachedDocument) { + return cachedDocument; + } } return null; @@ -1347,7 +1330,6 @@ export class MessageProcessor { uri: Uri, contents: CachedContent[], ): Promise | null> { - console.log('invalidate'); let documentCache = this._getDocumentCacheForFile(uri); if (!documentCache) { const project = await this._graphQLCache.getProjectForFile(uri); @@ -1386,7 +1368,7 @@ export class MessageProcessor { export function processDiagnosticsMessage( results: Diagnostic[], query: string, - range: RangeType | null, + range?: RangeType, ): Diagnostic[] { const queryLines = query.split('\n'); const totalLines = queryLines.length; diff --git a/packages/graphql-language-service/src/types.ts b/packages/graphql-language-service/src/types.ts index 57096358613..3f0817983d0 100644 --- a/packages/graphql-language-service/src/types.ts +++ b/packages/graphql-language-service/src/types.ts @@ -106,6 +106,7 @@ export interface IRange { setStart(line: number, character: number): void; containsPosition(position: IPosition): boolean; } + export type CachedContent = { documentString: string; range?: IRange; diff --git a/yarn.lock b/yarn.lock index d53b5499606..ade5e4da403 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1036,10 +1036,10 @@ resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.22.7.tgz#df8cf085ce92ddbdbf668a7f186ce848c9036cae" integrity sha512-7NF8pOkHP5o2vpmGgNGcfAeCvOYhGLyA3Z4eBQkT1RJlWu47n63bCs93QfJ2hIAFCil7L5P2IWhs1oToVgrL0Q== -"@babel/parser@^7.23.9": - version "7.23.9" - resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.23.9.tgz#7b903b6149b0f8fa7ad564af646c4c38a77fc44b" - integrity sha512-9tcKgqKbs3xGJ+NtKF2ndOBBLVwPjl1SHxPQkd36r3Dlirw3xWUeGaTbqr7uGZcTaxkVNwc+03SVP7aCdWrTlA== +"@babel/parser@^7.24.0": + version "7.24.0" + resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.24.0.tgz#26a3d1ff49031c53a97d03b604375f028746a9ac" + integrity sha512-QuP/FxEAzMSjXygs8v4N9dvdXzEHN4W1oF3PxuWAtPo08UdM17u89RDMgjLn/mlc56iM0HlLmVkO/wgR+rDgHg== "@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression@^7.18.6": version "7.18.6" @@ -2392,10 +2392,10 @@ "@babel/parser" "^7.12.13" "@babel/types" "^7.12.13" -"@babel/traverse@^7.16.8", "@babel/traverse@^7.17.3", "@babel/traverse@^7.20.5", "@babel/traverse@^7.20.7", "@babel/traverse@^7.21.0", "@babel/traverse@^7.21.2", "@babel/traverse@^7.22.5", "@babel/traverse@^7.22.6", "@babel/traverse@^7.22.8", "@babel/traverse@^7.23.7", "@babel/traverse@^7.7.2": - version "7.23.9" - resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.23.9.tgz#2f9d6aead6b564669394c5ce0f9302bb65b9d950" - integrity sha512-I/4UJ9vs90OkBtY6iiiTORVMyIhJ4kAVmsKo9KFc8UOxMeUfi2hvtIBsET5u9GizXE6/GFSuKCTNfgCswuEjRg== +"@babel/traverse@^7.16.8", "@babel/traverse@^7.17.3", "@babel/traverse@^7.20.5", "@babel/traverse@^7.20.7", "@babel/traverse@^7.21.0", "@babel/traverse@^7.21.2", "@babel/traverse@^7.22.5", "@babel/traverse@^7.22.6", "@babel/traverse@^7.22.8", "@babel/traverse@^7.23.2", "@babel/traverse@^7.23.7", "@babel/traverse@^7.7.2": + version "7.24.0" + resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.24.0.tgz#4a408fbf364ff73135c714a2ab46a5eab2831b1e" + integrity sha512-HfuJlI8qq3dEDmNU5ChzzpZRWq+oxCZQyMzIMEqLho+AQnhMnKQUzH6ydo3RBl/YjPCuk68Y6s0Gx0AeyULiWw== dependencies: "@babel/code-frame" "^7.23.5" "@babel/generator" "^7.23.6" @@ -2403,8 +2403,8 @@ "@babel/helper-function-name" "^7.23.0" "@babel/helper-hoist-variables" "^7.22.5" "@babel/helper-split-export-declaration" "^7.22.6" - "@babel/parser" "^7.23.9" - "@babel/types" "^7.23.9" + "@babel/parser" "^7.24.0" + "@babel/types" "^7.24.0" debug "^4.3.1" globals "^11.1.0" @@ -2460,10 +2460,10 @@ "@babel/helper-validator-identifier" "^7.22.20" to-fast-properties "^2.0.0" -"@babel/types@^7.23.9": - version "7.23.9" - resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.23.9.tgz#1dd7b59a9a2b5c87f8b41e52770b5ecbf492e002" - integrity sha512-dQjSq/7HaSjRM43FFGnv5keM2HsxpmyV1PfaSVm0nzzjwwTmjOe6J4bC8e3+pTEIgHaHj+1ZlLThRJ2auc/w1Q== +"@babel/types@^7.24.0": + version "7.24.0" + resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.24.0.tgz#3b951f435a92e7333eba05b7566fd297960ea1bf" + integrity sha512-+j7a5c253RfKh8iABBhywc8NSfP5LURe7Uh4qpsh6jc+aLJguvmIUBdjSdEMQv2bENrCR5MfRdjGo7vzS/ob7w== dependencies: "@babel/helper-string-parser" "^7.23.4" "@babel/helper-validator-identifier" "^7.22.20" @@ -3710,6 +3710,19 @@ "@graphql-tools/utils" "^10.0.0" tslib "^2.4.0" +"@graphql-tools/graphql-tag-pluck@^8.3.0": + version "8.3.0" + resolved "https://registry.yarnpkg.com/@graphql-tools/graphql-tag-pluck/-/graphql-tag-pluck-8.3.0.tgz#11bb8c627253137b39b34fb765cd6ebe506388b9" + integrity sha512-gNqukC+s7iHC7vQZmx1SEJQmLnOguBq+aqE2zV2+o1hxkExvKqyFli1SY/9gmukFIKpKutCIj+8yLOM+jARutw== + dependencies: + "@babel/core" "^7.22.9" + "@babel/parser" "^7.16.8" + "@babel/plugin-syntax-import-assertions" "^7.20.0" + "@babel/traverse" "^7.16.8" + "@babel/types" "^7.16.8" + "@graphql-tools/utils" "^10.0.13" + tslib "^2.4.0" + "@graphql-tools/import@7.0.0": version "7.0.0" resolved "https://registry.yarnpkg.com/@graphql-tools/import/-/import-7.0.0.tgz#a6a91a90a707d5f46bad0fd3fde2f407b548b2be"