Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating the parser and bumping the version #967

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 6 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
},
Expand Down
65 changes: 19 additions & 46 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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 '<<':
Expand Down