diff --git a/package-lock.json b/package-lock.json index 25de30fc5..6f5c0d66d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "prettier-plugin-solidity", - "version": "1.3.0", + "version": "1.3.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "prettier-plugin-solidity", - "version": "1.3.0", + "version": "1.3.1", "license": "MIT", "dependencies": { "@solidity-parser/parser": "^0.17.0", @@ -30,7 +30,7 @@ "jest-snapshot-serializer-raw": "^2.0.0", "jest-watch-typeahead": "^2.2.2", "lines-and-columns": "^2.0.3", - "prettier": "^3.1.0", + "prettier": "^3.1.1", "proxyquire": "^2.1.3", "solc": "^0.8.23-fixed", "webpack": "^5.88.2", diff --git a/package.json b/package.json index eafa3ae38..3e2ac3f9d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prettier-plugin-solidity", - "version": "1.3.0", + "version": "1.3.1", "description": "A Prettier Plugin for automatically formatting your Solidity code.", "type": "module", "main": "./src/index.js", @@ -103,7 +103,7 @@ "jest-snapshot-serializer-raw": "^2.0.0", "jest-watch-typeahead": "^2.2.2", "lines-and-columns": "^2.0.3", - "prettier": "^3.1.0", + "prettier": "^3.1.1", "proxyquire": "^2.1.3", "solc": "^0.8.23-fixed", "webpack": "^5.88.2", diff --git a/src/parser.js b/src/parser.js index 30088914e..ab5492dd7 100644 --- a/src/parser.js +++ b/src/parser.js @@ -61,15 +61,19 @@ function parse(text, _parsers, options = _parsers) { ctx.value = options.singleQuote ? `hex'${value}'` : `hex"${value}"`; }, Conditional(ctx) { - // We can remove parentheses only because we are sure that the - // `condition` must be a single `bool` value. - while ( - ctx.condition.type === 'TupleExpression' && - !ctx.condition.isArray && - ctx.condition.components.length === 1 && - ctx.condition.components[0].type !== 'Conditional' - ) { - [ctx.condition] = ctx.condition.components; + // TODO: while the behaviour is not stable, it should be behind the + // experimentalTernaries flag. + if (options.experimentalTernaries) { + // We can remove parentheses only because we are sure that the + // `condition` must be a single `bool` value. + while ( + ctx.condition.type === 'TupleExpression' && + !ctx.condition.isArray && + ctx.condition.components.length === 1 && + ctx.condition.components[0].type !== 'Conditional' + ) { + [ctx.condition] = ctx.condition.components; + } } }, BinaryOperation(ctx) { diff --git a/tests/format/ExperimentalTernaries/__snapshots__/jsfmt.spec.js.snap b/tests/format/ExperimentalTernaries/__snapshots__/jsfmt.spec.js.snap index 38c5cb466..a290e72ad 100644 --- a/tests/format/ExperimentalTernaries/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/ExperimentalTernaries/__snapshots__/jsfmt.spec.js.snap @@ -1895,13 +1895,13 @@ contract Conditional { // multiline conditions and consequents/alternates: string - storage multilineConditionsConsequentsAndAlternates = isAnAdorableKittyCat() && - (someReallyLongCondition || moreInThisLongCondition) + storage multilineConditionsConsequentsAndAlternates = (isAnAdorableKittyCat() && + (someReallyLongCondition || moreInThisLongCondition)) ? someReallyLargeExpression .thatWouldCauseALineBreak() .willCauseAnIndentButNotParens() - : isNotAnAdorableKittyCat() && - (someReallyLongCondition || moreInThisLongCondition) + : (isNotAnAdorableKittyCat() && + (someReallyLongCondition || moreInThisLongCondition)) ? bark() : shortCondition() ? shortConsequent() @@ -1910,13 +1910,13 @@ contract Conditional { .willCauseAnIndentButNotParens(); // Assignment also groups and indents as Variable Declaration: - assignment = isAnAdorableKittyCat() && - (someReallyLongCondition || moreInThisLongCondition) + assignment = (isAnAdorableKittyCat() && + (someReallyLongCondition || moreInThisLongCondition)) ? someReallyLargeExpression .thatWouldCauseALineBreak() .willCauseAnIndentButNotParens() - : isNotAnAdorableKittyCat() && - (someReallyLongCondition || moreInThisLongCondition) + : (isNotAnAdorableKittyCat() && + (someReallyLongCondition || moreInThisLongCondition)) ? bark() : shortCondition() ? shortConsequent() @@ -1931,32 +1931,33 @@ contract Conditional { ? "two" : x == 3 ? "three" - : x == 5 && + : (x == 5 && y == 7 && someOtherThing .thatIsSoLong - .thatItBreaksTheTestCondition() + .thatItBreaksTheTestCondition()) ? "four" : x == 6 ? "six" : "idk"; // long conditional, short consequent/alternate, not chained - do indent after ? - string storage longConditional = bifornCringerMoshedPerplexSawder == + string storage longConditional = (bifornCringerMoshedPerplexSawder == 2 / askTrovenaBeenaDependsRowans && glimseGlyphsHazardNoopsTieTie >= - averredBathersBoxroomBuggyNurl().anodyneCondosMalateOverateRetinol() + averredBathersBoxroomBuggyNurl() + .anodyneCondosMalateOverateRetinol()) ? "foo" : "bar"; // long conditional, short consequent/alternate, chained // (break on short consequents iff in chained ternary and its conditional broke) string - storage longConditionalChained = bifornCringerMoshedPerplexSawder == + storage longConditionalChained = (bifornCringerMoshedPerplexSawder == 2 / askTrovenaBeenaDependsRowans && glimseGlyphsHazardNoopsTieTie >= averredBathersBoxroomBuggyNurl() - .anodyneCondosMalateOverateRetinol() + .anodyneCondosMalateOverateRetinol()) ? "foo" : anotherCondition ? "bar" @@ -1977,7 +1978,7 @@ contract Conditional { ); // Conditional as a condition - (foo ? 1 : bar) ? 3 : 4; + (((foo ? 1 : bar))) ? 3 : 4; ( isCat() ? meow()