Skip to content

Commit 06d3982

Browse files
authored
fix: vue & svelte wo script tag (#3157)
* fix: vue and svelte files doesn't log error when parsing with no script tag (#2836)
1 parent aeedf76 commit 06d3982

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

.changeset/eight-swans-destroy.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'graphql-language-service-server': patch
3+
---
4+
5+
fix: vue and svelte files doesn't log errors anymore when parsing with no script tag (#2836)

packages/graphql-language-service-server/src/__tests__/findGraphQLTags-test.ts

+66
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,72 @@ query {id}
289289
query {id}`);
290290
});
291291

292+
it('no crash in Svelte files without <script>', async () => {
293+
const text = ``;
294+
295+
const consoleErrorSpy = jest
296+
.spyOn(process.stderr, 'write')
297+
.mockImplementation(() => true);
298+
299+
const contents = baseFindGraphQLTags(
300+
text,
301+
'.svelte',
302+
'',
303+
new Logger(tmpdir(), false),
304+
);
305+
// We should have no contents
306+
expect(contents).toMatchObject([]);
307+
308+
// Nothing should be logged as it's a managed error
309+
expect(consoleErrorSpy.mock.calls.length).toBe(0);
310+
311+
consoleErrorSpy.mockRestore();
312+
});
313+
314+
it('no crash in Svelte files with empty <script>', async () => {
315+
const text = `<script></script>`;
316+
317+
const consoleErrorSpy = jest
318+
.spyOn(process.stderr, 'write')
319+
.mockImplementation(() => true);
320+
321+
const contents = baseFindGraphQLTags(
322+
text,
323+
'.svelte',
324+
'',
325+
new Logger(tmpdir(), false),
326+
);
327+
// We should have no contents
328+
expect(contents).toMatchObject([]);
329+
330+
// Nothing should be logged as it's a managed error
331+
expect(consoleErrorSpy.mock.calls.length).toBe(0);
332+
333+
consoleErrorSpy.mockRestore();
334+
});
335+
336+
it('no crash in Svelte files with empty <script> (typescript)', async () => {
337+
const text = `<script lang="ts"></script>`;
338+
339+
const consoleErrorSpy = jest
340+
.spyOn(process.stderr, 'write')
341+
.mockImplementation(() => true);
342+
343+
const contents = baseFindGraphQLTags(
344+
text,
345+
'.svelte',
346+
'',
347+
new Logger(tmpdir(), false),
348+
);
349+
// We should have no contents
350+
expect(contents).toMatchObject([]);
351+
352+
// Nothing should be logged as it's a managed error
353+
expect(consoleErrorSpy.mock.calls.length).toBe(0);
354+
355+
consoleErrorSpy.mockRestore();
356+
});
357+
292358
it('finds multiple queries in a single file', async () => {
293359
const text = `something({
294360
else: () => gql\` query {} \`

packages/graphql-language-service-server/src/findGraphQLTags.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ function parseVueSFC(source: string): ParseVueSFCResult {
9090
try {
9191
scriptBlock = VueParser.compileScript(descriptor, { id: 'foobar' });
9292
} catch (error) {
93+
if (
94+
error instanceof Error &&
95+
error.message === '[@vue/compiler-sfc] SFC contains no <script> tags.'
96+
) {
97+
return {
98+
type: 'ok',
99+
scriptSetupAst: [],
100+
scriptAst: [],
101+
};
102+
}
93103
return { type: 'error', errors: [error as Error] };
94104
}
95105

@@ -118,7 +128,7 @@ export function findGraphQLTags(
118128
const parseVueSFCResult = parseVueSFC(text);
119129
if (parseVueSFCResult.type === 'error') {
120130
logger.error(
121-
`Could not parse the Vue file at ${uri} to extract the graphql tags:`,
131+
`Could not parse the "${ext}" file at ${uri} to extract the graphql tags:`,
122132
);
123133
for (const error of parseVueSFCResult.errors) {
124134
logger.error(String(error));

0 commit comments

Comments
 (0)