prettier-plugin-solidity 1.0.0 → 1.1.1

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 (144) hide show
  1. package/README.md +45 -11
  2. package/dist/standalone.js +3 -0
  3. package/dist/standalone.js.LICENSE.txt +15 -0
  4. package/dist/standalone.js.map +1 -0
  5. package/package.json +38 -12
  6. package/src/clean.js +4 -6
  7. package/src/common/util.js +64 -0
  8. package/src/nodes/BinaryOperation.js +10 -4
  9. package/src/nodes/ExpressionStatement.js +1 -1
  10. package/src/nodes/FunctionDefinition.js +1 -1
  11. package/src/nodes/HexLiteral.js +1 -1
  12. package/src/nodes/ImportDirective.js +5 -4
  13. package/src/nodes/InlineAssemblyStatement.js +1 -1
  14. package/src/nodes/SourceUnit.js +1 -1
  15. package/src/nodes/StringLiteral.js +1 -1
  16. package/src/nodes/print-comments.js +29 -19
  17. package/src/parser.js +7 -6
  18. package/src/prettier-comments/language-js/comments.js +12 -10
  19. package/src/printer.js +5 -4
  20. package/.c8rc +0 -11
  21. package/.eslintignore +0 -5
  22. package/.eslintrc +0 -9
  23. package/.nvmrc +0 -1
  24. package/.prettierignore +0 -4
  25. package/.prettierrc +0 -4
  26. package/CODE_OF_CONDUCT.md +0 -76
  27. package/HOW_TO_PUBLISH.md +0 -39
  28. package/STYLEGUIDE.md +0 -382
  29. package/coverage/clover.xml +0 -1171
  30. package/coverage/coverage-final.json +0 -99
  31. package/coverage/lcov-report/base.css +0 -224
  32. package/coverage/lcov-report/block-navigation.js +0 -87
  33. package/coverage/lcov-report/favicon.png +0 -0
  34. package/coverage/lcov-report/index.html +0 -176
  35. package/coverage/lcov-report/prettify.css +0 -1
  36. package/coverage/lcov-report/prettify.js +0 -2
  37. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  38. package/coverage/lcov-report/sorter.js +0 -196
  39. package/coverage/lcov-report/src/binary-operator-printers/arithmetic.js.html +0 -244
  40. package/coverage/lcov-report/src/binary-operator-printers/assignment.js.html +0 -175
  41. package/coverage/lcov-report/src/binary-operator-printers/bit.js.html +0 -103
  42. package/coverage/lcov-report/src/binary-operator-printers/comparison.js.html +0 -199
  43. package/coverage/lcov-report/src/binary-operator-printers/exponentiation.js.html +0 -148
  44. package/coverage/lcov-report/src/binary-operator-printers/index.html +0 -221
  45. package/coverage/lcov-report/src/binary-operator-printers/index.js.html +0 -124
  46. package/coverage/lcov-report/src/binary-operator-printers/logical.js.html +0 -208
  47. package/coverage/lcov-report/src/binary-operator-printers/shift.js.html +0 -103
  48. package/coverage/lcov-report/src/clean.js.html +0 -109
  49. package/coverage/lcov-report/src/comments/handler.js.html +0 -373
  50. package/coverage/lcov-report/src/comments/handlers/handleContractDefinitionComments.js.html +0 -265
  51. package/coverage/lcov-report/src/comments/handlers/handleModifierInvocationComments.js.html +0 -292
  52. package/coverage/lcov-report/src/comments/handlers/index.html +0 -146
  53. package/coverage/lcov-report/src/comments/handlers/index.js.html +0 -103
  54. package/coverage/lcov-report/src/comments/ignore.js.html +0 -184
  55. package/coverage/lcov-report/src/comments/index.html +0 -161
  56. package/coverage/lcov-report/src/comments/index.js.html +0 -97
  57. package/coverage/lcov-report/src/comments/printer.js.html +0 -277
  58. package/coverage/lcov-report/src/index.html +0 -191
  59. package/coverage/lcov-report/src/index.js.html +0 -268
  60. package/coverage/lcov-report/src/loc.js.html +0 -133
  61. package/coverage/lcov-report/src/nodes/ArrayTypeName.js.html +0 -115
  62. package/coverage/lcov-report/src/nodes/AssemblyAssignment.js.html +0 -130
  63. package/coverage/lcov-report/src/nodes/AssemblyBlock.js.html +0 -160
  64. package/coverage/lcov-report/src/nodes/AssemblyCall.js.html +0 -133
  65. package/coverage/lcov-report/src/nodes/AssemblyCase.js.html +0 -112
  66. package/coverage/lcov-report/src/nodes/AssemblyFor.js.html +0 -139
  67. package/coverage/lcov-report/src/nodes/AssemblyFunctionDefinition.js.html +0 -184
  68. package/coverage/lcov-report/src/nodes/AssemblyIf.js.html +0 -115
  69. package/coverage/lcov-report/src/nodes/AssemblyLocalDefinition.js.html +0 -160
  70. package/coverage/lcov-report/src/nodes/AssemblyMemberAccess.js.html +0 -112
  71. package/coverage/lcov-report/src/nodes/AssemblyStackAssignment.js.html +0 -112
  72. package/coverage/lcov-report/src/nodes/AssemblySwitch.js.html +0 -133
  73. package/coverage/lcov-report/src/nodes/BinaryOperation.js.html +0 -127
  74. package/coverage/lcov-report/src/nodes/Block.js.html +0 -166
  75. package/coverage/lcov-report/src/nodes/BooleanLiteral.js.html +0 -100
  76. package/coverage/lcov-report/src/nodes/Break.js.html +0 -100
  77. package/coverage/lcov-report/src/nodes/BreakStatement.js.html +0 -100
  78. package/coverage/lcov-report/src/nodes/CatchClause.js.html +0 -148
  79. package/coverage/lcov-report/src/nodes/Conditional.js.html +0 -151
  80. package/coverage/lcov-report/src/nodes/Continue.js.html +0 -100
  81. package/coverage/lcov-report/src/nodes/ContinueStatement.js.html +0 -100
  82. package/coverage/lcov-report/src/nodes/ContractDefinition.js.html +0 -226
  83. package/coverage/lcov-report/src/nodes/CustomErrorDefinition.js.html +0 -139
  84. package/coverage/lcov-report/src/nodes/DecimalNumber.js.html +0 -100
  85. package/coverage/lcov-report/src/nodes/DoWhileStatement.js.html +0 -157
  86. package/coverage/lcov-report/src/nodes/ElementaryTypeName.js.html +0 -115
  87. package/coverage/lcov-report/src/nodes/EmitStatement.js.html +0 -100
  88. package/coverage/lcov-report/src/nodes/EnumDefinition.js.html +0 -151
  89. package/coverage/lcov-report/src/nodes/EnumValue.js.html +0 -100
  90. package/coverage/lcov-report/src/nodes/EventDefinition.js.html +0 -145
  91. package/coverage/lcov-report/src/nodes/ExpressionStatement.js.html +0 -181
  92. package/coverage/lcov-report/src/nodes/FileLevelConstant.js.html +0 -121
  93. package/coverage/lcov-report/src/nodes/ForStatement.js.html +0 -223
  94. package/coverage/lcov-report/src/nodes/FunctionCall.js.html +0 -280
  95. package/coverage/lcov-report/src/nodes/FunctionDefinition.js.html +0 -457
  96. package/coverage/lcov-report/src/nodes/FunctionTypeName.js.html +0 -217
  97. package/coverage/lcov-report/src/nodes/HexLiteral.js.html +0 -133
  98. package/coverage/lcov-report/src/nodes/HexNumber.js.html +0 -100
  99. package/coverage/lcov-report/src/nodes/Identifier.js.html +0 -100
  100. package/coverage/lcov-report/src/nodes/IfStatement.js.html +0 -265
  101. package/coverage/lcov-report/src/nodes/ImportDirective.js.html +0 -247
  102. package/coverage/lcov-report/src/nodes/IndexAccess.js.html +0 -199
  103. package/coverage/lcov-report/src/nodes/IndexRangeAccess.js.html +0 -121
  104. package/coverage/lcov-report/src/nodes/InheritanceSpecifier.js.html +0 -130
  105. package/coverage/lcov-report/src/nodes/InlineAssemblyStatement.js.html +0 -148
  106. package/coverage/lcov-report/src/nodes/LabelDefinition.js.html +0 -118
  107. package/coverage/lcov-report/src/nodes/Mapping.js.html +0 -118
  108. package/coverage/lcov-report/src/nodes/MemberAccess.js.html +0 -466
  109. package/coverage/lcov-report/src/nodes/ModifierDefinition.js.html +0 -259
  110. package/coverage/lcov-report/src/nodes/ModifierInvocation.js.html +0 -190
  111. package/coverage/lcov-report/src/nodes/NameValueExpression.js.html +0 -115
  112. package/coverage/lcov-report/src/nodes/NameValueList.js.html +0 -148
  113. package/coverage/lcov-report/src/nodes/NewExpression.js.html +0 -100
  114. package/coverage/lcov-report/src/nodes/NumberLiteral.js.html +0 -109
  115. package/coverage/lcov-report/src/nodes/PragmaDirective.js.html +0 -100
  116. package/coverage/lcov-report/src/nodes/ReturnStatement.js.html +0 -157
  117. package/coverage/lcov-report/src/nodes/RevertStatement.js.html +0 -100
  118. package/coverage/lcov-report/src/nodes/SourceUnit.js.html +0 -133
  119. package/coverage/lcov-report/src/nodes/StateVariableDeclaration.js.html +0 -166
  120. package/coverage/lcov-report/src/nodes/StringLiteral.js.html +0 -148
  121. package/coverage/lcov-report/src/nodes/StructDefinition.js.html +0 -154
  122. package/coverage/lcov-report/src/nodes/ThrowStatement.js.html +0 -100
  123. package/coverage/lcov-report/src/nodes/TryStatement.js.html +0 -214
  124. package/coverage/lcov-report/src/nodes/TupleExpression.js.html +0 -160
  125. package/coverage/lcov-report/src/nodes/TypeDefinition.js.html +0 -100
  126. package/coverage/lcov-report/src/nodes/UnaryOperation.js.html +0 -121
  127. package/coverage/lcov-report/src/nodes/UncheckedStatement.js.html +0 -118
  128. package/coverage/lcov-report/src/nodes/UserDefinedTypeName.js.html +0 -100
  129. package/coverage/lcov-report/src/nodes/UsingForDeclaration.js.html +0 -166
  130. package/coverage/lcov-report/src/nodes/VariableDeclaration.js.html +0 -253
  131. package/coverage/lcov-report/src/nodes/VariableDeclarationStatement.js.html +0 -217
  132. package/coverage/lcov-report/src/nodes/WhileStatement.js.html +0 -154
  133. package/coverage/lcov-report/src/nodes/index.html +0 -1256
  134. package/coverage/lcov-report/src/nodes/index.js.html +0 -319
  135. package/coverage/lcov-report/src/nodes/print-comments.js.html +0 -166
  136. package/coverage/lcov-report/src/nodes/print-preserving-empty-lines.js.html +0 -244
  137. package/coverage/lcov-report/src/nodes/print-separated-item.js.html +0 -148
  138. package/coverage/lcov-report/src/nodes/print-separated-list.js.html +0 -154
  139. package/coverage/lcov-report/src/options.js.html +0 -238
  140. package/coverage/lcov-report/src/parser.js.html +0 -643
  141. package/coverage/lcov-report/src/printer.js.html +0 -214
  142. package/coverage/lcov.info +0 -2672
  143. package/jest.config.js +0 -25
  144. package/src/prettier-comments/common/util.js +0 -684
