@putout/printer 1.67.0 → 1.69.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.04.23, v1.69.0
2
+
3
+ feature:
4
+ - fb7202b @putout/printer: improve support of newline after IfStatement inside of ObjectMethod
5
+
6
+ 2023.04.23, v1.68.0
7
+
8
+ feature:
9
+ - 4975e30 @putout/printer: comment in ReturnStatement
10
+
1
11
  2023.04.21, v1.67.0
2
12
 
3
13
  feature:
@@ -55,10 +55,11 @@ module.exports.parseTrailingComments = (path, {write, maybe}) => {
55
55
  for (const {type, value, loc} of trailingComments) {
56
56
  if (type === 'CommentLine') {
57
57
  const sameLine = isSameLine(path, loc);
58
+
58
59
  maybe.write.space(sameLine);
59
60
  maybe.indent(!sameLine);
60
61
  write(`//${value}`);
61
- maybe.write.newline(!sameLine);
62
+ write.newline();
62
63
  }
63
64
  }
64
65
  };
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ module.exports.FunctionExpression = (path, {print, maybe, write, traverse}) => {
4
+ const {node} = path;
5
+
6
+ const {
7
+ generator,
8
+ async,
9
+ } = node;
10
+
11
+ maybe.write(async, 'async ');
12
+ write('function');
13
+ maybe.write(generator, '*');
14
+
15
+ const id = path.get('id');
16
+
17
+ if (id.node) {
18
+ write.space();
19
+ traverse(id);
20
+ }
21
+
22
+ print('(');
23
+
24
+ const params = path.get('params');
25
+ const n = params.length;
26
+
27
+ for (let i = 0; i < n; i++) {
28
+ print(params[i]);
29
+
30
+ if (i < n - 1)
31
+ print(', ');
32
+ }
33
+
34
+ print(') ');
35
+ print('__body');
36
+ };
37
+
@@ -3,65 +3,12 @@
3
3
  const {ArrowFunctionExpression} = require('./arrow-function-expression');
4
4
  const {FunctionDeclaration} = require('./function-declaration');
5
5
  const {ClassMethod} = require('./class-method');
6
+ const {ObjectMethod} = require('./object-method');
7
+ const {FunctionExpression} = require('./function-expression');
6
8
 
7
- module.exports.ArrowFunctionExpression = ArrowFunctionExpression;
8
9
  module.exports.FunctionDeclaration = FunctionDeclaration;
10
+ module.exports.FunctionExpression = FunctionExpression;
11
+ module.exports.ArrowFunctionExpression = ArrowFunctionExpression;
9
12
  module.exports.ClassMethod = ClassMethod;
13
+ module.exports.ObjectMethod = ObjectMethod;
10
14
 
11
- module.exports.FunctionExpression = (path, {print, maybe, write, traverse}) => {
12
- const {node} = path;
13
-
14
- const {
15
- generator,
16
- async,
17
- } = node;
18
-
19
- maybe.write(async, 'async ');
20
- write('function');
21
- maybe.write(generator, '*');
22
-
23
- const id = path.get('id');
24
-
25
- if (id.node) {
26
- write.space();
27
- traverse(id);
28
- }
29
-
30
- print('(');
31
-
32
- const params = path.get('params');
33
- const n = params.length;
34
-
35
- for (let i = 0; i < n; i++) {
36
- print(params[i]);
37
-
38
- if (i < n - 1)
39
- print(', ');
40
- }
41
-
42
- print(') ');
43
- print('__body');
44
- };
45
-
46
- module.exports.ObjectMethod = (path, {print}) => {
47
- const {kind} = path.node;
48
-
49
- if (kind !== 'method')
50
- print(`${kind} `);
51
-
52
- print('__key');
53
- print('(');
54
-
55
- const params = path.get('params');
56
- const n = params.length - 1;
57
-
58
- for (let i = 0; i <= n; i++) {
59
- print(params[i]);
60
-
61
- if (i < n)
62
- print(', ');
63
- }
64
-
65
- print(') ');
66
- print('__body');
67
- };
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ module.exports.ObjectMethod = (path, {print}) => {
4
+ const {kind} = path.node;
5
+
6
+ if (kind !== 'method')
7
+ print(`${kind} `);
8
+
9
+ print('__key');
10
+ print('(');
11
+
12
+ const params = path.get('params');
13
+ const n = params.length - 1;
14
+
15
+ for (let i = 0; i <= n; i++) {
16
+ print(params[i]);
17
+
18
+ if (i < n)
19
+ print(', ');
20
+ }
21
+
22
+ print(') ');
23
+ print('__body');
24
+ };
@@ -4,6 +4,7 @@ const {
4
4
  isCoupleLines,
5
5
  isForOf,
6
6
  isIf,
7
+ noTrailingComment,
7
8
  } = require('../is');
