forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
65 lines (57 loc) · 1.74 KB
/
utils.js
File metadata and controls
65 lines (57 loc) · 1.74 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
import { group, indent, softline } from "../../document/builders.js";
/**
* @typedef {import("../../document/builders.js").Doc} Doc
*/
function printExpand(doc, canHaveTrailingWhitespace = true) {
return [indent([softline, doc]), canHaveTrailingWhitespace ? softline : ""];
}
function shouldHugJsExpression(ast, options) {
const rootNode =
ast.type === "NGRoot"
? ast.node.type === "NGMicrosyntax" &&
ast.node.body.length === 1 &&
ast.node.body[0].type === "NGMicrosyntaxExpression"
? ast.node.body[0].expression
: ast.node
: ast.type === "JsExpressionRoot"
? ast.node
: ast;
return (
rootNode &&
(rootNode.type === "ObjectExpression" ||
rootNode.type === "ArrayExpression" ||
((options.parser === "__vue_expression" ||
options.parser === "__vue_ts_expression") &&
(rootNode.type === "TemplateLiteral" ||
rootNode.type === "StringLiteral")))
);
}
/**
* @param {string} code
* @param {Function} textToDoc
* @param {*} options
* @param {(ast: any, options: any) => boolean} [shouldHugJsExpression]
* @returns {Promise<Doc>}
*/
async function formatAttributeValue(
code,
textToDoc,
options,
shouldHugJsExpression,
) {
options = {
// strictly prefer single quote to avoid unnecessary html entity escape
__isInHtmlAttribute: true,
__embeddedInHtml: true,
...options,
};
let shouldHug = true;
if (shouldHugJsExpression) {
options.__onHtmlBindingRoot = (ast, options) => {
shouldHug = shouldHugJsExpression(ast, options);
};
}
const doc = await textToDoc(code, options, textToDoc);
return shouldHug ? group(doc) : printExpand(doc);
}
export { formatAttributeValue, printExpand, shouldHugJsExpression };