@putout/printer 1.100.0 → 1.101.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/ChangeLog CHANGED
@@ -1,3 +1,8 @@
1
+ 2023.05.18, v1.101.0
2
+
3
+ feature:
4
+ - d0a81ad @putout/printer: improve support of comments
5
+
1
6
  2023.05.17, v1.100.0
2
7
 
3
8
  feature:
@@ -1,6 +1,9 @@
1
1
  'use strict';
2
2
 
3
- const {hasTrailingComment} = require('./is');
3
+ const {
4
+ hasTrailingComment,
5
+ isLast,
6
+ } = require('./is');
4
7
 
5
8
  const {markBefore} = require('./mark');
6
9
 
@@ -68,7 +71,7 @@ module.exports.parseTrailingComments = (path, {write, maybe}, format) => {
68
71
  maybe.indent(!sameLine);
69
72
 
70
73
  write(`//${value}`);
71
- write.newline();
74
+ maybe.write.newline(!isLast(path));
72
75
  continue;
73
76
  }
74
77
 
@@ -10,6 +10,8 @@ const {
10
10
  isCallExpression,
11
11
  } = require('@babel/types');
12
12
 
13
+ const {isSimple} = require('@putout/operate');
14
+
13
15
  const {
14
16
  isCoupleLines,
15
17
  isStringAndIdentifier,
@@ -21,7 +23,7 @@ const isStringAndArray = ([a, b]) => isStringLiteral(a) && isArrayExpression(b);
21
23
  const isStringAndString = ([a, b]) => isStringLiteral(a) && isStringLiteral(b);
22
24
  const isIdentifierAndIdentifier = ([a, b]) => isIdentifier(a) && isIdentifier(b);
23
25
  const isInsideArray = (path) => path.parentPath.isArrayExpression();
24
- const isStringAndCall = ([a, b]) => isStringLiteral(a) && isCallExpression(b);
26
+ const isSimpleAndCall = ([a, b]) => isSimple(a) && isCallExpression(b);
25
27
 
26
28
  const isTwoLongStrings = ([a, b]) => {
27
29
  const LONG_STRING = 20;
@@ -221,7 +223,7 @@ function isNewlineBetweenElements(path, {elements}) {
221
223
  if (isInsideLoop(path))
222
224
  return false;
223
225
 
224
- if (isStringAndCall(elements))
226
+ if (isSimpleAndCall(elements))
225
227
  return false;
226
228
 
227
229
  if (isTwoStringsDifferentLength(elements))
@@ -9,7 +9,7 @@ const {
9
9
  satisfy,
10
10
  noTrailingComment,
11
11
  hasTrailingComment,
12
- exists,
12
+ isCoupleLines,
13
13
  } = require('../is');
14
14
 
15
15
  const satisfyAfter = satisfy([
@@ -19,23 +19,6 @@ const satisfyAfter = satisfy([
19
19
  isNextUp,
20
20
  ]);
21
21
 
22
- const isNextLiteral = (path) => {
23
- const next = path.getNextSibling();
24
-
25
- if (!exists(next))
26
- return false;
27
-
28
- const expression = next.get('expression');
29
-
30
- if (expression.isTemplateLiteral())
31
- return true;
32
-
33
- if (expression.isArrayExpression())
34
- return true;
35
-
36
- return expression.isLiteral();
37
- };
38
-
39
22
  const shouldBreakline = satisfy([
40
23
  isNewlineBetweenSiblings,
41
24
  isNotLastBody,
@@ -55,9 +38,6 @@ module.exports.ExpressionStatement = {
55
38
  }
56
39
  },
57
40
  afterIf: (path) => {
58
- if (hasTrailingComment(path) && isNextLiteral(path))
59
- return false;
60
-
61
41
  if (satisfyAfter(path))
62
42
  return true;
63
43
 
@@ -67,10 +47,14 @@ module.exports.ExpressionStatement = {
67
47
  return false;
68
48
  },
69
49
  after(path, {print, maybe, store}) {
70
- if (hasTrailingComment(path) && isLast(path)) {
50
+ if (hasTrailingComment(path) && isLast(path) && isCoupleLines(path)) {
71
51
  print.breakline();
72
52
  }
73
53
 
54
+ if (hasTrailingComment(path) && !isCoupleLines(path)) {
55
+ return;
56
+ }
57
+
74
58
  print.newline();
75
59
  maybe.markAfter(store(), path);
76
60
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.100.0",
3
+ "version": "1.101.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Simplest possible opinionated Babel AST printer for 🐊Putout",
@@ -31,6 +31,7 @@
31
31
  "@babel/traverse": "^7.21.2",
32
32
  "@babel/types": "^7.21.3",
33
33
  "@putout/compare": "^9.13.0",
34
+ "@putout/operate": "^8.11.0",
34
35
  "fullstore": "^3.0.0",
35
36
  "just-snake-case": "^3.2.0",
36
37
  "rendy": "^3.1.1"