prettier-plugin-solidity 1.1.1 → 1.1.2

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 (40) hide show
  1. package/dist/standalone.js +1 -1
  2. package/dist/standalone.js.map +1 -1
  3. package/package.json +4 -4
  4. package/src/common/printer-helpers.js +116 -0
  5. package/src/nodes/AssemblyBlock.js +5 -3
  6. package/src/nodes/AssemblyCall.js +1 -1
  7. package/src/nodes/AssemblyFunctionDefinition.js +4 -2
  8. package/src/nodes/AssemblyLocalDefinition.js +1 -1
  9. package/src/nodes/Block.js +4 -2
  10. package/src/nodes/CatchClause.js +1 -1
  11. package/src/nodes/ContractDefinition.js +6 -4
  12. package/src/nodes/CustomErrorDefinition.js +1 -1
  13. package/src/nodes/DoWhileStatement.js +1 -1
  14. package/src/nodes/EnumDefinition.js +1 -1
  15. package/src/nodes/EventDefinition.js +1 -1
  16. package/src/nodes/ExpressionStatement.js +1 -1
  17. package/src/nodes/ForStatement.js +1 -1
  18. package/src/nodes/FunctionCall.js +1 -1
  19. package/src/nodes/FunctionDefinition.js +6 -6
  20. package/src/nodes/FunctionTypeName.js +1 -1
  21. package/src/nodes/IfStatement.js +4 -2
  22. package/src/nodes/ImportDirective.js +1 -1
  23. package/src/nodes/InheritanceSpecifier.js +1 -1
  24. package/src/nodes/InlineAssemblyStatement.js +1 -1
  25. package/src/nodes/Mapping.js +15 -7
  26. package/src/nodes/ModifierDefinition.js +1 -1
  27. package/src/nodes/ModifierInvocation.js +4 -2
  28. package/src/nodes/NameValueList.js +1 -1
  29. package/src/nodes/SourceUnit.js +1 -1
  30. package/src/nodes/StructDefinition.js +1 -1
  31. package/src/nodes/TryStatement.js +4 -2
  32. package/src/nodes/TupleExpression.js +1 -1
  33. package/src/nodes/UsingForDeclaration.js +1 -1
  34. package/src/nodes/VariableDeclaration.js +1 -1
  35. package/src/nodes/VariableDeclarationStatement.js +1 -1
  36. package/src/nodes/WhileStatement.js +1 -1
  37. package/src/nodes/print-comments.js +0 -37
  38. package/src/nodes/print-preserving-empty-lines.js +0 -53
  39. package/src/nodes/print-separated-item.js +0 -21
  40. package/src/nodes/print-separated-list.js +0 -23
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-solidity",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "A Prettier Plugin for automatically formatting your Solidity code.",
5
5
  "main": "./src/index.js",
6
6
  "browser": "./dist/standalone.js",
@@ -83,7 +83,7 @@
83
83
  "c8": "^7.12.0",
84
84
  "cross-env": "^7.0.3",
85
85
  "dir-to-object": "^2.0.0",
86
- "eslint": "^8.30.0",
86
+ "eslint": "^8.31.0",
87
87
  "eslint-config-airbnb-base": "^15.0.0",
88
88
  "eslint-config-prettier": "^8.5.0",
89
89
  "eslint-plugin-import": "^2.26.0",
@@ -95,14 +95,14 @@
95
95
  "jest-snapshot-serializer-raw": "^1.2.0",
96
96
  "jest-watch-typeahead": "^2.2.1",
97
97
  "lines-and-columns": "^2.0.3",
98
- "prettier": "^3.0.0-alpha.4",
98
+ "prettier": "^2.8.1",
99
99
  "proxyquire": "^2.1.3",
100
100
  "solc": "^0.8.17",
101
101
  "webpack": "^5.75.0",
102
102
  "webpack-cli": "^5.0.1"
103
103
  },
104
104
  "dependencies": {
105
- "@solidity-parser/parser": "^0.14.5",
105
+ "@solidity-parser/parser": "^0.15.0",
106
106
  "semver": "^7.3.8",
107
107
  "solidity-comments-extractor": "^0.0.7"
108
108
  },
