@putout/printer 18.42.3 → 18.44.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.
Files changed (25) hide show
  1. package/ChangeLog +14 -1
  2. package/lib/printer.d.ts +4 -0
  3. package/lib/tokenize/expressions/call-expression/call-expression.js +14 -4
  4. package/lib/tokenize/expressions/conditional-expression/conditional-expression-multiline.js +0 -1
  5. package/lib/tokenize/expressions/function/{arrow-function-expression.js → arrow-function-expression/arrow-function-expression.js} +4 -16
  6. package/lib/tokenize/expressions/function/{class-method.js → class-method/class-method.js} +7 -11
  7. package/lib/tokenize/expressions/function/{function-declaration.js → function-declaration/function-declaration.js} +4 -11
  8. package/lib/tokenize/expressions/function/{function-expression.js → function-expression/function-expression.js} +3 -1
  9. package/lib/tokenize/expressions/function/functions.js +5 -5
  10. package/lib/tokenize/expressions/function/{object-method.js → object-method/object-method.js} +5 -3
  11. package/lib/tokenize/expressions/function/print-function-params.js +1 -1
  12. package/lib/tokenize/expressions/function/return-type.js +10 -0
  13. package/lib/tokenize/literals/index.js +2 -5
  14. package/lib/tokenize/literals/regexp-literal/regexp-literal.js +4 -0
  15. package/lib/tokenize/maybe/index.js +5 -3
  16. package/lib/tokenize/typescript/function/ts-call-signature-declaration.js +1 -1
  17. package/lib/tokenize/typescript/function/ts-construct-signature-declaration.js +1 -1
  18. package/lib/tokenize/typescript/function/ts-constructor-type.js +1 -1
  19. package/lib/tokenize/typescript/function/ts-declare-function.js +1 -1
  20. package/lib/tokenize/typescript/function/ts-declare-method.js +1 -1
  21. package/lib/tokenize/typescript/function/ts-function-type.js +1 -1
  22. package/lib/tokenize/typescript/function/ts-method-signature.js +1 -1
  23. package/lib/tokenize/typescript/index.js +1 -1
  24. package/lib/tokenize/typescript/ts-type-parameter-declaration/ts-type-parameter-declaration.js +18 -12
  25. package/package.json +4 -2
package/ChangeLog CHANGED
@@ -1,3 +1,16 @@
1
+ 2026.09.10, v18.44.0
2
+
3
+ feature:
4
+ - 6428169a @putout/printer: CallExpression: with first argument chainend call expression: new lines
5
+
6
+ 2026.09.07, v18.43.0
7
+
8
+ feature:
9
+ - 640e6e16 @putout/printer: return-type
10
+ - 356e5394 @putout/printer: return-type
11
+ - 5cadde8c @putout/printer: FunctionExpression: returnType
12
+ - 45a16f04 @putout/printer: ObjectMethod: printReturnType
13
+
1
14
  2026.09.02, v18.42.3
2
15
 
3
16
  fix:
@@ -891,7 +904,7 @@ fix:
891
904
  2026.01.18, v17.0.4
892
905
 
893
906
  feature:
894
- - 06823ba @putout/printer: print-params -> #print-params
907
+ - 06823ba @putout/printer: print-params -> #params
895
908
 
896
909
  2026.01.17, v17.0.3
897
910
 
package/lib/printer.d.ts CHANGED
@@ -19,8 +19,11 @@ export interface Semantics {
19
19
  }
20
20
 
21
21
  export type PrinterPrint = (input: string | Node) => void;
22
+
22
23
  export type Indent = () => void;
24
+
23
25
  export type Traverse = (input: Node) => void;
26
+
24
27
  export type MaybeCondition = (condition: boolean) => void;
25
28
 
