@putout/printer 1.71.1 → 1.72.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,14 @@
1
+ 2023.04.26, v1.72.0
2
+
3
+ feature:
4
+ - e1ca06a @putout/printer: improve support of SwitchCase
5
+ - 82b3293 @putout/printer: improve ability to determine linebreak in ObjectMethod
6
+
7
+ 2023.04.25, v1.71.2
8
+
9
+ feature:
10
+ - 9922208 @putout/printer: improve support of ForStatements newline
11
+
1
12
  2023.04.25, v1.71.1
2
13
 
3
14
  feature:
@@ -1,7 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  const {isNext} = require('./is');
4
- const {markBefore, isMarkedAfter} = require('./mark');
4
+
5
+ const {
6
+ markBefore,
7
+ isMarkedAfter,
8
+ } = require('./mark');
5
9
 
6
10
  module.exports.parseLeadingComments = (path, {print, maybe, indent}) => {
7
11
  const {leadingComments} = path.node;
@@ -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
+
@@ -11,6 +11,7 @@ const {
11
11
  isArrowFunctionExpression,
12
12
  isObjectMethod,
13
13
  } = require('@babel/types');
14
+
14
15
  const {markAfter} = require('../mark');
15
16
 
16
17
  const isFirstStatement = (path) => path.get('body.0')?.isStatement();
@@ -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
  };
@@ -3,13 +3,14 @@
3
3
  const {isLast} = require('../is');
4
4
 
5
5
  module.exports.ForStatement = {
6
- print(path, {print, maybe}) {
6
+ print(path, {print, maybe, indent}) {
7
7
  const {
8
8
  test,
9
9
  update,
10
10
  body,
11
11
  } = path.node;
12
12
 
13
+ indent();
13
14
  print('for (');
14
15
  print('__init');
15
16
  print(';');
@@ -31,13 +32,13 @@ module.exports.ForStatement = {
31
32
  maybe.indent.dec(is);
32
33
  }
33
34
  },
34
- after(path, {print}) {
35
- print.newline();
36
- },
37
35
  afterIf(path) {
38
36
  if (isLast(path))
39
37
  return false;
40
38
 
41
39
  return true;
42
40
  },
41
+ after(path, {print}) {
42
+ print.linebreak();
43
+ },
43
44
  };
@@ -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();
@@ -27,6 +27,7 @@ module.exports.ImportDeclaration = {
27
27
  imported,
28
28
  local,
29
29
  } = spec.node;
30
+
30
31
  maybe.print(index, ', ');
31
32
  maybe.print(!wasSpecifier, '{');
32
33
 
@@ -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.1",
3
+ "version": "1.72.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",