MCPcopy
hub / github.com/prettier/prettier / printAttributes

Function printAttributes

src/language-html/print/tag.js:221–330  ·  view source on GitHub ↗
(path, options, print)

Source from the content-addressed store, hash-verified

219}
220
221function printAttributes(path, options, print) {
222 const { node } = path;
223
224 const { attrs: attributes = [], startTagComments = [] } = node;
225
226 if (attributes.length === 0 && startTagComments.length === 0) {
227 return node.isSelfClosing
228 ? /**
229 * <br />
230 * ^
231 */
232 " "
233 : "";
234 }
235
236 const ignoreAttributeData =
237 node.prev?.kind === "comment" &&
238 getPrettierIgnoreAttributeCommentData(node.prev.value);
239
240 const hasPrettierIgnoreAttribute =
241 typeof ignoreAttributeData === "boolean"
242 ? () => ignoreAttributeData
243 : Array.isArray(ignoreAttributeData)
244 ? (attribute) => ignoreAttributeData.includes(attribute.rawName)
245 : () => false;
246
247 const properties = ["attrs", "startTagComments"].filter((property) =>
248 isNonEmptyArray(node[property]),
249 );
250 const printedAttributes = properties.flatMap((property) =>
251 path.map(
252 ({ node }) => ({
253 loc: locStart(node),
254 printed:
255 node.kind === "attribute" && hasPrettierIgnoreAttribute(node)
256 ? replaceEndOfLine(
257 options.originalText.slice(locStart(node), locEnd(node)),
258 )
259 : print(),
260 }),
261 property,
262 ),
263 );
264
265 if (properties.length > 1) {
266 printedAttributes.sort((a, b) => a.loc - b.loc);
267 }
268
269 const forceNotToBreakAttrContent =
270 node.kind === "element" &&
271 node.fullName === "script" &&
272 attributes.length === 1 &&
273 attributes[0].fullName === "src" &&
274 node.children.length === 0 &&
275 startTagComments.length === 0;
276 const shouldForceBreak = startTagComments.some(
277 (comment) => comment.type === "single",
278 );

Callers 1

printOpeningTagFunction · 0.85

Calls 12

locStartFunction · 0.90
replaceEndOfLineFunction · 0.90
locEndFunction · 0.90
isVueSfcBlockFunction · 0.90
indentFunction · 0.90
joinFunction · 0.90
isNonEmptyArrayFunction · 0.85
printFunction · 0.50
mapMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…