26
29
  export type MaybeIndent = {
@@ -59,6 +62,7 @@ export interface Options {
59
62
  }
60
63
 
61
64
  type Print = (ast: Node, options?: Options) => string;
65
+
62
66
  export const print: Print;
63
67
 
64
68
  export declare function maybeVisitor(plugin: Visitor, path: Node, printer: Printer, semantics: Semantics): void;
@@ -28,7 +28,16 @@ const isMaxArgs = createTypeChecker([
28
28
  ['+: node.arguments.length', '>=', getMaxArgsInOneLine],
29
29
  ]);
30
30
 
31
+ const isMemberCallee = createTypeChecker([
32
+ '-: -> !CallExpression',
33
+ '-: node.callee -> !MemberExpression',
34
+ '+: node.callee.object -> CallExpression',
35
+ ]);
36
+
37
+ const callWithArg = (fn) => (path) => fn(path.get('arguments')[0]);
38
+
31
39
  const isMultiline = createTypeChecker([
40
+ ['+', callWithArg(isMemberCallee)],
32
41
  ['-: -> !', tooLong],
33
42
  ['+', isInsideCall],
34
43
  ]);
@@ -72,7 +81,7 @@ function printArgs(args, printer, {maxArgs, multiline}) {
72
81
  if (multiline)
73
82
  if (maxArgs && isArrayExpression(arg))
74
83
  print.space();
75
- else if (n || maxArgs && !isObjectExpression(arg))
84
+ else if (n || maxArgs && !isObjectExpression(arg) || isMemberCallee(arg))
76
85
  print.breakline();
77
86
 
78
87
  print(arg);
@@ -82,15 +91,16 @@ function printArgs(args, printer, {maxArgs, multiline}) {
82
91
  continue;
83
92
  }
84
93
 
85
- if (i < n) {
94
+ if (isMemberCallee(arg) || i < n)
86
95
  print(',');
96
+
97
+ if (i < n)
87
98
  print.space();
88
- }
89
99
  }
90
100
 
91
101
  if (multiline) {
92
102
  indent.dec();
93
- maybe.print.breakline(n || maxArgs);
103
+ maybe.print.breakline(n || maxArgs || isMemberCallee(args[0]));
94
104
  }
95
105
  }
96
106
 
@@ -21,4 +21,3 @@ export const printConditionalExpressionMultiline = (path, {print, indent}) => {
21
21
  indent.dec();
22
22
  print('__alternate');
23
23
  };
24
-
@@ -1,27 +1,15 @@
1
- import {exists} from '#is';
2
1
  import {maybeParens} from '#maybe-parens';
3
- import {printParams} from '#print-params';
2
+ import {printParams} from '#params';
3
+ import {printReturnType} from '#return-type';
4
4
 
