Skip to content

Commit 57ac1c6

Browse files
authored
updating the parser and bumping the version (#967)
1 parent 6ed961c commit 57ac1c6

File tree

4 files changed

+29
-62
lines changed

4 files changed

+29
-62
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ You might have a multi-version project, where different files are compiled with
208208

209209
### Experimental Ternaries
210210

211+
_Added in v1.3.0_
212+
211213
Mimicking prettier's [new ternary formatting](https://prettier.io/blog/2023/11/13/curious-ternaries) for the community to try.
212214

213215
Valid options:

package-lock.json

Lines changed: 6 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "prettier-plugin-solidity",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "A Prettier Plugin for automatically formatting your Solidity code.",
55
"type": "module",
66
"main": "./src/index.js",
@@ -110,7 +110,7 @@
110110
"webpack-cli": "^5.1.4"
111111
},
112112
"dependencies": {
113-
"@solidity-parser/parser": "^0.16.2",
113+
"@solidity-parser/parser": "^0.17.0",
114114
"semver": "^7.5.4",
115115
"solidity-comments-extractor": "^0.0.8"
116116
},

src/parser.js

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,6 @@ const tryHug = (node, operators) => {
1414
return node;
1515
};
1616

17-
// The parser wrongly groups nested Conditionals in the falseExpression
18-
// in the following way:
19-
//
20-
// (a ? b : c) ? d : e;
21-
//
22-
// By reorganizing the group we have more flexibility when printing:
23-
//
24-
// a ? b : (c ? d : e);
25-
//
26-
// this is closer to the executed code and prints the same output.
27-
const rearrangeConditional = (ctx) => {
28-
while (ctx.condition.type === 'Conditional') {
29-
const falseExpression = {
30-
type: 'Conditional',
31-
condition: ctx.condition.falseExpression,
32-
trueExpression: ctx.trueExpression,
33-
falseExpression: ctx.falseExpression
34-
};
35-
rearrangeConditional(falseExpression);
36-
37-
ctx.falseExpression = falseExpression;
38-
ctx.trueExpression = ctx.condition.trueExpression;
39-
ctx.condition = ctx.condition.condition;
40-
}
41-
};
42-
4317
function parse(text, _parsers, options = _parsers) {
4418
const compiler = coerce(options.compiler);
4519
const parsed = parser.parse(text, { loc: true, range: true });
@@ -87,7 +61,6 @@ function parse(text, _parsers, options = _parsers) {
8761
ctx.value = options.singleQuote ? `hex'${value}'` : `hex"${value}"`;
8862
},
8963
Conditional(ctx) {
90-
rearrangeConditional(ctx);
9164
// We can remove parentheses only because we are sure that the
9265
// `condition` must be a single `bool` value.
9366
while (
@@ -116,34 +89,34 @@ function parse(text, _parsers, options = _parsers) {
11689
ctx.left = tryHug(ctx.left, ['*', '/', '%']);
11790
break;
11891
case '**':
119-
// If the compiler has not been given as an option using we leave a**b**c.
92+
// If the compiler has not been given as an option using we leave
93+
// a**b**c.
12094
if (!compiler) break;
12195

122-
if (satisfies(compiler, '<0.8.0')) {
123-
// If the compiler is less than 0.8.0 then a**b**c is formatted as
124-
// (a**b)**c.
125-
ctx.left = tryHug(ctx.left, ['**']);
96+
if (satisfies(compiler, '>=0.8.0')) {
97+
// If the compiler is greater than or equal to 0.8.0 then a**b**c
98+
// is formatted as a**(b**c).
99+
ctx.right = tryHug(ctx.right, ['**']);
126100
break;
127101
}
128102
if (
129-
ctx.left.type === 'BinaryOperation' &&
130-
ctx.left.operator === '**'
103+
ctx.right.type === 'BinaryOperation' &&
104+
ctx.right.operator === '**'
131105
) {
132-
// the parser still organizes the a**b**c as (a**b)**c so we need
133-
// to restructure it.
134-
ctx.right = {
106+
// the parser organizes the a**b**c as a**(b**c) so we need to
107+
// restructure it.
108+
const left = {
109+
type: 'BinaryOperation',
110+
operator: '**',
111+
left: ctx.left,
112+
right: ctx.right.left
113+
};
114+
ctx.left = {
135115
type: 'TupleExpression',
136-
components: [
137-
{
138-
type: 'BinaryOperation',
139-
operator: '**',
140-
left: ctx.left.right,
141-
right: ctx.right
142-
}
143-
],
116+
components: [left],
144117
isArray: false
145118
};
146-
ctx.left = ctx.left.left;
119+
ctx.right = ctx.right.right;
147120
}
148121
break;
149122
case '<<':

0 commit comments

Comments
 (0)