diff --git a/.changeset/calm-bears-argue.md b/.changeset/calm-bears-argue.md new file mode 100644 index 00000000..31a7004d --- /dev/null +++ b/.changeset/calm-bears-argue.md @@ -0,0 +1,5 @@ +--- +'@0no-co/graphqlsp': minor +--- + +Remove the co-located fragments check for the time being as it's broken in newer TS versions and breaks with barrel files diff --git a/README.md b/README.md index 5bcc208f..f106b383 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,6 @@ auto-complete and automatically generating [Typed-Document-nodes](https://the-gu - Diagnostics for adding fields that don't exist, are deprecated, missmatched argument types, ... - Auto-complete inside your editor for fields - When you save it will generate `typed-document-nodes` for your documents and cast them to the correct type -- Will warn you when you are importing from a file that is exporting fragments that you're not using ## Installation @@ -41,6 +40,7 @@ when on a TypeScript file or adding a file like [this](https://github.com/0no-co > If you are using VSCode ensure that your editor is using [the Workspace Version of TypeScript](https://code.visualstudio.com/docs/typescript/typescript-compiling#_using-the-workspace-version-of-typescript) > this can be done by manually selecting it or adding a `.vscode/config.json` with the contents of +> > ```json > { > "typescript.tsdk": "node_modules/typescript/lib", @@ -62,8 +62,6 @@ when on a TypeScript file or adding a file like [this](https://github.com/0no-co - `disableTypegen` disables type-generation in general, this could be needed if offset bugs are introduced - `scalars` allows you to pass an object of scalars that we'll feed into `graphql-code-generator` - `extraTypes` allows you to specify imports or declare types to help with `scalar` definitions -- `shouldCheckForColocatedFragments` when turned on, this will scan your imports to find - unused fragments and provide a message notifying you about them ### GraphQL Code Generator client-preset diff --git a/packages/graphqlsp/README.md b/packages/graphqlsp/README.md index bf025e63..6c7b8dce 100644 --- a/packages/graphqlsp/README.md +++ b/packages/graphqlsp/README.md @@ -10,7 +10,6 @@ auto-complete and automatically generating [Typed-Document-nodes](https://the-gu - Diagnostics for adding fields that don't exist, are deprecated, missmatched argument types, ... - Auto-complete inside your editor for fields - When you save it will generate `typed-document-nodes` for your documents and cast them to the correct type -- Will warn you when you are importing from a file that is exporting fragments that you're not using ## Installation @@ -55,8 +54,6 @@ when on a TypeScript file or adding a file like [this](https://github.com/0no-co - `disableTypegen` disables type-generation in general, this could be needed if offset bugs are introduced - `scalars` allows you to pass an object of scalars that we'll feed into `graphql-code-generator` - `extraTypes` allows you to specify imports or declare types to help with `scalar` definitions -- `shouldCheckForColocatedFragments` when turned on, this will scan your imports to find - unused fragments and provide a message notifying you about them ### GraphQL Code Generator client-preset diff --git a/packages/graphqlsp/src/diagnostics.ts b/packages/graphqlsp/src/diagnostics.ts index 7faae61b..879fa97b 100644 --- a/packages/graphqlsp/src/diagnostics.ts +++ b/packages/graphqlsp/src/diagnostics.ts @@ -268,121 +268,11 @@ const runDiagnostics = ( typeof diag.code === 'number' ? diag.code : diag.severity === 2 - ? USING_DEPRECATED_FIELD_CODE - : SEMANTIC_DIAGNOSTIC_CODE, + ? USING_DEPRECATED_FIELD_CODE + : SEMANTIC_DIAGNOSTIC_CODE, messageText: diag.message.split('\n')[0], })); - const importDiagnostics = checkImportsForFragments(source, info); - - return [...tsDiagnostics, ...importDiagnostics]; -}; - -const checkImportsForFragments = ( - source: ts.SourceFile, - info: ts.server.PluginCreateInfo -) => { - const imports = findAllImports(source); - - const shouldCheckForColocatedFragments = - info.config.shouldCheckForColocatedFragments ?? false; - const tsDiagnostics: ts.Diagnostic[] = []; - if (imports.length && shouldCheckForColocatedFragments) { - const typeChecker = info.languageService.getProgram()?.getTypeChecker(); - imports.forEach(imp => { - if (!imp.importClause) return; - - const importedNames: string[] = []; - if (imp.importClause.name) { - importedNames.push(imp.importClause?.name.text); - } - - if ( - imp.importClause.namedBindings && - ts.isNamespaceImport(imp.importClause.namedBindings) - ) { - // TODO: we might need to warn here when the fragment is unused as a namespace import - return; - } else if ( - imp.importClause.namedBindings && - ts.isNamedImportBindings(imp.importClause.namedBindings) - ) { - imp.importClause.namedBindings.elements.forEach(el => { - importedNames.push(el.name.text); - }); - } - - const symbol = typeChecker?.getSymbolAtLocation(imp.moduleSpecifier); - if (!symbol) return; - - const moduleExports = typeChecker?.getExportsOfModule(symbol); - if (!moduleExports) return; - - const missingImports = moduleExports - .map(exp => { - if (importedNames.includes(exp.name)) { - return; - } - - const declarations = exp.getDeclarations(); - const declaration = declarations?.find(x => { - // TODO: check whether the sourceFile.fileName resembles the module - // specifier - return true; - }); - - if (!declaration) return; - - const [template] = findAllTaggedTemplateNodes(declaration); - if (template) { - let node = template; - if ( - ts.isNoSubstitutionTemplateLiteral(node) || - ts.isTemplateExpression(node) - ) { - if (ts.isTaggedTemplateExpression(node.parent)) { - node = node.parent; - } else { - return; - } - } - - const text = resolveTemplate( - node, - node.getSourceFile().fileName, - info - ).combinedText; - try { - const parsed = parse(text, { noLocation: true }); - if ( - parsed.definitions.every( - x => x.kind === Kind.FRAGMENT_DEFINITION - ) - ) { - return `'${exp.name}'`; - } - } catch (e) { - return; - } - } - }) - .filter(Boolean); - - if (missingImports.length) { - tsDiagnostics.push({ - file: source, - length: imp.getText().length, - start: imp.getStart(), - category: ts.DiagnosticCategory.Message, - code: MISSING_FRAGMENT_CODE, - messageText: `Missing Fragment import(s) ${missingImports.join( - ', ' - )} from ${imp.moduleSpecifier.getText()}.`, - }); - } - }); - } - return tsDiagnostics; }; diff --git a/packages/graphqlsp/src/index.ts b/packages/graphqlsp/src/index.ts index 9c456b11..861fd66e 100644 --- a/packages/graphqlsp/src/index.ts +++ b/packages/graphqlsp/src/index.ts @@ -27,7 +27,6 @@ type Config = { disableTypegen?: boolean; extraTypes?: string; scalars?: Record; - shouldCheckForColocatedFragments?: boolean; }; function create(info: ts.server.PluginCreateInfo) {