-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract
printAssignment
, printBinaryishExpression
, and `printComm…
…ent` (#9722)
- Loading branch information
Showing
5 changed files
with
510 additions
and
447 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
"use strict"; | ||
|
||
const { | ||
builders: { concat, line, group, indent }, | ||
} = require("../../document"); | ||
const { | ||
hasLeadingOwnLineComment, | ||
isBinaryish, | ||
isMemberExpressionChain, | ||
isStringLiteral, | ||
} = require("../utils"); | ||
const { shouldInlineLogicalExpression } = require("./binaryish"); | ||
|
||
function printAssignment( | ||
leftNode, | ||
printedLeft, | ||
operator, | ||
rightNode, | ||
printedRight, | ||
options | ||
) { | ||
if (!rightNode) { | ||
return printedLeft; | ||
} | ||
|
||
const printed = printAssignmentRight( | ||
leftNode, | ||
rightNode, | ||
printedRight, | ||
options | ||
); | ||
|
||
return group(concat([printedLeft, operator, printed])); | ||
} | ||
|
||
function printAssignmentExpression(path, options, print) { | ||
const n = path.getValue(); | ||
return printAssignment( | ||
n.left, | ||
path.call(print, "left"), | ||
concat([" ", n.operator]), | ||
n.right, | ||
path.call(print, "right"), | ||
options | ||
); | ||
} | ||
|
||
function printVariableDeclarator(path, options, print) { | ||
const n = path.getValue(); | ||
return printAssignment( | ||
n.id, | ||
path.call(print, "id"), | ||
" =", | ||
n.init, | ||
n.init && path.call(print, "init"), | ||
options | ||
); | ||
} | ||
|
||
function printAssignmentRight(leftNode, rightNode, printedRight, options) { | ||
if (hasLeadingOwnLineComment(options.originalText, rightNode)) { | ||
return indent(concat([line, printedRight])); | ||
} | ||
|
||
const canBreak = | ||
(isBinaryish(rightNode) && !shouldInlineLogicalExpression(rightNode)) || | ||
(rightNode.type === "ConditionalExpression" && | ||
isBinaryish(rightNode.test) && | ||
!shouldInlineLogicalExpression(rightNode.test)) || | ||
rightNode.type === "StringLiteralTypeAnnotation" || | ||
(rightNode.type === "ClassExpression" && | ||
rightNode.decorators && | ||
rightNode.decorators.length) || | ||
((leftNode.type === "Identifier" || | ||
isStringLiteral(leftNode) || | ||
leftNode.type === "MemberExpression") && | ||
(isStringLiteral(rightNode) || isMemberExpressionChain(rightNode)) && | ||
// do not put values on a separate line from the key in json | ||
options.parser !== "json" && | ||
options.parser !== "json5") || | ||
rightNode.type === "SequenceExpression"; | ||
|
||
if (canBreak) { | ||
return group(indent(concat([line, printedRight]))); | ||
} | ||
|
||
return concat([" ", printedRight]); | ||
} | ||
|
||
module.exports = { | ||
printVariableDeclarator, | ||
printAssignmentExpression, | ||
printAssignment, | ||
printAssignmentRight, | ||
}; |
Oops, something went wrong.