Skip to content

Commit e9fc21a

Browse files
authoredMar 16, 2024··
fix: Fix vscode-graphql-syntax’s grammar to support string literals on separate lines [Reapply & Fix] (#3545)
* fix: Fix vscode-graphql-syntax’s grammar to support string literals on separate lines (#3518) * Allow newlines after `graphql(` before start of query string * Fix mistaken reverse order of optional `(` and generic `<.*>` * Replace `(` in second match with newline one * Match graphql calls separately for string literals inside calls * Add missing endCaptures to new rule * Remove lookbehind in grammar and add inner TS/JS source patterns Using a positive lookbehind can subtly break Textmate/VSCode’s syntax highlighting. The positive lookbehind (according to some online sources) can fail unexpectedly when they contain whitespace matches. This means that we should instead match explicitly, like the other patterns do. This means however, that we might match too much and I noticed that arguments to the function aren't highlighted correctly. According to the interpolation rule in `graphql.json` we now include patterns for JS/TS/etc as a fallback pattern. This fixes the issue and prevents the case where the rule was conflicting with normal JS/TS patterns in a different tmLanguage grammar. * Update test and add new “after” variable case * Revert changes to tests/__fixtures__/test-js.md
1 parent ece99f6 commit e9fc21a

File tree

5 files changed

+389
-198
lines changed

5 files changed

+389
-198
lines changed
 

‎.changeset/famous-games-begin.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vscode-graphql-syntax': patch
3+
---
4+
5+
Fix TextMate grammar to support string literals that don’t immediately follow a function call's left-parenthesis (`(`).

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

+32-16
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"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
{
6-
"contentName": "meta.embedded.block.graphql",
7-
"begin": "\\s*+(?:(?:(Relay)\\??\\.)(QL)|(gql|graphql|graphql\\.experimental)|(/\\* GraphQL \\*/))\\s*\\(?\\s*(`|')",
6+
"begin": "\\s*+(?:(Relay)\\??\\.)(QL)|(gql|graphql|graphql\\.experimental)\\s*(?:<.*>)?\\s*\\(",
7+
"end": "\\)",
88
"beginCaptures": {
99
"1": {
1010
"name": "variable.other.class.js"
@@ -14,29 +14,42 @@
1414
},
1515
"3": {
1616
"name": "entity.name.function.tagged-template.js"
17-
},
18-
"4": {
19-
"name": "comment.graphql.js"
20-
},
21-
"5": {
22-
"name": "punctuation.definition.string.template.begin.js"
23-
}
24-
},
25-
"end": "(`|')",
26-
"endCaptures": {
27-
"0": {
28-
"name": "punctuation.definition.string.template.end.js"
2917
}
3018
},
3119
"patterns": [
3220
{
33-
"include": "source.graphql"
21+
"contentName": "meta.embedded.block.graphql",
22+
"begin": "(`|')",
23+
"end": "(`|')",
24+
"beginCaptures": {
25+
"0": {
26+
"name": "punctuation.definition.string.template.begin.js"
27+
}
28+
},
29+
"endCaptures": {
30+
"0": {
31+
"name": "punctuation.definition.string.template.end.js"
32+
}
33+
},
34+
"patterns": [
35+
{
36+
"include": "source.graphql"
37+
}
38+
]
39+
},
40+
{
41+
"patterns": [
42+
{ "include": "source.js" },
43+
{ "include": "source.ts" },
44+
{ "include": "source.js.jsx" },
45+
{ "include": "source.tsx" }
46+
]
3447
}
3548
]
3649
},
3750
{
3851
"contentName": "meta.embedded.block.graphql",
39-
"begin": "\\s*+(?:(?:(Relay)\\??\\.)(QL)|(gql|graphql|graphql\\.experimental))\\s*\\(?\\s*(?:<.*>)(`|')",
52+
"begin": "\\s*+(?:(?:(?:(Relay)\\??\\.)(QL)|(gql|graphql|graphql\\.experimental)\\s*(?:<.*>)?\\s*)|(/\\* GraphQL \\*/))\\s*(`|')",
4053
"beginCaptures": {
4154
"1": {
4255
"name": "variable.other.class.js"
@@ -48,6 +61,9 @@
4861
"name": "entity.name.function.tagged-template.js"
4962
},
5063
"4": {
64+
"name": "comment.graphql.js"
65+
},
66+
"5": {
5167
"name": "punctuation.definition.string.template.begin.js"
5268
}
5369
},

‎packages/vscode-graphql-syntax/tests/__fixtures__/test.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,22 @@ const graphql = graphql(`
3737
}
3838
`);
3939

40+
const after1 = 'after';
41+
42+
const graphql = graphql(
43+
`
44+
query($id: ID!) { test }
45+
`,
46+
[var1, var2]
47+
);
48+
49+
const after2 = 'after';
50+
4051
const query = /* GraphQL */ 'query { id } ';
41-
const query = graphql('query { id } ');
52+
const query = graphql('query($id: ID!) { id } ');
53+
const query = graphql(
54+
'query($id: ID!) { test }'
55+
);
4256

4357
const queryWithInlineComment = `#graphql
4458
query {
@@ -59,7 +73,6 @@ const queryWithInlineComment = `#graphql
5973
}
6074
}
6175
`;
62-
// TODO: fix this
6376
const queryWithInlineComment = `
6477
#graphql
6578
query {

‎packages/vscode-graphql-syntax/tests/__fixtures__/test.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,27 @@ const query = graphql<SomeGeneric>`
2525
}
2626
`;
2727

28-
// TODO: Fix this
2928
const query = graphql<Generic>('query { id }');
3029

30+
const query = graphql(
31+
'query { id }'
32+
);
33+
34+
const query = graphql<Generic>(
35+
'query { id }'
36+
);
37+
38+
const query = graphql(`
39+
query { id }
40+
`);
41+
42+
const query = graphql(
43+
`
44+
query { id }
45+
`,
46+
[var1, var2]
47+
);
48+
3149
const queryWithInlineComment = `#graphql
3250
query {
3351
user(id: "5", name: boolean) {

‎packages/vscode-graphql-syntax/tests/__snapshots__/js-grammar.spec.ts.snap

+318-179
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.