Skip to content

Commit

Permalink
feat: add astro support for language server
Browse files Browse the repository at this point in the history
  • Loading branch information
XiNiHa committed Dec 16, 2023
1 parent fb9dee7 commit 3245a15
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/graphql-language-service-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"graphql": "^15.5.0 || ^16.0.0"
},
"dependencies": {
"@astrojs/compiler": "^2.3.4",
"@babel/parser": "^7.22.6",
"@babel/types": "^7.22.5",
"@graphql-tools/code-file-loader": "8.0.1",
Expand Down
57 changes: 57 additions & 0 deletions packages/graphql-language-service-server/src/findGraphQLTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Position, Range } from 'graphql-language-service';

import { parse, ParserOptions, ParserPlugin } from '@babel/parser';
import * as VueParser from '@vue/compiler-sfc';
import * as AstroCompiler from '@astrojs/compiler/sync';
import type { Logger } from 'vscode-languageserver';

// Attempt to be as inclusive as possible of source text.
Expand Down Expand Up @@ -117,6 +118,44 @@ function parseVueSFC(source: string): ParseVueSFCResult {
};
}

type ParseAstroResult =
| { type: 'error'; errors: string[] }
| {
type: 'ok';
scriptOffset: number;
scriptAst: any[];
};

function parseAstro(source: string): ParseAstroResult {
// eslint-disable-next-line unicorn/no-useless-undefined
const { ast, diagnostics } = AstroCompiler.parse(source, undefined);
if (diagnostics.some(d => d.severity === /* Error */ 1)) {
return {
type: 'error',
errors: diagnostics.map(d => JSON.stringify(d)),
};
}

for (const node of ast.children) {
if (node.type === 'frontmatter') {
try {
return {
type: 'ok',
scriptOffset: (node.position?.start.line ?? 1) - 1,
scriptAst: [parse(node.value, PARSER_OPTIONS)],
};
} catch (error) {
return {
type: 'error',
errors: [String(error)],
};
}
}
}

return { type: 'error', errors: ['Could not find frontmatter block'] };
}

export function findGraphQLTags(
text: string,
ext: string,
Expand All @@ -128,6 +167,7 @@ export function findGraphQLTags(
const plugins = BABEL_PLUGINS.slice(0, BABEL_PLUGINS.length);

const isVueLike = ext === '.vue' || ext === '.svelte';
const isAstro = ext === '.astro';

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

Expand All @@ -153,6 +193,23 @@ export function findGraphQLTags(
}

scriptOffset = parseVueSFCResult.scriptOffset;
} else if (isAstro) {
plugins.push('typescript');
PARSER_OPTIONS.plugins = plugins;

const parseAstroResult = parseAstro(text);
if (parseAstroResult.type === 'error') {
logger.error(
`Could not parse the "${ext}" file at ${uri} to extract the graphql tags:`,
);
for (const error of parseAstroResult.errors) {
logger.error(String(error));
}
return [];
}

parsedASTs.push(...parseAstroResult.scriptAst);
scriptOffset = parseAstroResult.scriptOffset;
} else {
const isTypeScript = ['.ts', '.tsx', '.cts', '.mts'].includes(ext);
if (isTypeScript) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const DEFAULT_SUPPORTED_EXTENSIONS = [
'.svelte',
'.cts',
'.mts',
'.astro',
];

/**
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
resolved "https://registry.yarnpkg.com/@arthurgeron/eslint-plugin-react-usememo/-/eslint-plugin-react-usememo-1.1.4.tgz#7c92ef49813191f5af18339242b60f4beddabc86"
integrity sha512-OIjOhplz6MT+HgJjKZT1SDGzhofSRZaYfNBc7yRl/eeuh2VfUlRQP9ulReBLmfwuQWyRLr0wcdazQNKq35MaEw==

"@astrojs/compiler@^2.3.4":
version "2.3.4"
resolved "https://registry.yarnpkg.com/@astrojs/compiler/-/compiler-2.3.4.tgz#4dbc169de1f071508bf30db390890f16cb266416"
integrity sha512-33/YtWoBCE0cBUNy1kh78FCDXBoBANX87ShgATlAHECYbG2+buNTAgq4Xgz4t5NgnEHPN21GIBC2Mvvwisoutw==

"@babel/cli@^7.21.0":
version "7.21.0"
resolved "https://registry.yarnpkg.com/@babel/cli/-/cli-7.21.0.tgz#1868eb70e9824b427fc607610cce8e9e7889e7e1"
Expand Down

0 comments on commit 3245a15

Please sign in to comment.