forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute.js
More file actions
70 lines (60 loc) · 1.89 KB
/
attribute.js
File metadata and controls
70 lines (60 loc) · 1.89 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
import { group } from "../../document/builders.js";
import { mapDoc } from "../../document/utils.js";
import printAngularAttribute from "./angular-attributes.js";
import printClassNames from "./class-names.js";
import printSrcset from "./srcset.js";
import { printStyleAttribute } from "./style.js";
import printVueAttribute from "./vue-attributes.js";
/**
* @typedef {import("../../document/builders.js").Doc} Doc
* @typedef {import("../../common/ast-path.js").default} AstPath
*/
function printAttribute(path, options) {
const { node } = path;
if (!node.value) {
return;
}
if (
// lit-html: html`<my-element obj=${obj}></my-element>`
/^PRETTIER_HTML_PLACEHOLDER_\d+_\d+_IN_JS$/u.test(
options.originalText.slice(
node.valueSpan.start.offset,
node.valueSpan.end.offset,
),
) || // lwc: html`<my-element data-for={value}></my-element>`
(options.parser === "lwc" &&
node.value.startsWith("{") &&
node.value.endsWith("}"))
) {
return [node.rawName, "=", node.value];
}
for (const getValuePrinter of [
printSrcset,
printStyleAttribute,
printClassNames,
printVueAttribute,
printAngularAttribute,
]) {
const printValue = getValuePrinter(path, options);
if (printValue) {
return printAttributeWithValuePrinter(printValue);
}
}
}
/**
* @param {(textToDoc, print, path, options) => Promise<Doc>} printValue
* @returns {(textToDoc, print, path, options) => Promise<Doc>}
*/
function printAttributeWithValuePrinter(printValue) {
return async (textToDoc, print, path, options) => {
let valueDoc = await printValue(textToDoc, print, path, options);
if (!valueDoc) {
return;
}
valueDoc = mapDoc(valueDoc, (doc) =>
typeof doc === "string" ? doc.replaceAll('"', """) : doc,
);
return [path.node.rawName, '="', group(valueDoc), '"'];
};
}
export default printAttribute;