Skip to content

Commit 98af530

Browse files
authored
feat: add astro support for VSCode extension (#3475)
* feat: add astro syntax grammar support * feat: add astro support for language server
1 parent 88ae243 commit 98af530

File tree

15 files changed

+195
-41
lines changed

15 files changed

+195
-41
lines changed

.changeset/grumpy-moles-grin.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'graphql-language-service-server': minor
3+
'vscode-graphql': minor
4+
'vscode-graphql-syntax': minor
5+
---
6+
7+
Add Astro file support

custom-words.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ yoshiakis
6565

6666
// packages and tools
6767
argparse
68+
astro
69+
astrojs
6870
changesets
6971
clsx
7072
codemirror

packages/graphql-language-service-server/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@
3838
},
3939
"COMMENT": "please do not remove depenencies without thorough testing. many dependencies are not imported directly, as they are peer dependencies",
4040
"dependencies": {
41+
"@astrojs/compiler": "^2.5.0",
4142
"@babel/parser": "^7.23.6",
4243
"@babel/types": "^7.23.5",
4344
"@graphql-tools/code-file-loader": "8.0.3",
4445
"@vue/compiler-sfc": "^3.4.5",
46+
"astrojs-compiler-sync": "^0.3.5",
4547
"cosmiconfig-toml-loader": "^1.0.0",
4648
"dotenv": "10.0.0",
4749
"fast-glob": "^3.2.7",
@@ -51,12 +53,12 @@
5153
"mkdirp": "^1.0.4",
5254
"node-abort-controller": "^3.0.1",
5355
"nullthrows": "^1.0.0",
56+
"source-map-js": "1.0.2",
57+
"svelte": "^4.1.1",
5458
"vscode-jsonrpc": "^8.0.1",
5559
"vscode-languageserver": "^8.0.1",
5660
"vscode-languageserver-types": "^3.17.2",
57-
"vscode-uri": "^3.0.2",
58-
"svelte": "^4.1.1",
59-
"source-map-js": "1.0.2"
61+
"vscode-uri": "^3.0.2"
6062
},
6163
"devDependencies": {
6264
"@types/glob": "^8.1.0",

packages/graphql-language-service-server/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const DEFAULT_SUPPORTED_EXTENSIONS = [
5454
'.tsx',
5555
'.vue',
5656
'.svelte',
57+
'.astro',
5758
'.cts',
5859
'.mts',
5960
] as const;

packages/graphql-language-service-server/src/findGraphQLTags.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { TAG_MAP } from './constants';
1919
import { ecmaParser, tsParser } from './parsers/babel';
2020
// import { svelteParser } from './parsers/svelte';
2121
import { vueParser } from './parsers/vue';
22+
import { astroParser } from './parsers/astro';
2223
import type { Logger, NoopLogger } from './Logger';
2324
import { RangeMapper } from './parsers/types';
2425

@@ -44,6 +45,7 @@ const parserMap = {
4445
// '.svelte': svelteParser,
4546
'.svelte': vueParser,
4647
'.vue': vueParser,
48+
'.astro': astroParser,
4749
};
4850

4951
export function findGraphQLTags(
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { parse } from 'astrojs-compiler-sync';
2+
import { Position, Range } from 'graphql-language-service';
3+
import { RangeMapper, SourceParser } from './types';
4+
import { babelParser } from './babel';
5+
6+
type ParseAstroResult =
7+
| { type: 'error'; errors: string[] }
8+
| {
9+
type: 'ok';
10+
scriptOffset: number;
11+
scriptAst: any[];
12+
};
13+
14+
function parseAstro(source: string): ParseAstroResult {
15+
// eslint-disable-next-line unicorn/no-useless-undefined
16+
const { ast, diagnostics } = parse(source, undefined);
17+
if (diagnostics.some(d => d.severity === /* Error */ 1)) {
18+
return {
19+
type: 'error',
20+
errors: diagnostics.map(d => JSON.stringify(d)),
21+
};
22+
}
23+
24+
for (const node of ast.children) {
25+
if (node.type === 'frontmatter') {
26+
try {
27+
return {
28+
type: 'ok',
29+
scriptOffset: (node.position?.start.line ?? 1) - 1,
30+
scriptAst: [babelParser(node.value, ['typescript'])],
31+
};
32+
} catch (error) {
33+
return {
34+
type: 'error',
35+
errors: [String(error)],
36+
};
37+
}
38+
}
39+
}
40+
41+
return { type: 'error', errors: ['Could not find frontmatter block'] };
42+
}
43+
44+
export const astroParser: SourceParser = (text, uri, logger) => {
45+
const parseAstroResult = parseAstro(text);
46+
if (parseAstroResult.type === 'error') {
47+
logger.error(
48+
`Could not parse the astro file at ${uri} to extract the graphql tags:`,
49+
);
50+
for (const error of parseAstroResult.errors) {
51+
logger.error(String(error));
52+
}
53+
return null;
54+
}
55+
56+
const rangeMapper: RangeMapper = range => {
57+
return new Range(
58+
new Position(
59+
range.start.line + parseAstroResult.scriptOffset,
60+
range.start.character,
61+
),
62+
new Position(
63+
range.end.line + parseAstroResult.scriptOffset,
64+
range.end.character,
65+
),
66+
);
67+
};
68+
return { asts: parseAstroResult.scriptAst, rangeMapper };
69+
};

packages/vscode-graphql-execution/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@
9797
"@types/node": "^16.18.4",
9898
"@types/vscode": "1.62.0",
9999
"@types/ws": "8.2.2",
100+
"@vscode/vsce": "^2.22.1-2",
100101
"esbuild": "0.18.10",
101-
"ovsx": "0.5.1",
102-
"vsce": "^2.13.0"
102+
"ovsx": "0.5.1"
103103
},
104104
"dependencies": {
105105
"@graphql-tools/code-file-loader": "8.0.3",

packages/vscode-graphql-syntax/grammars/graphql.js.json

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"scopeName": "inline.graphql",
3-
"injectionSelector": "L:(meta.embedded.block.javascript | meta.embedded.block.typescript | source.js | source.ts | source.tsx | source.vue | source.svelte) -source.graphql -inline.graphql -string -comment",
3+
"injectionSelector": "L:(meta.embedded.block.javascript | meta.embedded.block.typescript | source.js | source.ts | source.tsx | source.vue | source.svelte | source.astro) -source.graphql -inline.graphql -string -comment",
44
"patterns": [
55
{
66
"contentName": "meta.embedded.block.graphql",
@@ -28,7 +28,11 @@
2828
"name": "punctuation.definition.string.template.end.js"
2929
}
3030
},
31-
"patterns": [{ "include": "source.graphql" }]
31+
"patterns": [
32+
{
33+
"include": "source.graphql"
34+
}
35+
]
3236
},
3337
{
3438
"contentName": "meta.embedded.block.graphql",
@@ -53,7 +57,11 @@
5357
"name": "punctuation.definition.string.template.end.js"
5458
}
5559
},
56-
"patterns": [{ "include": "source.graphql" }]
60+
"patterns": [
61+
{
62+
"include": "source.graphql"
63+
}
64+
]
5765
},
5866
{
5967
"name": "taggedTemplates",
@@ -73,7 +81,11 @@
7381
"name": "punctuation.definition.string.template.end.js"
7482
}
7583
},
76-
"patterns": [{ "include": "source.graphql" }]
84+
"patterns": [
85+
{
86+
"include": "source.graphql"
87+
}
88+
]
7789
}
7890
]
7991
}

packages/vscode-graphql-syntax/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"source.tsx",
6363
"source.vue",
6464
"source.svelte",
65+
"source.astro",
6566
"text.html.markdown"
6667
],
6768
"scopeName": "inline.graphql",

packages/vscode-graphql/.vscodeignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,15 @@ renovate.json
1111
*.tsbuildinfo
1212
esbuild.js
1313
dist/**
14+
15+
# exclude all node_modules by default
16+
../../node_modules/**
17+
18+
# @astrojs/compiler relies on the filesystem structure, therefore can't be bundled
19+
!../../node_modules/@astrojs/compiler
20+
# astrojs-compiler-sync relies on the filesystem structure, therefore can't be bundled
21+
!../../node_modules/astrojs-compiler-sync
22+
# synckit is the dependency of astrojs-compiler-sync
23+
!../../node_modules/synckit
24+
# @pkgr/core is the dependency of synckit
25+
!../../node_modules/@pkgr/core

0 commit comments

Comments
 (0)