@putout/printer 18.6.5 → 18.6.6

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,10 @@
1
+ 2026.03.14, v18.6.6
2
+
3
+ feature:
4
+ - c4f79e0 2putout/printer: VariableDeclaration: conditions
5
+ - 42ea012 @putout/printer: VariableDeclaration: simplify
6
+ - 1d079db @putout/printer: VariableDeclaration: afterIf: skipAfter
7
+
1
8
  2026.03.14, v18.6.5
2
9
 
3
10
  feature:
@@ -10,6 +10,7 @@ import {
10
10
  isNext,
11
11
  callWithPrev,
12
12
  } from '#is';
13
+ import {isNewlineAfterSemicolon} from './is.js';
13
14
 
14
15
  const {
15
16
  isIfStatement,
@@ -67,7 +68,13 @@ const isNextAssign = createTypeChecker([
67
68
  ['+', callWithNext(callWithExpression(isAssignmentExpression))],
68
69
  ]);
69
70
 
71
+ const skipAfter = createTypeChecker([
72
+ ['-: parentPath -> !', isLast],
73
+ ['+: -> !', isInsideBlock],
74
+ ]);
75
+
70
76
  export const afterIf = createTypeChecker([
77
+ ['-', skipAfter],
71
78
  ['+', callWithNext(isIfStatement)],
72
79
  ['+', callWithNext(isFunctionDeclaration)],
73
80
  ['+', noNextParentBlock],
@@ -79,3 +86,8 @@ export const afterIf = createTypeChecker([
79
86
  ['+: parentPath -> ExportNamedDeclaration'],
80
87
  ['+: parentPath -> TSModuleBlock'],
81
88
  ]);
89
+
90
+ export const isIndentAfterNewline = createTypeChecker([
91
+ ['-: -> !', isNewlineAfterSemicolon],
92
+ ['+', afterIf],
93
+ ]);
@@ -0,0 +1,32 @@
1
+ import {createTypeChecker} from '#type-checker';
2
+ import {
3
+ callWithParent,
4
+ isNewlineBetweenSiblings,
5
+ isNext,
6
+ noTrailingComment,
7
+ } from '#is';
8
+
9
+ export const isInsideParentLike = callWithParent(createTypeChecker([
10
+ 'Program',
11
+ 'BlockStatement',
12
+ 'ExportNamedDeclaration',
13
+ 'LabeledStatement',
14
+ ]));
15
+
16
+ export const isNewlineAfterSemicolon = createTypeChecker([
17
+ ['+: parentPath -> SwitchCase'],
18
+ ['-: -> !', isInsideParentLike],
19
+ ['-: -> !', isNext],
20
+ ['+', noTrailingComment],
21
+ ['+', isNewlineBetweenSiblings],
22
+ ]);
23
+
24
+ export const isNeedBreaklineAfterComma = (path, {maxVariablesInOneLine}) => {
25
+ const {length} = path.node.declarations;
26
+ return length > maxVariablesInOneLine;
27
+ };
28
+
29
+ export const isNeedSpaceAfterComma = (path, {maxVariablesInOneLine}) => {
30
+ const {length} = path.node.declarations;
31
+ return length <= maxVariablesInOneLine;
32
+ };
@@ -2,25 +2,32 @@ import {createTypeChecker} from '#type-checker';
2
2
  import {isConcatenation} from '#is-concatenation';
3
3
  import {maybeDeclare} from '#maybe-declare';
4
4
  import {parseLeadingComments} from '#comments';
5
+ import {markAfter} from '#mark';
5
6
  import {
6
- isNext,
7
- isNewlineBetweenSiblings,
8
7
  exists,
9
- noTrailingComment,
10
8
  isInsideIf,
11
9
  isInsideBlock,
12
10
  isInsideTSModuleBlock,
13
11
  isInsideProgram,
14
12
  isInsideSwitchCase,
15
13
  isInsideBody,
16
- callWithParent,
17
- isLast,
14
+ hasLeadingComment,
18
15
  } from '#is';
19
- import {afterIf} from './after-if.js';
16
+ import {
17
+ afterIf,
18
+ isIndentAfterNewline,
19
+ } from './after-if.js';
20
20
  import {maybeSpaceAfterKeyword} from './maybe-space-after-keyword.js';
21
21
  import {beforeIf} from './before-if.js';
22
+ import {
23
+ isInsideParentLike,
24
+ isNeedBreaklineAfterComma,
25
+ isNeedSpaceAfterComma,
26
+ isNewlineAfterSemicolon,
27
+ } from './is.js';
22
28
 
23
29
  const isParentTSModuleBlock = (path) => path.parentPath.isTSModuleBlock();
30
+
24
31
  const isInsideBlockLike = createTypeChecker([
25
32
  isInsideProgram,
26
33
  isInsideBlock,
@@ -29,29 +36,12 @@ const isInsideBlockLike = createTypeChecker([
29
36
  isInsideBody,
30
37
  ]);
31
38
 
32
- const isParentSwitchCase = (path) => path.parentPath.isSwitchCase();
33
-
34
- const isInsideParentLike = callWithParent(createTypeChecker([
35
- 'Program',
36
- 'BlockStatement',
37
- 'ExportNamedDeclaration',
38
- 'LabeledStatement',
39
- ]));
40
-
41
39
  const isNeedSemicolon = createTypeChecker([
42
- isInsideParentLike,
43
- isParentSwitchCase,
44
- isParentTSModuleBlock,
45
- isInsideIf,
46
- isInsideBody,
47
- ]);
48
-
49
- const isNeedNewline = createTypeChecker([
50
- ['+', isParentSwitchCase],
51
- ['-: -> !', isInsideParentLike],
52
- ['-: -> !', isNext],
53
- ['+', noTrailingComment],
54
- ['+', isNewlineBetweenSiblings],
40
+ ['+', isInsideParentLike],
41
+ ['+: parentPath -> SwitchCase'],
42
+ ['+', isParentTSModuleBlock],
43
+ ['+', isInsideIf],
44
+ ['+', isInsideBody],
55
45
  ]);
56
46
 
57
47
  export const VariableDeclaration = {
@@ -59,12 +49,21 @@ export const VariableDeclaration = {
59
49
  before(path, {print}) {
60
50
  print.breakline();
61
51
  },
62
- print: maybeDeclare((path, {maybe, store, write, traverse, print, indent}, semantics) => {
52
+ print: maybeDeclare((path, printer, semantics) => {
53
+ const {kind} = path.node;
54
+ const {
55
+ maybe,
56
+ write,
57
+ traverse,
58
+ indent,
59
+ } = printer;
60
+
63
61
  const {maxVariablesInOneLine} = semantics;
64
62
 
65
- maybe.indent(isInsideBlockLike(path));
63
+ if (isInsideBlockLike(path))
64
+ indent();
66
65
 
67
- write(path.node.kind);
66
+ write(kind);
68
67
  maybeSpaceAfterKeyword(path, {
69
68
  write,
70
69
  });
@@ -88,47 +87,42 @@ export const VariableDeclaration = {
88
87
  traverse(init);
89
88
  }
90
89
 
91
- if (!isLast) {
92
- const next = declarations[index + 1];
93
-
94
- write(',');
95
-
96
- if (!next.node.leadingComments) {
97
- maybe.write.breakline(n > maxVariablesInOneLine);
98
- maybe.write.space(n <= maxVariablesInOneLine);
99
- continue;
100
- }
101
-
102
- parseLeadingComments(next, {print, maybe, indent}, semantics);
90
+ if (isLast)
91
+ continue;
92
+
93
+ const next = declarations[index + 1];
94
+
95
+ write(',');
96
+
97
+ if (hasLeadingComment(next)) {
98
+ parseLeadingComments(next, printer, semantics);
99
+ continue;
103
100
  }
101
+
102
+ if (isNeedBreaklineAfterComma(path, {maxVariablesInOneLine})) {
103
+ write.breakline();
104
+ continue;
105
+ }
106
+
107
+ if (isNeedSpaceAfterComma(path, {maxVariablesInOneLine}))
108
+ write.space();
104
109
  }
105
110
 
106
111
  maybe.indent.dec(n);
107
- maybe.write(isNeedSemicolon(path), ';');
108
112
 
109
- let wasNewline = false;
113
+ if (isNeedSemicolon(path))
114
+ write(';');
110
115
 
111
- if (isNeedNewline(path)) {
116
+ if (isNewlineAfterSemicolon(path))
112
117
  write.newline();
113
- wasNewline = true;
114
- }
115
118
 
116
- store(wasNewline);
119
+ if (isIndentAfterNewline(path)) {
120
+ indent();
121
+ markAfter(path);
122
+ }
117
123
  }),
118
124
  afterIf,
119
- after(path, {maybe, store, print}) {
120
- const wasNewline = store();
121
-
122
- if (skipAfter(path))
123
- return;
124
-
125
- maybe.indent(wasNewline);
125
+ after(path, {print}) {
126
126
  print.newline();
127
- maybe.markAfter(wasNewline, path);
128
127
  },
129
128
  };
130
-
131
- const skipAfter = createTypeChecker([
132
- ['-: parentPath -> !', isLast],
133
- ['+: -> !', isInsideBlock],
134
- ]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "18.6.5",
3
+ "version": "18.6.6",
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",