forked from prettier/plugin-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed.js
More file actions
90 lines (79 loc) · 2.2 KB
/
embed.js
File metadata and controls
90 lines (79 loc) · 2.2 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
const {
concat,
group,
indent,
lineSuffix,
mapDoc,
markAsRoot,
stripTrailingHardline
} = require("./prettier");
const { literalLineNoBreak } = require("./utils");
const parsers = {
css: "css",
javascript: "babel",
js: "babel",
less: "less",
markdown: "markdown",
ruby: "ruby",
scss: "scss"
};
const replaceNewlines = (doc) =>
mapDoc(doc, (currentDoc) =>
typeof currentDoc === "string" && currentDoc.includes("\n")
? concat(
currentDoc
.split(/(\n)/g)
.map((v, i) => (i % 2 === 0 ? v : literalLineNoBreak))
)
: currentDoc
);
const embed = (path, print, textToDoc, _opts) => {
const node = path.getValue();
// Currently we only support embedded formatting on heredoc nodes
if (node.type !== "heredoc") {
return null;
}
// First, ensure that we don't have any interpolation
const { beging, body, ending } = node;
if (body.some((part) => part.type !== "@tstring_content")) {
return null;
}
// Next, find the parser associated with this heredoc (if there is one). For
// example, if you use <<~CSS, we'd hook it up to the css parser.
const parser = parsers[beging.body.slice(3).toLowerCase()];
if (!parser) {
return null;
}
// Get the content as if it were a source string, and then pass that content
// into the embedded parser. Get back the doc node.
const content = body.map((part) => part.body).join("");
const formatted = concat([
literalLineNoBreak,
replaceNewlines(stripTrailingHardline(textToDoc(content, { parser })))
]);
// If we're using a squiggly heredoc, then we can properly handle indentation
// ourselves.
if (beging.body[2] === "~") {
return concat([
path.call(print, "beging"),
lineSuffix(
group(
concat([
indent(markAsRoot(formatted)),
literalLineNoBreak,
ending.trim()
])
)
)
]);
}
// Otherwise, we need to just assume it's formatted correctly and return the
// content as it is.
return markAsRoot(
concat([
path.call(print, "beging"),
lineSuffix(group(concat([formatted, literalLineNoBreak, ending.trim()])))
])
);
};
module.exports = embed;