prettier-plugin-solidity 1.0.0-beta.15 → 1.0.0-beta.19

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.
Files changed (54) hide show
  1. package/.github/workflows/CI.yml +6 -6
  2. package/README.md +64 -3
  3. package/package.json +16 -12
  4. package/src/nodes/FunctionCall.js +26 -23
  5. package/src/nodes/IndexAccess.js +29 -7
  6. package/src/nodes/MemberAccess.js +63 -26
  7. package/src/nodes/ModifierDefinition.js +14 -16
  8. package/src/nodes/ModifierInvocation.js +2 -0
  9. package/src/nodes/StringLiteral.js +2 -2
  10. package/src/nodes/TypeDefinition.js +5 -0
  11. package/src/nodes/VariableDeclarationStatement.js +21 -8
  12. package/src/nodes/index.js +1 -0
  13. package/src/nodes/print-separated-list.js +1 -6
  14. package/src/parser.js +15 -9
  15. package/tests/config/format-test.js +1 -0
  16. package/tests/format/AllSolidityFeatures/__snapshots__/jsfmt.spec.js.snap +1 -1
  17. package/tests/format/ExplicitVariableTypes/ExplicitVariableTypes.sol +1 -1
  18. package/tests/format/ExplicitVariableTypes/__snapshots__/jsfmt.spec.js.snap +6 -6
  19. package/tests/format/FunctionCalls/__snapshots__/jsfmt.spec.js.snap +98 -86
  20. package/tests/format/FunctionDefinitions/FunctionDefinitions.sol +2 -2
  21. package/tests/format/FunctionDefinitions/__snapshots__/jsfmt.spec.js.snap +2 -2
  22. package/tests/format/HexLiteral/HexLiteral.sol +1 -1
  23. package/tests/format/HexLiteral/__snapshots__/jsfmt.spec.js.snap +4 -4
  24. package/tests/format/Issues/Issue564.sol +9 -0
  25. package/tests/format/Issues/__snapshots__/jsfmt.spec.js.snap +37 -5
  26. package/tests/format/MemberAccess/MemberAccess.sol +46 -0
  27. package/tests/format/MemberAccess/__snapshots__/jsfmt.spec.js.snap +99 -0
  28. package/tests/format/ModifierInvocations/ModifierInvocations.sol +17 -0
  29. package/tests/format/ModifierInvocations/__snapshots__/jsfmt.spec.js.snap +51 -0
  30. package/tests/format/ModifierInvocations/jsfmt.spec.js +1 -0
  31. package/tests/format/MultipartStrings/__snapshots__/jsfmt.spec.js.snap +3 -1
  32. package/tests/format/Parentheses/AddNoParentheses.sol +1 -1
  33. package/tests/format/Parentheses/BitAndNoParentheses.sol +1 -1
  34. package/tests/format/Parentheses/BitOrNoParentheses.sol +1 -1
  35. package/tests/format/Parentheses/BitXorNoParentheses.sol +1 -1
  36. package/tests/format/Parentheses/DivNoParentheses.sol +1 -1
  37. package/tests/format/Parentheses/ExpNoParentheses.sol +1 -1
  38. package/tests/format/Parentheses/LogicNoParentheses.sol +1 -1
  39. package/tests/format/Parentheses/ModNoParentheses.sol +1 -1
  40. package/tests/format/Parentheses/MulNoParentheses.sol +1 -1
  41. package/tests/format/Parentheses/ShiftLNoParentheses.sol +1 -1
  42. package/tests/format/Parentheses/ShiftRNoParentheses.sol +1 -1
  43. package/tests/format/Parentheses/SubNoParentheses.sol +1 -1
  44. package/tests/format/Parentheses/__snapshots__/jsfmt.spec.js.snap +24 -24
  45. package/tests/format/Pragma/Pragma.sol +1 -0
  46. package/tests/format/Pragma/__snapshots__/jsfmt.spec.js.snap +2 -0
  47. package/tests/format/SplittableCommodity/__snapshots__/jsfmt.spec.js.snap +1 -1
  48. package/tests/format/StringLiteral/StringLiteral.sol +11 -0
  49. package/tests/format/StringLiteral/__snapshots__/jsfmt.spec.js.snap +36 -0
  50. package/tests/format/StringLiteral/jsfmt.spec.js +1 -0
  51. package/tests/format/TypeDefinition/TypeDefinition.sol +12 -0
  52. package/tests/format/TypeDefinition/__snapshots__/jsfmt.spec.js.snap +74 -0
  53. package/tests/format/TypeDefinition/jsfmt.spec.js +1 -0
  54. package/tests/format/quotes/__snapshots__/jsfmt.spec.js.snap +18 -6
