@putout/printer 18.6.6 → 18.7.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,9 @@
1
+ 2026.03.14, v18.7.0
2
+
3
+ feature:
4
+ - b18dfef @putout/printer: SwitchStatement: space: improve support
5
+ - e5d54cd @putout/printer: SwitchStatement: simplify
6
+
1
7
  2026.03.14, v18.6.6
2
8
 
3
9
  feature:
@@ -1,18 +1,21 @@
1
+ import {types} from '@putout/babel';
1
2
  import {
2
3
  isNext,
3
4
  exists,
4
5
  isLast,
5
6
  } from '#is';
6
7
  import {parseLeadingComments} from '#comments';
8
+ import {createTypeChecker} from '#type-checker';
7
9
  import {maybeSpaceAfterKeyword} from '../return-statement/maybe-space-after-keyword.js';
8
10
 
11
+ const {isBlockStatement} = types;
12
+
9
13
  export const SwitchStatement = {
10
14
  print(path, printer, semantics) {
11
15
  const {
12
16
  print,
13
17
  maybe,
14
18
  indent,
15
- write,
16
19
  traverse,
17
20
  } = printer;
18
21
 
@@ -20,7 +23,9 @@ export const SwitchStatement = {
20
23
  print('switch');
21
24
  print('(');
22
25
  print('__discriminant');
23
- print(') {');
26
+ print(')');
27
+ print.space();
28
+ print('{');
24
29
  print.newline();
25
30
 
26
31
  const cases = path.get('cases');
@@ -28,45 +33,46 @@ export const SwitchStatement = {
28
33
 
29
34
  for (const [index, switchCase] of cases.entries()) {
30
35
  const test = switchCase.get('test');
36
+ const isLast = index === n;
31
37
 
32
38
  indent();
33
39
 
34
40
  parseLeadingComments(switchCase, printer, semantics);
35
41
 
36
42
  if (exists(test)) {
37
- write('case');
43
+ print('case');
38
44
  maybeSpaceAfterKeyword(test, printer, semantics);
39
45
  traverse(test);
40
46
  } else {
41
- write('default');
47
+ print('default');
42
48
  }
43
49
 
44
50
  print(':');
45
51
 
52
+ if (isNewlineAfterColon(switchCase))
53
+ print.newline();
54
+
46
55
  const consequents = switchCase.get('consequent');
47
- const isBlock = switchCase.get('consequent.0')?.isBlockStatement();
56
+ const [first] = consequents;
57
+ const isBlock = isBlockStatement(first);
48
58
 
49
59
  maybe.indent.inc(!isBlock);
50
- maybe.print.newline(!isBlock && consequents.length);
51
60
 
52
61
  for (const consequent of consequents) {
53
- if (!consequent.isBlockStatement()) {
54
- print(consequent);
55
- continue;
56
- }
62
+ if (isBlockStatement(consequent))
63
+ print.space();
57
64
 
58
- print.space();
59
65
  print(consequent);
60
66
  }
61
67
 
62
68
  maybe.indent.dec(!isBlock);
63
- maybe.write.linebreak(index < n);
69
+ maybe.print.linebreak(!isLast);
64
70
  }
65
71
 
66
72
  print.indent();
67
73
  print('}');
68
74
 
69
- if (!isNext(path) && !isLast(path))
75
+ if (isNewlineAfterClosingCurlyBrace(path))
70
76
  print.newline();
71
77
  },
72
78
  afterSatisfy: () => [
@@ -77,3 +83,13 @@ export const SwitchStatement = {
77
83
  print.newline();
78
84
  },
79
85
  };
86
+
87
+ const isNewlineAfterClosingCurlyBrace = createTypeChecker([
88
+ ['-', isNext],
89
+ ['+: -> !', isLast],
90
+ ]);
91
+
92
+ const isNewlineAfterColon = createTypeChecker([
93
+ ['-: node.consequent.length -> !', Boolean],
94
+ ['+: node.consequent.0 -> !BlockStatement'],
95
+ ]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "18.6.6",
3
+ "version": "18.7.0",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Simplest possible opinionated Babel AST printer for 🐊Putout",