@putout/printer 1.100.0 → 1.101.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/ChangeLog CHANGED
@@ -1,3 +1,13 @@
1
+ 2023.05.18, v1.101.1
2
+
3
+ fix:
4
+ - 4aa709b @putout/printer: FunctionExpression: space
5
+
6
+ 2023.05.18, v1.101.0
7
+
8
+ feature:
9
+ - d0a81ad @putout/printer: improve support of comments
10
+
1
11
  2023.05.17, v1.100.0
2
12
 
3
13
  feature:
package/README.md CHANGED
@@ -89,6 +89,7 @@ print(ast, {
89
89
  },
90
90
  },
91
91
  });
92
+
92
93
  // returns
93
94
  'const {a/* [hello world] */= 5} = b;\n';
94
95
  ```
@@ -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))
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ const {exists} = require('../../is');
3
4
  module.exports.FunctionExpression = (path, {print, maybe, write, traverse}) => {
4
5
  const {node} = path;
5
6
 
@@ -14,8 +15,8 @@ module.exports.FunctionExpression = (path, {print, maybe, write, traverse}) => {
14
15
 
15
16
  const id = path.get('id');
16
17
 
17
- if (id.node) {
18
- write.space();
18
+ if (exists(id)) {
19
+ write(' ');
19
20
  traverse(id);
20
21
  }
21
22
 
@@ -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
  },
@@ -213,7 +213,6 @@ module.exports.tokenize = (ast, overrides = {}) => {
213
213
 
214
214
  const currentIndent = i;
215
215
  parseLeadingComments(path, printer, format);
216
-
217
216
  // this is main thing
218
217
  maybePlugin(currentTraverse, path, printer);
219
218
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.100.0",
3
+ "version": "1.101.1",
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"