@putout/printer 3.4.0 → 3.5.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,8 @@
1
+ 2023.08.10, v3.5.0
2
+
3
+ feature:
4
+ - ae4808e maybeParens
5
+
1
6
  2023.08.10, v3.4.0
2
7
 
3
8
  feature:
@@ -2,50 +2,40 @@
2
2
 
3
3
  const {exists} = require('../../is');
4
4
  const {printParams} = require('./params');
5
+ const {maybeParens} = require('./parens');
5
6
 
6
- module.exports.ArrowFunctionExpression = {
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 {async} = path.node;
22
-
23
- print('__typeParameters');
24
- maybe.print(async, 'async ');
25
-
26
- printParams(path, printer, semantics);
27
-
28
- const returnType = path.get('returnType');
29
-
30
- if (exists(returnType)) {
31
- write(':');
32
- write.space();
33
- traverse(returnType);
34
- }
35
-
36
- print.space();
37
- print('=>');
38
-
39
- const body = path.get('body');
40
-
41
- const insideCall = path.parentPath.isCallExpression();
42
- const isJSX = body.isJSXElement();
43
-
44
- maybe.print.space(!insideCall && isJSX);
45
- maybe.print.space(!isJSX);
46
- print('__body');
47
- },
48
- after(path, {write}) {
49
- write(')');
50
- },
51
- };
7
+ module.exports.ArrowFunctionExpression = maybeParens((path, printer, semantics) => {
8
+ const {
9
+ print,
10
+ maybe,
11
+ write,
12
+ traverse,
13
+ } = printer;
14
+
15
+ const {async} = path.node;
16
+
17
+ print('__typeParameters');
18
+ maybe.print(async, 'async ');
19
+
20
+ printParams(path, printer, semantics);
21
+
22
+ const returnType = path.get('returnType');
23
+
24
+ if (exists(returnType)) {
25
+ write(':');
26
+ write.space();
27
+ traverse(returnType);
28
+ }
29
+
30
+ print.space();
31
+ print('=>');
32
+
33
+ const body = path.get('body');
34
+
35
+ const insideCall = path.parentPath.isCallExpression();
36
+ const isJSX = body.isJSXElement();
37
+
38
+ maybe.print.space(!insideCall && isJSX);
39
+ maybe.print.space(!isJSX);
40
+ print('__body');
41
+ });
@@ -2,42 +2,32 @@
2
2
 
3
3
  const {exists} = require('../../is');
4
4
  const {printParams} = require('./params');
5
+ const {maybeParens} = require('./parens');
5
6
 
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
- },
43
- };
7
+ module.exports.FunctionExpression = maybeParens((path, printer, semantics) => {
8
+ const {
9
+ print,
10
+ maybe,
11
+ write,
12
+ traverse,
13
+ } = printer;
14
+
15
+ const {node} = path;
16
+ const {generator, async} = node;
17
+
18
+ maybe.write(async, 'async ');
19
+ write('function');
20
+ maybe.write(generator, '*');
21
+
22
+ const id = path.get('id');
23
+
24
+ if (exists(id)) {
25
+ write(' ');
26
+ traverse(id);
27
+ }
28
+
29
+ printParams(path, printer, semantics);
30
+
31
+ print.space();
32
+ print('__body');
33
+ });
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ module.exports.maybeParens = (print) => ({
4
+ condition(path) {
5
+ return path.node.extra?.parenthesized;
6
+ },
7
+ before(path, {write}) {
8
+ write('(');
9
+ },
10
+ print,
11
+ after(path, {write}) {
12
+ write(')');
13
+ },
14
+ });
@@ -1,10 +1,21 @@
1
1
  'use strict';
2
2
 
3
3
  const {isLast, isNext} = require('../../is');
4
- const {
5
- maybeParenOpen,
6
- maybeParenClose,
7
- } = require('./parens');
4
+
5
+ const {maybeParens} = require('../function/parens');
6
+
7
+ const isWord = (a) => /^(delete|typeof|void|throw)$/.test(a);
8
+
9
+ const unaryExpression = maybeParens((path, printer) => {
10
+ const {maybe, traverse} = printer;
11
+ const {prefix, operator} = path.node;
12
+ const argPath = path.get('argument');
13
+
14
+ maybe.print(prefix, operator);
15
+ maybe.print(isWord(operator), ' ');
16
+ traverse(argPath);
17
+ maybe.print(!prefix, operator);
18
+ });
8
19
 
9
20
  module.exports.UnaryExpression = unaryExpression;
10
21
  module.exports.UpdateExpression = unaryExpression;
@@ -40,20 +51,3 @@ function printUnary(path, name, {print}) {
40
51
  print(`${name} `);
41
52
  print('__argument');
42
53
  }
43
-
44
- const isWord = (a) => /^(delete|typeof|void|throw)$/.test(a);
45
-
46
- function unaryExpression(path, printer) {
47
- const {maybe, traverse} = printer;
48
- const {prefix, operator} = path.node;
49
- const argPath = path.get('argument');
50
-
51
- maybeParenOpen(path, printer);
52
-
53
- maybe.print(prefix, operator);
54
- maybe.print(isWord(operator), ' ');
55
- traverse(argPath);
56
- maybe.print(!prefix, operator);
57
-
58
- maybeParenClose(path, printer);
59
- }
@@ -1,13 +1,9 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- maybeParenOpen,
5
- maybeParenClose,
6
- } = require('../expressions/unary-expression/parens');
7
-
8
3
  const {maybeDecorators} = require('../maybe-get');
4
+ const {maybeParens} = require('../expressions/function/parens');
9
5
 
10
- module.exports.Identifier = (path, printer) => {
6
+ module.exports.Identifier = maybeParens((path, printer) => {
11
7
  const {
12
8
  write,
13
9
  maybe,
@@ -20,8 +16,6 @@ module.exports.Identifier = (path, printer) => {
20
16
 
21
17
  const typeAnnotation = path.get('typeAnnotation');
22
18
 
23
- maybeParenOpen(path, printer);
24
-
25
19
  for (const decorator of maybeDecorators(path)) {
26
20
  traverse(decorator);
27
21
  print(' ');
@@ -30,6 +24,4 @@ module.exports.Identifier = (path, printer) => {
30
24
  write(name);
31
25
  maybe.write(optional, '?');
32
26
  traverse(typeAnnotation);
33
-
34
- maybeParenClose(path, printer);
35
- };
27
+ });
@@ -2,19 +2,15 @@
2
2
 
3
3
  const {printParams} = require('../../expressions/function/params');
4
4
  const {printReturnType} = require('./print-return-type');
5
- const {
6
- maybeParenOpen,
7
- maybeParenClose,
8
- } = require('../../expressions/unary-expression/parens');
9
5
 
10
- module.exports.TSFunctionType = (path, printer, semantics) => {
6
+ const {maybeParens} = require('../../expressions/function/parens');
7
+
8
+ module.exports.TSFunctionType = maybeParens((path, printer, semantics) => {
11
9
  const {print} = printer;
12
10
 
13
- maybeParenOpen(path, printer);
14
11
  printParams(path, printer, semantics);
15
12
  print.space();
16
13
  print('=>');
17
14
  print.space();
18
15
  printReturnType(path, printer);
19
- maybeParenClose(path, printer);
20
- };
16
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "3.4.0",
3
+ "version": "3.5.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",