Repository navigation
Expand file tree
/
Copy pathgraphql.js
More file actions
130 lines (109 loc) · 3.57 KB
/
Copy pathgraphql.js
File metadata and controls
130 lines (109 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { hardline, indent, join } from "../../document/builders.js";
import {
escapeTemplateCharacters,
printTemplateExpressions,
} from "../print/template-literal.js";
import { hasLanguageComment } from "./utils.js";
async function printEmbedGraphQL(textToDoc, print, path /*, options*/) {
const { node } = path;
const numQuasis = node.quasis.length;
const expressionDocs = printTemplateExpressions(path, print);
const parts = [];
for (let i = 0; i < numQuasis; i++) {
const templateElement = node.quasis[i];
const isFirst = i === 0;
const isLast = i === numQuasis - 1;
const text = templateElement.value.cooked;
const lines = text.split("\n");
const numLines = lines.length;
const expressionDoc = expressionDocs[i];
const startsWithBlankLine =
numLines > 2 && lines[0].trim() === "" && lines[1].trim() === "";
const endsWithBlankLine =
numLines > 2 &&
lines[numLines - 1].trim() === "" &&
lines[numLines - 2].trim() === "";
const commentsAndWhitespaceOnly = lines.every((line) =>
/^\s*(?:#[^\n\r]*)?$/u.test(line),
);
// Bail out if an interpolation occurs within a comment.
if (!isLast && /#[^\n\r]*$/u.test(lines[numLines - 1])) {
return null;
}
let doc = null;
if (commentsAndWhitespaceOnly) {
doc = printGraphqlComments(lines);
} else {
doc = await textToDoc(text, { parser: "graphql" });
}
if (doc) {
doc = escapeTemplateCharacters(doc, false);
if (!isFirst && startsWithBlankLine) {
parts.push("");
}
parts.push(doc);
if (!isLast && endsWithBlankLine) {
parts.push("");
}
} else if (!isFirst && !isLast && startsWithBlankLine) {
parts.push("");
}
if (expressionDoc) {
parts.push(expressionDoc);
}
}
return ["`", indent([hardline, join(hardline, parts)]), hardline, "`"];
}
function printGraphqlComments(lines) {
const parts = [];
let seenComment = false;
const array = lines.map((textLine) => textLine.trim());
for (const [i, textLine] of array.entries()) {
// Lines are either whitespace only, or a comment (with potential whitespace
// around it). Drop whitespace-only lines.
if (textLine === "") {
continue;
}
if (array[i - 1] === "" && seenComment) {
// If a non-first comment is preceded by a blank (whitespace only) line,
// add in a blank line.
parts.push([hardline, textLine]);
} else {
parts.push(textLine);
}
seenComment = true;
}
// If `lines` was whitespace only, return `null`.
return parts.length === 0 ? null : join(hardline, parts);
}
/*
* react-relay and graphql-tag
* graphql`...`
* graphql.experimental`...`
* gql`...`
* GraphQL comment block
*
* This intentionally excludes Relay Classic tags, as Prettier does not
* support Relay Classic formatting.
*/
function isGraphQL({ node, parent }) {
return (
hasLanguageComment({ node, parent }, "GraphQL") ||
(parent &&
((parent.type === "TaggedTemplateExpression" &&
((parent.tag.type === "MemberExpression" &&
parent.tag.object.name === "graphql" &&
parent.tag.property.name === "experimental") ||
(parent.tag.type === "Identifier" &&
(parent.tag.name === "gql" || parent.tag.name === "graphql")))) ||
(parent.type === "CallExpression" &&
parent.callee.type === "Identifier" &&
parent.callee.name === "graphql")))
);
}
function printGraphql(path /*, options*/) {
if (isGraphQL(path)) {
return printEmbedGraphQL;
}
}
export default printGraphql;