Skip to content

Commit

Permalink
catch expression statements correctly (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Jul 15, 2023
1 parent e3cfed7 commit 2aabbfa
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-pots-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@0no-co/graphqlsp': patch
---

Catch expression statements
27 changes: 26 additions & 1 deletion packages/example/src/index.generated.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions packages/example/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { gql, createClient } from '@urql/core';
import { Pokemon, PokemonFields, WeakFields } from './Pokemon';

const PokemonsQuery = gql`
query Pok {
pokemons {
const x = gql`
query Pok($limit: Int!) {
pokemons(limit: $limit) {
id
name
fleeRate
classification
...pokemonFields
...weaknessFields
__typename
Expand All @@ -21,15 +22,6 @@ const client = createClient({
url: '',
});

client
.query(PokemonsQuery, {})
.toPromise()
.then(result => {
const fastAttacks = result.data?.pokemons?.map(
pokemon => pokemon?.attacks?.fast
);
});

const PokemonQuery = gql`
query Po($id: ID!) {
pokemon(id: $id) {
Expand Down
17 changes: 16 additions & 1 deletion packages/graphqlsp/src/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ts from 'typescript/lib/tsserverlibrary';
import {
ImportTypeNode,
isAsExpression,
isExpressionStatement,
isImportTypeNode,
isNamedImportBindings,
isNamespaceImport,
Expand Down Expand Up @@ -83,7 +85,20 @@ export function getGraphQLDiagnostics(
);
const lines = text.split('\n');

let startingPosition = node.pos + (tagTemplate.length + 1);
let isExpression = false;
if (isAsExpression(node.parent)) {
if (isExpressionStatement(node.parent.parent)) {
isExpression = true;
}
} else {
if (isExpressionStatement(node.parent)) {
isExpression = true;
}
}
// When we are dealing with a plain gql statement we have to add two these can be recognised
// by the fact that the parent is an expressionStatement
let startingPosition =
node.pos + (tagTemplate.length + (isExpression ? 2 : 1));
const endPosition = startingPosition + node.getText().length;
const graphQLDiagnostics = getDiagnostics(text, schema.current)
.map(x => {
Expand Down

0 comments on commit 2aabbfa

Please sign in to comment.