Skip to content

Commit

Permalink
Support parens (#99)
Browse files Browse the repository at this point in the history
* add failing test

* test passing

* syntax too

* fix: closing parenthesis at end position

* chore: add changeset

---------

Co-authored-by: Alessia Bellisario <github@bellisar.io>
  • Loading branch information
pvinis and alessbell committed Oct 16, 2023
1 parent 39e0b4f commit 2a3fc1b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-spoons-clean.md
@@ -0,0 +1,5 @@
---
"vscode-apollo": patch
---

Support tagging inside of parentheses
27 changes: 27 additions & 0 deletions src/language-server/__tests__/document.test.ts
Expand Up @@ -93,6 +93,33 @@ describe("extractGraphQLDocuments", () => {
expect(documents?.[0].syntaxErrors.length).toBe(0);
expect(documents?.[0].ast?.definitions.length).toBe(2);
});

it("works with parens", () => {
const textDocument = mockTextDocument(`
gql(\`
{
hero {
...Hero_character
}
}
\${Hero.fragments.character}
{
reviews(episode: NEWHOPE) {
...ReviewList_reviews
}
}
\${ReviewList.fragments.reviews}
\`)
`);
const documents = extractGraphQLDocuments(textDocument);

expect(documents?.length).toEqual(1);
expect(documents?.[0].syntaxErrors.length).toBe(0);
expect(documents?.[0].ast?.definitions.length).toBe(2);
});
});

describe("extracting documents from ReasonML extension nodes", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/language-server/document.ts
Expand Up @@ -91,7 +91,7 @@ function extractGraphQLDocumentsFromJSTemplateLiterals(
const documents: GraphQLDocument[] = [];

const regExp = new RegExp(
`(?:${tagName}\\s*\`|\`#graphql)([\\s\\S]+?)\``,
`(?:${tagName}(?:\\s|\\()*\`|\`#graphql)([\\s\\S]+?)\`\\)?`,
"gm"
);

Expand Down
4 changes: 2 additions & 2 deletions syntaxes/graphql.js.json
Expand Up @@ -5,7 +5,7 @@
{
"name": "taggedTemplates",
"contentName": "meta.embedded.block.graphql",
"begin": "(Relay\\.QL|gql|graphql(\\.experimental)?)(`)",
"begin": "(Relay\\.QL|gql|graphql(\\.experimental)?)\\(?(`)",
"beginCaptures": {
"1": {
"name": "entity.name.function.tagged-template.js"
Expand All @@ -14,7 +14,7 @@
"name": "punctuation.definition.string.template.begin.js"
}
},
"end": "`",
"end": "`\\)?",
"endCaptures": {
"0": {
"name": "punctuation.definition.string.template.end.js"
Expand Down

0 comments on commit 2a3fc1b

Please sign in to comment.