@putout/printer 1.77.0 → 1.79.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.02, v1.79.0
2
+
3
+ feature:
4
+ - c2ff505 @putout/printer: ImportsDeclaration: improve
5
+
6
+ 2023.05.01, v1.78.0
7
+
8
+ feature:
9
+ - ebd6259 @putout/printer: improve support of AssignmentPattern
10
+
1
11
  2023.05.01, v1.77.0
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,20 @@
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
  }
@@ -1,7 +1,10 @@
1
1
  'use strict';
2
2
 
3
3
  const {isIdentifier} = require('@babel/types');
4
- const {isForOf} = require('../is');
4
+ const {
5
+ isForOf,
6
+ isCoupleLines,
7
+ } = require('../is');
5
8
 
6
9
  module.exports.ObjectPattern = (path, {indent, print, maybe}) => {
7
10
  indent.inc();
@@ -25,13 +28,19 @@ module.exports.ObjectPattern = (path, {indent, print, maybe}) => {
25
28
  const {shorthand} = property.node;
26
29
 
27
30
  maybe.indent(is);
28
- print(keyPath);
31
+ maybe.print(!isAssign || !shorthand, keyPath);
29
32
 
30
33
  if (!shorthand) {
31
34
  print(': ');
32
35
  print(valuePath);
33
- } else if (isAssign)
36
+ } else if (isAssign) {
37
+ const couple = isCoupleLines(valuePath);
38
+ maybe.print.breakline(couple);
34
39
  print(valuePath);
40
+
41
+ maybe.print(couple, ',');
42
+ maybe.print.newline(couple);
43
+ }
35
44
 
36
45
  if (is) {
37
46
  print(',');
@@ -0,0 +1,100 @@
1
+ 'use strict';
2
+
3
+ const {markAfter} = require('../../mark');
4
+ const {isLast} = require('../../is');
5
+ const {parseSpecifiers} = require('./parse-specifiers');
6
+
7
+ module.exports.ImportDeclaration = {
8
+ print(path, {print, maybe, write, traverse, indent}) {
9
+ const isType = path.node.importKind === 'type';
10
+ const specifiers = path.get('specifiers');
11
+
12
+ print('import ');
13
+ maybe.print(isType, 'type ');
14
+
15
+ let wasSpecifier = false;
16
+ const n = specifiers.length - 1;
17
+
18
+ const {
19
+ defaults,
20
+ namespaces,
21
+ imports,
22
+ } = parseSpecifiers(specifiers);
23
+
24
+ for (const spec of defaults) {
25
+ traverse(spec.get('local'));
26
+ maybe.write(n, ',');
27
+ maybe.write.space(n);
28
+ }
29
+
30
+ for (const spec of namespaces) {
31
+ print('* as ');
32
+ print(spec.get('local'));
33
+ }
34
+
35
+ const importsCount = imports.length - 1;
36
+
37
+ for (const [index, spec] of imports.entries()) {
38
+ const last = index === importsCount;
39
+ const notLast = !last;
40
+
41
+ const {
42
+ imported,
43
+ local,
44
+ } = spec.node;
45
+
46
+ indent.inc();
47
+
48
+ maybe.write(!wasSpecifier, '{');
49
+ maybe.write.breakline(importsCount > 2);
50
+
51
+ wasSpecifier = true;
52
+ write(imported.name);
53
+
54
+ if (imported.name !== local.name) {
55
+ write(' as ');
56
+ write(spec.node.local.name);
57
+ }
58
+
59
+ if (importsCount < 2 && notLast) {
60
+ maybe.write(n, ',');
61
+ maybe.write.space(n);
62
+ }
63
+
64
+ if (importsCount > 2) {
65
+ maybe.write(n, ',');
66
+ maybe.write.newline(index === n);
67
+ }
68
+
69
+ indent.dec();
70
+ maybe.write(last, '}');
71
+ }
72
+
73
+ if (specifiers.length) {
74
+ print.space();
75
+ print('from');
76
+ print.space();
77
+ }
78
+
79
+ print('__source');
80
+ print(';');
81
+
82
+ if (!isLast(path))
83
+ print.newline();
84
+ },
85
+ afterIf(path) {
86
+ if (isLast(path))
87
+ return false;
88
+
89
+ if (path
90
+ .getNextSibling()
91
+ .isImportDeclaration())
92
+ return false;
93
+
94
+ return true;
95
+ },
96
+ after(path, {print}) {
97
+ print.newline();
98
+ markAfter(path);
99
+ },
100
+ };
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+
3
+ module.exports.parseSpecifiers = (specifiers) => {
4
+ const defaults = [];
5
+ const namespaces = [];
6
+ const imports = [];
7
+
8
+ for (const spec of specifiers) {
9
+ if (spec.isImportDefaultSpecifier()) {
10
+ defaults.push(spec);
11
+ continue;
12
+ }
13
+
14
+ if (spec.isImportNamespaceSpecifier()) {
15
+ namespaces.push(spec);
16
+ continue;
17
+ }
18
+
19
+ if (spec.isImportSpecifier()) {
20
+ imports.push(spec);
21
+ continue;
22
+ }
23
+ }
24
+
25
+ return {
26
+ defaults,
27
+ namespaces,
28
+ imports,
29
+ };
30
+ };
@@ -9,7 +9,7 @@ const {ReturnStatement} = require('./return-statement');
9
9
  const TryStatements = require('./try-statements');
10
10
  const {DebuggerStatement} = require('./debugger-statement');
11
11
  const {ForStatement} = require('./for-statement');
12
- const importDeclarations = require('./import-declaration');
12
+ const importDeclarations = require('./import-declaration/import-declaration');
13
13
  const exportDeclarations = require('./export-declarations');
14
14
  const {WhileStatement} = require('./while-statement');
15
15
  const {SwitchStatement} = require('./switch-statement');
@@ -4,6 +4,7 @@ const {
4
4
  isNext,
5
5
  isCoupleLines,
6
6
  isNewlineBetweenSiblings,
7
+ exists,
7
8
  } = require('../is');
8
9
 
9
10
  const {hasPrevNewline} = require('../mark');
@@ -23,7 +24,7 @@ module.exports.VariableDeclaration = {
23
24
  print('__declarations.0.id');
24
25
 
25
26
  const initPath = path.get('declarations.0.init');
26
- maybe.print(initPath.node, ' = ');
27
+ maybe.print(exists(initPath), ' = ');
27
28
  print('__declarations.0.init');
28
29
  maybe.print(isParentBlock(path), ';');
29
30
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.77.0",
3
+ "version": "1.79.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",
@@ -1,86 +0,0 @@
1
- 'use strict';
2
-
3
- const {markAfter} = require('../mark');
4
- const {isLast} = require('../is');
5
-
6
- module.exports.ImportDeclaration = {
7
- print(path, {print, maybe, write, traverse, indent}) {
8
- const isType = path.node.importKind === 'type';
9
- const specifiers = path.get('specifiers');
10
-
11
- print('import ');
12
- maybe.print(isType, 'type ');
13
-
14
- let wasSpecifier = false;
15
- const n = specifiers.length - 1;
16
-
17
- for (const [index, spec] of specifiers.entries()) {
18
- if (spec.isImportDefaultSpecifier()) {
19
- traverse(spec.get('local'));
20
- maybe.write(n, ',');
21
- maybe.write.space(n);
22
- continue;
23
- }
24
-
25
- if (spec.isImportNamespaceSpecifier()) {
26
- print('* as ');
27
- print(spec.get('local'));
28
- }
29
-
30
- if (spec.isImportSpecifier()) {
31
- const {
32
- imported,
33
- local,
34
- } = spec.node;
35
-
36
- indent.inc();
37
-
38
- maybe.write(!wasSpecifier, '{');
39
- maybe.write.breakline(n);
40
-
41
- wasSpecifier = true;
42
- write(imported.name);
43
-
44
- if (imported.name !== local.name) {
45
- write(' as ');
46
- write(spec.node.local.name);
47
- }
48
-
49
- maybe.write(n, ',');
50
- indent.dec();
51
- maybe.write.newline(n && index === n);
52
- maybe.write(index === n, '}');
53
-
54
- continue;
55
- }
56
- }
57
-
58
- if (specifiers.length) {
59
- print.space();
60
- print('from');
61
- print.space();
62
- }
63
-
64
- print('__source');
65
- print(';');
66
-
67
- if (!isLast(path))
68
- print.newline();
69
- },
70
- afterIf(path) {
71
- if (isLast(path))
72
- return false;
73
-
74
- if (path
75
- .getNextSibling()
76
- .isImportDeclaration())
77
- return false;
78
-
79
- return true;
80
- },
81
- after(path, {print}) {
82
- print.newline();
83
- markAfter(path);
84
- },
85
- };
86
-