From 2717712bd311a984c666554549ddcd72f2eeadac Mon Sep 17 00:00:00 2001 From: cyphercodes Date: Sat, 23 May 2026 18:38:03 +0300 Subject: [PATCH] fix: correct validation diagnostic highlight range --- .changeset/loud-fields-dance.md | 5 +++++ .../interface/__tests__/getDiagnostics.test.ts | 17 +++++++++++++++-- .../src/interface/getDiagnostics.ts | 11 ++++++----- 3 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 .changeset/loud-fields-dance.md diff --git a/.changeset/loud-fields-dance.md b/.changeset/loud-fields-dance.md new file mode 100644 index 00000000000..5148b143ea7 --- /dev/null +++ b/.changeset/loud-fields-dance.md @@ -0,0 +1,5 @@ +--- +'graphql-language-service': patch +--- + +Fix validation diagnostic ranges so highlights stop at the invalid token instead of including the following character. diff --git a/packages/graphql-language-service/src/interface/__tests__/getDiagnostics.test.ts b/packages/graphql-language-service/src/interface/__tests__/getDiagnostics.test.ts index 8016bb6e170..d5c73622a51 100644 --- a/packages/graphql-language-service/src/interface/__tests__/getDiagnostics.test.ts +++ b/packages/graphql-language-service/src/interface/__tests__/getDiagnostics.test.ts @@ -44,6 +44,19 @@ describe('getDiagnostics', () => { ); expect(error.severity).toEqual(DIAGNOSTIC_SEVERITY.Error); expect(error.source).toEqual('GraphQL: Validation'); + expect(error.range).toMatchObject({ + start: { line: 0, character: 18 }, + end: { line: 0, character: 23 }, + }); + }); + + it('does not include trailing whitespace in validation highlights', () => { + const error = validateQuery(parse('query {\n title \n}'), schema)[0]; + + expect(error.range).toMatchObject({ + start: { line: 1, character: 2 }, + end: { line: 1, character: 7 }, + }); }); it('catches with multiple highlighted nodes', () => { @@ -55,7 +68,7 @@ describe('getDiagnostics', () => { { range: { end: { - character: 20, + character: 19, line: 0, }, start: { @@ -67,7 +80,7 @@ describe('getDiagnostics', () => { { range: { end: { - character: 32, + character: 31, line: 0, }, start: { diff --git a/packages/graphql-language-service/src/interface/getDiagnostics.ts b/packages/graphql-language-service/src/interface/getDiagnostics.ts index d41c3ff80fc..0ee5996f46e 100644 --- a/packages/graphql-language-service/src/interface/getDiagnostics.ts +++ b/packages/graphql-language-service/src/interface/getDiagnostics.ts @@ -154,15 +154,16 @@ function annotations( // https://github.com/microsoft/TypeScript/pull/32695 const loc = error.locations[i]; const highlightLoc = getLocation(highlightNode); - const end = loc.column + (highlightLoc.end - highlightLoc.start); + const start = new Position(loc.line - 1, loc.column - 1); + const end = new Position( + start.line, + start.character + highlightLoc.end - highlightLoc.start, + ); highlightedNodes.push({ source: `GraphQL: ${type}`, message: error.message, severity, - range: new Range( - new Position(loc.line - 1, loc.column - 1), - new Position(loc.line - 1, end), - ), + range: new Range(start, end), }); } }