prettier-plugin-solidity 1.4.0 → 1.4.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-solidity",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "A Prettier Plugin for automatically formatting your Solidity code.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -1,6 +1,27 @@
1
+ import { doc } from 'prettier';
2
+ import { printComments } from '../common/printer-helpers.js';
3
+
4
+ const { hardline } = doc.builders;
5
+
1
6
  export const ExpressionStatement = {
2
- print: ({ node, path, print }) => [
3
- path.call(print, 'expression'),
4
- node.omitSemicolon ? '' : ';'
5
- ]
7
+ print: ({ node, options, path, print }) => {
8
+ const parts = [];
9
+
10
+ const parent = path.getParentNode();
11
+
12
+ if (parent.type === 'IfStatement') {
13
+ if (node.comments?.length) {
14
+ const comments = printComments(node, path, options);
15
+ if (comments?.length) {
16
+ parts.push(comments);
17
+ parts.push(hardline);
18
+ }
19
+ }
20
+ }
21
+
22
+ parts.push(path.call(print, 'expression'));
23
+ parts.push(node.omitSemicolon ? '' : ';');
24
+
25
+ return parts;
26
+ }
6
27
  };
@@ -1,5 +1,8 @@
1
1
  import { doc } from 'prettier';
2
- import { printSeparatedItem } from '../common/printer-helpers.js';
2
+ import {
3
+ printComments,
4
+ printSeparatedItem
5
+ } from '../common/printer-helpers.js';
3
6
 
4
7
  const { group, hardline, indent, line } = doc.builders;
5
8
 
@@ -19,9 +22,10 @@ const printFalseBody = (node, path, print) =>
19
22
  ? [' ', path.call(print, 'falseBody')]
20
23
  : group(indent([line, path.call(print, 'falseBody')]));
21
24
 
22
- const printElse = (node, path, print) => {
25
+ const printElse = (node, path, print, commentsBetweenIfAndElse) => {
23
26
  if (node.falseBody) {
24
- const elseOnSameLine = node.trueBody.type === 'Block';
27
+ const elseOnSameLine =
28
+ node.trueBody.type === 'Block' && commentsBetweenIfAndElse.length === 0;
25
29
  return [
26
30
  elseOnSameLine ? ' ' : hardline,
27
31
  'else',
@@ -32,11 +36,22 @@ const printElse = (node, path, print) => {
32
36
  };
33
37
 
34
38
  export const IfStatement = {
35
- print: ({ node, path, print }) => [
36
- 'if (',
37
- printSeparatedItem(path.call(print, 'condition')),
38
- ')',
39
- printTrueBody(node, path, print),
40
- printElse(node, path, print)
41
- ]
39
+ print: ({ node, options, path, print }) => {
40
+ const comments = node.comments || [];
41
+ const commentsBetweenIfAndElse = comments.filter(
42
+ (comment) => !comment.leading && !comment.trailing
43
+ );
44
+
45
+ const parts = [];
46
+
47
+ parts.push('if (', printSeparatedItem(path.call(print, 'condition')), ')');
48
+ parts.push(printTrueBody(node, path, print));
49
+ if (commentsBetweenIfAndElse.length && node.falseBody) {
50
+ parts.push(hardline);
51
+ parts.push(printComments(node, path, options));
52
+ }
53
+ parts.push(printElse(node, path, print, commentsBetweenIfAndElse));
54
+
55
+ return parts;
56
+ }
42
57
  };
@@ -257,51 +257,21 @@ function handleIfStatementComments(
257
257
  precedingNode === enclosingNode.trueBody &&
258
258
  followingNode === enclosingNode.falseBody
259
259
  ) {
260
- if (followingNode.type === "Block") {
261
- if(followingNode.statements.length === 0) {
262
- addDanglingComment(followingNode, comment);
263
- } else {
264
- addLeadingComment(followingNode.statements[followingNode.statements.length - 1], comment);
265
- }
266
- } else if (followingNode.type === "IfStatement") {
267
- if (followingNode.trueBody.type === "Block") {
268
- if(followingNode.trueBody.statements.length === 0) {
269
- addDanglingComment(followingNode.trueBody, comment);
270
- } else {
271
- addLeadingComment(followingNode.trueBody.statements[0], comment);
272
- }
273
- } else {
274
- addLeadingComment(followingNode.trueBody, comment);
275
- }
260
+ if (precedingNode.type === "ExpressionStatement") {
261
+ addTrailingComment(precedingNode, comment);
276
262
  } else {
277
- addLeadingComment(precedingNode, comment);
263
+ addDanglingComment(enclosingNode, comment);
278
264
  }
279
265
  return true;
280
266
  }
281
267
 
282
- if (enclosingNode.trueBody === followingNode) {
283
- if (followingNode.type === "Block") {
284
- if(followingNode.statements.length === 0) {
285
- addDanglingComment(followingNode, comment);
286
- } else {
287
- addLeadingComment(followingNode.statements[0], comment);
288
- }
289
- } else {
290
- addLeadingComment(followingNode, comment);
291
- }
268
+ if (followingNode.type === "ExpressionStatement") {
269
+ addBlockStatementFirstComment(followingNode, comment);
292
270
  return true;
293
271
  }
294
272
 
295
273
  if (followingNode.type === "IfStatement") {
296
- if (followingNode.trueBody.type === "Block") {
297
- if(followingNode.trueBody.statements.length === 0) {
298
- addDanglingComment(followingNode.trueBody, comment);
299
- } else {
300
- addLeadingComment(followingNode.trueBody.statements[0], comment);
301
- }
302
- } else {
303
- addLeadingComment(followingNode.trueBody, comment);
304
- }
274
+ addBlockOrNotComment(followingNode.trueBody, comment);
305
275
  return true;
306
276
  }
307
277