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: script offset if vue-sfc contains template above #3135

Merged
merged 5 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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/thin-poems-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-language-service-server': patch
---

fixed wrong script tag offset for vue-sfc
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,25 @@ query {id}
const contents = findGraphQLTags(text, '.vue');
expect(contents[0].template).toEqual(`
query {id}`);
expect(contents[0].range.start.line).toEqual(2);
expect(contents[0].range.end.line).toEqual(4);
});

it('finds queries in tagged templates in Vue SFC using <script setup> and template above', async () => {
const text = `<template>
<div/>
</template>
<script setup lang="ts">
gql\`
query {id}
\`;
</script>
`;
const contents = findGraphQLTags(text, '.vue');
expect(contents[0].template).toEqual(`
query {id}`);
expect(contents[0].range.start.line).toEqual(4);
expect(contents[0].range.end.line).toEqual(6);
});

it('finds queries in tagged templates in Vue SFC using normal <script>', async () => {
Expand All @@ -251,6 +270,25 @@ query {id}
const contents = findGraphQLTags(text, '.vue');
expect(contents[0].template).toEqual(`
query {id}`);
expect(contents[0].range.start.line).toEqual(2);
expect(contents[0].range.end.line).toEqual(4);
});

it('finds queries in tagged templates in Vue SFC using normal <script> and template above', async () => {
const text = `<template>
<div/>
</template>
<script lang="ts">
gql\`
query {id}
\`;
</script>
`;
const contents = findGraphQLTags(text, '.vue');
expect(contents[0].template).toEqual(`
query {id}`);
expect(contents[0].range.start.line).toEqual(4);
expect(contents[0].range.end.line).toEqual(6);
});

it('finds queries in tagged templates in Vue SFC using <script lang="tsx">', async () => {
Expand Down
16 changes: 12 additions & 4 deletions packages/graphql-language-service-server/src/findGraphQLTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type ParseVueSFCResult =
| { type: 'error'; errors: Error[] }
| {
type: 'ok';
scriptOffset: number;
scriptSetupAst?: import('@babel/types').Statement[];
scriptAst?: import('@babel/types').Statement[];
};
Expand All @@ -98,13 +99,15 @@ function parseVueSFC(source: string): ParseVueSFCResult {
type: 'ok',
scriptSetupAst: [],
scriptAst: [],
scriptOffset: 0
};
}
return { type: 'error', errors: [error as Error] };
}

return {
type: 'ok',
scriptOffset: scriptBlock.loc.start.line - 1,
scriptSetupAst: scriptBlock?.scriptSetupAst,
scriptAst: scriptBlock?.scriptAst,
};
Expand All @@ -124,6 +127,8 @@ export function findGraphQLTags(

let parsedASTs: { [key: string]: any }[] = [];

let scriptOffset = 0;

if (isVueLike) {
const parseVueSFCResult = parseVueSFC(text);
if (parseVueSFCResult.type === 'error') {
Expand All @@ -142,6 +147,8 @@ export function findGraphQLTags(
if (parseVueSFCResult.scriptSetupAst !== undefined) {
parsedASTs.push(...parseVueSFCResult.scriptSetupAst);
}

scriptOffset = parseVueSFCResult.scriptOffset;
} else {
const isTypeScript = ['.ts', '.tsx', '.cts', '.mts'].includes(ext);
if (isTypeScript) {
Expand Down Expand Up @@ -179,9 +186,10 @@ export function findGraphQLTags(
? node.quasis.map(quasi => quasi.value.raw).join('')
: node.quasis[0].value.raw;
const range = new Range(
new Position(loc.start.line - 1, loc.start.column),
new Position(loc.end.line - 1, loc.end.column),
new Position(loc.start.line - 1 + scriptOffset, loc.start.column),
new Position(loc.end.line - 1 + scriptOffset, loc.end.column),
);

result.push({
tag: '',
template,
Expand Down Expand Up @@ -226,8 +234,8 @@ export function findGraphQLTags(
}
if (loc) {
const range = new Range(
new Position(loc.start.line - 1, loc.start.column),
new Position(loc.end.line - 1, loc.end.column),
new Position(loc.start.line - 1 + scriptOffset, loc.start.column),
new Position(loc.end.line - 1 + scriptOffset, loc.end.column),
);

result.push({
Expand Down