@putout/printer 1.71.2 → 1.72.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,14 @@
1
+ 2023.04.26, v1.72.1
2
+
3
+ feature:
4
+ - d3c1dbe @putout/printer: improve support of ArrayPattern
5
+
6
+ 2023.04.26, v1.72.0
7
+
8
+ feature:
9
+ - e1ca06a @putout/printer: improve support of SwitchCase
10
+ - 82b3293 @putout/printer: improve ability to determine linebreak in ObjectMethod
11
+
1
12
  2023.04.25, v1.71.2
2
13
 
3
14
  feature:
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const {isNext} = require('./is');
4
+
4
5
  const {
5
6
  markBefore,
6
7
  isMarkedAfter,
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const isForOf = ({parentPath}) => parentPath.parentPath.parentPath.isForOfStatement();
3
+ const isForOf = ({parentPath}) => parentPath.parentPath.parentPath?.isForOfStatement();
4
4
 
5
5
  module.exports.ArrayPattern = (path, {indent, maybe, print}) => {
6
6
  print('[');
@@ -1,24 +1,34 @@
1
1
  'use strict';
2
2
 
3
- module.exports.ObjectMethod = (path, {print}) => {
4
- const {kind} = path.node;
5
-
6
- if (kind !== 'method')
7
- print(`${kind} `);
8
-
9
- print('__key');
10
- print('(');
11
-
12
- const params = path.get('params');
13
- const n = params.length - 1;
14
-
15
- for (let i = 0; i <= n; i++) {
16
- print(params[i]);
3
+ const {isNewlineBetweenSiblings} = require('../../is');
4
+ module.exports.ObjectMethod = {
5
+ print(path, {print}) {
6
+ const {kind} = path.node;
17
7
 
18
- if (i < n)
19
- print(', ');
20
- }
21
-
22
- print(') ');
23
- print('__body');
8
+ if (kind !== 'method')
9
+ print(`${kind} `);
10
+
11
+ print('__key');
12
+ print('(');
13
+
14
+ const params = path.get('params');
15
+ const n = params.length - 1;
16
+
17
+ for (let i = 0; i <= n; i++) {
18
+ print(params[i]);
19
+
20
+ if (i < n)
21
+ print(', ');
22
+ }
23
+
24
+ print(') ');
25
+ print('__body');
26
+ },
27
+ afterIf(path) {
28
+ return isNewlineBetweenSiblings(path);
29
+ },
30
+ after(path, {print}) {
31
+ print.linebreak();
32
+ },
24
33
  };
34
+
@@ -5,6 +5,8 @@ const {
5
5
  isNextParent,
6
6
  } = require('../is');
7
7
 
8
+ const insideCase = (path) => path.parentPath.isSwitchCase();
9
+
8
10
  module.exports.BreakStatement = {
9
11
  split(path, {print}) {
10
12
  print.newline();
@@ -20,6 +22,9 @@ module.exports.BreakStatement = {
20
22
  if (isNextParent(path))
21
23
  return true;
22
24
 
25
+ if (insideCase(path))
26
+ return true;
27
+
23
28
  return false;
24
29
  },
25
30
  };
@@ -42,4 +42,3 @@ module.exports.ForStatement = {
42
42
  print.linebreak();
43
43
  },
44
44
  };
45
-
@@ -14,8 +14,9 @@ module.exports.IfStatement = {
14
14
 
15
15
  const consequent = path.get('consequent');
16
16
  const alternate = path.get('alternate');
17
+ const isConsequentBlock = consequent.isBlockStatement();
17
18
 
18
- if (consequent.isBlockStatement()) {
19
+ if (isConsequentBlock) {
19
20
  print(' ');
20
21
  print(consequent);
21
22
  } else {
@@ -35,6 +36,7 @@ module.exports.IfStatement = {
35
36
  write.space();
36
37
  traverse(alternate);
37
38
  } else if (exists(alternate)) {
39
+ maybe.indent(!isConsequentBlock);
38
40
  write('else');
39
41
  print.newline();
40
42
  indent.inc();
@@ -1,42 +1,54 @@
1
1
  'use strict';
2
2
 
3
- module.exports.SwitchStatement = (path, {print, maybe}) => {
4
- print('switch');
5
- print.space();
6
- print('(');
7
- print('__discriminant');
8
- print(') {');
9
- print.newline();
10
-
11
- const cases = path.get('cases');
12
- const n = cases.length - 1;
13
-
14
- for (const [index, switchCase] of cases.entries()) {
15
- print('case');
3
+ const {isNext} = require('../is');
4
+
5
+ module.exports.SwitchStatement = {
6
+ print(path, {print, maybe}) {
7
+ print('switch');
16
8
  print.space();
17
- print(switchCase.get('test'));
18
- print(':');
9
+ print('(');
10
+ print('__discriminant');
11
+ print(') {');
12
+ print.newline();
19
13
 
20
- const isBlock = switchCase.get('consequent.0').isBlockStatement();
21
- maybe.indent.inc(!isBlock);
22
- maybe.print.newline(!isBlock);
14
+ const cases = path.get('cases');
15
+ const n = cases.length - 1;
23
16
 
24
- for (const consequent of switchCase.get('consequent')) {
25
- if (!consequent.isBlockStatement()) {
17
+ for (const [index, switchCase] of cases.entries()) {
18
+ print('case');
19
+ print.space();
20
+ print(switchCase.get('test'));
21
+ print(':');
22
+
23
+ const isBlock = switchCase.get('consequent.0').isBlockStatement();
24
+
25
+ maybe.indent.inc(!isBlock);
26
+ maybe.print.newline(!isBlock);
27
+
28
+ for (const consequent of switchCase.get('consequent')) {
29
+ if (!consequent.isBlockStatement()) {
30
+ print(consequent);
31
+ continue;
32
+ }
33
+
34
+ print.space();
26
35
  print(consequent);
27
- continue;
28
36
  }
29
37
 
30
- print.space();
31
- print(consequent);
32
- }
33
-
34
- if (index < n) {
35
- print.indent();
38
+ if (index < n) {
39
+ print.linebreak();
40
+ }
41
+
36
42
  maybe.indent.dec(!isBlock);
37
- print.newline();
38
43
  }
39
- }
40
-
41
- print('}');
44
+
45
+ print('}');
46
+ },
47
+ afterSatisfy: () => [
48
+ isNext,
49
+ ],
50
+ after(path, {print}) {
51
+ print.breakline();
52
+ print.breakline();
53
+ },
42
54
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.71.2",
3
+ "version": "1.72.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",