From ff0489863ecd49bebf7a69e40f320ef40f2681bd Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Sun, 11 Feb 2024 10:36:46 +0100 Subject: [PATCH] fix: offset issue w/ graphql syntax annotation (#217) --- .changeset/big-spiders-itch.md | 5 +++++ packages/example-tada/src/Pokemon.tsx | 2 +- packages/graphqlsp/src/ast/token.ts | 2 +- packages/graphqlsp/src/diagnostics.ts | 8 ++++---- 4 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 .changeset/big-spiders-itch.md diff --git a/.changeset/big-spiders-itch.md b/.changeset/big-spiders-itch.md new file mode 100644 index 00000000..f10392db --- /dev/null +++ b/.changeset/big-spiders-itch.md @@ -0,0 +1,5 @@ +--- +'@0no-co/graphqlsp': patch +--- + +Fix offset issue when using the graphql annotation diff --git a/packages/example-tada/src/Pokemon.tsx b/packages/example-tada/src/Pokemon.tsx index c3f44dbd..90865648 100644 --- a/packages/example-tada/src/Pokemon.tsx +++ b/packages/example-tada/src/Pokemon.tsx @@ -6,7 +6,7 @@ export const Fields = { Pokemon: graphql(` }`) } -export const PokemonFields = graphql(` +export const PokemonFields = graphql(/* GraphQL */` fragment pokemonFields on Pokemon { name weight { diff --git a/packages/graphqlsp/src/ast/token.ts b/packages/graphqlsp/src/ast/token.ts index e237cc1a..79d04e71 100644 --- a/packages/graphqlsp/src/ast/token.ts +++ b/packages/graphqlsp/src/ast/token.ts @@ -18,7 +18,7 @@ export const getToken = ( const input = text.split('\n'); const parser = onlineParser(); const state = parser.startState(); - let cPos = template.pos + 1; + let cPos = template.getStart() + 1; let foundToken: Token | undefined = undefined; for (let line = 0; line < input.length; line++) { diff --git a/packages/graphqlsp/src/diagnostics.ts b/packages/graphqlsp/src/diagnostics.ts index 86b99f8e..cc06b7fe 100644 --- a/packages/graphqlsp/src/diagnostics.ts +++ b/packages/graphqlsp/src/diagnostics.ts @@ -23,6 +23,7 @@ import { MISSING_FRAGMENT_CODE, getColocatedFragmentNames, } from './checkImports'; +import { NoSubstitutionTemplateLiteral } from 'typescript'; const clientDirectives = new Set([ 'populate', @@ -205,13 +206,12 @@ const runDiagnostics = ( // by the fact that the parent is an expressionStatement let startingPosition = - node.pos + + node.getStart() + (isCallExpression ? 0 : (node as ts.TaggedTemplateExpression).tag.getText().length + - (isExpression ? 2 : 1)); + (isExpression ? 2 : 0)); const endPosition = startingPosition + node.getText().length; - let docFragments = [...fragments]; if (isCallExpression) { try { @@ -312,7 +312,7 @@ const runDiagnostics = ( if (!op.name) { graphQLDiagnostics.push({ message: 'Operation needs a name for types to be generated.', - start: node.pos, + start: node.getStart(), code: MISSING_OPERATION_NAME_CODE, length: originalNode.getText().length, range: {} as any,