prettier-plugin-solidity 1.3.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-solidity",
3
- "version": "1.3.1",
3
+ "version": "1.4.0",
4
4
  "description": "A Prettier Plugin for automatically formatting your Solidity code.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -88,7 +88,7 @@
88
88
  },
89
89
  "devDependencies": {
90
90
  "@babel/code-frame": "^7.22.10",
91
- "c8": "^8.0.1",
91
+ "c8": "^9.0.0",
92
92
  "cross-env": "^7.0.3",
93
93
  "eslint": "^8.47.0",
94
94
  "eslint-config-airbnb-base": "^15.0.0",
@@ -96,7 +96,6 @@
96
96
  "eslint-plugin-import": "^2.28.1",
97
97
  "esm-utils": "^4.1.2",
98
98
  "esmock": "^2.3.8",
99
- "exports-loader": "^4.0.0",
100
99
  "jest": "^29.6.3",
101
100
  "jest-light-runner": "^0.6.0",
102
101
  "jest-snapshot-serializer-ansi": "^2.1.0",
@@ -105,14 +104,13 @@
105
104
  "lines-and-columns": "^2.0.3",
106
105
  "prettier": "^3.1.1",
107
106
  "proxyquire": "^2.1.3",
108
- "solc": "^0.8.23-fixed",
107
+ "solc": "^0.8.26",
109
108
  "webpack": "^5.88.2",
110
109
  "webpack-cli": "^5.1.4"
111
110
  },
112
111
  "dependencies": {
113
- "@solidity-parser/parser": "^0.17.0",
114
- "semver": "^7.5.4",
115
- "solidity-comments-extractor": "^0.0.8"
112
+ "@solidity-parser/parser": "^0.18.0",
113
+ "semver": "^7.5.4"
116
114
  },
