Skip to content

Commit 62a3910

Browse files
committed
update replacePlaceholdersWithWhiteSpace logic to handle template placeholders that span multiple rows
1 parent c416d34 commit 62a3910

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { Source } from "graphql";
2+
import { loadSchema } from "apollo-codegen-core/lib/loading";
3+
import { GraphQLDocument, extractGraphQLDocuments } from "../document";
4+
import { collectExecutableDefinitionDiagnositics } from "../diagnostics";
5+
import { TextDocument, Position } from "vscode-languageserver";
6+
import { stripIndent } from "common-tags";
7+
8+
describe("extractGraphQLDocuments", () => {
9+
describe("extracting docuemnts from JavaScript template literals", () => {
10+
const mockTextDocument = (text): TextDocument => ({
11+
getText: jest.fn().mockReturnValue(text),
12+
offsetAt(): number {
13+
return 0;
14+
},
15+
positionAt(): Position {
16+
return {
17+
character: 0,
18+
line: 0
19+
};
20+
},
21+
languageId: "javascript",
22+
lineCount: 0,
23+
uri: "",
24+
version: 1
25+
});
26+
27+
it("works with placeholders that span multiple rows", () => {
28+
const textDocument = mockTextDocument(`
29+
gql\`
30+
{
31+
hero {
32+
...Hero_character
33+
}
34+
}
35+
36+
\${Hero.fragments
37+
.character}
38+
\`
39+
`);
40+
const documents = extractGraphQLDocuments(textDocument);
41+
42+
expect(documents.length).toEqual(1);
43+
expect(documents[0].syntaxErrors.length).toBe(0);
44+
expect(documents[0].ast.definitions.length).toBe(1);
45+
});
46+
47+
it("works with multiple placeholders in a document", () => {
48+
const textDocument = mockTextDocument(`
49+
gql\`
50+
{
51+
hero {
52+
...Hero_character
53+
}
54+
}
55+
56+
\${Hero.fragments.character}
57+
58+
{
59+
reviews(episode: NEWHOPE) {
60+
...ReviewList_reviews
61+
}
62+
}
63+
64+
\${ReviewList.fragments.reviews}
65+
\`
66+
`);
67+
const documents = extractGraphQLDocuments(textDocument);
68+
69+
expect(documents.length).toEqual(1);
70+
expect(documents[0].syntaxErrors.length).toBe(0);
71+
expect(documents[0].ast.definitions.length).toBe(2);
72+
});
73+
});
74+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function extractGraphQLDocumentsFromPythonStrings(
131131
}
132132

133133
function replacePlaceholdersWithWhiteSpace(content: string) {
134-
return content.replace(/\$\{(.+)?\}/g, match => {
134+
return content.replace(/\$\{([\s\S]+?)\}/gm, match => {
135135
return Array(match.length).join(" ");
136136
});
137137
}

0 commit comments

Comments
 (0)