From 7738defeb4683ded5fbca8f7378c1b6604dbfbf4 Mon Sep 17 00:00:00 2001 From: jycouet Date: Fri, 28 Apr 2023 01:19:02 +0200 Subject: [PATCH] fix: vue and svelte files doesn't log error when parsing with no script tag (#2836) --- .changeset/eight-swans-destroy.md | 5 ++ .../src/__tests__/findGraphQLTags-test.ts | 68 ++++++++++++++++++- .../src/findGraphQLTags.ts | 14 +++- 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 .changeset/eight-swans-destroy.md diff --git a/.changeset/eight-swans-destroy.md b/.changeset/eight-swans-destroy.md new file mode 100644 index 00000000000..6f56edffaa1 --- /dev/null +++ b/.changeset/eight-swans-destroy.md @@ -0,0 +1,5 @@ +--- +'graphql-language-service-server': patch +--- + +fix: vue and svelte files doesn't log errors anymore when parsing with no script tag (#2836) diff --git a/packages/graphql-language-service-server/src/__tests__/findGraphQLTags-test.ts b/packages/graphql-language-service-server/src/__tests__/findGraphQLTags-test.ts index 48bd37be429..53d4b825f21 100644 --- a/packages/graphql-language-service-server/src/__tests__/findGraphQLTags-test.ts +++ b/packages/graphql-language-service-server/src/__tests__/findGraphQLTags-test.ts @@ -10,7 +10,7 @@ import { tmpdir } from 'node:os'; import { findGraphQLTags as baseFindGraphQLTags } from '../findGraphQLTags'; -jest.mock('../Logger'); +// jest.mock('../Logger'); import { Logger } from '../Logger'; @@ -289,6 +289,72 @@ query {id} query {id}`); }); + it('no crash in Svelte files without `; + + const consoleErrorSpy = jest + .spyOn(process.stderr, 'write') + .mockImplementation(() => true); + + const contents = baseFindGraphQLTags( + text, + '.svelte', + '', + new Logger(tmpdir(), false), + ); + // We should have no contents + expect(contents).toMatchObject([]); + + // Nothing should be logged as it's a managed error + expect(consoleErrorSpy.mock.calls.length).toBe(0); + + consoleErrorSpy.mockRestore(); + }); + + it('no crash in Svelte files with empty `; + + const consoleErrorSpy = jest + .spyOn(process.stderr, 'write') + .mockImplementation(() => true); + + const contents = baseFindGraphQLTags( + text, + '.svelte', + '', + new Logger(tmpdir(), false), + ); + // We should have no contents + expect(contents).toMatchObject([]); + + // Nothing should be logged as it's a managed error + expect(consoleErrorSpy.mock.calls.length).toBe(0); + + consoleErrorSpy.mockRestore(); + }); + it('finds multiple queries in a single file', async () => { const text = `something({ else: () => gql\` query {} \` diff --git a/packages/graphql-language-service-server/src/findGraphQLTags.ts b/packages/graphql-language-service-server/src/findGraphQLTags.ts index a73c757d4b5..c3dcd033d27 100644 --- a/packages/graphql-language-service-server/src/findGraphQLTags.ts +++ b/packages/graphql-language-service-server/src/findGraphQLTags.ts @@ -90,6 +90,18 @@ function parseVueSFC(source: string): ParseVueSFCResult { try { scriptBlock = VueParser.compileScript(descriptor, { id: 'foobar' }); } catch (error) { + if (error instanceof Error) { + if ( + error.message === '[@vue/compiler-sfc] SFC contains no