Skip to content

Commit

Permalink
fix: vue & svelte wo script tag (#3157)
Browse files Browse the repository at this point in the history
* fix: vue and svelte files doesn't log error when parsing with no script tag (#2836)
  • Loading branch information
jycouet committed May 3, 2023
1 parent aeedf76 commit 06d3982
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
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 @@ -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> (typescript)', 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
12 changes: 11 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,16 @@ function parseVueSFC(source: string): ParseVueSFCResult {
try {
scriptBlock = VueParser.compileScript(descriptor, { id: 'foobar' });
} catch (error) {
if (
error instanceof Error &&
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 +128,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 06d3982

Please sign in to comment.