package/package.json CHANGED
@@ -1,17 +1,31 @@
1
1
  {
2
2
  "name": "prettier-plugin-solidity",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "description": "A Prettier Plugin for automatically formatting your Solidity code.",
5
- "main": "src",
5
+ "main": "./src/index.js",
6
+ "browser": "./dist/standalone.js",
7
+ "unpkg": "./dist/standalone.js",
6
8
  "scripts": {
9
+ "build": "webpack --env production",
10
+ "build:dev": "webpack --env development",
7
11
  "eslint": "eslint 'scripts/**' 'src/**' 'tests/**'",
8
12
  "generateindexes": "cross-env SCRIPT=true node scripts/generateIndexes.js",
9
13
  "lint": "npm run eslint && npm run prettier -- --list-different",
10
14
  "lint:fix": "npm run eslint -- --fix && npm run prettier -- --write",
15
+ "prepublishOnly": "npm run build",
11
16
  "prettier": "prettier './*.{js,json,md,yml}' '{scripts,src,tests}/**/*.js'",
12
17
  "test": "jest",
13
- "test:all": "cross-env FULL_TEST=1 c8 jest"
18
+ "test:all": "cross-env FULL_TEST=1 c8 jest",
19
+ "test:standalone": "cross-env TEST_STANDALONE=1 FULL_TEST=1 jest"
14
20
  },
21
+ "files": [
22
+ "src",
23
+ "dist/standalone.js",
24
+ "dist/standalone.js.LICENSE.txt",
25
+ "dist/standalone.js.map",
26
+ "LICENSE",
27
+ "README.md"
28
+ ],
15
29
  "repository": {
16
30
  "type": "git",
17
31
  "url": "git+https://github.com/prettier-solidity/prettier-plugin-solidity.git"
@@ -69,30 +83,42 @@
69
83
  "c8": "^7.12.0",
70
84
  "cross-env": "^7.0.3",
71
85
  "dir-to-object": "^2.0.0",
72
- "eslint": "^8.27.0",
86
+ "eslint": "^8.30.0",
73
87
  "eslint-config-airbnb-base": "^15.0.0",
74
88
  "eslint-config-prettier": "^8.5.0",
75
89
  "eslint-plugin-import": "^2.26.0",
90
+ "exports-loader": "^4.0.0",
76
91
  "jest": "^29.3.1",
77
92
  "jest-light-runner": "^0.4.1",
78
93
  "jest-mock-now": "^1.3.0",
79
94
  "jest-snapshot-serializer-ansi": "^1.0.0",
80
95
  "jest-snapshot-serializer-raw": "^1.2.0",
81
- "jest-watch-typeahead": "^2.2.0",
96
+ "jest-watch-typeahead": "^2.2.1",
82
97
  "lines-and-columns": "^2.0.3",
83
- "prettier": "^2.7.1",
98
+ "prettier": "^3.0.0-alpha.4",
84
99
  "proxyquire": "^2.1.3",
85
- "solc": "^0.8.17"
100
+ "solc": "^0.8.17",
101
+ "webpack": "^5.75.0",
102
+ "webpack-cli": "^5.0.1"
86
103
  },
87
104
  "dependencies": {
88
105
  "@solidity-parser/parser": "^0.14.5",
89
- "emoji-regex": "^10.2.1",
90
- "escape-string-regexp": "^4.0.0",
91
106
  "semver": "^7.3.8",
92
- "solidity-comments-extractor": "^0.0.7",
93
- "string-width": "^4.2.3"
107
+ "solidity-comments-extractor": "^0.0.7"
94
108
  },
95
109
  "peerDependencies": {
96
- "prettier": "^2.3.0"
110
+ "prettier": ">=2.3.0 || >=3.0.0-alpha.0"
111
+ },
112
+ "browserslist": {
113
+ "production": [
114
+ ">0.2%",
115
+ "not dead",
116
+ "not op_mini all"
117
+ ],
118
+ "development": [
119
+ "last 1 chrome version",
120
+ "last 1 firefox version",
121
+ "last 1 safari version"
122
+ ]
97
123
  }
98
124
  }
