prettier-plugin-solidity 1.1.3 → 1.2.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.
Files changed (106) hide show
  1. package/README.md +48 -9
  2. package/dist/standalone.cjs +3 -0
  3. package/dist/standalone.cjs.map +1 -0
  4. package/package.json +42 -33
  5. package/src/binary-operator-printers/arithmetic.js +12 -14
  6. package/src/binary-operator-printers/assignment.js +4 -6
  7. package/src/binary-operator-printers/bit.js +2 -2
  8. package/src/binary-operator-printers/comparison.js +11 -13
  9. package/src/binary-operator-printers/exponentiation.js +4 -6
  10. package/src/binary-operator-printers/index.js +7 -13
  11. package/src/binary-operator-printers/logical.js +12 -14
  12. package/src/binary-operator-printers/shift.js +2 -2
  13. package/src/clean.js +1 -1
  14. package/src/comments/handler.js +7 -15
  15. package/src/comments/handlers/handleContractDefinitionComments.js +9 -11
  16. package/src/comments/handlers/handleModifierInvocationComments.js +9 -11
  17. package/src/comments/handlers/index.js +5 -3
  18. package/src/comments/ignore.js +1 -1
  19. package/src/comments/index.js +2 -4
  20. package/src/comments/printer.js +5 -10
  21. package/src/common/backward-compatibility.js +46 -0
  22. package/src/common/printer-helpers.js +63 -41
  23. package/src/common/util.js +6 -22
  24. package/src/index.js +14 -12
  25. package/src/loc.js +1 -1
  26. package/src/nodes/ArrayTypeName.js +1 -3
  27. package/src/nodes/AssemblyAssignment.js +4 -8
  28. package/src/nodes/AssemblyBlock.js +6 -11
  29. package/src/nodes/AssemblyCall.js +2 -4
  30. package/src/nodes/AssemblyCase.js +1 -3
  31. package/src/nodes/AssemblyFor.js +4 -8
  32. package/src/nodes/AssemblyFunctionDefinition.js +6 -11
  33. package/src/nodes/AssemblyIf.js +1 -3
  34. package/src/nodes/AssemblyLocalDefinition.js +4 -9
  35. package/src/nodes/AssemblyMemberAccess.js +1 -3
  36. package/src/nodes/AssemblyStackAssignment.js +1 -3
  37. package/src/nodes/AssemblySwitch.js +4 -8
  38. package/src/nodes/BinaryOperation.js +2 -5
  39. package/src/nodes/Block.js +6 -11
  40. package/src/nodes/BooleanLiteral.js +1 -3
  41. package/src/nodes/Break.js +1 -3
  42. package/src/nodes/BreakStatement.js +1 -3
  43. package/src/nodes/CatchClause.js +2 -4
  44. package/src/nodes/Conditional.js +4 -8
  45. package/src/nodes/Continue.js +1 -3
  46. package/src/nodes/ContinueStatement.js +1 -3
  47. package/src/nodes/ContractDefinition.js +11 -17
  48. package/src/nodes/CustomErrorDefinition.js +2 -4
  49. package/src/nodes/DecimalNumber.js +1 -3
  50. package/src/nodes/DoWhileStatement.js +4 -9
  51. package/src/nodes/ElementaryTypeName.js +1 -3
  52. package/src/nodes/EmitStatement.js +1 -3
  53. package/src/nodes/EnumDefinition.js +4 -9
  54. package/src/nodes/EnumValue.js +1 -3
  55. package/src/nodes/EventDefinition.js +2 -4
  56. package/src/nodes/ExpressionStatement.js +4 -9
  57. package/src/nodes/FileLevelConstant.js +1 -3
  58. package/src/nodes/ForStatement.js +4 -9
  59. package/src/nodes/FunctionCall.js +4 -9
  60. package/src/nodes/FunctionDefinition.js +11 -18
  61. package/src/nodes/FunctionTypeName.js +4 -9
  62. package/src/nodes/HexLiteral.js +4 -9
  63. package/src/nodes/HexNumber.js +1 -3
  64. package/src/nodes/Identifier.js +1 -3
  65. package/src/nodes/IfStatement.js +6 -11
  66. package/src/nodes/ImportDirective.js +12 -17
  67. package/src/nodes/IndexAccess.js +4 -8
  68. package/src/nodes/IndexRangeAccess.js +1 -3
  69. package/src/nodes/InheritanceSpecifier.js +2 -4
  70. package/src/nodes/InlineAssemblyStatement.js +3 -4
  71. package/src/nodes/LabelDefinition.js +4 -8
  72. package/src/nodes/Mapping.js +17 -18
  73. package/src/nodes/MemberAccess.js +6 -10
  74. package/src/nodes/ModifierDefinition.js +4 -9
  75. package/src/nodes/ModifierInvocation.js +3 -5
  76. package/src/nodes/NameValueExpression.js +1 -3
  77. package/src/nodes/NameValueList.js +4 -9
  78. package/src/nodes/NewExpression.js +1 -3
  79. package/src/nodes/NumberLiteral.js +1 -3
  80. package/src/nodes/PragmaDirective.js +1 -3
  81. package/src/nodes/ReturnStatement.js +4 -8
  82. package/src/nodes/RevertStatement.js +1 -3
  83. package/src/nodes/SourceUnit.js +4 -9
  84. package/src/nodes/StateVariableDeclaration.js +4 -8
  85. package/src/nodes/StringLiteral.js +5 -9
  86. package/src/nodes/StructDefinition.js +13 -22
  87. package/src/nodes/ThrowStatement.js +1 -3
  88. package/src/nodes/TryStatement.js +6 -11
  89. package/src/nodes/TupleExpression.js +4 -9
  90. package/src/nodes/TypeDefinition.js +1 -3
  91. package/src/nodes/UnaryOperation.js +1 -3
  92. package/src/nodes/UncheckedStatement.js +4 -8
  93. package/src/nodes/UserDefinedTypeName.js +1 -3
  94. package/src/nodes/UsingForDeclaration.js +28 -44
  95. package/src/nodes/VariableDeclaration.js +4 -9
  96. package/src/nodes/VariableDeclarationStatement.js +6 -11
  97. package/src/nodes/WhileStatement.js +4 -9
  98. package/src/nodes/index.js +72 -78
  99. package/src/options.js +1 -1
  100. package/src/parser.js +5 -12
  101. package/src/prettier-comments/language-js/comments.js +13 -23
  102. package/src/printer.js +6 -6
  103. package/dist/standalone.js +0 -3
  104. package/dist/standalone.js.map +0 -1
  105. package/src/prettier-comments/index.js +0 -3
  106. /package/dist/{standalone.js.LICENSE.txt → standalone.cjs.LICENSE.txt} +0 -0
