MCPcopy
hub / github.com/prettier/prettier / attachComments

Function attachComments

src/main/comments/attach.js:113–262  ·  view source on GitHub ↗
(ast, options)

Source from the content-addressed store, hash-verified

111
112const returnFalse = () => false;
113function attachComments(ast, options) {
114 const { comments } = ast;
115 delete ast.comments;
116
117 if (!isNonEmptyArray(comments) || !options.printer.canAttachComment) {
118 return;
119 }
120
121 const tiesToBreak = [];
122 const {
123 printer: {
124 features: { experimental_avoidAstMutation: avoidAstMutation },
125 handleComments = {},
126 },
127 originalText: text,
128 } = options;
129 const {
130 ownLine: handleOwnLineComment = returnFalse,
131 endOfLine: handleEndOfLineComment = returnFalse,
132 remaining: handleRemainingComment = returnFalse,
133 } = handleComments;
134
135 const decoratedComments = comments.map((comment, index) => ({
136 ...decorateComment(ast, comment, options),
137 comment,
138 text,
139 options,
140 ast,
141 isLastComment: comments.length - 1 === index,
142 // TODO: Move placement here
143 placement: undefined,
144 }));
145
146 // For easier debug, save these to comment even `avoidAstMutation`
147 const attachPropertiesToComment =
148 process.env.NODE_ENV !== "production" || !avoidAstMutation;
149
150 for (const [index, context] of decoratedComments.entries()) {
151 const {
152 comment,
153 precedingNode,
154 enclosingNode,
155 followingNode,
156 text,
157 options,
158 ast,
159 isLastComment,
160 } = context;
161
162 const placement = isOwnLineComment(text, options, decoratedComments, index)
163 ? "ownLine"
164 : isEndOfLineComment(text, options, decoratedComments, index)
165 ? "endOfLine"
166 : "remaining";
167
168 let args;
169 if (avoidAstMutation) {
170 context.placement = placement;

Callers 1

prepareToPrintFunction · 0.90

Calls 12

addLeadingCommentFunction · 0.90
addTrailingCommentFunction · 0.90
addDanglingCommentFunction · 0.90
isNonEmptyArrayFunction · 0.85
decorateCommentFunction · 0.85
isOwnLineCommentFunction · 0.85
isEndOfLineCommentFunction · 0.85
handleOwnLineCommentFunction · 0.85
handleEndOfLineCommentFunction · 0.85
handleRemainingCommentFunction · 0.85
breakTiesFunction · 0.85
mapMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…