Skip to content

Commit

Permalink
fix: vue and svelte files doesn't log error when parsing with no scri…
Browse files Browse the repository at this point in the history
…pt tag (#2836)
  • Loading branch information
jycouet committed Apr 27, 2023
1 parent aeedf76 commit 7738def
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-swans-destroy.md
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -289,6 +289,72 @@ query {id}
query {id}`);
});

it('no crash in Svelte files without <script>', async () => {
const text = ``;

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 <script>', async () => {
const text = `<script></script>`;

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 <script>', async () => {
const text = `<script lang="ts"></script>`;

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 {} \`
Expand Down
14 changes: 13 additions & 1 deletion packages/graphql-language-service-server/src/findGraphQLTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <script> tags.'
) {
return {
type: 'ok',
scriptSetupAst: [],
scriptAst: [],
};
}
}

return { type: 'error', errors: [error as Error] };
}

Expand Down Expand Up @@ -118,7 +130,7 @@ export function findGraphQLTags(
const parseVueSFCResult = parseVueSFC(text);
if (parseVueSFCResult.type === 'error') {
logger.error(
`Could not parse the Vue file at ${uri} to extract the graphql tags:`,
`Could not parse the ${ext} file at ${uri} to extract the graphql tags:`,
);
for (const error of parseVueSFCResult.errors) {
logger.error(String(error));
Expand Down

0 comments on commit 7738def

Please sign in to comment.