diff --git a/README.md b/README.md index b4b64f9e7..dcc641913 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,8 @@ You might have a multi-version project, where different files are compiled with ### Experimental Ternaries +_Added in v1.3.0_ + Mimicking prettier's [new ternary formatting](https://prettier.io/blog/2023/11/13/curious-ternaries) for the community to try. Valid options: diff --git a/package-lock.json b/package-lock.json index 463c5c5df..25de30fc5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "prettier-plugin-solidity", - "version": "1.2.0", + "version": "1.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "prettier-plugin-solidity", - "version": "1.2.0", + "version": "1.3.0", "license": "MIT", "dependencies": { - "@solidity-parser/parser": "^0.16.2", + "@solidity-parser/parser": "^0.17.0", "semver": "^7.5.4", "solidity-comments-extractor": "^0.0.8" }, @@ -1347,12 +1347,9 @@ } }, "node_modules/@solidity-parser/parser": { - "version": "0.16.2", - "resolved": "https://registry.npmjs.org/@solidity-parser/parser/-/parser-0.16.2.tgz", - "integrity": "sha512-PI9NfoA3P8XK2VBkK5oIfRgKDsicwDZfkVq9ZTBCQYGOP1N2owgY2dyLGyU5/J/hQs8KRk55kdmvTLjy3Mu3vg==", - "dependencies": { - "antlr4ts": "^0.5.0-alpha.4" - } + "version": "0.17.0", + "resolved": "https://registry.npmjs.org/@solidity-parser/parser/-/parser-0.17.0.tgz", + "integrity": "sha512-Nko8R0/kUo391jsEHHxrGM07QFdnPGvlmox4rmH0kNiNAashItAilhy4Mv4pK5gQmW5f4sXAF58fwJbmlkGcVw==" }, "node_modules/@types/babel__core": { "version": "7.20.1", @@ -1828,11 +1825,6 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, - "node_modules/antlr4ts": { - "version": "0.5.0-alpha.4", - "resolved": "https://registry.npmjs.org/antlr4ts/-/antlr4ts-0.5.0-alpha.4.tgz", - "integrity": "sha512-WPQDt1B74OfPv/IMS2ekXAKkTZIHl88uMetg6q3OTqgFxZ/dxDXI0EWLyZid/1Pe6hTftyg5N7gel5wNAGxXyQ==" - }, "node_modules/anymatch": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", diff --git a/package.json b/package.json index 7fb7ecb96..eafa3ae38 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prettier-plugin-solidity", - "version": "1.2.0", + "version": "1.3.0", "description": "A Prettier Plugin for automatically formatting your Solidity code.", "type": "module", "main": "./src/index.js", @@ -110,7 +110,7 @@ "webpack-cli": "^5.1.4" }, "dependencies": { - "@solidity-parser/parser": "^0.16.2", + "@solidity-parser/parser": "^0.17.0", "semver": "^7.5.4", "solidity-comments-extractor": "^0.0.8" }, diff --git a/src/parser.js b/src/parser.js index d17db276a..30088914e 100644 --- a/src/parser.js +++ b/src/parser.js @@ -14,32 +14,6 @@ const tryHug = (node, operators) => { return node; }; -// The parser wrongly groups nested Conditionals in the falseExpression -// in the following way: -// -// (a ? b : c) ? d : e; -// -// By reorganizing the group we have more flexibility when printing: -// -// a ? b : (c ? d : e); -// -// this is closer to the executed code and prints the same output. -const rearrangeConditional = (ctx) => { - while (ctx.condition.type === 'Conditional') { - const falseExpression = { - type: 'Conditional', - condition: ctx.condition.falseExpression, - trueExpression: ctx.trueExpression, - falseExpression: ctx.falseExpression - }; - rearrangeConditional(falseExpression); - - ctx.falseExpression = falseExpression; - ctx.trueExpression = ctx.condition.trueExpression; - ctx.condition = ctx.condition.condition; - } -}; - function parse(text, _parsers, options = _parsers) { const compiler = coerce(options.compiler); const parsed = parser.parse(text, { loc: true, range: true }); @@ -87,7 +61,6 @@ function parse(text, _parsers, options = _parsers) { ctx.value = options.singleQuote ? `hex'${value}'` : `hex"${value}"`; }, Conditional(ctx) { - rearrangeConditional(ctx); // We can remove parentheses only because we are sure that the // `condition` must be a single `bool` value. while ( @@ -116,34 +89,34 @@ function parse(text, _parsers, options = _parsers) { ctx.left = tryHug(ctx.left, ['*', '/', '%']); break; case '**': - // If the compiler has not been given as an option using we leave a**b**c. + // If the compiler has not been given as an option using we leave + // a**b**c. if (!compiler) break; - if (satisfies(compiler, '<0.8.0')) { - // If the compiler is less than 0.8.0 then a**b**c is formatted as - // (a**b)**c. - ctx.left = tryHug(ctx.left, ['**']); + if (satisfies(compiler, '>=0.8.0')) { + // If the compiler is greater than or equal to 0.8.0 then a**b**c + // is formatted as a**(b**c). + ctx.right = tryHug(ctx.right, ['**']); break; } if ( - ctx.left.type === 'BinaryOperation' && - ctx.left.operator === '**' + ctx.right.type === 'BinaryOperation' && + ctx.right.operator === '**' ) { - // the parser still organizes the a**b**c as (a**b)**c so we need - // to restructure it. - ctx.right = { + // the parser organizes the a**b**c as a**(b**c) so we need to + // restructure it. + const left = { + type: 'BinaryOperation', + operator: '**', + left: ctx.left, + right: ctx.right.left + }; + ctx.left = { type: 'TupleExpression', - components: [ - { - type: 'BinaryOperation', - operator: '**', - left: ctx.left.right, - right: ctx.right - } - ], + components: [left], isArray: false }; - ctx.left = ctx.left.left; + ctx.right = ctx.right.right; } break; case '<<':