@@ -0,0 +1,116 @@
1
+ const {
2
+ doc: {
3
+ builders: { group, indent, join, line, softline, hardline }
4
+ },
5
+ util: { isNextLineEmptyAfterIndex }
6
+ } = require('prettier');
7
+ const { prettierVersionSatisfies } = require('./util');
8
+
9
+ const printComments = (node, path, options, filter = () => true) => {
10
+ if (!node.comments) return '';
11
+ const doc = join(
12
+ line,
13
+ path
14
+ .map((commentPath) => {
15
+ const comment = commentPath.getValue();
16
+ if (comment.trailing || comment.leading || comment.printed) {
17
+ return null;
18
+ }
19
+ if (!filter(comment)) {
20
+ return null;
21
+ }
22
+ comment.printed = true;
23
+ return options.printer.printComment(commentPath);
24
+ }, 'comments')
25
+ .filter(Boolean)
26
+ );
27
+
28
+ // The following if statement will never be 100% covered in a single run
29
+ // since it depends on the version of Prettier being used.
30
+ // Mocking the behaviour will introduce a lot of maintenance in the tests.
31
+ /* c8 ignore start */
32
+ return prettierVersionSatisfies('^2.3.0')
33
+ ? doc.parts // Prettier V2
34
+ : doc; // Prettier V3
35
+ /* c8 ignore stop */
36
+ };
37
+
38
+ function printPreservingEmptyLines(path, key, options, print) {
39
+ const parts = [];
40
+ path.each((childPath, index) => {
41
+ const node = childPath.getValue();
42
+ const nodeType = node.type;
43
+
44
+ if (
45
+ // Avoid adding a hardline at the beginning of the document.
46
+ parts.length !== 0 &&
47
+ // LabelDefinition adds a dedented line so we don't have to prepend a
48
+ // hardline.
49
+ nodeType !== 'LabelDefinition'
50
+ ) {
51
+ parts.push(hardline);
52
+ }
53
+
54
+ if (index > 0) {
55
+ if (
56
+ ['ContractDefinition', 'FunctionDefinition'].includes(nodeType) &&
57
+ parts[parts.length - 2] !== hardline
58
+ ) {
59
+ parts.push(hardline);
60
+ }
61
+ }
62
+
63
+ parts.push(print(childPath));
64
+
65
+ if (
66
+ isNextLineEmptyAfterIndex(
67
+ options.originalText,
68
+ options.locEnd(node) + 1
69
+ ) ||
70
+ nodeType === 'FunctionDefinition'
71
+ ) {
72
+ parts.push(hardline);
73
+ }
74
+ }, key);
75
+
76
+ if (parts.length > 1 && parts[parts.length - 1] === hardline) {
77
+ parts.pop();
78
+ }
79
+
80
+ return parts;
81
+ }
82
+
83
+ // This function will add an indentation to the `item` and separate it from the
84
+ // rest of the `doc` in most cases by a `softline`.
85
+ const printSeparatedItem = (
86
+ item,
87
+ {
88
+ firstSeparator = softline,
89
+ lastSeparator = firstSeparator,
90
+ grouped = true
91
+ } = {}
92
+ ) => {
93
+ const doc = [indent([firstSeparator, item]), lastSeparator];
94
+ return grouped ? group(doc) : doc;
95
+ };
96
+
97
+ // This function will add an indentation to the `list` and separate it from the
98
+ // rest of the `doc` in most cases by a `softline`.
99
+ // the list itself will be printed with a separator that in most cases is a
100
+ // comma (,) and a `line`
101
+ const printSeparatedList = (
102
+ list,
103
+ { firstSeparator, separator = [',', line], lastSeparator, grouped } = {}
104
+ ) =>
105
+ printSeparatedItem(join(separator, list), {
106
+ firstSeparator,
107
+ lastSeparator,
108
+ grouped
109
+ });
110
+
111
+ module.exports = {
112
+ printComments,
113
+ printPreservingEmptyLines,
114
+ printSeparatedList,
115
+ printSeparatedItem
116
+ };
@@ -4,9 +4,11 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedItem = require('./print-separated-item');
8
- const printComments = require('./print-comments');
9
- const printPreservingEmptyLines = require('./print-preserving-empty-lines');
7
+ const {
8
+ printComments,
9
+ printPreservingEmptyLines,
10
+ printSeparatedItem
11
+ } = require('../common/printer-helpers');
10
12
 
