forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiparser.js
More file actions
127 lines (106 loc) · 2.87 KB
/
multiparser.js
File metadata and controls
127 lines (106 loc) · 2.87 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
import { stripTrailingHardline } from "../document/utils.js";
import createGetVisitorKeysFunction from "./create-get-visitor-keys-function.js";
import normalizeFormatOptions from "./normalize-format-options.js";
import parse from "./parse.js";
async function printEmbeddedLanguages(
/** @type {import("../common/ast-path.js").default} */ path,
genericPrint,
options,
printAstToDoc,
embeds,
) {
const {
embeddedLanguageFormatting,
printer: {
embed,
hasPrettierIgnore = () => false,
getVisitorKeys: printerGetVisitorKeys,
},
} = options;
if (!embed || embeddedLanguageFormatting !== "auto") {
return;
}
if (embed.length > 2) {
throw new Error(
"printer.embed has too many parameters. The API changed in Prettier v3. Please update your plugin. See https://prettier.io/docs/en/plugins.html#optional-embed",
);
}
const getVisitorKeys = createGetVisitorKeysFunction(
embed.getVisitorKeys ?? printerGetVisitorKeys,
);
const embedCallResults = [];
recurse();
const originalPathStack = path.stack;
for (const { print, node, pathStack } of embedCallResults) {
try {
path.stack = pathStack;
const doc = await print(textToDocForEmbed, genericPrint, path, options);
if (doc) {
embeds.set(node, doc);
}
} catch (error) {
/* c8 ignore next 3 */
if (process.env.PRETTIER_DEBUG) {
throw error;
}
}
}
path.stack = originalPathStack;
function textToDocForEmbed(text, partialNextOptions) {
return textToDoc(text, partialNextOptions, options, printAstToDoc);
}
function recurse() {
const { node } = path;
if (node === null || typeof node !== "object" || hasPrettierIgnore(path)) {
return;
}
for (const key of getVisitorKeys(node)) {
if (Array.isArray(node[key])) {
path.each(recurse, key);
} else {
path.call(recurse, key);
}
}
const result = embed(path, options);
if (!result) {
return;
}
if (typeof result === "function") {
embedCallResults.push({
print: result,
node,
pathStack: [...path.stack],
});
return;
}
if (
process.env.NODE_ENV !== "production" &&
typeof result.then === "function"
) {
throw new Error(
"`embed` should return an async function instead of Promise.",
);
}
embeds.set(node, result);
}
}
async function textToDoc(
text,
partialNextOptions,
parentOptions,
printAstToDoc,
) {
const options = await normalizeFormatOptions(
{
...parentOptions,
...partialNextOptions,
parentParser: parentOptions.parser,
originalText: text,
},
{ passThrough: true },
);
const { ast } = await parse(text, options);
const doc = await printAstToDoc(ast, options);
return stripTrailingHardline(doc);
}
export { printEmbeddedLanguages };