MCPcopy
hub / github.com/prettier/prettier / printBinaryishExpressions

Function printBinaryishExpressions

src/language-js/print/binaryish.js:195–361  ·  view source on GitHub ↗
(
  path,
  options,
  print,
  isNested,
  isInsideParenthesis,
)

Source from the content-addressed store, hash-verified

193// level, things are naturally broken up correctly, i.e. `&&` is
194// broken before `+`.
195function printBinaryishExpressions(
196 path,
197 options,
198 print,
199 isNested,
200 isInsideParenthesis,
201) {
202 const { node } = path;
203
204 // Simply print the node normally.
205 if (!isBinaryish(node)) {
206 return [group(print())];
207 }
208
209 /** @type{Doc[]} */
210 let parts = [];
211
212 // We treat BinaryExpression and LogicalExpression nodes the same.
213
214 // Put all operators with the same precedence level in the same
215 // group. The reason we only need to do this with the `left`
216 // expression is because given an expression like `1 + 2 - 3`, it
217 // is always parsed like `((1 + 2) - 3)`, meaning the `left` side
218 // is where the rest of the expression will exist. Binary
219 // expressions on the right side mean they have a difference
220 // precedence level and should be treated as a separate group, so
221 // print them normally. (This doesn't hold for the `**` operator,
222 // which is unique in that it is right-associative.)
223 // @ts-expect-error -- FIXME
224 if (shouldFlatten(node.operator, node.left.operator)) {
225 // Flatten them out by recursively calling this function.
226 parts = path.call(
227 () =>
228 printBinaryishExpressions(
229 path,
230 options,
231 print,
232 /* isNested */ true,
233 isInsideParenthesis,
234 ),
235 "left",
236 );
237 } else {
238 parts.push(group(print("left")));
239 }
240
241 const shouldInline = shouldInlineLogicalExpression(node);
242 const rightNodeToCheckComments =
243 node.right.type === "ChainExpression" ? node.right.expression : node.right;
244 const lineBeforeOperator =
245 (node.type === "NGPipeExpression" ||
246 node.operator === "|>" ||
247 isVueFilterSequenceExpression(path, options)) &&
248 !hasLeadingOwnLineComment(options.originalText, rightNodeToCheckComments);
249 const hasTypeCastComment = hasComment(
250 rightNodeToCheckComments,
251 CommentCheckFlags.Leading,
252 isTypeCastComment,

Callers 1

printBinaryishExpressionFunction · 0.85

Calls 15

groupFunction · 0.90
shouldFlattenFunction · 0.90
hasLeadingOwnLineCommentFunction · 0.90
hasCommentFunction · 0.90
indentFunction · 0.90
joinFunction · 0.90
alignFunction · 0.90
getDocTypeFunction · 0.90
cleanDocFunction · 0.90
printCommentsFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…