Skip to content

Commit

Permalink
fix: script offset if vue-sfc contains template above (#3135)
Browse files Browse the repository at this point in the history
Co-authored-by: Rikki Schulte <rikki.schulte@gmail.com>
  • Loading branch information
KammererTob and acao authored May 10, 2023
1 parent 707f3cb commit 28b1b5a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
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

0 comments on commit 28b1b5a

Please sign in to comment.