@putout/printer 1.126.0 → 1.128.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,14 @@
1
+ 2023.06.01, v1.128.0
2
+
3
+ feature:
4
+ - 118671c @putout/printer: ArrowFunctionExpression: add ability to drop braces
5
+
6
+ 2023.06.01, v1.127.0
7
+
8
+ feature:
9
+ - e2ad1fe @putout/printer: DoWhileStatement: add support
10
+ - 5ed5799 @putout/printer: IfStatement: alternate: space
11
+
1
12
  2023.06.01, v1.126.0
2
13
 
3
14
  feature:
package/README.md CHANGED
@@ -82,6 +82,8 @@ write(ast, {
82
82
  space: ' ',
83
83
  comments: true,
84
84
  splitter: '\n',
85
+ roundBraceOpen: '(',
86
+ roundBraceClose: ')',
85
87
  },
86
88
  visitors: {
87
89
  AssignmentPattern(path, {write}) {
@@ -1,7 +1,14 @@
1
1
  'use strict';
2
2
 
3
+ const isOneArgArrow = (path) => {
4
+ if (path.type !== 'ArrowFunctionExpression')
5
+ return false;
6
+
7
+ return path.node.params.length === 1;
8
+ };
9
+
3
10
  module.exports.printParams = (path, {print}) => {
4
- print('(');
11
+ printBraceOpen(path, {print});
5
12
 
6
13
  const params = path.get('params');
7
14
  const n = params.length;
@@ -15,5 +22,19 @@ module.exports.printParams = (path, {print}) => {
15
22
  }
16
23
  }
17
24
 
18
- print(')');
25
+ printBraceClose(path, {print});
19
26
  };
27
+
28
+ function printBraceOpen(path, {print}) {
29
+ if (isOneArgArrow(path))
30
+ return print.roundBraceOpen();
31
+
32
+ return print('(');
33
+ }
34
+
35
+ function printBraceClose(path, {print}) {
36
+ if (isOneArgArrow(path))
37
+ return print.roundBraceClose();
38
+
39
+ print(')');
40
+ }
@@ -0,0 +1,12 @@
1
+ 'use strict';
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(')');
12
+ };
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const {markAfter} = require('../mark');
4
- const {exists} = require('../is');
3
+ const {markAfter} = require('../../mark');
4
+ const {exists} = require('../../is');
5
5
 
6
6
  const isEmptyConsequent = (path) => path.get('consequent').isEmptyStatement();
7
7
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  const {ExpressionStatement} = require('./expression-statement');
4
4
  const {VariableDeclaration} = require('./variable-declaration/variable-declaration');
5
- const {IfStatement} = require('./if-statement');
5
+ const {IfStatement} = require('./if-statement/if-statement');
6
6
  const {ForOfStatement} = require('./for-of-statement');
7
7
  const {BlockStatement} = require('./block-statement');
8
8
  const {ReturnStatement} = require('./return-statement/return-statement');
@@ -18,6 +18,7 @@ const {SwitchStatement} = require('./switch-statement');
18
18
  const {ForInStatement} = require('./for-in-statement');
19
19
  const {ExportDefaultDeclaration} = require('./export-declaration/export-default-declaration');
20
20
  const {BreakStatement} = require('./break-statement');
21
+ const {DoWhileStatement} = require('./do-while-statement');
21
22
 
22
23
  const {
23
24
  ExportNamespaceSpecifier,
@@ -28,6 +29,7 @@ module.exports = {
28
29
  ...importDeclarations,
29
30
  ...exportDeclarations,
30
31
  BlockStatement,
32
+ DoWhileStatement,
31
33
  ExpressionStatement,
32
34
  ExportSpecifier,
33
35
  ExportNamespaceSpecifier,
@@ -34,6 +34,7 @@ const {
34
34
 
35
35
  const isString = (a) => typeof a === 'string';
36
36
  const {assign} = Object;
37
+ const callWith = (fn, a) => () => fn(a);
37
38
 
38
39
  const traversers = {
39
40
  ...expressions,
@@ -53,6 +54,8 @@ function initFormat(format) {
53
54
  space: ' ',
54
55
  comments: true,
55
56
  splitter: '\n',
57
+ roundBraceOpen: '(',
58
+ roundBraceClose: ')',
56
59
  ...format,
57
60
  };
58
61
  }
@@ -110,6 +113,16 @@ module.exports.tokenize = (ast, overrides = {}) => {
110
113
  });
111
114
  };
112
115
 
116
+ const roundBraceOpen = callWith(addToken, {
117
+ type: TYPES.ROUND_BRACE_OPEN,
118
+ value: format.roundBraceOpen,
119
+ });
120
+
121
+ const roundBraceClose = callWith(addToken, {
122
+ type: TYPES.ROUND_BRACE_Close,
123
+ value: format.roundBraceClose,
124
+ });
125
+
113
126
  const linebreak = () => {
114
127
  indent();
115
128
  newline();
@@ -202,6 +215,8 @@ module.exports.tokenize = (ast, overrides = {}) => {
202
215
  assign(print, write, {
203
216
  space,
204
217
  round,
218
+ roundBraceOpen,
219
+ roundBraceClose,
205
220
  });
206
221
 
207
222
  assign(printer, {
package/lib/types.js CHANGED
@@ -8,4 +8,6 @@ module.exports.TYPES = {
8
8
  INDENT: 'Indent',
9
9
  DEBUG: 'Debug',
10
10
  SPACE: 'Space',
11
+ ROUND_BRACE_OPEN: '(',
12
+ ROUND_BRACE_CLOSE: ')',
11
13
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.126.0",
3
+ "version": "1.128.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",