Skip to content

Commit

Permalink
condition can be a Conditional if it's wrapped in a parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
Janther committed Nov 22, 2023
1 parent 10114fc commit 873cf8e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/nodes/Conditional.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const experimentalTernaries = (node, path, print) => {

// 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.
// If the `conditionDoc` breaks into multiple lines, we add parentheses,
// unless it already is a `TupleExpression`.
// This can only be done because we are sure that the `condition` must be a
// single `bool` value.
const conditionDoc = path.call(print, 'condition');
Expand All @@ -19,7 +20,9 @@ const experimentalTernaries = (node, path, print) => {
parent.type === 'Conditional' && parent.trueExpression === node
? hardlineWithoutBreakParent
: '',
ifBreak(['(', printSeparatedItem(conditionDoc), ')'], conditionDoc),
node.condition.type === 'TupleExpression'
? conditionDoc
: ifBreak(['(', printSeparatedItem(conditionDoc), ')'], conditionDoc),
' ?'
],
{ id: `Conditional.condition-${groupIndex}` }
Expand Down
5 changes: 3 additions & 2 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ function parse(text, _parsers, options = _parsers) {
},
Conditional(ctx) {
rearrangeConditional(ctx);
if (
while (
ctx.condition.type === 'TupleExpression' &&
!ctx.condition.isArray &&
ctx.condition.components.length === 1
ctx.condition.components.length === 1 &&
ctx.condition.components[0].type !== 'Conditional'
) {
[ctx.condition] = ctx.condition.components;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/format/ExperimentalTernaries/ExperimentalTernaries.sol
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ contract Conditional {
: row[field]
);

// Conditional as a condition
(((foo ? 1 : bar))) ? 3 : 4;
(isCat() ? meow()
: isDog() ? bark()
: silent()) ? 1: 2;

// In a return, break and over-indent:
if (short) {
return foo ? 1 : 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ contract Conditional {
: row[field]
);
// Conditional as a condition
(((foo ? 1 : bar))) ? 3 : 4;
(isCat() ? meow()
: isDog() ? bark()
: silent()) ? 1: 2;
// In a return, break and over-indent:
if (short) {
return foo ? 1 : 2;
Expand Down Expand Up @@ -359,6 +365,23 @@ contract Conditional {
: row[field]
);
// Conditional as a condition
(
foo ?
1
: bar
) ?
3
: 4;
(
isCat() ?
meow()
: isDog() ? bark()
: silent()
) ?
1
: 2;
// In a return, break and over-indent:
if (short) {
return foo ? 1 : 2;
Expand Down Expand Up @@ -570,6 +593,12 @@ contract Conditional {
: row[field]
);
// Conditional as a condition
(((foo ? 1 : bar))) ? 3 : 4;
(isCat() ? meow()
: isDog() ? bark()
: silent()) ? 1: 2;
// In a return, break and over-indent:
if (short) {
return foo ? 1 : 2;
Expand Down Expand Up @@ -774,6 +803,18 @@ contract Conditional {
: row[field]
);
// Conditional as a condition
(foo ? 1 : bar) ? 3 : 4;
(
isCat()
? meow()
: isDog()
? bark()
: silent()
)
? 1
: 2;
// In a return, break and over-indent:
if (short) {
return foo ? 1 : 2;
Expand Down

0 comments on commit 873cf8e

Please sign in to comment.