diff --git a/.changeset/big-spiders-itch.md b/.changeset/big-spiders-itch.md new file mode 100644 index 0000000..f10392d --- /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 c3f44db..9086564 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 e237cc1..79d04e7 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 86b99f8..cc06b7f 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,