@putout/printer 1.78.0 → 1.79.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.02, v1.79.1
2
+
3
+ fix:
4
+ - 1d1af3a @putout/printer: chaining
5
+
6
+ 2023.05.02, v1.79.0
7
+
8
+ feature:
9
+ - c2ff505 @putout/printer: ImportsDeclaration: improve
10
+
1
11
  2023.05.01, v1.78.0
2
12
 
3
13
  feature:
@@ -19,4 +19,3 @@ function shouldPrint(path) {
19
19
 
20
20
  return path.parentPath.isArrayPattern();
21
21
  }
22
-
@@ -4,9 +4,12 @@ const {
4
4
  isUnaryExpression,
5
5
  isArrowFunctionExpression,
6
6
  isLogicalExpression,
7
+ isIfStatement,
8
+
7
9
  } = require('@babel/types');
8
10
 
9
11
  const {chain} = require('./chain');
12
+ const {satisfy} = require('../../is');
10
13
 
11
14
  module.exports.MemberExpression = (path, {print, maybe, traverse}) => {
12
15
  const {computed} = path.node;
@@ -56,16 +59,17 @@ module.exports.OptionalMemberExpression = (path, {print}) => {
56
59
 
57
60
  const isCall = (a) => a.type === 'CallExpression';
58
61
 
62
+ const isExcludedFromChain = satisfy([
63
+ isUnaryExpression,
64
+ isArrowFunctionExpression,
65
+ isLogicalExpression,
66
+ isIfStatement,
67
+ ]);
68
+
59
69
  function likeChain(path) {
60
70
  const [root, properties] = chain(path);
61
71
 
62
- if (isUnaryExpression(root))
63
- return false;
64
-
65
- if (isArrowFunctionExpression(root))
66
- return false;
67
-
68
- if (isLogicalExpression(root))
72
+ if (isExcludedFromChain(root))
69
73
  return false;
70
74
 
71
75
  const calls = properties.filter(isCall);
@@ -1,7 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  const {isIdentifier} = require('@babel/types');
4
- const {isForOf, isCoupleLines} = require('../is');
4
+
5
+ const {
6
+ isForOf,
7
+ isCoupleLines,
8
+ } = require('../is');
5
9
 
6
10
  module.exports.ObjectPattern = (path, {indent, print, maybe}) => {
7
11
  indent.inc();
@@ -0,0 +1,98 @@
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.getNextSibling().isImportDeclaration())
90
+ return false;
91
+
92
+ return true;
93
+ },
94
+ after(path, {print}) {
95
+ print.newline();
96
+ markAfter(path);
97
+ },
98
+ };
@@ -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');
@@ -5,7 +5,6 @@ const {
5
5
  isCoupleLines,
6
6
  isNewlineBetweenSiblings,
7
7
  exists,
8
-
9
8
  } = require('../is');
10
9
 
11
10
  const {hasPrevNewline} = require('../mark');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.78.0",
3
+ "version": "1.79.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 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
-