5
5
  export const ArrowFunctionExpression = maybeParens((path, printer, semantics) => {
6
- const {
7
- print,
8
- maybe,
9
- write,
10
- traverse,
11
- } = printer;
6
+ const {print, maybe} = printer;
12
7
 
13
8
  const {async} = path.node;
14
9
 
15
10
  maybe.print(async, 'async ');
16
11
  printParams(path, printer, semantics);
17
-
18
- const returnType = path.get('returnType');
19
-
20
- if (exists(returnType)) {
21
- write(':');
22
- write.space();
23
- traverse(returnType);
24
- }
12
+ printReturnType(path, printer);
25
13
 
26
14
  print.space();
27
15
  print('=>');
@@ -2,10 +2,11 @@ import {
2
2
  isNext,
3
3
  hasTrailingComment,
4
4
  } from '#is';
5
- import {maybeDecorators} from '../../maybe/maybe-decorators.js';
6
- import {printKey} from '../object-expression/print-key.js';
7
- import {printKind} from './kind.js';
8
- import {printFunctionParams} from './print-function-params.js';
5
+ import {printReturnType} from '#return-type';
6
+ import {maybeDecorators} from '../../../maybe/maybe-decorators.js';
7
+ import {printKey} from '../../object-expression/print-key.js';
8
+ import {printKind} from '../kind.js';
9
+ import {printFunctionParams} from '../print-function-params.js';
9
10
 
10
11
  const noTrailingCommentAndNext = (path) => {
11
12
  if (hasTrailingComment(path))
@@ -18,7 +19,7 @@ export const ClassMethod = {
18
19
  print: maybeDecorators((path, printer, semantics) => {
19
20
  const {print} = printer;
20
21
  const {node} = path;
21
- const {accessibility, returnType} = node;
22
+ const {accessibility} = node;
22
23
 
23
24
  if (accessibility) {
24
25
  print(accessibility);
@@ -43,12 +44,7 @@ export const ClassMethod = {
43
44
  printKind(path, printer);
44
45
  printKey(path, printer);
45
46
  printFunctionParams(path, printer, semantics);
46
-
47
- if (returnType) {
48
- print(':');
49
- print.space();
50
- print('__returnType');
51
- }
47
+ printReturnType(path, printer);
52
48
 
53
49
  print.space();
54
50
  print('__body');
@@ -1,5 +1,5 @@
1
1
  import {types} from '@putout/babel';
2
- import {printParams} from '#print-params';
2
+ import {printParams} from '#params';
3
3
  import {markAfter} from '#mark';
4
4
  import {
5
5
  callWithNext,
@@ -7,6 +7,7 @@ import {
7
7
  isNextParent,
8
8
  } from '#is';
9
9
  import {createTypeChecker} from '#type-checker';
10
+ import {printReturnType} from '#return-type';
10
11
 
11
12
  const {
12
13
  isExpressionStatement,
@@ -44,11 +45,7 @@ export const FunctionDeclaration = {
44
45
  print(path, printer, semantics) {
45
46
  const {print, maybe} = printer;
46
47
 
47
- const {
48
- async,
49
- generator,
50
- returnType,
51
- } = path.node;
48
+ const {async, generator} = path.node;
52
49
 
53
50
  maybe.print(async, 'async ');
54
51
 
@@ -63,11 +60,7 @@ export const FunctionDeclaration = {
63
60
 
64
61
  print('__id');
65
62
  printParams(path, printer, semantics);
66
-
67
- if (returnType) {
68
- print(': ');
69
- print('__returnType');
70
- }
63
+ printReturnType(path, printer);
71
64
 
72
65
  print.space();
73
66
  print('__body');
@@ -1,6 +1,7 @@
1
1
  import {exists} from '#is';
2
2
  import {maybeParens} from '#maybe-parens';
3
- import {printParams} from '#print-params';
3
+ import {printParams} from '#params';
4
+ import {printReturnType} from '#return-type';
4
5
 
5
6
  export const FunctionExpression = maybeParens((path, printer, semantics) => {
6
7
  const {
@@ -25,6 +26,7 @@ export const FunctionExpression = maybeParens((path, printer, semantics) => {
25
26
  }
26
27
 
27
28
  printParams(path, printer, semantics);
29
+ printReturnType(path, printer);
28
30
 
29
31
  print.space();
30
32
  print('__body');
@@ -1,5 +1,5 @@
1
- export {ArrowFunctionExpression} from './arrow-function-expression.js';
2
- export {FunctionDeclaration} from './function-declaration.js';
3
- export {ClassPrivateMethod, ClassMethod} from './class-method.js';
4
- export {ObjectMethod} from './object-method.js';
5
- export {FunctionExpression} from './function-expression.js';
1
+ export {ArrowFunctionExpression} from './arrow-function-expression/arrow-function-expression.js';
2
+ export {FunctionDeclaration} from './function-declaration/function-declaration.js';
3
+ export {ClassPrivateMethod, ClassMethod} from './class-method/class-method.js';
4
+ export {ObjectMethod} from './object-method/object-method.js';
5
+ export {FunctionExpression} from './function-expression/function-expression.js';
@@ -2,10 +2,11 @@ import {
2
2
  isNewlineBetweenSiblings,
3
3
  isNext,
4
4
  } from '#is';
5
- import {printParams} from '#print-params';
5
+ import {printParams} from '#params';
6
6
  import {markAfter} from '#mark';
7
- import {printKey} from '../object-expression/print-key.js';
8
- import {printKind} from './kind.js';
7
+ import {printReturnType} from '#return-type';
8
+ import {printKey} from '../../object-expression/print-key.js';
9
+ import {printKind} from '../kind.js';
9
10
 
10
11
  export const ObjectMethod = {
11
12
  beforeIf(path) {
@@ -21,6 +22,7 @@ export const ObjectMethod = {
21
22
  printKind(path, printer);
22
23
  printKey(path, printer);
23
24
  printParams(path, printer, semantics);
25
+ printReturnType(path, printer);
24
26
 
25
27
  print.space();
26
28
  print('__body');
@@ -2,7 +2,7 @@ import {
2
2
  hasLeadingComment,
3
3
  hasDecorators,
4
4
  } from '#is';
5
- import {printParams} from '#print-params';
5
+ import {printParams} from '#params';
6
6
 
7
7
  const isAllParamsHasLeadingComments = (path) => {
8
8
  const params = path.get('params');
@@ -0,0 +1,10 @@
1
+ export const printReturnType = (path, printer) => {
2
+ const {print} = printer;
3
+ const {returnType} = path.node;
4
+
5
+ if (returnType) {
6
+ print(':');
7
+ print.space();
8
+ print('__returnType');
9
+ }
10
+ };
@@ -4,6 +4,8 @@ export {Decorator} from './decorator/decorator.js';
4
4
  export {StringLiteral} from './string-literal.js';
5
5
  export {DirectiveLiteral} from './directive-literal.js';
6
6
  export {VoidPattern} from './void-pattern/void-pattern.js';
7
+ export {RegExpLiteral} from './regexp-literal/regexp-literal.js';
8
+
7
9
  export const BigIntLiteral = (path, {write}) => {
8
10
  write(path.node.raw);
9
11
  };
@@ -27,11 +29,6 @@ export const BooleanLiteral = (path, {write}) => {
27
29
  write(path.node.value);
28
30
  };
29
31
 
30
- export const RegExpLiteral = (path, {print}) => {
31
- const {raw, pattern} = path.node;
32
- print(raw || `/${pattern}/`);
33
- };
34
-
35
32
  export const NullLiteral = (path, {write}) => {
36
33
  write('null');
37
34
  };
@@ -0,0 +1,4 @@
1
+ export const RegExpLiteral = (path, {print}) => {
2
+ const {raw, pattern} = path.node;
3
+ print(raw || `/${pattern}/`);
4
+ };
@@ -25,9 +25,11 @@ export const maybeThrow = (a, path, b) => {
25
25
 
26
26
  const maybeStatement = (ast) => isStatement(ast) ? ast : expressionStatement(ast);
27
27
 
28
- const maybeProgram = (ast) => isProgram(ast) ? ast : program([
29
- maybeStatement(ast),
30
- ]);
28
+ const maybeProgram = (ast) => isProgram(ast)
29
+ ? ast
30
+ : program([
31
+ maybeStatement(ast),
32
+ ]);
31
33
 
32
34
  export const maybeFile = (ast) => isFile(ast) ? ast : file(maybeProgram(ast));
33
35
 
@@ -1,4 +1,4 @@
1
- import {printParams} from '#print-params';
1
+ import {printParams} from '#params';
2
2
  import {printReturnType} from './print-return-type.js';
3
3
 
4
4
  export const TSCallSignatureDeclaration = (path, printer, semantics) => {
@@ -1,4 +1,4 @@
1
- import {printParams} from '#print-params';
1
+ import {printParams} from '#params';
2
2
  import {
3
3
  hasReturnType,
4
4
  printReturnType,
@@ -1,4 +1,4 @@
1
- import {printParams} from '#print-params';
1
+ import {printParams} from '#params';
2
2
  import {printReturnType} from './print-return-type.js';
3
3
 
4
4
  export const TSConstructorType = (path, printer, semantics) => {
@@ -1,5 +1,5 @@
1
1
  import {isNext} from '#is';
2
- import {printParams} from '#print-params';
2
+ import {printParams} from '#params';
3
3
  import {maybeDeclare} from '#maybe-declare';
4
4
 
5
5
  const isInsideDefaultExport = (path) => {
@@ -1,4 +1,4 @@
1
- import {printParams} from '#print-params';
1
+ import {printParams} from '#params';
2
2
 
3
3
  export const TSDeclareMethod = (path, printer, semantics) => {
4
4
  const {print} = printer;
@@ -1,5 +1,5 @@
1
1
  import {maybeParens} from '#maybe-parens';
2
- import {printParams} from '#print-params';
2
+ import {printParams} from '#params';
3
3
  import {printReturnType} from './print-return-type.js';
4
4
 
5
5
  export const TSFunctionType = maybeParens((path, printer, semantics) => {
@@ -1,4 +1,4 @@
1
- import {printParams} from '#print-params';
1
+ import {printParams} from '#params';
2
2
  import {printKind} from '../../expressions/function/kind.js';
3
3
  import {
4
4
  hasReturnType,
@@ -1,4 +1,4 @@
1
- import {printParams} from '#print-params';
1
+ import {printParams} from '#params';
2
2
  import {maybePrintTypeAnnotation} from '../maybe/maybe-type-annotation.js';
3
3
 
4
4
  export {TSNonNullExpression} from './ts-non-null-expression/ts-non-null-expression.js';
@@ -1,5 +1,5 @@
1
1
  import {types} from '@putout/babel';
2
- import {printParams} from '#print-params';
2
+ import {printParams} from '#params';
3
3
  import {createPrintSpace} from '../../expressions/function/print-function-params.js';
4
4
 
5
5
  const {isTSUnionType} = types;
@@ -14,19 +14,25 @@ export const TSTypeParameterDeclaration = (path, printer, semantics) => {
14
14
  printer,
15
15
  });
16
16
 
17
- const printAfterOpen = !isNewline ? noop : () => {
18
- indent.inc();
19
- print.breakline();
20
- };
17
+ const printAfterOpen = !isNewline
18
+ ? noop
19
+ : () => {
20
+ indent.inc();
21
+ print.breakline();
22
+ };
21
23
 
22
- const printAfterClose = !isNewline ? noop : () => {
23
- print.breakline();
24
- };
24
+ const printAfterClose = !isNewline
25
+ ? noop
26
+ : () => {
27
+ print.breakline();
28
+ };
25
29
 
26
- const printBeforeClose = !isNewline ? noop : () => {
27
- indent.dec();
28
- print.breakline();
29
- };
30
+ const printBeforeClose = !isNewline
31
+ ? noop
32
+ : () => {
33
+ indent.dec();
34
+ print.breakline();
35
+ };
30
36
 
31
37
  printParams(path, printer, semantics, {
32
38
  printSpace,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "18.42.3",
3
+ "version": "18.44.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",
@@ -10,6 +10,7 @@
10
10
  ".": "./lib/printer.js",
11
11
  "./is": "./lib/tokenize/is.js",
12
12
  "./params": "./lib/tokenize/expressions/function/params.js",
13
+ "./return-type": "./lib/tokenize/expressions/function/return-type.js",
13
14
  "./type-checker": "./lib/tokenize/type-checker/type-checker.js",
14
15
  "./type-checker/instrument": "./lib/tokenize/type-checker/instrument.js",
15
16
  "./type-checker/report": "./lib/tokenize/type-checker/report.js",
@@ -65,7 +66,8 @@
65
66
  "#mark": "./lib/tokenize/mark.js",
66
67
  "#maybe-parens": "./lib/tokenize/maybe/maybe-parens.js",
67
68
  "#maybe-declare": "./lib/tokenize/maybe/maybe-declare.js",
68
- "#print-params": "./lib/tokenize/expressions/function/params.js",
69
+ "#params": "./lib/tokenize/expressions/function/params.js",
70
+ "#return-type": "./lib/tokenize/expressions/function/return-type.js",
69
71
  "#import-attributes": "./lib/tokenize/statements/import-declaration/import-attribute.js",
70
72
  "#types": "./lib/types.js",
71
73
  "#type-checker": "./lib/tokenize/type-checker/type-checker.js",