@@ -15,7 +15,7 @@ jobs:
15
15
  steps:
16
16
  - uses: actions/setup-node@v1
17
17
  with:
18
- node-version: 12
18
+ node-version: 14
19
19
  - uses: actions/checkout@v2
20
20
  - uses: actions/cache@v2
21
21
  id: cache
@@ -33,7 +33,7 @@ jobs:
33
33
  runs-on: ubuntu-latest
34
34
  strategy:
35
35
  matrix:
36
- node: [12, 14, 15]
36
+ node: [14, 16]
37
37
  steps:
38
38
  - uses: actions/setup-node@v1
39
39
  with:
@@ -51,12 +51,12 @@ jobs:
51
51
  run: npm run test:all
52
52
 
53
53
  test_macos:
54
- name: Test on MacOS with Node 12
54
+ name: Test on MacOS with Node 14
55
55
  runs-on: macos-latest
56
56
  steps:
57
57
  - uses: actions/setup-node@v1
58
58
  with:
59
- node-version: 12
59
+ node-version: 14
60
60
  - uses: actions/checkout@v2
61
61
  - uses: actions/cache@v2
62
62
  id: cache
@@ -70,12 +70,12 @@ jobs:
70
70
  run: npm run test:all
71
71
 
72
72
  test_windows:
73
- name: Test on Windows with Node 12
73
+ name: Test on Windows with Node 14
74
74
  runs-on: windows-latest
75
75
  steps:
76
76
  - uses: actions/setup-node@v1
77
77
  with:
78
- node-version: 12
78
+ node-version: 14
79
79
  - uses: actions/checkout@v2
80
80
  - name: Install
81
81
  run: npm install
package/README.md CHANGED
@@ -159,7 +159,7 @@ The solidity versions taken into consideration during formatting are:
159
159
  bytes1 public a;
160
160
  bytes1 public b;
161
161
 
162
- uint public c = 1 ** 2 ** 3;
162
+ uint public c = 1**2**3;
163
163
 
164
164
  // "compiler": "0.7.6" (or lesser)
165
165
  // "explicitTypes": "never"
@@ -258,6 +258,21 @@ code --install-extension JuanBlanco.solidity
258
258
 
259
259
  This extension provides basic integration with Prettier for most cases no further action is needed.
260
260
 
261
+ Make sure your editor has format on save set to true.
262
+ When you save VSCode will ask you what formatter would you like to use for the solidity language, you can choose `JuanBlanco.solidity`.
263
+
264
+ At this point VSCode's `settings.json` should have a configuration similar to this:
265
+
266
+ ```JSON
267
+ {
268
+ "editor.formatOnSave": true,
269
+ "solidity.formatter": "prettier", // This is the default so it might be missing.
270
+ "[solidity]": {
271
+ "editor.defaultFormatter": "JuanBlanco.solidity"
272
+ }
273
+ }
274
+ ```
275
+
261
276
  If you want more control over other details, you should proceed to install [`prettier-vscode`](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode).
262
277
 
263
278
  ```Bash
@@ -270,13 +285,59 @@ To interact with 3rd party plugins, `prettier-vscode` will look in the project's
270
285
  npm install --save-dev prettier prettier-plugin-solidity
271
286
  ```
272
287
 
273
- This will allow you to specify the version of the plugin in case you need to freeze the formatting since new versions of this plugin will implement tweaks on the possible formats.
288
+ This will allow you to specify the version of the plugin in case you want to use the latest version of the plugin or need to freeze the formatting since new versions of this plugin will implement tweaks on the possible formats.
274
289
 
275
290
  You'll have to let VSCode what formatter you prefer.
276
- As a final check, make sure that VSCode is configured to format files on save.
291
+ This can be done by opening the command palette and executing:
292
+
293
+ ```
294
+ >Preferences: Configure Language Specific Settings...
295
+
296
+ # Select Language
297
+ solidity
298
+ ```
299
+
300
+ Now VSCode's `settings.json` should have this:
301
+
302
+ ```JSON
303
+ {
304
+ "editor.formatOnSave": true,
305
+ "[solidity]": {
306
+ "editor.defaultFormatter": "esbenp.prettier-vscode"
307
+ }
308
+ }
309
+ ```
277
310
 
