1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.extractGraphQLDocuments = exports.GraphQLDocument = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | const location_1 = require("graphql/language/location");
|
6 | const vscode_languageserver_1 = require("vscode-languageserver");
|
7 | const getDiagnostics_1 = require("@apollographql/graphql-language-service-interface/dist/getDiagnostics");
|
8 | const source_1 = require("./utilities/source");
|
9 | class GraphQLDocument {
|
10 | constructor(source) {
|
11 | this.source = source;
|
12 | this.syntaxErrors = [];
|
13 | try {
|
14 | this.ast = (0, graphql_1.parse)(source);
|
15 | }
|
16 | catch (error) {
|
17 | if (maybeCommentedOut(source.body))
|
18 | return;
|
19 | const range = (0, source_1.rangeInContainingDocument)(source, (0, getDiagnostics_1.getRange)(error.locations[0], source.body));
|
20 | this.syntaxErrors.push({
|
21 | severity: vscode_languageserver_1.DiagnosticSeverity.Error,
|
22 | message: error.message,
|
23 | source: "GraphQL: Syntax",
|
24 | range,
|
25 | });
|
26 | }
|
27 | }
|
28 | containsPosition(position) {
|
29 | if (position.line < this.source.locationOffset.line - 1)
|
30 | return false;
|
31 | const end = (0, source_1.positionFromSourceLocation)(this.source, (0, location_1.getLocation)(this.source, this.source.body.length));
|
32 | return position.line <= end.line;
|
33 | }
|
34 | }
|
35 | exports.GraphQLDocument = GraphQLDocument;
|
36 | function extractGraphQLDocuments(document, tagName = "gql") {
|
37 | switch (document.languageId) {
|
38 | case "graphql":
|
39 | return [
|
40 | new GraphQLDocument(new graphql_1.Source(document.getText(), document.uri)),
|
41 | ];
|
42 | case "javascript":
|
43 | case "javascriptreact":
|
44 | case "typescript":
|
45 | case "typescriptreact":
|
46 | case "vue":
|
47 | return extractGraphQLDocumentsFromJSTemplateLiterals(document, tagName);
|
48 | case "python":
|
49 | return extractGraphQLDocumentsFromPythonStrings(document, tagName);
|
50 | case "ruby":
|
51 | return extractGraphQLDocumentsFromRubyStrings(document, tagName);
|
52 | case "dart":
|
53 | return extractGraphQLDocumentsFromDartStrings(document, tagName);
|
54 | case "reason":
|
55 | return extractGraphQLDocumentsFromReasonStrings(document, tagName);
|
56 | case "elixir":
|
57 | return extractGraphQLDocumentsFromElixirStrings(document, tagName);
|
58 | default:
|
59 | return null;
|
60 | }
|
61 | }
|
62 | exports.extractGraphQLDocuments = extractGraphQLDocuments;
|
63 | function extractGraphQLDocumentsFromJSTemplateLiterals(document, tagName) {
|
64 | const text = document.getText();
|
65 | const documents = [];
|
66 | const regExp = new RegExp(`${tagName}\\s*\`([\\s\\S]+?)\``, "gm");
|
67 | let result;
|
68 | while ((result = regExp.exec(text)) !== null) {
|
69 | const contents = replacePlaceholdersWithWhiteSpace(result[1]);
|
70 | const position = document.positionAt(result.index + (tagName.length + 1));
|
71 | const locationOffset = {
|
72 | line: position.line + 1,
|
73 | column: position.character + 1,
|
74 | };
|
75 | const source = new graphql_1.Source(contents, document.uri, locationOffset);
|
76 | documents.push(new GraphQLDocument(source));
|
77 | }
|
78 | if (documents.length < 1)
|
79 | return null;
|
80 | return documents;
|
81 | }
|
82 | function extractGraphQLDocumentsFromPythonStrings(document, tagName) {
|
83 | const text = document.getText();
|
84 | const documents = [];
|
85 | const regExp = new RegExp(`\\b(${tagName}\\s*\\(\\s*[bfru]*("(?:"")?|'(?:'')?))([\\s\\S]+?)\\2\\s*\\)`, "gm");
|
86 | let result;
|
87 | while ((result = regExp.exec(text)) !== null) {
|
88 | const contents = replacePlaceholdersWithWhiteSpace(result[3]);
|
89 | const position = document.positionAt(result.index + result[1].length);
|
90 | const locationOffset = {
|
91 | line: position.line + 1,
|
92 | column: position.character + 1,
|
93 | };
|
94 | const source = new graphql_1.Source(contents, document.uri, locationOffset);
|
95 | documents.push(new GraphQLDocument(source));
|
96 | }
|
97 | if (documents.length < 1)
|
98 | return null;
|
99 | return documents;
|
100 | }
|
101 | function extractGraphQLDocumentsFromRubyStrings(document, tagName) {
|
102 | const text = document.getText();
|
103 | const documents = [];
|
104 | const regExp = new RegExp(`(<<-${tagName})([\\s\\S]+?)${tagName}`, "gm");
|
105 | let result;
|
106 | while ((result = regExp.exec(text)) !== null) {
|
107 | const contents = replacePlaceholdersWithWhiteSpace(result[2]);
|
108 | const position = document.positionAt(result.index + result[1].length);
|
109 | const locationOffset = {
|
110 | line: position.line + 1,
|
111 | column: position.character + 1,
|
112 | };
|
113 | const source = new graphql_1.Source(contents, document.uri, locationOffset);
|
114 | documents.push(new GraphQLDocument(source));
|
115 | }
|
116 | if (documents.length < 1)
|
117 | return null;
|
118 | return documents;
|
119 | }
|
120 | function extractGraphQLDocumentsFromDartStrings(document, tagName) {
|
121 | const text = document.getText();
|
122 | const documents = [];
|
123 | const regExp = new RegExp(`\\b(${tagName}\\(\\s*r?("""|'''))([\\s\\S]+?)\\2\\s*\\)`, "gm");
|
124 | let result;
|
125 | while ((result = regExp.exec(text)) !== null) {
|
126 | const contents = replacePlaceholdersWithWhiteSpace(result[3]);
|
127 | const position = document.positionAt(result.index + result[1].length);
|
128 | const locationOffset = {
|
129 | line: position.line + 1,
|
130 | column: position.character + 1,
|
131 | };
|
132 | const source = new graphql_1.Source(contents, document.uri, locationOffset);
|
133 | documents.push(new GraphQLDocument(source));
|
134 | }
|
135 | if (documents.length < 1)
|
136 | return null;
|
137 | return documents;
|
138 | }
|
139 | function extractGraphQLDocumentsFromReasonStrings(document, tagName) {
|
140 | const text = document.getText();
|
141 | const documents = [];
|
142 | const reasonFileFilter = new RegExp(/(\[%(graphql|relay\.))/g);
|
143 | if (!reasonFileFilter.test(text)) {
|
144 | return documents;
|
145 | }
|
146 | const reasonRegexp = new RegExp(/(?<=\[%(graphql|relay\.\w*)[\s\S]*{\|)[.\s\S]+?(?=\|})/gm);
|
147 | let result;
|
148 | while ((result = reasonRegexp.exec(text)) !== null) {
|
149 | const contents = result[0];
|
150 | const position = document.positionAt(result.index);
|
151 | const locationOffset = {
|
152 | line: position.line + 1,
|
153 | column: position.character + 1,
|
154 | };
|
155 | const source = new graphql_1.Source(contents, document.uri, locationOffset);
|
156 | documents.push(new GraphQLDocument(source));
|
157 | }
|
158 | if (documents.length < 1)
|
159 | return null;
|
160 | return documents;
|
161 | }
|
162 | function extractGraphQLDocumentsFromElixirStrings(document, tagName) {
|
163 | const text = document.getText();
|
164 | const documents = [];
|
165 | const regExp = new RegExp(`\\b(${tagName}\\(\\s*r?("""))([\\s\\S]+?)\\2\\s*\\)`, "gm");
|
166 | let result;
|
167 | while ((result = regExp.exec(text)) !== null) {
|
168 | const contents = replacePlaceholdersWithWhiteSpace(result[3]);
|
169 | const position = document.positionAt(result.index + result[1].length);
|
170 | const locationOffset = {
|
171 | line: position.line + 1,
|
172 | column: position.character + 1,
|
173 | };
|
174 | const source = new graphql_1.Source(contents, document.uri, locationOffset);
|
175 | documents.push(new GraphQLDocument(source));
|
176 | }
|
177 | if (documents.length < 1)
|
178 | return null;
|
179 | return documents;
|
180 | }
|
181 | function replacePlaceholdersWithWhiteSpace(content) {
|
182 | return content.replace(/\$\{([\s\S]+?)\}/gm, (match) => {
|
183 | return Array(match.length).join(" ");
|
184 | });
|
185 | }
|
186 | function maybeCommentedOut(content) {
|
187 | return ((content.indexOf("/*") > -1 && content.indexOf("*/") > -1) ||
|
188 | content.split("//").length > 1);
|
189 | }
|
190 |
|
\ | No newline at end of file |