Skip to content

Commit

Permalink
remove co-located fragment checks for now
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Dec 5, 2023
1 parent 6e0b8cd commit 17fa628
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 119 deletions.
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

0 comments on commit 17fa628

Please sign in to comment.