-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathheredocs.js
36 lines (31 loc) · 1.19 KB
/
heredocs.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
const { concat, group, lineSuffix, join } = require("../../prettier");
const { literallineWithoutBreakParent } = require("../../utils");
function printHeredoc(path, opts, print) {
const { body, ending } = path.getValue();
const parts = body.map((part, index) => {
if (part.type !== "@tstring_content") {
// In this case, the part of the string is an embedded expression
return path.call(print, "body", index);
}
// In this case, the part of the string is just regular string content
return join(literallineWithoutBreakParent, part.body.split("\n"));
});
// We use a literalline break because matching indentation is required
// for the heredoc contents and ending. If the line suffix contains a
// break-parent, all ancestral groups are broken, and heredocs automatically
// break lines in groups they appear in. We prefer them to appear in-line if
// possible, so we use a literalline without the break-parent.
return group(
concat([
path.call(print, "beging"),
lineSuffix(
group(
concat([literallineWithoutBreakParent].concat(parts).concat(ending))
)
)
])
);
}
module.exports = {
heredoc: printHeredoc
};