diff --git a/src/nodes/Conditional.js b/src/nodes/Conditional.js index 27289a6b0..5e95ad2fd 100644 --- a/src/nodes/Conditional.js +++ b/src/nodes/Conditional.js @@ -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'); @@ -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}` } diff --git a/src/parser.js b/src/parser.js index e9d7427b0..7cf14cc0a 100644 --- a/src/parser.js +++ b/src/parser.js @@ -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; } diff --git a/tests/format/ExperimentalTernaries/ExperimentalTernaries.sol b/tests/format/ExperimentalTernaries/ExperimentalTernaries.sol index 6b85b232f..bb628b0f3 100644 --- a/tests/format/ExperimentalTernaries/ExperimentalTernaries.sol +++ b/tests/format/ExperimentalTernaries/ExperimentalTernaries.sol @@ -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; diff --git a/tests/format/ExperimentalTernaries/__snapshots__/jsfmt.spec.js.snap b/tests/format/ExperimentalTernaries/__snapshots__/jsfmt.spec.js.snap index 0c8cefad0..c6d33e42b 100644 --- a/tests/format/ExperimentalTernaries/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/ExperimentalTernaries/__snapshots__/jsfmt.spec.js.snap @@ -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; @@ -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; @@ -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; @@ -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;