@putout/printer 3.2.0 → 3.4.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,13 @@
1
+ 2023.08.10, v3.4.0
2
+
3
+ feature:
4
+ - d29fdfd (Arrow)FunctionExpression: parens
5
+
6
+ 2023.08.10, v3.3.0
7
+
8
+ feature:
9
+ - e1396e3 DoWhileStatement: indent, newline
10
+
1
11
  2023.08.10, v3.2.0
2
12
 
3
13
  feature:
@@ -7,16 +7,12 @@ function CallExpression(path, {indent, print, maybe, traverse}) {
7
7
 
8
8
  const callee = path.get('callee');
9
9
  const typeParameters = path.get('typeParameters');
10
- const isFn = callee.isFunction();
11
10
 
12
- maybe.write(isFn, '(');
13
11
  traverse(callee);
14
12
 
15
13
  if (exists(typeParameters))
16
14
  traverse(typeParameters);
17
15
 
18
- maybe.write(isFn, ')');
19
-
20
16
  if (path.node.optional)
21
17
  print('?.');
22
18
 
@@ -4,8 +4,8 @@ const {exists} = require('../../is');
4
4
  const {printParams} = require('./params');
5
5
 
6
6
  module.exports.ArrowFunctionExpression = {
7
- condition({parentPath}) {
8
- return parentPath.isLogicalExpression();
7
+ condition(path) {
8
+ return path.node.extra?.parenthesized;
9
9
  },
10
10
  before(path, {write}) {
11
11
  write('(');
@@ -21,7 +21,6 @@ module.exports.ArrowFunctionExpression = {
21
21
  const {async} = path.node;
22
22
 
23
23
  print('__typeParameters');
24
-
25
24
  maybe.print(async, 'async ');
26
25
 
27
26
  printParams(path, printer, semantics);
@@ -3,30 +3,41 @@
3
3
  const {exists} = require('../../is');
4
4
  const {printParams} = require('./params');
5
5
 
6
- module.exports.FunctionExpression = (path, printer, semantics) => {
7
- const {
8
- print,
9
- maybe,
10
- write,
11
- traverse,
12
- } = printer;
13
-
14
- const {node} = path;
15
- const {generator, async} = node;
16
-
17
- maybe.write(async, 'async ');
18
- write('function');
19
- maybe.write(generator, '*');
20
-
21
- const id = path.get('id');
22
-
23
- if (exists(id)) {
24
- write(' ');
25
- traverse(id);
26
- }
27
-
28
- printParams(path, printer, semantics);
29
-
30
- print.space();
31
- print('__body');
6
+ module.exports.FunctionExpression = {
7
+ condition(path) {
8
+ return path.node.extra?.parenthesized;
9
+ },
10
+ before(path, {write}) {
11
+ write('(');
12
+ },
13
+ print(path, printer, semantics) {
14
+ const {
15
+ print,
16
+ maybe,
17
+ write,
18
+ traverse,
19
+ } = printer;
20
+
21
+ const {node} = path;
22
+ const {generator, async} = node;
23
+
24
+ maybe.write(async, 'async ');
25
+ write('function');
26
+ maybe.write(generator, '*');
27
+
28
+ const id = path.get('id');
29
+
30
+ if (exists(id)) {
31
+ write(' ');
32
+ traverse(id);
33
+ }
34
+
35
+ printParams(path, printer, semantics);
36
+
37
+ print.space();
38
+ print('__body');
39
+ },
40
+ after(path, {write}) {
41
+ write(')');
42
+ },
32
43
  };
@@ -21,6 +21,13 @@ const isFirstStatement = (path) => path.node.body[0];
21
21
  const isFirstDirective = (path) => path.node.directives?.[0];
22
22
  const isMethodOrArrow = (path) => isArrowFunctionExpression(path) || isObjectMethod(path);
23
23
 
24
+ const parentIfWithoutElse = ({parentPath}) => {
25
+ if (!parentPath.isIfStatement())
26
+ return false;
27
+
28
+ return !parentPath.node.alternate;
29
+ };
30
+
24
31
  module.exports.BlockStatement = {
25
32
  print(path, {indent, maybe, write, traverse}, semantics) {
26
33
  const body = path.get('body');
@@ -65,6 +72,9 @@ module.exports.BlockStatement = {
65
72
  function shouldAddNewlineAfter(path) {
66
73
  const {parentPath} = path;
67
74
 
75
+ if (parentPath.isDoWhileStatement())
76
+ return false;
77
+
68
78
  if (parentPath.isBlockStatement())
69
79
  return true;
70
80
 
@@ -74,7 +84,7 @@ function shouldAddNewlineAfter(path) {
74
84
  if (insideIfWithNoBody(path))
75
85
  return false;
76
86
 
77
- if (path.parentPath.isIfStatement() && path.find(isMethodOrArrow))
87
+ if (parentIfWithoutElse(path) && path.find(isMethodOrArrow))
78
88
  return true;
79
89
 
80
90
  if (parentPath.isStatement() && !path.node.body.length && !isNext(parentPath))
@@ -1,12 +1,22 @@
1
1
  'use strict';
2
2
 
3
- module.exports.DoWhileStatement = (path, {print}) => {
4
- print('do');
5
- print.space();
6
- print('__body');
7
- print.space();
8
- print('while ');
9
- print('(');
10
- print('__test');
11
- print(')');
3
+ const {isLast} = require('../is');
4
+ const notLast = (path) => !isLast(path);
5
+
6
+ module.exports.DoWhileStatement = {
7
+ print(path, {print, indent}) {
8
+ indent();
9
+ print('do');
10
+ print.space();
11
+ print('__body');
12
+ print.space();
13
+ print('while ');
14
+ print('(');
15
+ print('__test');
16
+ print(')');
17
+ },
18
+ afterSatisfy: () => [notLast],
19
+ after(path, {print}) {
20
+ print.newline();
21
+ },
12
22
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "3.2.0",
3
+ "version": "3.4.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",