@@ -1,14 +1,18 @@
1
- const {
2
- doc: {
3
- builders: { group, indent, join, line, softline, hardline }
4
- },
5
- util: { isNextLineEmptyAfterIndex }
6
- } = require('prettier');
7
- const { prettierVersionSatisfies } = require('./util');
1
+ import { doc } from 'prettier';
2
+ import {
3
+ isFirst,
4
+ isLast,
5
+ isNextLineEmpty,
6
+ isPrettier2,
7
+ next,
8
+ previous
9
+ } from './backward-compatibility.js';
8
10
 
9
- const printComments = (node, path, options, filter = () => true) => {
11
+ const { group, indent, join, line, softline, hardline } = doc.builders;
12
+
13
+ export const printComments = (node, path, options, filter = () => true) => {
10
14
  if (!node.comments) return '';
11
- const doc = join(
15
+ const document = join(
12
16
  line,
13
17
  path
14
18
  .map((commentPath) => {
@@ -29,13 +33,29 @@ const printComments = (node, path, options, filter = () => true) => {
29
33
  // since it depends on the version of Prettier being used.
30
34
  // Mocking the behaviour will introduce a lot of maintenance in the tests.
31
35
  /* c8 ignore start */
32
- return prettierVersionSatisfies('^2.3.0')
33
- ? doc.parts // Prettier V2
34
- : doc; // Prettier V3
36
+ return isPrettier2
37
+ ? document.parts // Prettier V2
38
+ : document; // Prettier V3
35
39
  /* c8 ignore stop */
36
40
  };
37
41
 
38
- function printPreservingEmptyLines(path, key, options, print) {
42
+ const shouldHaveEmptyLine = (node, checkForLeading) =>
43
+ Boolean(
44
+ // if node is not FunctionDefinition, it should have an empty line
45
+ node.type !== 'FunctionDefinition' ||
46
+ // if FunctionDefinition is not abstract, it should have an empty line
47
+ node.body ||
48
+ // if FunctionDefinition has the comment we are looking for (trailing or
49
+ // leading), it should have an empty line
50
+ node.comments?.some((comment) => checkForLeading && comment.leading)
51
+ );
52
+
53
+ const separatingLine = (firstNode, secondNode) =>
54
+ shouldHaveEmptyLine(firstNode, false) || shouldHaveEmptyLine(secondNode, true)
55
+ ? hardline
56
+ : '';
57
+
58
+ export function printPreservingEmptyLines(path, key, options, print) {
39
59
  const parts = [];
40
60
  path.each((childPath, index) => {
41
61
  const node = childPath.getValue();
@@ -51,38 +71,47 @@ function printPreservingEmptyLines(path, key, options, print) {
51
71
  parts.push(hardline);
52
72
  }
53
73
 
54
- if (index > 0) {
55
- if (
56
- ['ContractDefinition', 'FunctionDefinition'].includes(nodeType) &&
57
- parts[parts.length - 2] !== hardline
58
- ) {
74
+ // Only attempt to prepend an empty line if `node` is not the first item
75
+ // and an empty line hasn't already been appended after the previous `node`
76
+ if (
77
+ !isFirst(childPath, key, index) &&
78
+ parts[parts.length - 2] !== hardline
79
+ ) {
80
+ if (nodeType === 'FunctionDefinition') {
81
+ // Prepend FunctionDefinition with an empty line if there should be a
82
+ // separation with the previous `node`
83
+ parts.push(separatingLine(previous(childPath, key, index), node));
84
+ } else if (nodeType === 'ContractDefinition') {
85
+ // Prepend ContractDefinition with an empty line
59
86
  parts.push(hardline);
60
87
  }
61
88
  }
62
89
 
63
90
  parts.push(print(childPath));
64
91
 
65
- if (
66
- isNextLineEmptyAfterIndex(
67
- options.originalText,
68
- options.locEnd(node) + 1
69
- ) ||
70
- nodeType === 'FunctionDefinition'
71
- ) {
72
- parts.push(hardline);
92
+ // Only attempt to append an empty line if `node` is not the last item
93
+ if (!isLast(childPath, key, index)) {
94
+ if (isNextLineEmpty(options.originalText, options.locEnd(node) + 1)) {
95
+ // Append an empty line if the original text already had an one after
96
+ // the current `node`
97
+ parts.push(hardline);
98
+ } else if (nodeType === 'FunctionDefinition') {
99
+ // Append FunctionDefinition with an empty line if there should be a
100
+ // separation with the next `node`
101
+ parts.push(separatingLine(node, next(childPath, key, index)));
102
+ } else if (nodeType === 'ContractDefinition') {
103
+ // Append ContractDefinition with an empty line
104
+ parts.push(hardline);
105
+ }
73
106
  }
74
107
  }, key);
75
108
 
76
- if (parts.length > 1 && parts[parts.length - 1] === hardline) {
77
- parts.pop();
78
- }
79
-
80
109
  return parts;
81
110
  }
82
111
 
83
112
  // This function will add an indentation to the `item` and separate it from the
84
113
  // rest of the `doc` in most cases by a `softline`.
85
- const printSeparatedItem = (
114
+ export const printSeparatedItem = (
86
115
  item,
87
116
  {
88
117
  firstSeparator = softline,
@@ -90,15 +119,15 @@ const printSeparatedItem = (
90
119
  grouped = true
91
120
  } = {}
92
121
  ) => {
93
- const doc = [indent([firstSeparator, item]), lastSeparator];
94
- return grouped ? group(doc) : doc;
122
+ const document = [indent([firstSeparator, item]), lastSeparator];
123
+ return grouped ? group(document) : document;
95
124
  };
96
125
 
97
126
  // This function will add an indentation to the `list` and separate it from the
98
127
  // rest of the `doc` in most cases by a `softline`.
99
128
  // the list itself will be printed with a separator that in most cases is a
100
129
  // comma (,) and a `line`
101
- const printSeparatedList = (
130
+ export const printSeparatedList = (
102
131
  list,
103
132
  { firstSeparator, separator = [',', line], lastSeparator, grouped } = {}
104
133
  ) =>
@@ -107,10 +136,3 @@ const printSeparatedList = (
107
136
  lastSeparator,
108
137
  grouped
109
138
  });
110
-
111
- module.exports = {
112
- printComments,
113
- printPreservingEmptyLines,
114
- printSeparatedList,
115
- printSeparatedItem
116
- };
@@ -1,18 +1,9 @@
1
- const {
2
- util: { getNextNonSpaceNonCommentCharacterIndex, makeString },
3
- version
4
- } = require('prettier');
5
- const satisfies = require('semver/functions/satisfies');
1
+ import { util, version } from 'prettier';
2
+ import satisfies from 'semver/functions/satisfies.js';
6
3
 
7
- const prettierVersionSatisfies = (range) => satisfies(version, range);
4
+ export const prettierVersionSatisfies = (range) => satisfies(version, range);
8
5
 
9
- function getNextNonSpaceNonCommentCharacter(text, node, locEnd) {
10
- return text.charAt(
11
- getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd)
12
- );
13
- }
14
-
15
- function printString(rawContent, options) {
6
+ export function printString(rawContent, options) {
16
7
  const double = { quote: '"', regex: /"/g };
17
8
  const single = { quote: "'", regex: /'/g };
18
9
 
@@ -44,10 +35,10 @@ function printString(rawContent, options) {
44
35
  // is enclosed with `enclosingQuote`, but it isn't. The string could contain
45
36
  // unnecessary escapes (such as in `"\'"`). Always using `makeString` makes
46
37
  // sure that we consistently output the minimum amount of escaped quotes.
47
- return makeString(rawContent, enclosingQuote);
38
+ return util.makeString(rawContent, enclosingQuote);
48
39
  }
49
40
 
50
- function hasNodeIgnoreComment(node) {
41
+ export function hasNodeIgnoreComment(node) {
51
42
  return (
52
43
  node &&
53
44
  node.comments &&
@@ -55,10 +46,3 @@ function hasNodeIgnoreComment(node) {
55
46
  node.comments.some((comment) => comment.value.trim() === 'prettier-ignore')
56
47
  );
57
48
  }
58
-
59
- module.exports = {
60
- getNextNonSpaceNonCommentCharacter,
61
- printString,
62
- prettierVersionSatisfies,
63
- hasNodeIgnoreComment
64
- };
package/src/index.js CHANGED
@@ -1,9 +1,9 @@
1
- const { handleComments, printComment } = require('./comments');
2
- const massageAstNode = require('./clean');
3
- const loc = require('./loc');
4
- const options = require('./options');
5
- const parse = require('./parser');
6
- const print = require('./printer');
1
+ import * as comments from './comments/index.js';
2
+ import massageAstNode from './clean.js';
3
+ import loc from './loc.js';
4
+ import options from './options.js';
5
+ import parse from './parser.js';
6
+ import print from './printer.js';
7
7
 
8
8
  // https://prettier.io/docs/en/plugins.html#languages
9
9
  // https://github.com/ikatyang/linguist-languages/blob/master/data/Solidity.json
@@ -35,14 +35,14 @@ const printers = {
35
35
  'solidity-ast': {
36
36
  canAttachComment,
37
37
  handleComments: {
38
- ownLine: handleComments.handleOwnLineComment,
39
- endOfLine: handleComments.handleEndOfLineComment,
40
- remaining: handleComments.handleRemainingComment
38
+ ownLine: comments.solidityHandleOwnLineComment,
39
+ endOfLine: comments.solidityHandleEndOfLineComment,
40
+ remaining: comments.solidityHandleRemainingComment
41
41
  },
42
- isBlockComment: handleComments.isBlockComment,
42
+ isBlockComment: comments.isBlockComment,
43
43
  massageAstNode,
44
44
  print,
45
- printComment
45
+ printComment: comments.printComment
46
46
  }
47
47
  };
48
48
 
@@ -52,10 +52,12 @@ const defaultOptions = {
52
52
  tabWidth: 4
53
53
  };
54
54
 
55
- module.exports = {
55
+ const plugin = {
56
56
  languages,
57
57
  parsers,
58
58
  printers,
59
59
  options,
60
60
  defaultOptions
61
61
  };
62
+
63
+ export default plugin;
package/src/loc.js CHANGED
@@ -10,7 +10,7 @@ function getRange(index, node) {
10
10
  return null;
11
11
  }
12
12
 
13
- module.exports = {
13
+ export default {
14
14
  locEnd: (node) => getRange(1, node),
15
15
  locStart: (node) => getRange(0, node)
16
16
  };
@@ -1,4 +1,4 @@
1
- const ArrayTypeName = {
1
+ export const ArrayTypeName = {
2
2
  print: ({ node, path, print }) => [
3
3
  path.call(print, 'baseTypeName'),
4
4
  '[',
@@ -6,5 +6,3 @@ const ArrayTypeName = {
6
6
  ']'
7
7
  ]
8
8
  };
9
-
10
- module.exports = ArrayTypeName;
@@ -1,15 +1,11 @@
1
- const {
2
- doc: {
3
- builders: { join }
4
- }
5
- } = require('prettier');
1
+ import { doc } from 'prettier';
6
2
 
7
- const AssemblyAssignment = {
3
+ const { join } = doc.builders;
4
+
5
+ export const AssemblyAssignment = {
8
6
  print: ({ path, print }) => [
9
7
  join(', ', path.map(print, 'names')),
10
8
  ' := ',
11
9
  path.call(print, 'expression')
12
10
  ]
13
11
  };
14
-
15
- module.exports = AssemblyAssignment;
@@ -1,16 +1,13 @@
1
- const {
2
- doc: {
3
- builders: { hardline }
4
- }
5
- } = require('prettier');
6
-
7
- const {
1
+ import { doc } from 'prettier';
2
+ import {
8
3
  printComments,
9
4
  printPreservingEmptyLines,
10
5
  printSeparatedItem
11
- } = require('../common/printer-helpers');
6
+ } from '../common/printer-helpers.js';
7
+
8
+ const { hardline } = doc.builders;
12
9
 
13
- const AssemblyBlock = {
10
+ export const AssemblyBlock = {
14
11
  print: ({ node, options, path, print }) => [
15
12
  '{',
16
13
  printSeparatedItem(
@@ -23,5 +20,3 @@ const AssemblyBlock = {
23
20
  '}'
24
21
  ]
25
22
  };
26
-
27
- module.exports = AssemblyBlock;
@@ -1,6 +1,6 @@
1
- const { printSeparatedList } = require('../common/printer-helpers');
1
+ import { printSeparatedList } from '../common/printer-helpers.js';
2
2
 
3
- const AssemblyCall = {
3
+ export const AssemblyCall = {
4
4
  print: ({ node, path, print, options }) =>
5
5
  node.arguments.length === 0 &&
6
6
  options.originalText.charAt(options.locEnd(node)) !== ')'
@@ -12,5 +12,3 @@ const AssemblyCall = {
12
12
  ')'
13
13
  ]
14
14
  };
15
-
16
- module.exports = AssemblyCall;
@@ -1,9 +1,7 @@
1
- const AssemblyCase = {
1
+ export const AssemblyCase = {
2
2
  print: ({ node, path, print }) => [
3
3
  node.default ? 'default' : ['case ', path.call(print, 'value')],
4
4
  ' ',
5
5
  path.call(print, 'block')
6
6
  ]
7
7
  };
8
-
9
- module.exports = AssemblyCase;
@@ -1,10 +1,8 @@
1
- const {
2
- doc: {
3
- builders: { join }
4
- }
5
- } = require('prettier');
1
+ import { doc } from 'prettier';
6
2
 
7
- const AssemblyFor = {
3
+ const { join } = doc.builders;
4
+
5
+ export const AssemblyFor = {
8
6
  print: ({ path, print }) =>
9
7
  join(' ', [
10
8
  'for',
@@ -14,5 +12,3 @@ const AssemblyFor = {
14
12
  path.call(print, 'body')
15
13
  ])
16
14
  };
17
-
18
- module.exports = AssemblyFor;
@@ -1,15 +1,12 @@
1
- const {
2
- doc: {
3
- builders: { line }
4
- }
5
- } = require('prettier');
6
-
7
- const {
1
+ import { doc } from 'prettier';
2
+ import {
8
3
  printSeparatedItem,
9
4
  printSeparatedList
10
- } = require('../common/printer-helpers');
5
+ } from '../common/printer-helpers.js';
6
+
7
+ const { line } = doc.builders;
11
8
 
12
- const AssemblyFunctionDefinition = {
9
+ export const AssemblyFunctionDefinition = {
13
10
  print: ({ node, path, print }) => [
14
11
  'function ',
15
12
  node.name,
@@ -31,5 +28,3 @@ const AssemblyFunctionDefinition = {
31
28
  path.call(print, 'body')
32
29
  ]
33
30
  };
34
-
35
- module.exports = AssemblyFunctionDefinition;
@@ -1,4 +1,4 @@
1
- const AssemblyIf = {
1
+ export const AssemblyIf = {
2
2
  print: ({ path, print }) => [
3
3
  'if ',
4
4
  path.call(print, 'condition'),
@@ -6,5 +6,3 @@ const AssemblyIf = {
6
6
  path.call(print, 'body')
7
7
  ]
8
8
  };
9
-
10
- module.exports = AssemblyIf;
@@ -1,12 +1,9 @@
1
- const {
2
- doc: {
3
- builders: { line }
4
- }
5
- } = require('prettier');
1
+ import { doc } from 'prettier';
2
+ import { printSeparatedList } from '../common/printer-helpers.js';
6
3
 
7
- const { printSeparatedList } = require('../common/printer-helpers');
4
+ const { line } = doc.builders;
8
5
 
9
- const AssemblyLocalDefinition = {
6
+ export const AssemblyLocalDefinition = {
10
7
  print: ({ node, path, print }) => {
11
8
  const parts = [
12
9
  'let',
@@ -21,5 +18,3 @@ const AssemblyLocalDefinition = {
21
18
  return parts;
22
19
  }
23
20
  };
24
-
25
- module.exports = AssemblyLocalDefinition;
@@ -1,9 +1,7 @@
1
- const AssemblyMemberAccess = {
1
+ export const AssemblyMemberAccess = {
2
2
  print: ({ path, print }) => [
3
3
  path.call(print, 'expression'),
4
4
  '.',
5
5
  path.call(print, 'memberName')
6
6
  ]
7
7
  };
8
-
9
- module.exports = AssemblyMemberAccess;
@@ -1,9 +1,7 @@
1
- const AssemblyStackAssignment = {
1
+ export const AssemblyStackAssignment = {
2
2
  print: ({ node, path, print }) => [
3
3
  path.call(print, 'expression'),
4
4
  ' =: ',
5
5
  node.name
6
6
  ]
7
7
  };
8
-
9
- module.exports = AssemblyStackAssignment;
@@ -1,10 +1,8 @@
1
- const {
2
- doc: {
3
- builders: { hardline, join }
4
- }
5
- } = require('prettier');
1
+ import { doc } from 'prettier';
6
2
 
7
- const AssemblySwitch = {
3
+ const { hardline, join } = doc.builders;
4
+
5
+ export const AssemblySwitch = {
8
6
  print: ({ path, print }) => [
9
7
  'switch ',
10
8
  path.call(print, 'expression'),
@@ -12,5 +10,3 @@ const AssemblySwitch = {
12
10
  join(hardline, path.map(print, 'cases'))
13
11
  ]
14
12
  };
15
-
16
- module.exports = AssemblySwitch;
@@ -1,7 +1,6 @@
1
- /* eslint-disable consistent-return */
2
- const printers = require('../binary-operator-printers');
1
+ import * as printers from '../binary-operator-printers/index.js';
3
2
 
4
- const BinaryOperation = {
3
+ export const BinaryOperation = {
5
4
  print: ({ node, path, print, options }) => {
6
5
  const binaryOperationPrinter = Object.values(printers).find((printer) =>
7
6
  printer.match(node.operator)
@@ -16,5 +15,3 @@ const BinaryOperation = {
16
15
  return binaryOperationPrinter.print(node, path, print, options);
17
16
  }
18
17
  };
19
-
20
- module.exports = BinaryOperation;
@@ -1,15 +1,12 @@
1
- const {
2
- doc: {
3
- builders: { hardline, indent }
4
- }
5
- } = require('prettier');
6
-
7
- const {
1
+ import { doc } from 'prettier';
2
+ import {
8
3
  printComments,
9
4
  printPreservingEmptyLines
10
- } = require('../common/printer-helpers');
5
+ } from '../common/printer-helpers.js';
6
+
7
+ const { hardline, indent } = doc.builders;
11
8
 
12
- const Block = {
9
+ export const Block = {
13
10
  print: ({ node, options, path, print }) =>
14
11
  // if block is empty, just return the pair of braces
15
12
  node.statements.length === 0 && !node.comments
@@ -25,5 +22,3 @@ const Block = {
25
22
  '}'
26
23
  ]
27
24
  };
28
-
29
- module.exports = Block;
@@ -1,5 +1,3 @@
1
- const BooleanLiteral = {
1
+ export const BooleanLiteral = {
2
2
  print: ({ node }) => (node.value ? 'true' : 'false')
3
3
  };
4
-
5
- module.exports = BooleanLiteral;
@@ -1,5 +1,3 @@
1
- const Break = {
1
+ export const Break = {
2
2
  print: () => 'break'
3
3
  };
4
-
5
- module.exports = Break;
@@ -1,5 +1,3 @@
1
- const BreakStatement = {
1
+ export const BreakStatement = {
2
2
  print: () => 'break;'
3
3
  };
4
-
5
- module.exports = BreakStatement;
@@ -1,4 +1,4 @@
1
- const { printSeparatedList } = require('../common/printer-helpers');
1
+ import { printSeparatedList } from '../common/printer-helpers.js';
2
2
 
3
3
  const parameters = (node, path, print) =>
4
4
  node.parameters
@@ -10,12 +10,10 @@ const parameters = (node, path, print) =>
10
10
  ]
11
11
  : '';
12
12
 
13
- const CatchClause = {
13
+ export const CatchClause = {
14
14
  print: ({ node, path, print }) => [
15
15
  'catch ',
16
16
  parameters(node, path, print),
17
17
  path.call(print, 'body')
18
18
  ]
19
19
  };
20
-
21
- module.exports = CatchClause;
@@ -1,10 +1,8 @@
1
- const {
2
- doc: {
3
- builders: { group, indent, line }
4
- }
5
- } = require('prettier');
1
+ import { doc } from 'prettier';
6
2
 
7
- const Conditional = {
3
+ const { group, indent, line } = doc.builders;
4
+
5
+ export const Conditional = {
8
6
  print: ({ path, print }) =>
9
7
  group([
10
8
  path.call(print, 'condition'),
@@ -18,5 +16,3 @@ const Conditional = {
18
16
  ])
19
17
  ])
20
18
  };
21
-
22
- module.exports = Conditional;
@@ -1,5 +1,3 @@
1
- const Continue = {
1
+ export const Continue = {
2
2
  print: () => 'continue'
3
3
  };
4
-
5
- module.exports = Continue;
@@ -1,5 +1,3 @@
1
- const ContinueStatement = {
1
+ export const ContinueStatement = {
2
2
  print: () => 'continue;'
3
3
  };
4
-
5
- module.exports = ContinueStatement;
@@ -1,15 +1,12 @@
1
- const {
2
- doc: {
3
- builders: { group, line, hardline }
4
- }
5
- } = require('prettier');
6
-
7
- const {
1
+ import { doc } from 'prettier';
2
+ import {
8
3
  printComments,
9
4
  printPreservingEmptyLines,
10
5
  printSeparatedItem,
11
6
  printSeparatedList
12
- } = require('../common/printer-helpers');
7
+ } from '../common/printer-helpers.js';
8
+
9
+ const { group, line, hardline } = doc.builders;
13
10
 
14
11
  const inheritance = (node, path, print) =>
15
12
  node.baseContracts.length > 0
@@ -21,18 +18,17 @@ const inheritance = (node, path, print) =>
21
18
  ]
22
19
  : line;
23
20
 
24
- const body = (node, path, options, print) =>
25
- node.subNodes.length > 0 || node.comments
21
+ const body = (node, path, options, print) => {
22
+ const comments = printComments(node, path, options);
23
+ return node.subNodes.length > 0 || (comments && comments.length)
26
24
  ? printSeparatedItem(
27
- [
28
- printPreservingEmptyLines(path, 'subNodes', options, print),
29
- printComments(node, path, options)
30
- ],
25
+ [printPreservingEmptyLines(path, 'subNodes', options, print), comments],
31
26
  { firstSeparator: hardline, grouped: false }
32
27
  )
33
28
  : '';
29
+ };
34
30
 
35
- const ContractDefinition = {
31
+ export const ContractDefinition = {
36
32
  print: ({ node, options, path, print }) => [
37
33
  group([
38
34
  node.kind === 'abstract' ? 'abstract contract' : node.kind,
@@ -45,5 +41,3 @@ const ContractDefinition = {
45
41
  '}'
46
42
  ]
47
43
  };
48
-
49
- module.exports = ContractDefinition;