11
13
  const AssemblyBlock = {
12
14
  print: ({ node, options, path, print }) => [
@@ -1,4 +1,4 @@
1
- const printSeparatedList = require('./print-separated-list');
1
+ const { printSeparatedList } = require('../common/printer-helpers');
2
2
 
3
3
  const AssemblyCall = {
4
4
  print: ({ node, path, print, options }) =>
@@ -4,8 +4,10 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedItem = require('./print-separated-item');
8
- const printSeparatedList = require('./print-separated-list');
7
+ const {
8
+ printSeparatedItem,
9
+ printSeparatedList
10
+ } = require('../common/printer-helpers');
9
11
 
10
12
  const AssemblyFunctionDefinition = {
11
13
  print: ({ node, path, print }) => [
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedList = require('./print-separated-list');
7
+ const { printSeparatedList } = require('../common/printer-helpers');
8
8
 
9
9
  const AssemblyLocalDefinition = {
10
10
  print: ({ node, path, print }) => {
@@ -4,8 +4,10 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printPreservingEmptyLines = require('./print-preserving-empty-lines');
8
- const printComments = require('./print-comments');
7
+ const {
8
+ printComments,
9
+ printPreservingEmptyLines
10
+ } = require('../common/printer-helpers');
9
11
 
10
12
  const Block = {
11
13
  print: ({ node, options, path, print }) =>
@@ -1,4 +1,4 @@
1
- const printSeparatedList = require('./print-separated-list');
1
+ const { printSeparatedList } = require('../common/printer-helpers');
2
2
 
3
3
  const parameters = (node, path, print) =>
4
4
  node.parameters
@@ -4,10 +4,12 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedItem = require('./print-separated-item');
8
- const printSeparatedList = require('./print-separated-list');
9
- const printPreservingEmptyLines = require('./print-preserving-empty-lines');
10
- const printComments = require('./print-comments');
7
+ const {
8
+ printComments,
9
+ printPreservingEmptyLines,
10
+ printSeparatedItem,
11
+ printSeparatedList
12
+ } = require('../common/printer-helpers');
11
13
 
12
14
  const inheritance = (node, path, print) =>
13
15
  node.baseContracts.length > 0
@@ -1,4 +1,4 @@
1
- const printSeparatedList = require('./print-separated-list');
1
+ const { printSeparatedList } = require('../common/printer-helpers');
2
2
 
3
3
  const parameters = (node, path, print) =>
4
4
  node.parameters && node.parameters.length > 0
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedItem = require('./print-separated-item');
7
+ const { printSeparatedItem } = require('../common/printer-helpers');
8
8
 
9
9
  const printBody = (node, path, print) =>
10
10
  node.body.type === 'Block'
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedList = require('./print-separated-list');
7
+ const { printSeparatedList } = require('../common/printer-helpers');
8
8
 
9
9
  const EnumDefinition = {
10
10
  print: ({ node, path, print }) =>
@@ -1,4 +1,4 @@
1
- const printSeparatedList = require('./print-separated-list');
1
+ const { printSeparatedList } = require('../common/printer-helpers');
2
2
 
3
3
  const parameters = (node, path, print) =>
4
4
  node.parameters && node.parameters.length > 0
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printComments = require('./print-comments');
7
+ const { printComments } = require('../common/printer-helpers');
8
8
 
9
9
  const ExpressionStatement = {
10
10
  print: ({ node, options, path, print }) => {
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedList = require('./print-separated-list');
7
+ const { printSeparatedList } = require('../common/printer-helpers');
8
8
 
9
9
  const initExpression = (node, path, print) =>
10
10
  node.initExpression ? path.call(print, 'initExpression') : '';
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedList = require('./print-separated-list');
7
+ const { printSeparatedList } = require('../common/printer-helpers');
8
8
 
9
9
  const printObject = (path, print, options) => {
10
10
  const identifiers = path.map(print, 'identifiers');
@@ -5,9 +5,11 @@ const {
5
5
  util: { getNextNonSpaceNonCommentCharacterIndex }
6
6
  } = require('prettier');
7
7
 
8
- const printSeparatedList = require('./print-separated-list');
9
- const printSeparatedItem = require('./print-separated-item');
10
- const printComments = require('./print-comments');
8
+ const {
9
+ printComments,
10
+ printSeparatedItem,
11
+ printSeparatedList
12
+ } = require('../common/printer-helpers');
11
13
 
12
14
  const functionName = (node, options) => {
13
15
  if (node.isConstructor && !node.name) return 'constructor';
@@ -73,9 +75,7 @@ const override = (node, path, print) => {
73
75
  };
74
76
 
75
77
  const stateMutability = (node) =>
76
- node.stateMutability && node.stateMutability !== 'default'
77
- ? [line, node.stateMutability]
78
- : '';
78
+ node.stateMutability ? [line, node.stateMutability] : '';
79
79
 
80
80
  const modifiers = (node, path, print) =>
81
81
  node.modifiers.length > 0
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedList = require('./print-separated-list');
7
+ const { printSeparatedList } = require('../common/printer-helpers');
8
8
 
9
9
  const returnTypes = (node, path, print) =>
10
10
  node.returnTypes.length > 0
@@ -4,8 +4,10 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printComments = require('./print-comments');
8
- const printSeparatedItem = require('./print-separated-item');
7
+ const {
8
+ printComments,
9
+ printSeparatedItem
10
+ } = require('../common/printer-helpers');
9
11
 
10
12
  const printTrueBody = (node, path, print) => {
11
13
  if (node.trueBody.type === 'Block') {
@@ -6,7 +6,7 @@ const {
6
6
  const coerce = require('semver/functions/coerce');
7
7
  const satisfies = require('semver/functions/satisfies');
8
8
 
9
- const printSeparatedList = require('./print-separated-list');
9
+ const { printSeparatedList } = require('../common/printer-helpers');
10
10
  const { printString } = require('../common/util');
11
11
 
12
12
  const ImportDirective = {
@@ -1,4 +1,4 @@
1
- const printSeparatedList = require('./print-separated-list');
1
+ const { printSeparatedList } = require('../common/printer-helpers');
2
2
 
3
3
  const printArguments = (node, path, print) =>
4
4
  node.arguments && node.arguments.length
@@ -1,6 +1,6 @@
1
1
  // @TODO: add support for assembly language specifier
2
2
  const { printString } = require('../common/util');
3
- const printSeparatedList = require('./print-separated-list');
3
+ const { printSeparatedList } = require('../common/printer-helpers');
4
4
 
5
5
  const InlineAssemblyStatement = {
6
6
  print: ({ node, path, print, options }) => [
@@ -1,11 +1,19 @@
1
1
  const Mapping = {
2
- print: ({ path, print }) => [
3
- 'mapping(',
4
- path.call(print, 'keyType'),
5
- ' => ',
6
- path.call(print, 'valueType'),
7
- ')'
8
- ]
2
+ print: ({ path, print }) => {
3
+ const keyType = path.call(print, 'keyType');
4
+ const keyName = path.call(print, 'keyName');
5
+ const valueType = path.call(print, 'valueType');
6
+ const valueName = path.call(print, 'valueName');
7
+ return [
8
+ 'mapping(',
9
+ keyType,
10
+ keyName ? ` ${keyName}` : '',
11
+ ' => ',
12
+ valueType,
13
+ valueName ? ` ${valueName}` : '',
14
+ ')'
15
+ ];
16
+ }
9
17
  };
10
18
 
11
19
  module.exports = Mapping;
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedList = require('./print-separated-list');
7
+ const { printSeparatedList } = require('../common/printer-helpers');
8
8
 
9
9
  const modifierParameters = (node, path, print) => {
10
10
  if (node.parameters && node.parameters.length > 0) {
@@ -1,5 +1,7 @@
1
- const printSeparatedList = require('./print-separated-list');
2
- const printComments = require('./print-comments');
1
+ const {
2
+ printComments,
3
+ printSeparatedList
4
+ } = require('../common/printer-helpers');
3
5
 
4
6
  const modifierArguments = (node, path, print, options) => {
5
7
  if (node.arguments) {
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedList = require('./print-separated-list');
7
+ const { printSeparatedList } = require('../common/printer-helpers');
8
8
 
9
9
  const NameValueList = {
10
10
  print: ({ node, path, print, options }) =>
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printPreservingEmptyLines = require('./print-preserving-empty-lines');
7
+ const { printPreservingEmptyLines } = require('../common/printer-helpers');
8
8
 
9
9
  const SourceUnit = {
10
10
  print: ({ options, path, print }) => [
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedList = require('./print-separated-list');
7
+ const { printSeparatedList } = require('../common/printer-helpers');
8
8
 
9
9
  const StructDefinition = {
10
10
  print: ({ node, path, print }) => [
@@ -4,8 +4,10 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedItem = require('./print-separated-item');
8
- const printSeparatedList = require('./print-separated-list');
7
+ const {
8
+ printSeparatedItem,
9
+ printSeparatedList
10
+ } = require('../common/printer-helpers');
9
11
 
10
12
  const returnParameters = (node, path, print) =>
11
13
  node.returnParameters
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedList = require('./print-separated-list');
7
+ const { printSeparatedList } = require('../common/printer-helpers');
8
8
 
9
9
  const contents = (node, path, print) =>
10
10
  node.components &&
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedList = require('./print-separated-list');
7
+ const { printSeparatedList } = require('../common/printer-helpers');
8
8
 
9
9
  const UsingForDeclaration = {
10
10
  print: ({ node, path, print, options }) => [
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedList = require('./print-separated-list');
7
+ const { printSeparatedList } = require('../common/printer-helpers');
8
8
 
9
9
  const indexed = (node) => (node.isIndexed ? ' indexed' : '');
10
10
 
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedList = require('./print-separated-list');
7
+ const { printSeparatedList } = require('../common/printer-helpers');
8
8
 
9
9
  const embraceVariables = (doc, embrace) =>
10
10
  embrace ? ['(', printSeparatedList(doc), ')'] : doc;
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const printSeparatedItem = require('./print-separated-item');
7
+ const { printSeparatedItem } = require('../common/printer-helpers');
8
8
 
9
9
  const printBody = (node, path, print) =>
10
10
  node.body.type === 'Block'
@@ -1,37 +0,0 @@
1
- const {
2
- doc: {
3
- builders: { join, line }
4
- }
5
- } = require('prettier');
6
- const { prettierVersionSatisfies } = require('../common/util');
7
-
8
- const printComments = (node, path, options, filter = () => true) => {
9
- if (!node.comments) return '';
10
- const doc = join(
11
- line,
12
- path
13
- .map((commentPath) => {
14
- const comment = commentPath.getValue();
15
- if (comment.trailing || comment.leading || comment.printed) {
16
- return null;
17
- }
18
- if (!filter(comment)) {
19
- return null;
20
- }
21
- comment.printed = true;
22
- return options.printer.printComment(commentPath);
23
- }, 'comments')
24
- .filter(Boolean)
25
- );
26
-
27
- // The following if statement will never be 100% covered in a single run
28
- // since it depends on the version of Prettier being used.
29
- // Mocking the behaviour will introduce a lot of maintenance in the tests.
30
- /* c8 ignore start */
31
- return prettierVersionSatisfies('^2.3.0')
32
- ? doc.parts // Prettier V2
33
- : doc; // Prettier V3
34
- /* c8 ignore stop */
35
- };
36
-
37
- module.exports = printComments;
@@ -1,53 +0,0 @@
1
- const {
2
- doc: {
3
- builders: { hardline }
4
- },
5
- util: { isNextLineEmptyAfterIndex }
6
- } = require('prettier');
7
-
8
- function printPreservingEmptyLines(path, key, options, print) {
9
- const parts = [];
10
- path.each((childPath) => {
11
- const node = childPath.getValue();
12
- const nodeType = node.type;
13
-
14
- if (
15
- // Avoid adding a hardline at the beginning of the document.
16
- parts.length !== 0 &&
17
- // LabelDefinition adds a dedented line so we don't have to prepend a
18
- // hardline.
19
- nodeType !== 'LabelDefinition'
20
- ) {
21
- parts.push(hardline);
22
- }
23
-
24
- if (childPath.getName() > 0) {
25
- if (
26
- ['ContractDefinition', 'FunctionDefinition'].includes(nodeType) &&
27
- parts[parts.length - 2] !== hardline
28
- ) {
29
- parts.push(hardline);
30
- }
31
- }
32
-
33
- parts.push(print(childPath));
34
-
35
- if (
36
- isNextLineEmptyAfterIndex(
37
- options.originalText,
38
- options.locEnd(node) + 1
39
- ) ||
40
- nodeType === 'FunctionDefinition'
41
- ) {
42
- parts.push(hardline);
43
- }
44
- }, key);
45
-
46
- if (parts.length > 1 && parts[parts.length - 1] === hardline) {
47
- parts.pop();
48
- }
49
-
50
- return parts;
51
- }
52
-
53
- module.exports = printPreservingEmptyLines;
@@ -1,21 +0,0 @@
1
- const {
2
- doc: {
3
- builders: { group, indent, softline }
4
- }
5
- } = require('prettier');
6
-
7
- // This function will add an indentation to the `item` and separate it from the
8
- // rest of the `doc` in most cases by a `softline`.
9
- const printSeparatedItem = (
10
- item,
11
- {
12
- firstSeparator = softline,
13
- lastSeparator = firstSeparator,
14
- grouped = true
15
- } = {}
16
- ) => {
17
- const doc = [indent([firstSeparator, item]), lastSeparator];
18
- return grouped ? group(doc) : doc;
19
- };
20
-
21
- module.exports = printSeparatedItem;