@putout/printer 12.23.1 → 12.25.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,16 @@
1
+ 2025.02.07, v12.25.0
2
+
3
+ feature:
4
+ - ab68c19 @putout/printer: improve decorators support
5
+
6
+ 2025.02.07, v12.24.0
7
+
8
+ fix:
9
+ - d936102 @putout/printer: ExportDefeaultDeclaration: newline
10
+
11
+ feature:
12
+ - d5169a3 @putout/printer: BinaryExpression inside LogicalExpression: comment
13
+
1
14
  2025.02.06, v12.23.1
2
15
 
3
16
  fix:
@@ -18,6 +18,7 @@ const {
18
18
  isTSPropertySignature,
19
19
  isSpreadElement,
20
20
  isClassBody,
21
+ isBinaryExpression,
21
22
  } = types;
22
23
 
23
24
  const isProperty = satisfy([
@@ -116,7 +117,7 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
116
117
 
117
118
  for (const [index, {type, value}] of leadingComments.entries()) {
118
119
  if (type === 'CommentLine') {
119
- if (!path.isClassProperty() || index)
120
+ if (index || !path.isClassProperty() && !path.isBinaryExpression())
120
121
  maybe.indent(isIndent);
121
122
 
122
123
  maybeInsideFn(insideFn, {
@@ -134,6 +135,12 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
134
135
  if (index === count) {
135
136
  maybe.print.breakline(propIs);
136
137
  maybe.print.newline(!propIs);
138
+
139
+ if (isBinaryExpression(path)) {
140
+ indent.inc();
141
+ indent();
142
+ indent.dec();
143
+ }
137
144
  } else {
138
145
  print.newline();
139
146
  }
@@ -40,10 +40,15 @@ const isTrailingIsLeading = (path) => path.node.trailingComments === path.getNex
40
40
  const isNewlineAfter = (path) => {
41
41
  const {parentPath} = path;
42
42
 
43
+ if (isDecorator(path)) {
44
+ const {loc} = path.node.trailingComments[0];
45
+ return !isSameLine(path, loc);
46
+ }
47
+
43
48
  if (isMemberExpression(parentPath))
44
49
  return false;
45
50
 
46
- return !isLast(path) && !isDecorator(path);
51
+ return !isLast(path);
47
52
  };
48
53
 
49
54
  module.exports.isTrailingIsLeading = isTrailingIsLeading;
@@ -106,7 +111,7 @@ module.exports.parseTrailingComments = (path, {write, maybe, indent}, semantics)
106
111
 
107
112
  if (type === 'CommentLine') {
108
113
  const nextLineInChain = commentOnNextLine && likeChain;
109
- const shouldIndent = !sameLine && (!commentOnNextLine || path.isDecorator());
114
+ const shouldIndent = !sameLine && !commentOnNextLine;
110
115
 
111
116
  maybe.write.breakline(commentOnNextLine);
112
117
  maybe.write.space(sameLine);
@@ -5,10 +5,10 @@ const {
5
5
  hasTrailingComment,
6
6
  } = require('../../is');
7
7
 
8
- const {printParams} = require('./params');
9
8
  const {maybeDecorators} = require('../../maybe/maybe-decorators');
10
9
  const {printKey} = require('../object-expression/print-key');
11
10
  const {printKind} = require('./kind');
11
+ const {printFunctionParams} = require('./print-function-params');
12
12
 
13
13
  const noTrailingCommentAndNext = (path) => {
14
14
  if (hasTrailingComment(path))
@@ -45,7 +45,7 @@ const ClassMethod = {
45
45
 
46
46
  printKind(path, printer);
47
47
  printKey(path, printer);
48
- printParams(path, printer, semantics);
48
+ printFunctionParams(path, printer, semantics);
49
49
 
50
50
  if (returnType) {
51
51
  print(':');
@@ -1,23 +1,26 @@
1
1
  'use strict';
2
2
 
3
3
  const {parseComments} = require('../../comment/comment');
4
-
4
+ const noop = () => {};
5
5
  const parseParams = (path) => path.get('params');
6
6
 
7
7
  module.exports.printParams = (path, printer, semantics, customization = {}) => {
8
8
  const {extra, typeParameters} = path.node;
9
- const {
10
- params = parseParams(path),
11
- braceOpen = '(',
12
- braceClose = ')',
13
- } = customization;
14
-
15
9
  const {
16
10
  print,
17
11
  maybe,
18
12
  traverse,
19
13
  } = printer;
20
14
 
15
+ const {
16
+ params = parseParams(path),
17
+ braceOpen = '(',
18
+ braceClose = ')',
19
+ printSpace = print.space,
20
+ printBeforeFirst = noop,
21
+ printAfterLast = noop,
22
+ } = customization;
23
+
21
24
  if (typeParameters)
22
25
  traverse(path.get('typeParameters'));
23
26
 
@@ -27,6 +30,7 @@ module.exports.printParams = (path, printer, semantics, customization = {}) => {
27
30
  }, semantics);
28
31
 
29
32
  parseComments(path, printer, semantics);
33
+ printBeforeFirst();
30
34
 
31
35
  const n = params.length - 1;
32
36
 
@@ -37,12 +41,13 @@ module.exports.printParams = (path, printer, semantics, customization = {}) => {
37
41
 
38
42
  if (!isLast) {
39
43
  print(',');
40
- print.space();
44
+ printSpace();
41
45
  }
42
46
  }
43
47
 
44
48
  maybe.print(extra?.trailingComma, ',');
45
49
 
50
+ printAfterLast();
46
51
  printBraceClose(path, {
47
52
  print,
48
53
  braceClose,
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+
3
+ const {printParams} = require('./params');
4
+
5
+ const hasDecorators = ({decorators}) => decorators?.length;
6
+
7
+ module.exports.printFunctionParams = (path, printer, semantics) => {
8
+ const {print} = printer;
9
+ const {params} = path.node;
10
+ const isNewline = params.filter(hasDecorators).length;
11
+ const printSpace = isNewline ? print.breakline : print.space;
12
+
13
+ const printBeforeFirst = createPrint({
14
+ type: 'inc',
15
+ printer,
16
+ isNewline,
17
+ });
18
+
19
+ const printAfterLast = createPrint({
20
+ type: 'dec',
21
+ printer,
22
+ isNewline,
23
+ });
24
+
25
+ printParams(path, printer, semantics, {
26
+ printBeforeFirst,
27
+ printSpace,
28
+ printAfterLast,
29
+ });
30
+ };
31
+
32
+ const createPrint = ({isNewline, printer, type}) => () => {
33
+ if (!isNewline)
34
+ return;
35
+
36
+ const {indent, print} = printer;
37
+ indent[type]();
38
+ print.breakline();
39
+ };
@@ -3,7 +3,7 @@
3
3
  const {TemplateLiteral} = require('./template-literal');
4
4
  const {Identifier} = require('./identifier');
5
5
 
6
- const {Decorator} = require('../expressions/decorator');
6
+ const {Decorator} = require('../expressions/decorator/decorator');
7
7
  const {StringLiteral} = require('./string-literal');
8
8
 
9
9
  module.exports = {
@@ -22,4 +22,3 @@ module.exports.ExportDefaultDeclaration = {
22
22
  print.newline();
23
23
  },
24
24
  };
25
-
@@ -1,24 +1,33 @@
1
1
  'use strict';
2
2
 
3
- module.exports.TSParameterProperty = (path, {print, maybe}) => {
3
+ const {hasLeadingComment} = require('../../is');
4
+
5
+ module.exports.TSParameterProperty = (path, {print, maybe, indent}) => {
4
6
  const {
5
7
  decorators,
6
8
  readonly,
7
9
  accessibility,
8
10
  } = path.node;
9
11
 
10
- const decoratorsLength = decorators?.length;
12
+ const decoratorsLength = decorators?.length > 1;
13
+ const isNewline = decoratorsLength || hasLeadingComment(path);
11
14
 
12
15
  maybe.print.breakline(decoratorsLength);
13
16
 
14
- if (decorators)
17
+ if (decorators) {
15
18
  for (const decorator of path.get('decorators')) {
16
- print.indent();
19
+ maybe.indent(decoratorsLength);
17
20
  print(decorator);
18
21
  }
19
-
20
- maybe.print.breakline(decoratorsLength);
21
- maybe.indent(decoratorsLength);
22
+
23
+ maybe.print.breakline(decoratorsLength);
24
+
25
+ if (isNewline)
26
+ indent();
27
+
28
+ if (!hasLeadingComment(path))
29
+ print.space();
30
+ }
22
31
 
23
32
  if (accessibility) {
24
33
  print(accessibility);
@@ -31,6 +40,6 @@ module.exports.TSParameterProperty = (path, {print, maybe}) => {
31
40
  }
32
41
 
33
42
  print('__parameter');
34
- maybe.print(decoratorsLength, ',');
43
+ maybe.print(isNewline, ',');
35
44
  maybe.print.breakline(decoratorsLength);
36
45
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "12.23.1",
3
+ "version": "12.25.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 for 🐊Putout",