prettier-plugin-solidity 1.1.4-dev → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-solidity",
3
- "version": "1.1.4-dev",
3
+ "version": "1.2.0",
4
4
  "description": "A Prettier Plugin for automatically formatting your Solidity code.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -110,7 +110,7 @@
110
110
  "webpack-cli": "^5.1.4"
111
111
  },
112
112
  "dependencies": {
113
- "@solidity-parser/parser": "^0.16.1",
113
+ "@solidity-parser/parser": "^0.16.2",
114
114
  "semver": "^7.5.4",
115
115
  "solidity-comments-extractor": "^0.0.7"
116
116
  },
@@ -1,5 +1,5 @@
1
1
  import { util } from 'prettier';
2
- import { getNextNonSpaceNonCommentCharacter } from '../../common/util.js';
2
+ import { getNextNonSpaceNonCommentCharacter } from '../../common/backward-compatibility.js';
3
3
 
4
4
  const { addLeadingComment, addTrailingComment, addDanglingComment } = util;
5
5
 
@@ -1,5 +1,5 @@
1
1
  import { util } from 'prettier';
2
- import { getNextNonSpaceNonCommentCharacter } from '../../common/util.js';
2
+ import { getNextNonSpaceNonCommentCharacter } from '../../common/backward-compatibility.js';
3
3
 
4
4
  const { addLeadingComment, addTrailingComment, addDanglingComment } = util;
5
5
 
@@ -0,0 +1,46 @@
1
+ import { util } from 'prettier';
2
+ import { prettierVersionSatisfies } from './util.js';
3
+
4
+ export const isPrettier2 = prettierVersionSatisfies('^2.3.0');
5
+
6
+ // The functions in this file will never be 100% covered in a single run
7
+ // since it depends on the version of Prettier being used.
8
+ // Mocking the behaviour will introduce a lot of maintenance in the tests.
9
+
10
+ export function isNextLineEmpty(text, startIndex) {
11
+ return isPrettier2
12
+ ? util.isNextLineEmptyAfterIndex(text, startIndex)
13
+ : util.isNextLineEmpty(text, startIndex); // V3 deprecated `isNextLineEmptyAfterIndex`
14
+ }
15
+
16
+ export function getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) {
17
+ return isPrettier2
18
+ ? util.getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd)
19
+ : util.getNextNonSpaceNonCommentCharacterIndex(text, locEnd(node)); // V3 signature changed
20
+ }
21
+
22
+ export function getNextNonSpaceNonCommentCharacter(text, node, locEnd) {
23
+ return isPrettier2
24
+ ? text.charAt(
25
+ util.getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd)
26
+ )
27
+ : util.getNextNonSpaceNonCommentCharacter(text, locEnd(node)); // V3 exposes this function directly
28
+ }
29
+
30
+ export function isFirst(path, _, index) {
31
+ return isPrettier2 ? index === 0 : path.isFirst;
32
+ }
33
+
34
+ export function isLast(path, key, index) {
35
+ return isPrettier2
36
+ ? index === path.getParentNode()[key].length - 1
37
+ : path.isLast;
38
+ }
39
+
40
+ export function previous(path, key, index) {
41
+ return isPrettier2 ? path.getParentNode()[key][index - 1] : path.previous;
42
+ }
43
+
44
+ export function next(path, key, index) {
45
+ return isPrettier2 ? path.getParentNode()[key][index + 1] : path.next;
46
+ }
@@ -1,5 +1,12 @@
1
1
  import { doc } from 'prettier';
2
- import { isNextLineEmpty, isPrettier2 } from './util.js';
2
+ import {
3
+ isFirst,
4
+ isLast,
5
+ isNextLineEmpty,
6
+ isPrettier2,
7
+ next,
8
+ previous
9
+ } from './backward-compatibility.js';
3
10
 
4
11
  const { group, indent, join, line, softline, hardline } = doc.builders;
5
12
 