117
115
  "peerDependencies": {
118
116
  "prettier": ">=2.3.0"
@@ -7,12 +7,12 @@ function isIndentableBlockComment(comment) {
7
7
  // we can fix the indentation of each line. The stars in the `/*` and
8
8
  // `*/` delimiters are not included in the comment value, so add them
9
9
  // back first.
10
- const lines = `*${comment.raw}*`.split('\n');
10
+ const lines = `*${comment.value}*`.split('\n');
11
11
  return lines.length > 1 && lines.every((line) => line.trim()[0] === '*');
12
12
  }
13
13
 
14
14
  function printIndentableBlockComment(comment) {
15
- const lines = comment.raw.split('\n');
15
+ const lines = comment.value.split('\n');
16
16
 
17
17
  return [
18
18
  '/*',
@@ -49,10 +49,10 @@ export function printComment(commentPath, options) {
49
49
  return printed;
50
50
  }
51
51
 
52
- return `/*${comment.raw}*/`;
52
+ return `/*${comment.value}*/`;
53
53
  }
54
54
  case 'LineComment':
55
- return `//${comment.raw.trimEnd()}`;
55
+ return `//${comment.value.trimEnd()}`;
56
56
  default:
57
57
  throw new Error(`Not a comment: ${JSON.stringify(comment)}`);
58
58
  }
@@ -8,15 +8,19 @@ import {
8
8
  const { hardline } = doc.builders;
9
9
 
10
10
  export const AssemblyBlock = {
11
- print: ({ node, options, path, print }) => [
12
- '{',
13
- printSeparatedItem(
14
- [
15
- printPreservingEmptyLines(path, 'operations', options, print),
16
- printComments(node, path, options)
17
- ],
18
- { firstSeparator: hardline, grouped: false }
19
- ),
20
- '}'
21
- ]
11
+ print: ({ node, options, path, print }) =>
12
+ // if block is empty, just return the pair of braces
13
+ node.operations.length === 0 && !node.comments
14
+ ? '{}'
15
+ : [
16
+ '{',
17
+ printSeparatedItem(
18
+ [
19
+ printPreservingEmptyLines(path, 'operations', options, print),
20
+ printComments(node, path, options)
21
+ ],
22
+ { firstSeparator: hardline, grouped: false }
23
+ ),
24
+ '}'
25
+ ]
22
26
  };
@@ -1,7 +1,12 @@
1
+ import { doc } from 'prettier';
2
+
3
+ const { hardline } = doc.builders;
4
+
1
5
  export const AssemblyStackAssignment = {
2
6
  print: ({ node, path, print }) => [
3
7
  path.call(print, 'expression'),
4
- ' =: ',
8
+ hardline,
9
+ '=: ',
5
10
  node.name
6
11
  ]
7
12
  };
@@ -3,7 +3,6 @@ import { printSeparatedItem } from '../common/printer-helpers.js';
3
3
 
4
4
  const { group, hardline, ifBreak, indent, line, softline } = doc.builders;
5
5
 
6
- let groupIndex = 0;
7
6
  const experimentalTernaries = (node, path, print, options) => {
8
7
  const parent = path.getParentNode();
9
8
  const isNested = parent.type === 'Conditional';
@@ -30,11 +29,9 @@ const experimentalTernaries = (node, path, print, options) => {
30
29
 
31
30
  const conditionAndTrueExpressionGroup = group(
32
31
  [conditionDoc, trueExpressionDoc],
33
- { id: `Conditional.trueExpressionDoc-${groupIndex}` }
32
+ { id: Symbol('Conditional.trueExpression') }
34
33
  );
35
34
 
36
- groupIndex += 1;
37
-
38
35
  // For the odd case of `tabWidth` of 1 or 0 we initiate `fillTab` as a single
39
36
  // space.
40
37
  let fillTab = ' ';
@@ -1,27 +1,6 @@
1
- import { doc } from 'prettier';
2
- import { printComments } from '../common/printer-helpers.js';
3
-
4
- const { hardline } = doc.builders;
5
-
6
1
  export const ExpressionStatement = {
7
- print: ({ node, options, path, print }) => {
8
- const parts = [];
9
-
10
- const parent = path.getParentNode();
11
-
12
- if (parent.type === 'IfStatement') {
13
- if (node.comments?.length) {
14
- const comments = printComments(node, path, options);
15
- if (comments?.length) {
16
- parts.push(comments);
17
- parts.push(hardline);
18
- }
19
- }
20
- }
21
-
22
- parts.push(path.call(print, 'expression'));
23
- parts.push(node.omitSemicolon ? '' : ';');
24
-
25
- return parts;
26
- }
2
+ print: ({ node, path, print }) => [
3
+ path.call(print, 'expression'),
4
+ node.omitSemicolon ? '' : ';'
5
+ ]
27
6
  };
@@ -24,7 +24,6 @@ const printArguments = (path, print) =>
24
24
  lastSeparator: [softline, ')']
25
25
  });
26
26
 
27
- let groupIndex = 0;
28
27
  export const FunctionCall = {
29
28
  print: ({ node, path, print, options }) => {
30
29
  let expressionDoc = path.call(print, 'expression');
@@ -42,11 +41,9 @@ export const FunctionCall = {
42
41
  // arguments accordingly.
43
42
  if (expressionDoc.label === 'MemberAccessChain') {
44
43
  expressionDoc = group(expressionDoc.contents, {
45
- id: `FunctionCall.expression-${groupIndex}`
44
+ id: Symbol('FunctionCall.expression')
46
45
  });
47
46
 
48
- groupIndex += 1;
49
-
50
47
  argumentsDoc = indentIfBreak(argumentsDoc, {
51
48
  groupId: expressionDoc.id
52
49
  });
@@ -24,16 +24,19 @@ const stateMutability = (node) =>
24
24
  : '';
25
25
 
26
26
  export const FunctionTypeName = {
27
- print: ({ node, path, print }) => [
28
- 'function(',
29
- printSeparatedList(path.map(print, 'parameterTypes')),
30
- ')',
31
- indent(
32
- group([
33
- visibility(node),
34
- stateMutability(node),
35
- returnTypes(node, path, print)
36
- ])
37
- )
38
- ]
27
+ print: ({ node, path, print }) =>
28
+ group([
29
+ 'function(',
30
+ printSeparatedList(path.map(print, 'parameterTypes'), {
31
+ grouped: false
32
+ }),
33
+ ')',
34
+ indent(
35
+ group([
36
+ visibility(node),
37
+ stateMutability(node),
38
+ returnTypes(node, path, print)
39
+ ])
40
+ )
41
+ ])
39
42
  };
@@ -1,11 +1,11 @@
1
1
  import { doc } from 'prettier';
2
2
  import { printString } from '../common/util.js';
3
3
 
4
- const { join, line } = doc.builders;
4
+ const { join, hardline } = doc.builders;
5
5
 
6
6
  export const HexLiteral = {
7
7
  print: ({ node, options }) => {
8
8
  const list = node.parts.map((part) => `hex${printString(part, options)}`);
9
- return join(line, list);
9
+ return join(hardline, list);
10
10
  }
11
11
  };
@@ -1,8 +1,5 @@
1
1
  import { doc } from 'prettier';
2
- import {
3
- printComments,
4
- printSeparatedItem
5
- } from '../common/printer-helpers.js';
2
+ import { printSeparatedItem } from '../common/printer-helpers.js';
6
3
 
7
4
  const { group, hardline, indent, line } = doc.builders;
8
5
 
@@ -22,10 +19,9 @@ const printFalseBody = (node, path, print) =>
22
19
  ? [' ', path.call(print, 'falseBody')]
23
20
  : group(indent([line, path.call(print, 'falseBody')]));
24
21
 
25
- const printElse = (node, path, print, commentsBetweenIfAndElse) => {
22
+ const printElse = (node, path, print) => {
26
23
  if (node.falseBody) {
27
- const elseOnSameLine =
28
- node.trueBody.type === 'Block' && commentsBetweenIfAndElse.length === 0;
24
+ const elseOnSameLine = node.trueBody.type === 'Block';
29
25
  return [
30
26
  elseOnSameLine ? ' ' : hardline,
31
27
  'else',
@@ -36,22 +32,11 @@ const printElse = (node, path, print, commentsBetweenIfAndElse) => {
36
32
  };
37
33
 
38
34
  export const IfStatement = {
39
- print: ({ node, options, path, print }) => {
40
- const comments = node.comments || [];
41
- const commentsBetweenIfAndElse = comments.filter(
42
- (comment) => !comment.leading && !comment.trailing
43
- );
44
-
45
- const parts = [];
46
-
47
- parts.push('if (', printSeparatedItem(path.call(print, 'condition')), ')');
48
- parts.push(printTrueBody(node, path, print));
49
- if (commentsBetweenIfAndElse.length && node.falseBody) {
50
- parts.push(hardline);
51
- parts.push(printComments(node, path, options));
52
- }
53
- parts.push(printElse(node, path, print, commentsBetweenIfAndElse));
54
-
55
- return parts;
56
- }
35
+ print: ({ node, path, print }) => [
36
+ 'if (',
37
+ printSeparatedItem(path.call(print, 'condition')),
38
+ ')',
39
+ printTrueBody(node, path, print),
40
+ printElse(node, path, print)
41
+ ]
57
42
  };
@@ -2,7 +2,6 @@ import { doc } from 'prettier';
2
2
 
3
3
  const { group, indent, indentIfBreak, label, softline } = doc.builders;
4
4
 
5
- let groupIndex = 0;
6
5
  export const IndexAccess = {
7
6
  print: ({ path, print }) => {
8
7
  let baseDoc = path.call(print, 'base');
@@ -16,11 +15,9 @@ export const IndexAccess = {
16
15
  // arguments accordingly.
17
16
  if (baseDoc.label === 'MemberAccessChain') {
18
17
  baseDoc = group(baseDoc.contents, {
19
- id: `IndexAccess.base-${groupIndex}`
18
+ id: Symbol('IndexAccess.base')
20
19
  });
21
20
 
22
- groupIndex += 1;
23
-
24
21
  indexDoc = indentIfBreak(indexDoc, {
25
22
  groupId: baseDoc.id
26
23
  });
@@ -1,20 +1,14 @@
1
1
  import { doc } from 'prettier';
2
2
  import { printSeparatedList } from '../common/printer-helpers.js';
3
3
 
4
- const { group, hardline, indent, line } = doc.builders;
4
+ const { dedent, group, indent, line } = doc.builders;
5
5
 
6
6
  const modifierParameters = (node, path, print) => {
7
7
  if (node.parameters?.length > 0) {
8
8
  return [
9
9
  '(',
10
10
  printSeparatedList(path.map(print, 'parameters'), {
11
- separator: [
12
- ',',
13
- // To keep consistency any list of parameters will split if it's longer than 2.
14
- // For more information see:
15
- // https://github.com/prettier-solidity/prettier-plugin-solidity/issues/256
16
- node.parameters.length > 2 ? hardline : line
17
- ]
11
+ separator: [',', line]
18
12
  }),
19
13
  ')'
20
14
  ];
@@ -38,8 +32,8 @@ const override = (node, path, print) => {
38
32
 
39
33
  const body = (node, path, print) => {
40
34
  if (!node.body) return ';';
41
- if (node.isVirtual) return group([' ', path.call(print, 'body')]);
42
- return [' ', path.call(print, 'body')];
35
+ if (node.isVirtual) return group(path.call(print, 'body'));
36
+ return [path.call(print, 'body')];
43
37
  };
44
38
 
45
39
  export const ModifierDefinition = {
@@ -47,7 +41,13 @@ export const ModifierDefinition = {
47
41
  'modifier ',
48
42
  node.name,
49
43
  modifierParameters(node, path, print),
50
- group(indent([virtual(node), override(node, path, print)])),
44
+ group(
45
+ indent([
46
+ virtual(node),
47
+ override(node, path, print),
48
+ node.body ? dedent(line) : ''
49
+ ])
50
+ ),
51
51
  body(node, path, print)
52
52
  ]
53
53
  };
@@ -9,7 +9,6 @@ const embraceVariables = (document, embrace) =>
9
9
  const initialValue = (node, path, print) =>
10
10
  node.initialValue ? [' = ', path.call(print, 'initialValue')] : '';
11
11
 
12
- let groupIndex = 0;
13
12
  export const VariableDeclarationStatement = {
14
13
  print: ({ node, path, print }) => {
15
14
  const startsWithVar =
@@ -23,9 +22,8 @@ export const VariableDeclarationStatement = {
23
22
  node.variables.length > 1 || startsWithVar
24
23
  )
25
24
  ],
26
- { id: `VariableDeclarationStatement.variables-${groupIndex}` }
25
+ { id: Symbol('VariableDeclarationStatement.variables') }
27
26
  );
28
- groupIndex += 1;
29
27
  const initialValueDoc = initialValue(node, path, print);
30
28
 
31
29
  return group([
package/src/parser.js CHANGED
@@ -1,4 +1,3 @@
1
- import extractComments from 'solidity-comments-extractor';
2
1
  // https://prettier.io/docs/en/plugins.html#parsers
3
2
  import parser from '@solidity-parser/parser';
4
3
  import coerce from 'semver/functions/coerce.js';
@@ -16,8 +15,7 @@ const tryHug = (node, operators) => {
16
15
 
17
16
  function parse(text, _parsers, options = _parsers) {
18
17
  const compiler = coerce(options.compiler);
19
- const parsed = parser.parse(text, { loc: true, range: true });
20
- parsed.comments = extractComments(text);
18
+ const parsed = parser.parse(text, { loc: true, range: true, comments: true });
21
19
 
22
20
  parser.visit(parsed, {
23
21
  PragmaDirective(ctx) {
@@ -257,21 +257,51 @@ function handleIfStatementComments(
257
257
  precedingNode === enclosingNode.trueBody &&
258
258
  followingNode === enclosingNode.falseBody
259
259
  ) {
260
- if (precedingNode.type === "ExpressionStatement") {
261
- addTrailingComment(precedingNode, comment);
260
+ if (followingNode.type === "Block") {
261
+ if(followingNode.statements.length === 0) {
262
+ addDanglingComment(followingNode, comment);
263
+ } else {
264
+ addLeadingComment(followingNode.statements[followingNode.statements.length - 1], comment);
265
+ }
266
+ } else if (followingNode.type === "IfStatement") {
267
+ if (followingNode.trueBody.type === "Block") {
268
+ if(followingNode.trueBody.statements.length === 0) {
269
+ addDanglingComment(followingNode.trueBody, comment);
270
+ } else {
271
+ addLeadingComment(followingNode.trueBody.statements[0], comment);
272
+ }
273
+ } else {
274
+ addLeadingComment(followingNode.trueBody, comment);
275
+ }
262
276
  } else {
263
- addDanglingComment(enclosingNode, comment);
277
+ addLeadingComment(precedingNode, comment);
264
278
  }
265
279
  return true;
266
280
  }
267
281
 
268
- if (followingNode.type === "ExpressionStatement") {
269
- addBlockStatementFirstComment(followingNode, comment);
282
+ if (enclosingNode.trueBody === followingNode) {
283
+ if (followingNode.type === "Block") {
284
+ if(followingNode.statements.length === 0) {
285
+ addDanglingComment(followingNode, comment);
286
+ } else {
287
+ addLeadingComment(followingNode.statements[0], comment);
288
+ }
289
+ } else {
290
+ addLeadingComment(followingNode, comment);
291
+ }
270
292
  return true;
271
293
  }
272
294
 
273
295
  if (followingNode.type === "IfStatement") {
274
- addBlockOrNotComment(followingNode.trueBody, comment);
296
+ if (followingNode.trueBody.type === "Block") {
297
+ if(followingNode.trueBody.statements.length === 0) {
298
+ addDanglingComment(followingNode.trueBody, comment);
299
+ } else {
300
+ addLeadingComment(followingNode.trueBody.statements[0], comment);
301
+ }
302
+ } else {
303
+ addLeadingComment(followingNode.trueBody, comment);
304
+ }
275
305
  return true;
276
306
  }
277
307