package/src/clean.js CHANGED
@@ -1,8 +1,6 @@
1
- // eslint-disable-next-line no-unused-vars
2
- function clean(ast, newObj, parent) {
3
- ['code', 'codeStart', 'loc', 'range', 'raw'].forEach((name) => {
4
- delete newObj[name]; // eslint-disable-line no-param-reassign
5
- });
6
- }
1
+ // Prettier offers a clean way to define ignored properties.
2
+ const ignoredProperties = new Set(['loc', 'range', 'comments']);
3
+ function clean(/* ast, newObj, parent */) {}
4
+ clean.ignoredProperties = ignoredProperties;
7
5
 
8
6
  module.exports = clean;
@@ -0,0 +1,64 @@
1
+ const {
2
+ util: { getNextNonSpaceNonCommentCharacterIndex, makeString },
3
+ version
4
+ } = require('prettier');
5
+ const satisfies = require('semver/functions/satisfies');
6
+
7
+ const prettierVersionSatisfies = (range) => satisfies(version, range);
8
+
9
+ function getNextNonSpaceNonCommentCharacter(text, node, locEnd) {
10
+ return text.charAt(
11
+ getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd)
12
+ );
13
+ }
14
+
15
+ function printString(rawContent, options) {
16
+ const double = { quote: '"', regex: /"/g };
17
+ const single = { quote: "'", regex: /'/g };
18
+
19
+ const preferred = options.singleQuote ? single : double;
20
+ const alternate = preferred === single ? double : single;
21
+
22
+ let shouldUseAlternateQuote = false;
23
+
24
+ // If `rawContent` contains at least one of the quote preferred for enclosing
25
+ // the string, we might want to enclose with the alternate quote instead, to
26
+ // minimize the number of escaped quotes.
27
+ // Also check for the alternate quote, to determine if we're allowed to swap
28
+ // the quotes on a DirectiveLiteral.
29
+ if (
30
+ rawContent.includes(preferred.quote) ||
31
+ rawContent.includes(alternate.quote)
32
+ ) {
33
+ const numPreferredQuotes = (rawContent.match(preferred.regex) || []).length;
34
+ const numAlternateQuotes = (rawContent.match(alternate.regex) || []).length;
35
+
36
+ shouldUseAlternateQuote = numPreferredQuotes > numAlternateQuotes;
37
+ }
38
+
39
+ const enclosingQuote = shouldUseAlternateQuote
40
+ ? alternate.quote
41
+ : preferred.quote;
42
+
43
+ // It might sound unnecessary to use `makeString` even if the string already
44
+ // is enclosed with `enclosingQuote`, but it isn't. The string could contain
45
+ // unnecessary escapes (such as in `"\'"`). Always using `makeString` makes
46
+ // sure that we consistently output the minimum amount of escaped quotes.
47
+ return makeString(rawContent, enclosingQuote);
48
+ }
49
+
50
+ function hasNodeIgnoreComment(node) {
51
+ return (
52
+ node &&
53
+ node.comments &&
54
+ node.comments.length > 0 &&
55
+ node.comments.some((comment) => comment.value.trim() === 'prettier-ignore')
56
+ );
57
+ }
58
+
59
+ module.exports = {
60
+ getNextNonSpaceNonCommentCharacter,
61
+ printString,
62
+ prettierVersionSatisfies,
63
+ hasNodeIgnoreComment
64
+ };
@@ -3,11 +3,17 @@ const printers = require('../binary-operator-printers');
3
3
 
4
4
  const BinaryOperation = {
5
5
  print: ({ node, path, print, options }) => {
6
- const printerKeys = Object.keys(printers);
7
- for (let i = 0, len = printerKeys.length; i < len; i += 1) {
8
- if (printers[printerKeys[i]].match(node.operator))
9
- return printers[printerKeys[i]].print(node, path, print, options);
6
+ const binaryOperationPrinter = Object.values(printers).find((printer) =>
7
+ printer.match(node.operator)
8
+ );
9
+ if (binaryOperationPrinter === undefined) {
10
+ throw new Error(
11
+ `Assertion error: no printer found for operator ${JSON.stringify(
12
+ node.operator
13
+ )}`
14
+ );
10
15
  }
16
+ return binaryOperationPrinter.print(node, path, print, options);
11
17
  }
12
18
  };
13
19
 
@@ -15,7 +15,7 @@ const ExpressionStatement = {
15
15
  if (parent.type === 'IfStatement') {
16
16
  if (node.comments && node.comments.length) {
17
17
  const comments = printComments(node, path, options);
18
- if (comments && comments.parts && comments.parts.length) {
18
+ if (comments && comments.length) {
19
19
  parts.push(comments);
20
20
  parts.push(hardline);
21
21
  }
@@ -47,7 +47,7 @@ const parameters = (parametersType, node, path, print, options) => {
47
47
  )
48
48
  ) === ')'
49
49
  );
50
- return parameterComments.parts.length > 0
50
+ return parameterComments.length > 0
51
51
  ? printSeparatedItem(parameterComments)
52
52
  : '';
53
53
  }
@@ -4,7 +4,7 @@ const {
4
4
  }
5
5
  } = require('prettier');
6
6
 
7
- const { printString } = require('../prettier-comments/common/util');
7
+ const { printString } = require('../common/util');
8
8
 
9
9
  const HexLiteral = {
10
10
  print: ({ node, options }) => {
@@ -3,10 +3,11 @@ const {
3
3
  builders: { group, line, softline }
4
4
  }
5
5
  } = require('prettier');
6
- const semver = require('semver');
6
+ const coerce = require('semver/functions/coerce');
7
+ const satisfies = require('semver/functions/satisfies');
7
8
 
8
9
  const printSeparatedList = require('./print-separated-list');
9
- const { printString } = require('../prettier-comments/common/util');
10
+ const { printString } = require('../common/util');
10
11
 
11
12
  const ImportDirective = {
12
13
  print: ({ node, options }) => {
@@ -18,14 +19,14 @@ const ImportDirective = {
18
19
  doc = [importPath, ' as ', node.unitAlias];
19
20
  } else if (node.symbolAliases) {
20
21
  // import { Foo, Bar as Qux } from "./Foo.sol";
21
- const compiler = semver.coerce(options.compiler);
22
+ const compiler = coerce(options.compiler);
22
23
  const symbolAliases = node.symbolAliases.map(([a, b]) =>
23
24
  b ? `${a} as ${b}` : a
24
25
  );
25
26
  let firstSeparator;
26
27
  let separator;
27
28
 
28
- if (compiler && semver.satisfies(compiler, '>=0.7.4')) {
29
+ if (compiler && satisfies(compiler, '>=0.7.4')) {
29
30
  // if the compiler exists and is greater than or equal to 0.7.4 we will
30
31
  // split the ImportDirective.
31
32
  firstSeparator = options.bracketSpacing ? line : softline;
@@ -1,5 +1,5 @@
1
1
  // @TODO: add support for assembly language specifier
2
- const { printString } = require('../prettier-comments/common/util');
2
+ const { printString } = require('../common/util');
3
3
  const printSeparatedList = require('./print-separated-list');
4
4
 
5
5
  const InlineAssemblyStatement = {
@@ -9,7 +9,7 @@ const printPreservingEmptyLines = require('./print-preserving-empty-lines');
9
9
  const SourceUnit = {
10
10
  print: ({ options, path, print }) => [
11
11
  printPreservingEmptyLines(path, 'children', options, print),
12
- line
12
+ options.parentParser ? '' : line
13
13
  ]
14
14
  };
15
15
 
@@ -3,7 +3,7 @@ const {
3
3
  builders: { join, hardline }
4
4
  }
5
5
  } = require('prettier');
6
- const { printString } = require('../prettier-comments/common/util');
6
+ const { printString } = require('../common/util');
7
7
 
8
8
  const StringLiteral = {
9
9
  print: ({ node, options }) => {
@@ -3,25 +3,35 @@ const {
3
3
  builders: { join, line }
4
4
  }
5
5
  } = require('prettier');
6
+ const { prettierVersionSatisfies } = require('../common/util');
6
7
 
7
- const printComments = (node, path, options, filter = () => true) =>
8
- node.comments
9
- ? join(
10
- line,
11
- path
12
- .map((commentPath) => {
13
- const comment = commentPath.getValue();
14
- if (comment.trailing || comment.leading || comment.printed) {
15
- return null;
16
- }
17
- if (!filter(comment)) {
18
- return null;
19
- }
20
- comment.printed = true;
21
- return options.printer.printComment(commentPath);
22
- }, 'comments')
23
- .filter(Boolean)
24
- )
25
- : '';
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
+ };
26
36
 
27
37
  module.exports = printComments;
package/src/parser.js CHANGED
@@ -1,7 +1,8 @@
1
1
  const extractComments = require('solidity-comments-extractor');
2
2
  // https://prettier.io/docs/en/plugins.html#parsers
3
3
  const parser = require('@solidity-parser/parser');
4
- const semver = require('semver');
4
+ const coerce = require('semver/functions/coerce');
5
+ const satisfies = require('semver/functions/satisfies');
5
6
 
6
7
  const tryHug = (node, operators) => {
7
8
  if (node.type === 'BinaryOperation' && operators.includes(node.operator))
@@ -13,8 +14,8 @@ const tryHug = (node, operators) => {
13
14
  return node;
14
15
  };
15
16
 
16
- function parse(text, _parsers, options) {
17
- const compiler = semver.coerce(options.compiler);
17
+ function parse(text, _parsers, options = _parsers) {
18
+ const compiler = coerce(options.compiler);
18
19
  const parsed = parser.parse(text, { loc: true, range: true });
19
20
  parsed.comments = extractComments(text);
20
21
 
@@ -25,7 +26,7 @@ function parse(text, _parsers, options) {
25
26
  // if the compiler option has not been provided we leave.
26
27
  if (!compiler) return;
27
28
  // we make a check against each pragma directive in the document.
28
- if (!semver.satisfies(compiler, ctx.value)) {
29
+ if (!satisfies(compiler, ctx.value)) {
29
30
  // @TODO: investigate the best way to warn that would apply to
30
31
  // different editors.
31
32
  // eslint-disable-next-line no-console
@@ -64,7 +65,7 @@ function parse(text, _parsers, options) {
64
65
  // if the compiler is below 0.8.0 we will recognize the type 'byte' as an
65
66
  // alias of 'bytes1'. Otherwise we will ignore this and enforce always
66
67
  // 'bytes1'.
67
- const pre080 = compiler && semver.satisfies(compiler, '<0.8.0');
68
+ const pre080 = compiler && satisfies(compiler, '<0.8.0');
68
69
  if (!pre080 && ctx.name === 'byte') ctx.name = 'bytes1';
69
70
  },
70
71
  BinaryOperation(ctx) {
@@ -87,7 +88,7 @@ function parse(text, _parsers, options) {
87
88
  // If the compiler has not been given as an option using we leave a**b**c.
88
89
  if (!compiler) break;
89
90
 
90
- if (semver.satisfies(compiler, '<0.8.0')) {
91
+ if (satisfies(compiler, '<0.8.0')) {
91
92
  // If the compiler is less than 0.8.0 then a**b**c is formatted as
92
93
  // (a**b)**c.
93
94
  ctx.left = tryHug(ctx.left, ['**']);
@@ -5,10 +5,12 @@ const {
5
5
  addLeadingComment,
6
6
  addTrailingComment,
7
7
  addDanglingComment,
8
- getNextNonSpaceNonCommentCharacterIndex
8
+ getNextNonSpaceNonCommentCharacterIndex,
9
+ hasNewline,
10
+ hasNewlineInRange
9
11
  }
10
12
  } = require('prettier');
11
- const privateUtil = require("../common/util");
13
+ const { getNextNonSpaceNonCommentCharacter } = require("../../common/util");
12
14
 
13
15
  function handleOwnLineComment(comment, text, options, ast, isLastComment) {
14
16
  const { precedingNode, enclosingNode, followingNode } = comment;
@@ -241,7 +243,7 @@ function handleIfStatementComments(
241
243
  // if (a /* comment */) {}
242
244
  // The only workaround I found is to look at the next character to see if
243
245
  // it is a ).
244
- const nextCharacter = privateUtil.getNextNonSpaceNonCommentCharacter(
246
+ const nextCharacter = getNextNonSpaceNonCommentCharacter(
245
247
  text,
246
248
  comment,
247
249
  options.locEnd
@@ -310,7 +312,7 @@ function handleWhileComments(
310
312
  // while (a /* comment */) {}
311
313
  // The only workaround I found is to look at the next character to see if
312
314
  // it is a ).
313
- const nextCharacter = privateUtil.getNextNonSpaceNonCommentCharacter(
315
+ const nextCharacter = getNextNonSpaceNonCommentCharacter(
314
316
  text,
315
317
  comment,
316
318
  options.locEnd
@@ -391,7 +393,7 @@ function handleConditionalExpressionComments(
391
393
  ) {
392
394
  const isSameLineAsPrecedingNode =
393
395
  precedingNode &&
394
- !privateUtil.hasNewlineInRange(
396
+ !hasNewlineInRange(
395
397
  text,
396
398
  options.locEnd(precedingNode),
397
399
  options.locStart(comment)
@@ -469,7 +471,7 @@ function handleMethodNameComments(
469
471
  enclosingNode.key === precedingNode &&
470
472
  // special Property case: { key: /*comment*/(value) };
471
473
  // comment should be attached to value instead of key
472
- privateUtil.getNextNonSpaceNonCommentCharacter(
474
+ getNextNonSpaceNonCommentCharacter(
473
475
  text,
474
476
  precedingNode,
475
477
  options.locEnd
@@ -506,7 +508,7 @@ function handleFunctionNameComments(
506
508
  options
507
509
  ) {
508
510
  if (
509
- privateUtil.getNextNonSpaceNonCommentCharacter(
511
+ getNextNonSpaceNonCommentCharacter(
510
512
  text,
511
513
  comment,
512
514
  options.locEnd
@@ -550,7 +552,7 @@ function handleCommentAfterArrowParams(text, enclosingNode, comment, options) {
550
552
 
551
553
  function handleCommentInEmptyParens(text, enclosingNode, comment, options) {
552
554
  if (
553
- privateUtil.getNextNonSpaceNonCommentCharacter(
555
+ getNextNonSpaceNonCommentCharacter(
554
556
  text,
555
557
  comment,
556
558
  options.locEnd
@@ -621,7 +623,7 @@ function handleLastFunctionArgComments(
621
623
  enclosingNode.type === "FunctionDeclaration" ||
622
624
  enclosingNode.type === "ObjectMethod" ||
623
625
  enclosingNode.type === "ClassMethod") &&
624
- privateUtil.getNextNonSpaceNonCommentCharacter(
626
+ getNextNonSpaceNonCommentCharacter(
625
627
  text,
626
628
  comment,
627
629
  options.locEnd
@@ -755,7 +757,7 @@ function handleImportDeclarationComments(
755
757
  precedingNode.type === "ImportSpecifier" &&
756
758
  enclosingNode &&
757
759
  enclosingNode.type === "ImportDeclaration" &&
758
- privateUtil.hasNewline(text, options.locEnd(comment))
760
+ hasNewline(text, options.locEnd(comment))
759
761
  ) {
760
762
  addTrailingComment(precedingNode, comment);
761
763
  return true;
package/src/printer.js CHANGED
@@ -1,14 +1,15 @@
1
- const prettier = require('prettier');
2
- const semver = require('semver');
3
1
  const nodes = require('./nodes');
4
- const { hasNodeIgnoreComment } = require('./prettier-comments/common/util');
2
+ const {
3
+ hasNodeIgnoreComment,
4
+ prettierVersionSatisfies
5
+ } = require('./common/util');
5
6
  const ignoreComments = require('./comments/ignore');
6
7
 
7
8
  let checked = false;
8
9
 
9
10
  function prettierVersionCheck() {
10
11
  if (checked) return;
11
- if (!semver.satisfies(prettier.version, '>=2.3.0')) {
12
+ if (!prettierVersionSatisfies('>=2.3.0 || >=3.0.0-alpha.0')) {
12
13
  throw new Error(
13
14
  'The version of prettier in your node-modules does not satisfy the required ">=2.3.0" constraint. Please update the version of Prettier.'
14
15
  );
package/.c8rc DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "check-coverage": true,
3
- "branches": 99,
4
- "lines": 100,
5
- "functions": 100,
6
- "statements": 100,
7
- "exclude": ["/node_modules/", "/scripts/", "/src/prettier-comments/"],
8
- "include": ["src/**/*.js", "!src/prettier-comments/**/*.js"],
9
- "reporter": ["lcov", "text"],
10
- "temp-dir": "./coverage/"
11
- }
package/.eslintignore DELETED
@@ -1,5 +0,0 @@
1
- coverage/**/*.js
2
- tests/format/**/*.sol
3
- tests/format/RespectDefaultOptions/respect-default-options.js
4
- tests/config/**/*.js
5
- src/prettier-comments/**/*.js
package/.eslintrc DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "env": {
3
- "jest": true
4
- },
5
- "extends": ["airbnb-base", "prettier"],
6
- "globals": {
7
- "run_spec": false
8
- }
9
- }
package/.nvmrc DELETED
@@ -1 +0,0 @@
1
- 14
package/.prettierignore DELETED
@@ -1,4 +0,0 @@
1
- coverage/**/*.js
2
- tests/format/**/*.sol
3
- tests/format/RespectDefaultOptions/respect-default-options.js
4
- src/prettier-comments/**/*.js
package/.prettierrc DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "singleQuote": true,
3
- "trailingComma": "none"
4
- }
@@ -1,76 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, sex characteristics, gender identity and expression,
9
- level of experience, education, socioeconomic status, nationality, personal
10
- appearance, race, religion, or sexual identity and orientation.
11
-
12
- ## Our Standards
13
-
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
16
-
17
- - Using welcoming and inclusive language
18
- - Being respectful of differing viewpoints and experiences
19
- - Gracefully accepting constructive criticism
20
- - Focusing on what is best for the community
21
- - Showing empathy towards other community members
22
-
23
- Examples of unacceptable behavior by participants include:
24
-
25
- - The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- - Trolling, insulting/derogatory comments, and personal or political attacks
28
- - Public or private harassment
29
- - Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
31
- - Other conduct which could reasonably be considered inappropriate in a
32
- professional setting
33
-
34
- ## Our Responsibilities
35
-
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
39
-
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
45
-
46
- ## Scope
47
-
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
54
-
55
- ## Enforcement
56
-
57
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at mattia.richetto@gmail.com. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
63
-
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
67
-
68
- ## Attribution
69
-
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72
-
73
- [homepage]: https://www.contributor-covenant.org
74
-
75
- For answers to common questions about this code of conduct, see
76
- https://www.contributor-covenant.org/faq
package/HOW_TO_PUBLISH.md DELETED
@@ -1,39 +0,0 @@
1
- # How to publish
2
-
3
- ```bash
4
- nvm use
5
-
6
- git checkout main
7
-
8
- git pull
9
-
10
- npm i
11
-
12
- npm run test:all
13
- ```
14
-
15
- - Create a branch with the name of the new version number, e.g.: `git checkout -b 1.0.0-beta.4`
16
-
17
- - Bump the version in the package.json file
18
-
19
- - `npm i`
20
-
21
- ```bash
22
- git add .
23
-
24
- git commit -m 'bump version'
25
-
26
- git push
27
- ```
28
-
29
- - Once the PR is merged, then `git checkout main` and `git pull`
30
-
31
- - Uncomment the first line of the `.npmrc` file and comment the second line
32
-
33
- - Then publish the package using the following command: `NPM_TOKEN=${NPM_TOKEN} npm publish`
34
-
35
- > N.B.: you need to replace `${NPM_TOKEN}` with a valid npm token
36
-
37
- - Once the new version is published, add a new release in GitHub; https://github.com/prettier-solidity/prettier-plugin-solidity/releases/new
38
-
39
- - The tag version and the name should be prefixed by a `v` followed by the latest published version number; e.g.: `v1.0.0-beta.4`