@@ -26,12 +33,28 @@ export const printComments = (node, path, options, filter = () => true) => {
26
33
  // since it depends on the version of Prettier being used.
27
34
  // Mocking the behaviour will introduce a lot of maintenance in the tests.
28
35
  /* c8 ignore start */
29
- return isPrettier2()
36
+ return isPrettier2
30
37
  ? document.parts // Prettier V2
31
38
  : document; // Prettier V3
32
39
  /* c8 ignore stop */
33
40
  };
34
41
 
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
+
35
58
  export function printPreservingEmptyLines(path, key, options, print) {
36
59
  const parts = [];
37
60
  path.each((childPath, index) => {
@@ -48,29 +71,41 @@ export function printPreservingEmptyLines(path, key, options, print) {
48
71
  parts.push(hardline);
49
72
  }
50
73
 
51
- if (index > 0) {
52
- if (
53
- ['ContractDefinition', 'FunctionDefinition'].includes(nodeType) &&
54
- parts[parts.length - 2] !== hardline
55
- ) {
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
56
86
  parts.push(hardline);
57
87
  }
58
88
  }
59
89
 
60
90
  parts.push(print(childPath));
61
91
 
62
- if (
63
- isNextLineEmpty(options.originalText, options.locEnd(node) + 1) ||
64
- nodeType === 'FunctionDefinition'
65
- ) {
66
- 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
+ }
67
106
  }
68
107
  }, key);
69
108
 
70
- if (parts.length > 1 && parts[parts.length - 1] === hardline) {
71
- parts.pop();
72
- }
73
-
74
109
  return parts;
75
110
  }
76
111
 
@@ -2,32 +2,6 @@ import { util, version } from 'prettier';
2
2
  import satisfies from 'semver/functions/satisfies.js';
3
3
 
4
4
  export const prettierVersionSatisfies = (range) => satisfies(version, range);
5
- export const isPrettier2 = () => prettierVersionSatisfies('^2.3.0');
6
-
7
- // The following functions will never be 100% covered in a single run
8
- // since it depends on the version of Prettier being used.
9
- // Mocking the behaviour will introduce a lot of maintenance in the tests.
10
- /* c8 ignore start */
11
- export function isNextLineEmpty(text, startIndex) {
12
- return isPrettier2()
13
- ? util.isNextLineEmptyAfterIndex(text, startIndex)
14
- : util.isNextLineEmpty(text, startIndex); // V3 deprecated `isNextLineEmptyAfterIndex`
15
- }
16
-
17
- export function getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) {
18
- return isPrettier2()
19
- ? util.getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd)
20
- : util.getNextNonSpaceNonCommentCharacterIndex(text, locEnd(node)); // V3 signature changed
21
- }
22
-
23
- export function getNextNonSpaceNonCommentCharacter(text, node, locEnd) {
24
- return isPrettier2()
25
- ? text.charAt(
26
- util.getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd)
27
- )
28
- : util.getNextNonSpaceNonCommentCharacter(text, locEnd(node)); // V3 exposes this function directly
29
- }
30
- /* c8 ignore stop */
31
5
 
32
6
  export function printString(rawContent, options) {
33
7
  const double = { quote: '"', regex: /"/g };
@@ -1,5 +1,5 @@
1
1
  import { doc } from 'prettier';
2
- import { getNextNonSpaceNonCommentCharacter } from '../common/util.js';
2
+ import { getNextNonSpaceNonCommentCharacter } from '../common/backward-compatibility.js';
3
3
  import {
4
4
  printComments,
5
5
  printSeparatedItem,
@@ -1,5 +1,5 @@
1
1
  import { util } from "prettier";
2
- import { getNextNonSpaceNonCommentCharacter } from "../../common/util.js";
2
+ import { getNextNonSpaceNonCommentCharacter } from "../../common/backward-compatibility.js";
3
3
 
4
4
  const {
5
5
  addLeadingComment,