Skip to content

Commit

Permalink
Extract printAssignment, printBinaryishExpression, and `printComm…
Browse files Browse the repository at this point in the history
…ent` (#9722)
  • Loading branch information
fisker committed Nov 19, 2020
1 parent 05236cf commit 92d31d1
Show file tree
Hide file tree
Showing 5 changed files with 510 additions and 447 deletions.
95 changes: 95 additions & 0 deletions src/language-js/print/assignment.js
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,
};

0 comments on commit 92d31d1

Please sign in to comment.