@putout/printer 18.4.0 → 18.4.1

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,12 @@
1
+ 2026.03.10, v18.4.1
2
+
3
+ feature:
4
+ - 4f2fb23 @putout/printer: is-concatenation
5
+ - 9c1917c @putout/printer: BinaryExpression: concatenate: simplify
6
+ - a8f5542 @putout/printer: ObjectExpression: ObjectProperty: isSpaceAfterComma
7
+ - e79a33c @putout/printer: ObjectExpression: ObjectProperty: space
8
+ - d206ffa @putout/printer: ObjectProperty: isCommaAfterProperty
9
+
1
10
  2026.03.10, v18.4.0
2
11
 
3
12
  feature:
@@ -1,8 +1,6 @@
1
1
  import {maybeParens} from '#maybe-parens';
2
- import {
3
- concatenate,
4
- isConcatenation,
5
- } from './concatenate.js';
2
+ import {isConcatenation} from '#is-concatenation';
3
+ import {concatenate} from './concatenate.js';
6
4
  import {maybeSpace} from './maybe-space.js';
7
5
 
8
6
  export const BinaryExpression = maybeParens((path, {print, indent, maybe}) => {
@@ -1,52 +1,9 @@
1
- import {types} from '@putout/babel';
1
+ import {maybeInsideBinary} from './maybe-inside-binary.js';
2
2
 
3
- const {
4
- isStringLiteral,
5
- isTemplateLiteral,
6
- isBinaryExpression,
7
- } = types;
8
-
9
- const isStringLike = (a) => {
10
- if (isStringLiteral(a))
11
- return true;
12
-
13
- return isTemplateLiteral(a);
14
- };
15
-
16
- export const isConcatenation = (path) => {
17
- const {parentPath} = path;
18
- const {operator} = path.node;
19
-
20
- if (operator !== '+')
21
- return false;
22
-
23
- const startLine = path.node.loc?.start.line;
24
- const endLine = path.node.loc?.end.line;
25
-
26
- if (startLine === endLine)
27
- return false;
28
-
29
- const left = path.get('left');
30
- const right = path.get('right');
31
-
32
- if (isStringLike(left) && isStringLike(right) && isBinaryExpression(parentPath))
33
- return true;
34
-
35
- return isBinaryExpression(left) && isStringLike(right);
36
- };
37
-
38
- export const concatenate = (path, {print, indent}) => {
39
- if (!path.parentPath.isBinaryExpression()) {
40
- indent.inc();
41
- print.breakline();
42
- }
43
-
3
+ export const concatenate = maybeInsideBinary((path, {print}) => {
44
4
  print('__left');
45
5
  print.space();
46
6
  print('+');
47
7
  print.breakline();
48
8
  print('__right');
49
-
50
- if (!path.parentPath.isBinaryExpression())
51
- indent.dec();
52
- };
9
+ });
@@ -0,0 +1,16 @@
1
+ import {types} from '@putout/babel';
2
+ import {createTypeChecker} from '#type-checker';
3
+
4
+ const {isBinaryExpression} = types;
5
+
6
+ export const isConcatenation = createTypeChecker([
7
+ ['-: node.loc', isSameLine],
8
+ ['+', isBinaryExpression],
9
+ ]);
10
+
11
+ function isSameLine(loc) {
12
+ const startLine = loc?.start.line;
13
+ const endLine = loc?.end.line;
14
+
15
+ return startLine === endLine;
16
+ }
@@ -0,0 +1,20 @@
1
+ import {types} from '@putout/babel';
2
+
3
+ const {isBinaryExpression} = types;
4
+ const isInsideBinary = ({parentPath}) => isBinaryExpression(parentPath);
5
+
6
+ export const maybeInsideBinary = (fn) => (path, printer, semantics) => {
7
+ const {indent, print} = printer;
8
+ const insideBinary = isInsideBinary(path);
9
+
10
+ if (!insideBinary) {
11
+ indent.inc();
12
+ print.breakline();
13
+ }
14
+
15
+ fn(path, printer, semantics);
16
+
17
+ if (!insideBinary)
18
+ indent.dec();
19
+ };
20
+
@@ -7,8 +7,12 @@ import {
7
7
  const isLastProperty = ({node, parentPath}) => node === parentPath.node.properties.at(-1);
8
8
 
9
9
  export const isCommaAfterProperty = createTypeChecker([
10
- ['-: -> !SpreadElement'],
11
- ['-: -> !', isMultilineOption],
12
10
  ['+: -> !', isLastProperty],
11
+ ['-: -> !', isMultilineOption],
13
12
  ['+', isTrailingCommaOption],
14
13
  ]);
14
+
15
+ export const isCommaAfterSpread = createTypeChecker([
16
+ ['-: -> !SpreadElement'],
17
+ ['+', isCommaAfterProperty],
18
+ ]);
@@ -13,7 +13,7 @@ import {
13
13
  import {parseComments} from '../../comment/comment.js';
14
14
  import {isInsideTuple} from './is-inside-tuple.js';
15
15
  import {isLooksLikeChain} from '../member-expression/is-looks-like-chain.js';
16
- import {isCommaAfterProperty} from './comma.js';
16
+ import {isCommaAfterSpread} from './comma.js';
17
17
  import {isMultilineOption} from '../array-expression/is.js';
18
18
  import {isLinebreakAfterProperty} from './linebreak.js';
19
19
  import {isNewlineAfterProperty} from './newline.js';
@@ -60,10 +60,10 @@ const isInsideTupleLike = createTypeChecker([
60
60
  export const isMultiline = createTypeChecker([
61
61
  ['-', hasNoProperties],
62
62
  ['-: parentPath -> ForOfStatement'],
63
- ['+: node.properties.0 -> SpreadElement'],
64
63
  ['-', notLastArgInsideCall],
65
64
  ['-', isForOf],
66
65
  ['-', isIf],
66
+ ['+: node.properties.0 -> SpreadElement'],
67
67
  ['+', isCoupleLines],
68
68
  ['+', hasValue],
69
69
  ]);
@@ -110,7 +110,7 @@ export const ObjectExpression = (path, printer, semantics) => {
110
110
  if (hasTrailingComment(property))
111
111
  continue;
112
112
 
113
- maybe.print(isCommaAfterProperty(property, {
113
+ maybe.print(isCommaAfterSpread(property, {
114
114
  multiline,
115
115
  trailingComma,
116
116
  }), ',');
@@ -1,6 +1,8 @@
1
- import {isConcatenation} from '../binary-expression/concatenate.js';
1
+ import {isConcatenation} from '#is-concatenation';
2
2
  import {isMultiline} from './object-expression.js';
3
3
  import {printKey} from './print-key.js';
4
+ import {isCommaAfterProperty} from './comma.js';
5
+ import {isSpaceAfterComma} from './space.js';
4
6
 
5
7
  export const ObjectProperty = (path, printer, semantics) => {
6
8
  const {trailingComma} = semantics;
@@ -12,8 +14,6 @@ export const ObjectProperty = (path, printer, semantics) => {
12
14
  } = printer;
13
15
 
14
16
  const value = path.get('value');
15
- const properties = path.parentPath.get('properties');
16
- const isLast = path === properties.at(-1);
17
17
  const multiline = isMultiline(path.parentPath);
18
18
 
19
19
  printKey(path, printer);
@@ -24,11 +24,9 @@ export const ObjectProperty = (path, printer, semantics) => {
24
24
  traverse(value);
25
25
  }
26
26
 
27
- if (multiline) {
28
- maybe.write(!isLast || trailingComma, ',');
29
- return;
30
- }
27
+ if (isCommaAfterProperty(path, {multiline, trailingComma}))
28
+ write(',');
31
29
 
32
- if (!isLast && properties.length)
33
- write(', ');
30
+ if (isSpaceAfterComma(path, {multiline}))
31
+ write.space();
34
32
  };
@@ -0,0 +1,9 @@
1
+ import {createTypeChecker} from '#type-checker';
2
+ import {isMultilineOption} from '../array-expression/is.js';
3
+
4
+ const isLast = ({node, parentPath}) => node === parentPath.node.properties.at(-1);
5
+
6
+ export const isSpaceAfterComma = createTypeChecker([
7
+ ['-', isMultilineOption],
8
+ ['+: -> !', isLast],
9
+ ]);
@@ -1,6 +1,7 @@
1
1
  import {types} from '@putout/babel';
2
2
  import {hasPrevNewline} from '#mark';
3
3
  import {createTypeChecker} from '#type-checker';
4
+ import {isConcatenation} from '#is-concatenation';
4
5
  import {
5
6
  isNext,
6
7
  isCoupleLines,
@@ -17,7 +18,6 @@ import {
17
18
  callWithParent,
18
19
  } from '#is';
19
20
  import {maybeSpaceAfterKeyword} from './maybe-space-after-keyword.js';
20
- import {isConcatenation} from '../../expressions/binary-expression/concatenate.js';
21
21
  import {parseLeadingComments} from '../../comment/comment.js';
22
22
  import {maybeDeclare} from '../../maybe/maybe-declare.js';
23
23
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "18.4.0",
3
+ "version": "18.4.1",
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",
@@ -68,7 +68,8 @@
68
68
  "#types": "./lib/types.js",
69
69
  "#type-checker": "./lib/tokenize/type-checker/type-checker.js",
70
70
  "#type-checker/instrument": "./lib/tokenize/type-checker/instrument.js",
71
- "#type-checker/report": "./lib/tokenize/type-checker/report.js"
71
+ "#type-checker/report": "./lib/tokenize/type-checker/report.js",
72
+ "#is-concatenation": "./lib/tokenize/expressions/binary-expression/is-contatenation.js"
72
73
  },
73
74
  "devDependencies": {
74
75
  "@babel/parser": "^7.28.5",