MCPcopy
hub / github.com/prettier/prettier / printEmbedGraphQL

Function printEmbedGraphQL

src/language-js/embed/graphql.js:8–68  ·  view source on GitHub ↗
(textToDoc, print, path, options)

Source from the content-addressed store, hash-verified

6import { hasLanguageComment } from "./utilities.js";
7
8async function printEmbedGraphQL(textToDoc, print, path, options) {
9 const { node } = path;
10
11 const numQuasis = node.quasis.length;
12
13 const expressionDocs = printTemplateExpressions(path, options, print);
14 const parts = [];
15
16 for (let i = 0; i < numQuasis; i++) {
17 const templateElement = node.quasis[i];
18 const isFirst = i === 0;
19 const isLast = i === numQuasis - 1;
20 const text = templateElement.value.cooked;
21
22 const lines = text.split("\n");
23 const numLines = lines.length;
24
25 // Bail out if an interpolation occurs within a comment.
26 if (!isLast && /#[^\n\r]*$/.test(lines[numLines - 1])) {
27 return null;
28 }
29
30 const startsWithBlankLine =
31 numLines > 2 && lines[0].trim() === "" && lines[1].trim() === "";
32 const endsWithBlankLine =
33 numLines > 2 &&
34 lines[numLines - 1].trim() === "" &&
35 lines[numLines - 2].trim() === "";
36
37 const commentsAndWhitespaceOnly = lines.every((line) =>
38 /^\s*(?:#[^\n\r]*)?$/.test(line),
39 );
40
41 let doc;
42
43 if (commentsAndWhitespaceOnly) {
44 doc = printGraphqlComments(lines);
45 } else {
46 doc = await textToDoc(text, { parser: "graphql" });
47 }
48
49 if (doc) {
50 doc = escapeTemplateCharacters(doc, false);
51 if (!isFirst && startsWithBlankLine) {
52 parts.push("");
53 }
54 parts.push(doc);
55 if (!isLast && endsWithBlankLine) {
56 parts.push("");
57 }
58 } else if (!isFirst && !isLast && startsWithBlankLine) {
59 parts.push("");
60 }
61
62 if (!isLast) {
63 parts.push(expressionDocs[i]);
64 }
65 }

Callers

nothing calls this directly

Calls 9

printTemplateExpressionsFunction · 0.90
escapeTemplateCharactersFunction · 0.90
indentFunction · 0.90
joinFunction · 0.90
printGraphqlCommentsFunction · 0.85
textToDocFunction · 0.85
splitMethod · 0.80
testMethod · 0.45
trimMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…