Skip to content

Commit 0c4d9fa

Browse files
rlarchertrevor-scheer
authored andcommitted
Use length of tag nam rather than hard code length of gql for parsing… (#1326)
Use length of tag nam rather than hard code length of gql for parsing offset
1 parent 12e62b8 commit 0c4d9fa

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- `apollo-graphql`
2020
- <First `apollo-graphql` related entry goes here>
2121
- `apollo-language-server`
22+
- Use offset equal to length of tagname when parsing JS documents[#1050](https://github.com/apollographql/apollo-tooling/issues/1050)
2223
- Allow template literal placeholders that span multiple rows[#1299](https://github.com/apollographql/apollo-tooling/pull/1299)
2324
- `apollo-tools`
2425
- <First `apollo-tools` related entry goes here>

packages/apollo-language-server/src/__tests__/document.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,32 @@ describe("extractGraphQLDocuments", () => {
6666
expect(documents[0].syntaxErrors.length).toBe(0);
6767
expect(documents[0].ast.definitions.length).toBe(2);
6868
});
69+
70+
it("works with a custom tagname", () => {
71+
const textDocument = mockTextDocument(`
72+
gqltag\`
73+
{
74+
hero {
75+
...Hero_character
76+
}
77+
}
78+
79+
\${Hero.fragments.character}
80+
81+
{
82+
reviews(episode: NEWHOPE) {
83+
...ReviewList_reviews
84+
}
85+
}
86+
87+
\${ReviewList.fragments.reviews}
88+
\`
89+
`);
90+
const documents = extractGraphQLDocuments(textDocument, "gqltag");
91+
92+
expect(documents.length).toEqual(1);
93+
expect(documents[0].syntaxErrors.length).toBe(0);
94+
expect(documents[0].ast.definitions.length).toBe(2);
95+
});
6996
});
7097
});

packages/apollo-language-server/src/document.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function extractGraphQLDocumentsFromJSTemplateLiterals(
8686
let result;
8787
while ((result = regExp.exec(text)) !== null) {
8888
const contents = replacePlaceholdersWithWhiteSpace(result[1]);
89-
const position = document.positionAt(result.index + 4);
89+
const position = document.positionAt(result.index + (tagName.length + 1));
9090
const locationOffset: SourceLocation = {
9191
line: position.line + 1,
9292
column: position.character + 1

0 commit comments

Comments
 (0)