-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
multiparser.js
75 lines (65 loc) · 1.93 KB
/
multiparser.js
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
"use strict";
const {
utils: { stripTrailingHardline },
} = require("../document/index.js");
const { normalize } = require("./options.js");
const comments = require("./comments.js");
function printSubtree(path, print, options, printAstToDoc) {
if (options.printer.embed && options.embeddedLanguageFormatting === "auto") {
return options.printer.embed(
path,
print,
(text, partialNextOptions, textToDocOptions) =>
textToDoc(
text,
partialNextOptions,
options,
printAstToDoc,
textToDocOptions
),
options
);
}
}
function textToDoc(
text,
partialNextOptions,
parentOptions,
printAstToDoc,
// TODO: remove `stripTrailingHardline` in v3.0.0
{ stripTrailingHardline: shouldStripTrailingHardline = false } = {}
) {
const nextOptions = normalize(
{
...parentOptions,
...partialNextOptions,
parentParser: parentOptions.parser,
originalText: text,
},
{ passThrough: true }
);
const result = require("./parser.js").parse(text, nextOptions);
const { ast } = result;
text = result.text;
const astComments = ast.comments;
delete ast.comments;
comments.attach(astComments, ast, text, nextOptions);
// @ts-expect-error -- Casting to `unique symbol` isn't allowed in JSDoc comment
nextOptions[Symbol.for("comments")] = astComments || [];
// @ts-expect-error -- Casting to `unique symbol` isn't allowed in JSDoc comment
nextOptions[Symbol.for("tokens")] = ast.tokens || [];
const doc = printAstToDoc(ast, nextOptions);
comments.ensureAllCommentsPrinted(astComments);
if (shouldStripTrailingHardline) {
// TODO: move this to `stripTrailingHardline` function in `/src/document/doc-utils.js`
if (typeof doc === "string") {
return doc.replace(/(?:\r?\n)*$/, "");
}
return stripTrailingHardline(doc);
}
/* istanbul ignore next */
return doc;
}
module.exports = {
printSubtree,
};