Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: vue & svelte wo script tag #3157

Merged
merged 3 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>', 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