278
311
  Note: By design, Prettier prioritizes a local over a global configuration. If you have a `.prettierrc` file in your porject, your VSCode's default settings or rules in `settings.json` are ignored ([prettier/prettier-vscode#1079](https://github.com/prettier/prettier-vscode/issues/1079)).
279
312
 
313
+ ## Edge cases
314
+
315
+ Prettier Solidity does its best to be pretty and consistent, but in some cases it falls back to doing things that are less than ideal.
316
+
317
+ ### Modifiers in constructors
318
+
319
+ Modifiers with no arguments are formatted with their parentheses removed, except for constructors. The reason for this is that Prettier Solidity cannot always tell apart a modifier from a base constructor. So modifiers in constructors are not modified. For example, this:
320
+
321
+ ```solidity
322
+ contract Foo is Bar {
323
+ constructor() Bar() modifier1 modifier2() modifier3(42) {}
324
+
325
+ function f() modifier1 modifier2() modifier3(42) {}
326
+ }
327
+ ```
328
+
329
+ will be formatted as
330
+
331
+ ```solidity
332
+ contract Foo is Bar {
333
+ constructor() Bar() modifier1 modifier2() modifier3(42) {}
334
+
335
+ function f() modifier1 modifier2 modifier3(42) {}
336
+ }
337
+ ```
338
+
339
+ Notice that the unnecessary parentheses in `modifier2` were removed in the function but not in the constructor.
340
+
280
341
  ## Contributing
281
342
 
282
343
  1. [Fork it](https://github.com/prettier-solidity/prettier-plugin-solidity/fork)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-solidity",
3
- "version": "1.0.0-beta.15",
3
+ "version": "1.0.0-beta.19",
4
4
  "description": "A Prettier Plugin for automatically formatting your Solidity code.",
5
5
  "main": "src",
6
6
  "scripts": {
@@ -50,6 +50,10 @@
50
50
  {
51
51
  "email": "klahott@gmail.com",
52
52
  "name": "Klaus Hott Vidal"
53
+ },
54
+ {
55
+ "email": "soham@zemse.in",
56
+ "name": "Soham Zemse"
53
57
  }
54
58
  ],
55
59
  "license": "MIT",
@@ -61,30 +65,30 @@
61
65
  "node": ">=12"
62
66
  },
63
67
  "devDependencies": {
64
- "@babel/code-frame": "^7.14.5",
68
+ "@babel/code-frame": "^7.16.0",
65
69
  "cross-env": "^7.0.3",
66
70
  "dir-to-object": "^2.0.0",
67
- "eslint": "^7.30.0",
68
- "eslint-config-airbnb-base": "^14.2.1",
71
+ "eslint": "^8.2.0",
72
+ "eslint-config-airbnb-base": "^15.0.0",
69
73
  "eslint-config-prettier": "^8.3.0",
70
- "eslint-plugin-import": "^2.23.4",
71
- "jest": "^27.0.6",
74
+ "eslint-plugin-import": "^2.25.3",
75
+ "jest": "^27.3.1",
72
76
  "jest-mock-now": "^1.3.0",
73
77
  "jest-snapshot-serializer-ansi": "^1.0.0",
74
78
  "jest-snapshot-serializer-raw": "^1.2.0",
75
- "jest-watch-typeahead": "^0.6.4",
79
+ "jest-watch-typeahead": "^1.0.0",
76
80
  "lines-and-columns": "^1.1.6",
77
81
  "outdent": "^0.8.0",
78
- "prettier": "^2.3.2",
79
- "solc": "^0.8.6"
82
+ "prettier": "^2.4.1",
83
+ "solc": "^0.8.10"
80
84
  },
81
85
  "dependencies": {
82
- "@solidity-parser/parser": "^0.13.2",
83
- "emoji-regex": "^9.2.2",
86
+ "@solidity-parser/parser": "^0.14.0",
87
+ "emoji-regex": "^10.0.0",
84
88
  "escape-string-regexp": "^4.0.0",
85
89
  "semver": "^7.3.5",
86
90
  "solidity-comments-extractor": "^0.0.7",
87
- "string-width": "^4.2.2"
91
+ "string-width": "^4.2.3"
88
92
  },
