Skip to content

Commit

Permalink
cleanup and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Janther committed Nov 21, 2023
1 parent 20dd0e7 commit 250a00f
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions src/nodes/Conditional.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { doc } from 'prettier';
import { printSeparatedItem } from '../common/printer-helpers.js';

const { group, indent, line, ifBreak, hardline, hardlineWithoutBreakParent } =
const { group, hardline, hardlineWithoutBreakParent, ifBreak, indent, line } =
doc.builders;

let groupIndex = 0;
const experimentalTernaries = (node, path, print) => {
const parent = path.getParentNode();

const falseExpressionIsConditional =
node.falseExpression.type === 'Conditional';
const trueExpressionIsConditional =
node.trueExpression.type === 'Conditional';

// If the current `Conditional` is nested in another `Conditional`'s
// `trueExpression`, we add a line without propagating the break group.
// If the `conditionDoc` breaks into multiple lines, we add parentheses.
// This can only be done because we are sure that the `condition` must be a
// single `bool` value.
const conditionDoc = path.call(print, 'condition');
const conditionGroup = group(
[
Expand All @@ -27,6 +27,10 @@ const experimentalTernaries = (node, path, print) => {

groupIndex += 1;

// If the `conditionGroup` breaks we force a new line to separate the
// `trueExpression` and `falseExpression`.
// In the case of `Conditional`, `VariableDeclarationStatement` and
// `ReturnStatement` we avoid propagating the group breaking.
const expressionSeparator = ifBreak(
['Conditional', 'VariableDeclarationStatement', 'ReturnStatement'].includes(
parent.type
Expand All @@ -37,27 +41,38 @@ const experimentalTernaries = (node, path, print) => {
{ groupId: conditionGroup.id }
);

const document = group([
conditionGroup,
group(
indent([
trueExpressionIsConditional ? '' : expressionSeparator,
path.call(print, 'trueExpression')
])
),
parent.type === 'Conditional' || falseExpressionIsConditional
// We avoid prepending a separation if the `trueExpression` is a
// `Conditional` since it's added by default in the `conditionGroup`.
const trueIsConditional = node.trueExpression.type === 'Conditional';
const trueExpressionDoc = printSeparatedItem(
[
trueIsConditional ? '' : expressionSeparator,
path.call(print, 'trueExpression')
],
{ firstSeparator: '' }
);

// We force a new line if it's a nested `Conditional` or if the
// `falseExpression` is a `Conditional`. Otherwise we add a normal separator.
const falseIsConditional = node.falseExpression.type === 'Conditional';
const falseExpressionDoc = [
parent.type === 'Conditional' || falseIsConditional
? hardlineWithoutBreakParent
: expressionSeparator,
': ',
path.call(print, 'falseExpression')
];

const document = group([
conditionGroup,
trueExpressionDoc,
falseExpressionDoc
]);

return parent.type === 'VariableDeclarationStatement'
? ifBreak(
indent([
trueExpressionIsConditional || falseExpressionIsConditional
? hardline
: line,
trueIsConditional || falseIsConditional ? hardline : line,
document
]),
document
Expand Down

0 comments on commit 250a00f

Please sign in to comment.