@putout/printer 11.2.0 → 11.3.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,15 @@
1
+ 2024.12.12, v11.3.0
2
+
3
+ feature:
4
+ - b5de5fc @putout/printer: split BinaryExpression and LogicalExpression
5
+ - 262f6ab @putout/printer: LogicalExpressions: maxElementsInOneLine
6
+ - 4bccded @putout/printer: LogicalExpressions: newline
7
+
8
+ 2024.12.12, v11.2.1
9
+
10
+ feature:
11
+ - 56da95d @putout/printer: maybeParens
12
+
1
13
  2024.12.12, v11.2.0
2
14
 
3
15
  feature:
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {isParens} = require('../unary-expression/parens');
3
+ const {isParens} = require('../../maybe/maybe-parens');
4
4
 
5
5
  module.exports.maybePrintLeftBrace = (path, printer, semantics) => {
6
6
  maybeWriteBrace(path, printer, semantics, {
@@ -6,56 +6,33 @@ const {
6
6
  } = require('./concatenate');
7
7
 
8
8
  const {maybeSpace} = require('./maybe-space');
9
- const {isParens} = require('../unary-expression/parens');
9
+ const {maybeParens} = require('../../maybe/maybe-parens');
10
10
 
11
- const BinaryExpression = {
12
- condition(path) {
13
- if (isParens(path))
14
- return true;
15
-
16
- return path.parentPath.isAwaitExpression();
17
- },
18
- before(path, {print}) {
19
- print('(');
20
- },
21
- print(path, {print, indent, maybe}) {
22
- const {operator} = path.node;
23
-
24
- if (operator === 'instanceof') {
25
- print('__left');
26
- print(' instanceof ');
27
- print('__right');
28
-
29
- return;
30
- }
31
-
32
- if (operator === 'in') {
33
- print('__left');
34
- print(' in ');
35
- print('__right');
36
-
37
- return;
38
- }
39
-
40
- if (isConcatenation(path))
41
- return concatenate(path, {
42
- print,
43
- indent,
44
- maybe,
45
- });
46
-
11
+ module.exports.BinaryExpression = maybeParens((path, {print, indent, maybe}) => {
12
+ const {operator} = path.node;
13
+
14
+ if (operator === 'in' || operator === 'instanceof') {
47
15
  print('__left');
48
- print.space();
49
- print(path.node.operator);
50
- maybeSpace(path, {
16
+ print(' ');
17
+ print(operator);
18
+ print(' ');
19
+ print('__right');
20
+
21
+ return;
22
+ }
23
+
24
+ if (isConcatenation(path))
25
+ return concatenate(path, {
51
26
  print,
27
+ indent,
28
+ maybe,
52
29
  });
53
- print('__right');
54
- },
55
- after(path, {print}) {
56
- print(')');
57
- },
58
- };
59
-
60
- module.exports.BinaryExpression = BinaryExpression;
61
- module.exports.LogicalExpression = BinaryExpression;
30
+
31
+ print('__left');
32
+ print.space();
33
+ print(path.node.operator);
34
+ maybeSpace(path, {
35
+ print,
36
+ });
37
+ print('__right');
38
+ });
@@ -17,6 +17,9 @@ module.exports.isConcatenation = (path) => {
17
17
  const {parentPath} = path;
18
18
  const {operator} = path.node;
19
19
 
20
+ if (operator !== '+')
21
+ return false;
22
+
20
23
  const startLine = path.node.loc?.start.line;
21
24
  const endLine = path.node.loc?.end.line;
22
25
 
@@ -26,9 +29,6 @@ module.exports.isConcatenation = (path) => {
26
29
  const left = path.get('left');
27
30
  const right = path.get('right');
28
31
 
29
- if (operator !== '+')
30
- return false;
31
-
32
32
  if (isStringLike(left) && isStringLike(right) && isBinaryExpression(parentPath))
33
33
  return true;
34
34
 
@@ -1,24 +1,15 @@
1
1
  'use strict';
2
2
 
3
- const {isParens} = require('./unary-expression/parens');
3
+ const {maybeParens} = require('../maybe/maybe-parens');
4
4
 
5
- module.exports.ConditionalExpression = {
6
- condition: isParens,
7
- before(path, {print}) {
8
- print('(');
9
- },
10
- print(path, {print}) {
11
- print('__test');
12
- print.space();
13
- print('?');
14
- print.space();
15
- print('__consequent');
16
- print.space();
17
- print(':');
18
- print.space();
19
- print('__alternate');
20
- },
21
- after(path, {print}) {
22
- print(')');
23
- },
24
- };
5
+ module.exports.ConditionalExpression = maybeParens((path, {print}) => {
6
+ print('__test');
7
+ print.space();
8
+ print('?');
9
+ print.space();
10
+ print('__consequent');
11
+ print.space();
12
+ print(':');
13
+ print.space();
14
+ print('__alternate');
15
+ });
@@ -35,12 +35,8 @@ const {RestElement} = require('./rest-element');
35
35
  const {SpreadElement} = require('./spread-element');
36
36
  const {SequenceExpression} = require('./sequence-expression/sequence-expression');
37
37
  const {TaggedTemplateExpression} = require('./tagged-template-expression');
38
-
39
- const {
40
- BinaryExpression,
41
- LogicalExpression,
42
- } = require('./binary-expression/binary-expression');
43
-
38
+ const {BinaryExpression} = require('./binary-expression/binary-expression');
39
+ const {LogicalExpression} = require('./logical-expression/logical-expression');
44
40
  const {ConditionalExpression} = require('./conditional-expression');
45
41
  const {StaticBlock} = require('./class/static-block');
46
42
  const {RecordExpression} = require('./object-expression/record-expression');
@@ -0,0 +1,52 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ isReturnStatement,
5
+ isVariableDeclarator,
6
+ } = require('@putout/babel').types;
7
+
8
+ module.exports.isRootOk = (path) => {
9
+ return isReturnStatement(path) || isVariableDeclarator(path);
10
+ };
11
+
12
+ module.exports.chain = (path) => {
13
+ const [downCount] = down(path);
14
+ const [upCount, root] = up(path);
15
+
16
+ return [
17
+ root,
18
+ downCount + upCount,
19
+ downCount,
20
+ upCount,
21
+ ];
22
+ };
23
+
24
+ function down(current) {
25
+ let count = 0;
26
+
27
+ do {
28
+ ++count;
29
+ current = current.get('left');
30
+ const {operator} = current.node;
31
+
32
+ if (operator !== '||' && operator !== '&&')
33
+ break;
34
+ } while (current.isLogicalExpression());
35
+
36
+ return [count];
37
+ }
38
+
39
+ function up(current) {
40
+ let count = 0;
41
+
42
+ do {
43
+ ++count;
44
+ current = current.parentPath;
45
+ } while (current.isLogicalExpression());
46
+
47
+ return [
48
+ count, {
49
+ type: current.type,
50
+ },
51
+ ];
52
+ }
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ const {isParens} = require('../../maybe/maybe-parens');
4
+ const {chain, isRootOk} = require('./chain');
5
+
6
+ module.exports.LogicalExpression = {
7
+ condition(path) {
8
+ if (isParens(path))
9
+ return true;
10
+
11
+ return path.parentPath.isAwaitExpression();
12
+ },
13
+ before(path, {print}) {
14
+ print('(');
15
+ },
16
+ print(path, {print, maybe}, semantics) {
17
+ print('__left');
18
+
19
+ const needNewLine = isNewLine(path, semantics);
20
+
21
+ maybe.indent.inc(needNewLine);
22
+ needNewLine ? print.breakline() : print.space();
23
+ maybe.indent.dec(needNewLine);
24
+
25
+ print(path.node.operator);
26
+ print.space();
27
+ print('__right');
28
+ },
29
+ after(path, {print}) {
30
+ print(')');
31
+ },
32
+ };
33
+
34
+ function isNewLine(path, semantics) {
35
+ const [root, count] = chain(path);
36
+
37
+ if (!isRootOk(root))
38
+ return false;
39
+
40
+ if (count <= semantics.maxElementsInOneLine)
41
+ return false;
42
+
43
+ const {operator} = path.node;
44
+
45
+ return operator === '||' || operator === '&&';
46
+ }
@@ -1,9 +1,10 @@
1
1
  'use strict';
2
2
 
3
+ const isParens = (path) => path.node.extra?.parenthesized;
4
+
5
+ module.exports.isParens = isParens;
3
6
  module.exports.maybeParens = (print) => ({
4
- condition(path) {
5
- return path.node.extra?.parenthesized;
6
- },
7
+ condition: isParens,
7
8
  before(path, {write}) {
8
9
  write('(');
9
10
  },
@@ -1,25 +1,17 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- maybeParenOpen,
5
- maybeParenClose,
6
- } = require('../../expressions/unary-expression/parens');
7
-
3
+ const {maybeParens} = require('../../maybe/maybe-parens');
8
4
  const insideTypeDeclaration = ({parentPath}) => parentPath.isTSTypeAliasDeclaration();
9
5
 
10
- module.exports.TSUnionType = (path, printer, {maxTypesInOneLine}) => {
6
+ module.exports.TSUnionType = maybeParens((path, printer, {maxTypesInOneLine}) => {
11
7
  const types = path.get('types');
12
8
  const {length} = types;
13
9
 
14
- maybeParenOpen(path, printer);
15
-
16
10
  if (!insideTypeDeclaration(path) || length <= maxTypesInOneLine)
17
11
  printInOneLine(types, printer);
18
12
  else
19
13
  printInCoupleLines(types, printer);
20
-
21
- maybeParenClose(path, printer);
22
- };
14
+ });
23
15
 
24
16
  function printInOneLine(types, {traverse, write}) {
25
17
  const n = types.length - 1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "11.2.0",
3
+ "version": "11.3.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",
@@ -1,13 +0,0 @@
1
- 'use strict';
2
-
3
- const isParens = (path) => path.node.extra?.parenthesized;
4
-
5
- module.exports.isParens = isParens;
6
-
7
- module.exports.maybeParenOpen = (path, {maybe}) => {
8
- maybe.write(isParens(path), '(');
9
- };
10
-
11
- module.exports.maybeParenClose = (path, {maybe}) => {
12
- maybe.write(isParens(path), ')');
13
- };