Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove co-located fragment checks for now #123

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-bears-argue.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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",
Expand All @@ -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

Expand Down
3 changes: 0 additions & 3 deletions packages/graphqlsp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
114 changes: 2 additions & 112 deletions packages/graphqlsp/src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
1 change: 0 additions & 1 deletion packages/graphqlsp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type Config = {
disableTypegen?: boolean;
extraTypes?: string;
scalars?: Record<string, unknown>;
shouldCheckForColocatedFragments?: boolean;
};

function create(info: ts.server.PluginCreateInfo) {
Expand Down
133 changes: 0 additions & 133 deletions test/e2e/fragments.test.ts

This file was deleted.