89
93
  "peerDependencies": {
90
94
  "prettier": "^2.3.0"
@@ -1,58 +1,61 @@
1
1
  const {
2
2
  doc: {
3
- builders: { label, line, softline }
3
+ builders: { group, ifBreak, indent, label, line, softline }
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
7
  const printSeparatedList = require('./print-separated-list');
8
8
 
9
- let functionCallId = 0;
10
-
11
- const printObject = (node, path, print, options, groupId) => [
9
+ const printObject = (node, path, print, options) => [
12
10
  '{',
13
11
  printSeparatedList(
14
12
  path
15
13
  .map(print, 'arguments')
16
14
  .map((arg, index) => [node.names[index], ': ', arg]),
17
15
  {
18
- groupId,
19
16
  firstSeparator: options.bracketSpacing ? line : softline,
20
17
  lastSeparator: [options.bracketSpacing ? line : softline, '})']
21
18
  }
22
19
  )
23
20
  ];
24
21
 
25
- const printArguments = (path, print, groupId) =>
22
+ const printArguments = (path, print) =>
26
23
  printSeparatedList(path.map(print, 'arguments'), {
27
- groupId,
28
24
  lastSeparator: [softline, ')']
29
25
  });
30
26
 
27
+ let groupIndex = 0;
31
28
  const FunctionCall = {
32
29
  print: ({ node, path, print, options }) => {
30
+ let expressionDoc = path.call(print, 'expression');
33
31
  let argumentsDoc = ')';
34
- const functionCallLabel = { type: 'FunctionCall', groupId: null };
32
+
35
33
  if (node.arguments && node.arguments.length > 0) {
36
- functionCallLabel.groupId = `FunctionCall-${functionCallId}`;
37
- functionCallId += 1;
38
34
  if (node.names && node.names.length > 0) {
39
- argumentsDoc = printObject(
40
- node,
41
- path,
42
- print,
43
- options,
44
- functionCallLabel.groupId
45
- );
35
+ argumentsDoc = printObject(node, path, print, options);
46
36
  } else {
47
- argumentsDoc = printArguments(path, print, functionCallLabel.groupId);
37
+ argumentsDoc = printArguments(path, print);
48
38
  }
49
39
  }
50
40
 
51
- return label(JSON.stringify(functionCallLabel), [
52
- path.call(print, 'expression'),
53
- '(',
54
- argumentsDoc
55
- ]);
41
+ // If we are at the end of a MemberAccessChain we should indent the
42
+ // arguments accordingly.
43
+ if (expressionDoc.label === 'MemberAccessChain') {
44
+ expressionDoc = group(expressionDoc.contents, {
45
+ id: `FunctionCall.expression-${groupIndex}`
46
+ });
47
+
48
+ groupIndex += 1;
49
+
50
+ argumentsDoc = ifBreak(indent(argumentsDoc), argumentsDoc, {
51
+ groupId: expressionDoc.id
52
+ });
53
+ // We wrap the expression in a label in case there is an IndexAccess or
54
+ // a FunctionCall following this IndexAccess.
55
+ return label('MemberAccessChain', [expressionDoc, '(', argumentsDoc]);
56
+ }
57
+
58
+ return [expressionDoc, '(', argumentsDoc];
56
59
  }
57
60
  };
58
61
 
@@ -1,16 +1,38 @@
1
1
  const {
2
2
  doc: {
3
- builders: { group, indent, softline }
3
+ builders: { group, ifBreak, indent, label, softline }
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
+ let groupIndex = 0;
7
8
  const IndexAccess = {
8
- print: ({ path, print }) => [
9
- path.call(print, 'base'),
10
- '[',
11
- group([indent([softline, path.call(print, 'index')]), softline]),
12
- ']'
13
- ]
9
+ print: ({ path, print }) => {
10
+ let baseDoc = path.call(print, 'base');
11
+ let indexDoc = group([
12
+ indent([softline, path.call(print, 'index')]),
13
+ softline,
14
+ ']'
15
+ ]);
16
+
17
+ // If we are at the end of a MemberAccessChain we should indent the
18
+ // arguments accordingly.
19
+ if (baseDoc.label === 'MemberAccessChain') {
20
+ baseDoc = group(baseDoc.contents, {
21
+ id: `IndexAccess.base-${groupIndex}`
22
+ });
23
+
24
+ groupIndex += 1;
25
+
26
+ indexDoc = ifBreak(indent(indexDoc), indexDoc, {
27
+ groupId: baseDoc.id
28
+ });
29
+ // We wrap the expression in a label in case there is an IndexAccess or
30
+ // a FunctionCall following this IndexAccess.
31
+ return label('MemberAccessChain', [baseDoc, '[', indexDoc]);
32
+ }
33
+
34
+ return [baseDoc, '[', indexDoc];
35
+ }
14
36
  };
15
37
 
16
38
  module.exports = IndexAccess;
@@ -1,6 +1,6 @@
1
1
  const {
2
2
  doc: {
3
- builders: { group, ifBreak, indent, softline }
3
+ builders: { group, indent, label, softline }
4
4
  }
5
5
  } = require('prettier');
6
6
 
@@ -47,43 +47,80 @@ const isEndOfChain = (node, path) => {
47
47
  return true;
48
48
  };
49
49
 
50
- const shallIndent = (path) => {
51
- let i = 0;
52
- let elderNode = path.getParentNode(i);
53
- while (elderNode) {
54
- if (
55
- elderNode.type === 'VariableDeclarationStatement' ||
56
- elderNode.type === 'BinaryOperation'
57
- )
58
- return false;
59
- i += 1;
60
- elderNode = path.getParentNode(i);
61
- }
62
- return true;
50
+ /**
51
+ * processChain expects the doc[] of the full chain of MemberAccess.
52
+ *
53
+ * It uses the separator label to split the chain into 2 arrays.
54
+ * The first array is the doc[] corresponding to the first element before the
55
+ * first separator.
56
+ * The second array contains the rest of the chain.
57
+ *
58
+ * The second array is grouped and indented, while the first element's
59
+ * formatting logic remains separated.
60
+ *
61
+ * That way the first element can safely split into multiple lines and the rest
62
+ * of the chain will continue its formatting rules as normal.
63
+ *
64
+ * i.e.
65
+ * ```
66
+ * functionCall(arg1, arg2).rest.of.chain
67
+ *
68
+ * functionCall(arg1, arg2)
69
+ * .long
70
+ * .rest
71
+ * .of
72
+ * .chain
73
+ *
74
+ * functionCall(
75
+ * arg1,
76
+ * arg2
77
+ * ).rest.of.chain
78
+ *
79
+ * functionCall(
80
+ * arg1,
81
+ * arg2
82
+ * )
83
+ * .long
84
+ * .rest
85
+ * .of
86
+ * .chain
87
+ * ```
88
+ *
89
+ * NOTE: As described in the examples above, the rest of the chain will be grouped
90
+ * and try to stay in the same line as the end of the first element.
91
+ *
92
+ * @param {doc[]} chain is the full chain of MemberAccess
93
+ * @returns a processed doc[] with the proper grouping and indentation ready to
94
+ * be printed.
95
+ */
96
+ const processChain = (chain) => {
97
+ const firstSeparatorIndex = chain.findIndex(
98
+ (element) => element.label === 'separator'
99
+ );
100
+ // The doc[] before the first separator
101
+ const firstExpression = chain.slice(0, firstSeparatorIndex);
102
+ // The doc[] containing the rest of the chain
103
+ const restOfChain = group(indent(chain.slice(firstSeparatorIndex)));
104
+
105
+ // We wrap the expression in a label in case there is an IndexAccess or
106
+ // a FunctionCall following this MemberAccess.
107
+ return label('MemberAccessChain', group([firstExpression, restOfChain]));
63
108
  };
64
109
 
65
110
  const MemberAccess = {
66
111
  print: ({ node, path, print }) => {
67
112
  let expressionDoc = path.call(print, 'expression');
68
- let separator = [softline, '.'];
69
- let labelData;
70
- if (expressionDoc.label) {
71
- labelData = JSON.parse(expressionDoc.label);
72
- expressionDoc = expressionDoc.contents.flat();
73
- }
74
- if (labelData && labelData.groupId) {
75
- separator = ifBreak('.', [softline, '.'], {
76
- groupId: labelData.groupId
77
- });
113
+ if (Array.isArray(expressionDoc)) {
114
+ expressionDoc = expressionDoc.flat();
78
115
  }
79
116
 
80
117
  const doc = [
81
118
  expressionDoc,
82
- shallIndent(path) ? indent(separator) : separator,
119
+ label('separator', [softline, '.']),
83
120
  node.memberName
84
121
  ].flat();
85
122
 
86
- return isEndOfChain(node, path) ? group(doc) : doc;
123
+ return isEndOfChain(node, path) ? processChain(doc) : doc;
87
124
  }
88
125
  };
89
126
 
@@ -7,25 +7,23 @@ const {
7
7
  const printSeparatedList = require('./print-separated-list');
8
8
 
9
9
  const modifierParameters = (node, path, print) => {
10
- if (node.parameters) {
11
- return node.parameters.length > 0
12
- ? [
13
- '(',
14
- printSeparatedList(path.map(print, 'parameters'), {
15
- separator: [
16
- ',',
17
- // To keep consistency any list of parameters will split if it's longer than 2.
18
- // For more information see:
19
- // https://github.com/prettier-solidity/prettier-plugin-solidity/issues/256
20
- node.parameters.length > 2 ? hardline : line
21
- ]
22
- }),
23
- ')'
10
+ if (node.parameters && node.parameters.length > 0) {
11
+ return [
12
+ '(',
13
+ printSeparatedList(path.map(print, 'parameters'), {
14
+ separator: [
15
+ ',',
16
+ // To keep consistency any list of parameters will split if it's longer than 2.
17
+ // For more information see:
18
+ // https://github.com/prettier-solidity/prettier-plugin-solidity/issues/256
19
+ node.parameters.length > 2 ? hardline : line
24
20
  ]
25
- : '()';
21
+ }),
22
+ ')'
23
+ ];
26
24
  }
27
25
 
28
- return '';
26
+ return '()';
29
27
  };
30
28
 
31
29
  const virtual = (node) => (node.isVirtual ? [line, 'virtual'] : '');
@@ -2,6 +2,8 @@ const printSeparatedList = require('./print-separated-list');
2
2
 
3
3
  const modifierArguments = (node, path, print) => {
4
4
  if (node.arguments) {
5
+ // We always print parentheses at this stage because the parser already
6
+ // stripped them in FunctionDefinitions that are not a constructor.
5
7
  return node.arguments.length > 0
6
8
  ? ['(', printSeparatedList(path.map(print, 'arguments')), ')']
7
9
  : '()';
@@ -1,6 +1,6 @@
1
1
  const {
2
2
  doc: {
3
- builders: { join, line }
3
+ builders: { join, hardline }
4
4
  }
5
5
  } = require('prettier');
6
6
  const { printString } = require('../prettier-comments/common/util');
@@ -14,7 +14,7 @@ const StringLiteral = {
14
14
  (node.isUnicode[index] ? 'unicode' : '') + printString(part, options)
15
15
  );
16
16
 
17
- return join(line, list);
17
+ return join(hardline, list);
18
18
  }
19
19
  };
20
20
 
@@ -0,0 +1,5 @@
1
+ const TypeDefinition = {
2
+ print: ({ node }) => ['type ', node.name, ' is ', node.definition.name, ';']
3
+ };
4
+
5
+ module.exports = TypeDefinition;
@@ -1,28 +1,41 @@
1
1
  const {
2
2
  doc: {
3
- builders: { group }
3
+ builders: { group, ifBreak, indent }
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
7
  const printSeparatedList = require('./print-separated-list');
8
8
 
9
- const embraceVariables = (doc, embrace) => (embrace ? ['(', doc, ')'] : doc);
9
+ const embraceVariables = (doc, embrace) =>
10
+ embrace ? ['(', printSeparatedList(doc), ')'] : doc;
10
11
 
11
12
  const initialValue = (node, path, print) =>
12
13
  node.initialValue ? [' = ', path.call(print, 'initialValue')] : '';
13
14
 
15
+ let groupIndex = 0;
14
16
  const VariableDeclarationStatement = {
15
17
  print: ({ node, path, print }) => {
16
18
  const startsWithVar =
17
19
  node.variables.filter((x) => x && x.typeName).length === 0;
18
20
 
21
+ const declarationDoc = group(
22
+ [
23
+ startsWithVar ? 'var ' : '',
24
+ embraceVariables(
25
+ path.map(print, 'variables'),
26
+ node.variables.length > 1 || startsWithVar
27
+ )
28
+ ],
29
+ { id: `VariableDeclarationStatement.variables-${groupIndex}` }
30
+ );
31
+ groupIndex += 1;
32
+ const initialValueDoc = initialValue(node, path, print);
33
+
19
34
  return group([
20
- startsWithVar ? 'var ' : '',
21
- embraceVariables(
22
- printSeparatedList(path.map(print, 'variables')),
23
- node.variables.length > 1 || startsWithVar
24
- ),
25
- initialValue(node, path, print),
35
+ declarationDoc,
36
+ ifBreak(indent(initialValueDoc), initialValueDoc, {
37
+ groupId: declarationDoc.id
38
+ }),
26
39
  node.omitSemicolon ? '' : ';'
27
40
  ]);
28
41
  }
@@ -65,6 +65,7 @@ module.exports = {
65
65
  ThrowStatement: require('./ThrowStatement'),
66
66
  TryStatement: require('./TryStatement'),
67
67
  TupleExpression: require('./TupleExpression'),
68
+ TypeDefinition: require('./TypeDefinition'),
68
69
  TypeNameExpression: require('./TypeNameExpression'),
69
70
  UnaryOperation: require('./UnaryOperation'),
70
71
  UncheckedStatement: require('./UncheckedStatement'),
@@ -14,14 +14,9 @@ const {
14
14
  const printSeparatedList = (
15
15
  list,
16
16
  {
17
- groupId,
18
17
  firstSeparator = softline,
19
18
  separator = [',', line],
20
19
  lastSeparator = firstSeparator
21
20
  } = {}
22
- ) =>
23
- group(
24
- [indent([firstSeparator, join(separator, list)]), lastSeparator],
25
- groupId ? { id: groupId } : {}
26
- );
21
+ ) => group([indent([firstSeparator, join(separator, list)]), lastSeparator]);
27
22
  module.exports = printSeparatedList;
package/src/parser.js CHANGED
@@ -20,15 +20,6 @@ function parse(text, _parsers, options) {
20
20
 
21
21
  parser.visit(parsed, {
22
22
  PragmaDirective(ctx) {
23
- // @TODO: remove hack once the retrieval of @solidity-parser/parser is fixed
24
- ctx.value = ctx.value
25
- .replace(/([<>=])/g, ' $1')
26
- .replace(/< =/g, '<=')
27
- .replace(/> =/g, '>=')
28
- .trim();
29
- if (ctx.value.split(' ').length > 1) {
30
- ctx.value = semver.validRange(ctx.value);
31
- }
32
23
  // if the pragma is not for solidity we leave.
33
24
  if (ctx.name !== 'solidity') return;
34
25
  // if the compiler option has not been provided we leave.
@@ -43,6 +34,21 @@ function parse(text, _parsers, options) {
43
34
  );
44
35
  }
45
36
  },
37
+ ModifierDefinition(ctx) {
38
+ if (!ctx.parameters) {
39
+ ctx.parameters = [];
40
+ }
41
+ },
42
+ FunctionDefinition(ctx) {
43
+ if (!ctx.isConstructor) {
44
+ ctx.modifiers.forEach((modifier) => {
45
+ if (modifier.arguments && modifier.arguments.length === 0) {
46
+ // eslint-disable-next-line no-param-reassign
47
+ modifier.arguments = null;
48
+ }
49
+ });
50
+ }
51
+ },
46
52
  ForStatement(ctx) {
47
53
  if (ctx.initExpression) {
48
54
  ctx.initExpression.omitSemicolon = true;
@@ -61,6 +61,7 @@ const testsWithAstChanges = new Map(
61
61
  "Parentheses/BitXorNoParentheses.sol",
62
62
  "Parentheses/LogicNoParentheses.sol",
63
63
  "HexLiteral/HexLiteral.sol",
64
+ "ModifierInvocations/ModifierInvocations.sol",
64
65
  ].map((fixture) => {
65
66
  const [file, compareBytecode = () => true] = Array.isArray(fixture)
66
67
  ? fixture
@@ -750,7 +750,7 @@ contract DualIndex {
750
750
  mapping(uint256 => mapping(uint256 => uint256)) data;
751
751
  address public admin;
752
752
 
753
- modifier restricted {
753
+ modifier restricted() {
754
754
  if (msg.sender == admin) _;
755
755
  }
756
756
 
@@ -1,5 +1,5 @@
1
1
  // SPDX-License-Identifier: MIT
2
- pragma solidity 0.8.6;
2
+ pragma solidity 0.8.10;
3
3
 
4
4
  contract VariableTypesMixed {
5
5
  uint256 public a;