@putout/printer 1.76.1 → 1.78.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,13 @@
1
+ 2023.05.01, v1.78.0
2
+
3
+ feature:
4
+ - ebd6259 @putout/printer: improve support of AssignmentPattern
5
+
6
+ 2023.05.01, v1.77.0
7
+
8
+ feature:
9
+ - 3e016f5 @putout/printer: improve support of ImportDeclaration
10
+
1
11
  2023.05.01, v1.76.1
2
12
 
3
13
  feature:
package/README.md CHANGED
@@ -81,13 +81,13 @@ print(ast, {
81
81
  },
82
82
  visitors: {
83
83
  AssignmentPattern(path, {print}) {
84
- print(' /* [hello world] */= ');
84
+ print('/* [hello world] */= ');
85
85
  print('__right');
86
86
  },
87
87
  },
88
88
  });
89
89
  // returns
90
- 'const {a /* [hello world] */= 5} = b;\n';
90
+ 'const {/* [hello world] */= 5} = b;\n';
91
91
  ```
92
92
 
93
93
  ### `print`
@@ -103,7 +103,7 @@ print(ast, {
103
103
  visitors: {
104
104
  AssignmentPattern(path, {print, maybe}) {
105
105
  maybe.print.newline(path.parentPath.isCallExpression());
106
- print(' /* [hello world] */= ');
106
+ print('/* [hello world] */= ');
107
107
  print('__right');
108
108
  },
109
109
  },
@@ -2,21 +2,21 @@
2
2
 
3
3
  const isArg = (path) => path.parentPath.isFunction();
4
4
 
5
- module.exports.AssignmentPattern = (path, {print, maybe}) => {
6
- maybe.print(shouldPrint(path), '__left');
7
- print(' = ');
8
- print('__right');
5
+ module.exports.AssignmentPattern = {
6
+ print(path, {print, maybe}) {
7
+ maybe.print(shouldPrint(path), '__left');
8
+ print(' = ');
9
+ print('__right');
10
+ },
9
11
  };
10
12
 
11
13
  function shouldPrint(path) {
12
- if (path.parentPath.isObjectProperty() && !path.parentPath.node.shorthand)
14
+ if (path.parentPath.isObjectProperty())
13
15
  return true;
14
16
 
15
17
  if (isArg(path))
16
18
  return true;
17
19
 
18
- if (path.parentPath.isArrayPattern())
19
- return true;
20
-
21
- return false;
20
+ return path.parentPath.isArrayPattern();
22
21
  }
22
+
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const {isIdentifier} = require('@babel/types');
4
- const {isForOf} = require('../is');
4
+ const {isForOf, isCoupleLines} = require('../is');
5
5
 
6
6
  module.exports.ObjectPattern = (path, {indent, print, maybe}) => {
7
7
  indent.inc();
@@ -25,13 +25,19 @@ module.exports.ObjectPattern = (path, {indent, print, maybe}) => {
25
25
  const {shorthand} = property.node;
26
26
 
27
27
  maybe.indent(is);
28
- print(keyPath);
28
+ maybe.print(!isAssign || !shorthand, keyPath);
29
29
 
30
30
  if (!shorthand) {
31
31
  print(': ');
32
32
  print(valuePath);
33
- } else if (isAssign)
33
+ } else if (isAssign) {
34
+ const couple = isCoupleLines(valuePath);
35
+ maybe.print.breakline(couple);
34
36
  print(valuePath);
37
+
38
+ maybe.print(couple, ',');
39
+ maybe.print.newline(couple);
40
+ }
35
41
 
36
42
  if (is) {
37
43
  print(',');
@@ -4,7 +4,7 @@ const {markAfter} = require('../mark');
4
4
  const {isLast} = require('../is');
5
5
 
6
6
  module.exports.ImportDeclaration = {
7
- print(path, {print, maybe}) {
7
+ print(path, {print, maybe, write, traverse, indent}) {
8
8
  const isType = path.node.importKind === 'type';
9
9
  const specifiers = path.get('specifiers');
10
10
 
@@ -16,7 +16,9 @@ module.exports.ImportDeclaration = {
16
16
 
17
17
  for (const [index, spec] of specifiers.entries()) {
18
18
  if (spec.isImportDefaultSpecifier()) {
19
- print(spec.get('local'));
19
+ traverse(spec.get('local'));
20
+ maybe.write(n, ',');
21
+ maybe.write.space(n);
20
22
  continue;
21
23
  }
22
24
 
@@ -31,18 +33,23 @@ module.exports.ImportDeclaration = {
31
33
  local,
32
34
  } = spec.node;
33
35
 
34
- maybe.print(index, ', ');
35
- maybe.print(!wasSpecifier, '{');
36
+ indent.inc();
37
+
38
+ maybe.write(!wasSpecifier, '{');
39
+ maybe.write.breakline(n);
36
40
 
37
41
  wasSpecifier = true;
38
- print(imported.name);
42
+ write(imported.name);
39
43
 
40
44
  if (imported.name !== local.name) {
41
- print(' as ');
42
- print(spec.node.local.name);
45
+ write(' as ');
46
+ write(spec.node.local.name);
43
47
  }
44
48
 
45
- maybe.print(index === n, '}');
49
+ maybe.write(n, ',');
50
+ indent.dec();
51
+ maybe.write.newline(n && index === n);
52
+ maybe.write(index === n, '}');
46
53
 
47
54
  continue;
48
55
  }
@@ -76,3 +83,4 @@ module.exports.ImportDeclaration = {
76
83
  markAfter(path);
77
84
  },
78
85
  };
86
+
@@ -4,6 +4,8 @@ const {
4
4
  isNext,
5
5
  isCoupleLines,
6
6
  isNewlineBetweenSiblings,
7
+ exists,
8
+
7
9
  } = require('../is');
8
10
 
9
11
  const {hasPrevNewline} = require('../mark');
@@ -23,7 +25,7 @@ module.exports.VariableDeclaration = {
23
25
  print('__declarations.0.id');
24
26
 
25
27
  const initPath = path.get('declarations.0.init');
26
- maybe.print(initPath.node, ' = ');
28
+ maybe.print(exists(initPath), ' = ');
27
29
  print('__declarations.0.init');
28
30
  maybe.print(isParentBlock(path), ';');
29
31
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.76.1",
3
+ "version": "1.78.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 fro 🐊Putout",