8
9
 
9
10
  const {isFunction} = require('@babel/types');
@@ -46,7 +47,7 @@ module.exports.ObjectExpression = (path, {print, maybe, indent}) => {
46
47
  }
47
48
 
48
49
  print(property);
49
- maybe.print.newline(manyLines);
50
+ maybe.print.newline(manyLines && noTrailingComment(property));
50
51
  }
51
52
 
52
53
  indent.dec();
@@ -85,3 +85,5 @@ module.exports.satisfy = (conditions) => (path) => {
85
85
 
86
86
  return false;
87
87
  };
88
+
89
+ module.exports.noTrailingComment = (path) => !path.node.trailingComments;
@@ -23,7 +23,9 @@ module.exports.maybeThrow = (a, path, b) => {
23
23
  }));
24
24
  };
25
25
 
26
- const maybeProgram = (ast) => isProgram(ast) ? ast : Program([ExpressionStatement(ast)]);
26
+ const maybeProgram = (ast) => isProgram(ast) ? ast : Program([
27
+ ExpressionStatement(ast),
28
+ ]);
27
29
 
28
30
  module.exports.maybeFile = (ast) => isFile(ast) ? ast : File(maybeProgram(ast));
29
31
 
@@ -6,6 +6,7 @@ const {
6
6
  isLast,
7
7
  exists,
8
8
  } = require('../is');
9
+ const {isObjectMethod} = require('@babel/types');
9
10
 
10
11
  const isFirstStatement = (path) => path.get('body.0')?.isStatement();
11
12
 
@@ -48,6 +49,9 @@ function shouldAddNewlineAfter(path) {
48
49
  if (!isNext(path) && !isNext(path.parentPath) && isParentProgram(path.parentPath))
49
50
  return false;
50
51
 
52
+ if (path.find(isObjectMethod))
53
+ return true;
54
+
51
55
  if (parentPath.isStatement() && !path.node.body.length && !isNext(parentPath))
52
56
  return false;
53
57
 
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const {markAfter} = require('../mark');
4
+ const {exists} = require('../is');
4
5
 
5
6
  const isEmptyConsequent = (path) => path.get('consequent').isEmptyStatement();
6
7
 
@@ -33,7 +34,7 @@ module.exports.IfStatement = {
33
34
  write('else');
34
35
  write.space();
35
36
  traverse(alternate);
36
- } else if (alternate.node) {
37
+ } else if (exists(alternate)) {
37
38
  write('else');
38
39
  print.newline();
39
40
  indent.inc();
@@ -53,8 +54,7 @@ module.exports.IfStatement = {
53
54
  return true;
54
55
  },
55
56
  after: (path, {print}) => {
56
- print.indent();
57
- print.newline();
57
+ print.linebreak();
58
58
  markAfter(path);
59
59
  },
60
60
  };
@@ -1,25 +1,38 @@
1
1
  'use strict';
2
2
 
3
- const {isPrevBody} = require('../is');
3
+ const {
4
+ isPrevBody,
5
+ noTrailingComment,
6
+ } = require('../is');
7
+
4
8
  const {hasPrevNewline} = require('../mark');
5
9
  const isBodyLength = ({parentPath}) => parentPath.node?.body?.length > 2;
6
10
 
7
- module.exports.ReturnStatement = (path, {indent, traverse, print}) => {
8
- if (!hasPrevNewline(path) && isBodyLength(path) || isPrevBody(path)) {
11
+ module.exports.ReturnStatement = {
12
+ beforeIf(path) {
13
+ return !hasPrevNewline(path) && isBodyLength(path) || isPrevBody(path);
14
+ },
15
+ before(path, {print}) {
9
16
  print.indent();
10
17
  print.newline();
11
- }
12
-
13
- indent();
14
- print('return');
15
-
16
- const argPath = path.get('argument');
17
-
18
- if (argPath.node) {
19
- print(' ');
20
- traverse(argPath);
21
- }
22
-
23
- print(';');
24
- print.newline();
18
+ },
19
+ print(path, {indent, traverse, print}) {
20
+ indent();
21
+ print('return');
22
+
23
+ const argPath = path.get('argument');
24
+
25
+ if (argPath.node) {
26
+ print(' ');
27
+ traverse(argPath);
28
+ }
29
+
30
+ print(';');
31
+ },
32
+ afterSatisfy: () => [
33
+ noTrailingComment,
34
+ ],
35
+ after(path, {print}) {
36
+ print.newline();
37
+ },
25
38
  };
@@ -25,4 +25,3 @@ module.exports.TSTypeParameter = (path, {write, traverse}) => {
25
25
 
26
26
  traverse(constraint);
27
27
  };
28
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.67.0",
3
+ "version": "1.69.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Easiest possible opinionated Babel AST printer made with ❤️ to use in 🐊Putout",