@putout/printer 1.50.0 → 1.51.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,10 @@
1
+ 2023.04.17, v1.51.0
2
+
3
+ feature:
4
+ - 0fc4fb0 @putout/printer: add ability to preserve newline between ExpressionStatements
5
+ - 8073d64 @putout/printer: CallExpression: simplify
6
+ - b94009b @putout/printer: add ability to preserve newline
7
+
1
8
  2023.04.14, v1.50.0
2
9
 
3
10
  fix:
@@ -1,14 +1,8 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- isFirst,
5
- isNext,
6
- } = require('./is');
3
+ const {isNext} = require('./is');
7
4
 
8
- const {
9
- markBefore,
10
- hasPrevNewline,
11
- } = require('./mark');
5
+ const {markBefore} = require('./mark');
12
6
 
13
7
  module.exports.parseLeadingComments = (path, {print, maybe}) => {
14
8
  const {leadingComments} = path.node;
@@ -16,9 +10,6 @@ module.exports.parseLeadingComments = (path, {print, maybe}) => {
16
10
  if (!leadingComments || !leadingComments.length)
17
11
  return;
18
12
 
19
- if (shouldAddNewlineBefore(path))
20
- print.linebreak();
21
-
22
13
  const isClass = !path.isClassMethod();
23
14
 
24
15
  for (const {type, value} of leadingComments) {
@@ -65,10 +56,6 @@ module.exports.parseTrailingComments = (path, {write, maybe}) => {
65
56
  }
66
57
  };
67
58
 
68
- function shouldAddNewlineBefore(path) {
69
- return path.isStatement() && !isFirst(path) && !hasPrevNewline(path);
70
- }
71
-
72
59
  function isSameLine(path, loc) {
73
60
  return path.node.loc.start.line === loc.start.line;
74
61
  }
@@ -2,58 +2,55 @@
2
2
 
3
3
  const {exists} = require('../is');
4
4
 
5
- const CallExpression = {
6
- print(path, {indent, print, maybe, traverse}) {
7
- const isParentCall = toLong(path) && path.parentPath.isCallExpression();
8
- const callee = path.get('callee');
9
- const typeParameters = path.get('typeParameters');
10
- const isFn = callee.isFunction();
11
-
12
- maybe.write(isFn, '(');
13
- traverse(callee);
14
-
15
- if (exists(typeParameters))
16
- traverse(typeParameters);
17
-
18
- maybe.write(isFn, ')');
19
-
20
- if (path.node.optional)
21
- print('?.');
22
-
23
- print('(');
24
-
25
- const args = path.get('arguments');
26
- const n = args.length - 1;
27
-
28
- maybe.indent.inc(isParentCall);
5
+ function CallExpression(path, {indent, print, maybe, traverse}) {
6
+ const isParentCall = toLong(path) && path.parentPath.isCallExpression();
7
+ const callee = path.get('callee');
8
+ const typeParameters = path.get('typeParameters');
9
+ const isFn = callee.isFunction();
10
+
11
+ maybe.write(isFn, '(');
12
+ traverse(callee);
13
+
14
+ if (exists(typeParameters))
15
+ traverse(typeParameters);
16
+
17
+ maybe.write(isFn, ')');
18
+
19
+ if (path.node.optional)
20
+ print('?.');
21
+
22
+ print('(');
23
+
24
+ const args = path.get('arguments');
25
+ const n = args.length - 1;
26
+
27
+ maybe.indent.inc(isParentCall);
28
+
29
+ for (const [i, arg] of args.entries()) {
30
+ const isObject = arg.isObjectExpression();
29
31
 
30
- for (const [i, arg] of args.entries()) {
31
- const isObject = arg.isObjectExpression();
32
-
33
- if (isParentCall && !isObject) {
34
- print.newline();
35
- indent();
36
- }
37
-
38
- print(arg);
39
-
40
- if (isParentCall) {
41
- print(',');
42
- continue;
43
- }
44
-
45
- maybe.print(i < n, ', ');
32
+ if (isParentCall && !isObject) {
33
+ print.newline();
34
+ indent();
46
35
  }
47
36
 
37
+ print(arg);
38
+
48
39
  if (isParentCall) {
49
- indent.dec();
50
- print.breakline();
40
+ print(',');
41
+ continue;
51
42
  }
52
43
 
53
- print(')');
54
- },
55
- };
56
-
44
+ maybe.print(i < n, ', ');
45
+ }
46
+
47
+ if (isParentCall) {
48
+ indent.dec();
49
+ print.breakline();
50
+ }
51
+
52
+ print(')');
53
+ }
57
54
  module.exports.OptionalCallExpression = CallExpression;
58
55
  module.exports.CallExpression = CallExpression;
59
56
 
@@ -38,7 +38,6 @@ module.exports.isParentBlock = isParentBlock;
38
38
  module.exports.isLast = isLast;
39
39
  module.exports.isParentLast = (path) => isLast(path.parentPath);
40
40
  module.exports.isCoupleLines = isCoupleLines;
41
- module.exports.isNextCoupleLines = isNextCoupleLines;
42
41
 
43
42
  function isCoupleLines(path) {
44
43
  const start = path.node?.loc?.start?.line;
@@ -47,10 +46,6 @@ function isCoupleLines(path) {
47
46
  return end > start;
48
47
  }
49
48
  module.exports.exists = (a) => a.node;
50
-
51
- function isNextCoupleLines(path) {
52
- return isCoupleLines(path.getNextSibling());
53
- }
54
49
  module.exports.isStringAndIdentifier = ([a, b]) => isStringLiteral(a) && isIdentifier(b);
55
50
 
56
51
  const isIfOrStatement = (a) => isIfStatement(a) || isStatement(a);
@@ -71,3 +66,13 @@ module.exports.isForOf = (path) => {
71
66
 
72
67
  return false;
73
68
  };
69
+
70
+ module.exports.isNewlineBetweenStatements = (path) => {
71
+ const endCurrent = path.node?.loc?.end?.line;
72
+ const startNext = path.getNextSibling().node?.loc?.start?.line;
73
+
74
+ if (!startNext)
75
+ return false;
76
+
77
+ return startNext - endCurrent > 1;
78
+ };
@@ -5,20 +5,10 @@ const {
5
5
  isParentProgram,
6
6
  isLast,
7
7
  isParentBlock,
8
- isCoupleLines,
9
- isNextCoupleLines,
10
8
  isParentLast,
9
+ isNewlineBetweenStatements,
11
10
  } = require('../is');
12
11
 
13
- const isNextDifferent = (path) => {
14
- const next = path.getNextSibling();
15
-
16
- if (!next.isExpressionStatement())
17
- return true;
18
-
19
- return next.node.expression.type !== path.node.expression.type;
20
- };
21
-
22
12
  module.exports.ExpressionStatement = {
23
13
  print(path, {indent, print, maybe, store}) {
24
14
  indent();
@@ -41,6 +31,9 @@ module.exports.ExpressionStatement = {
41
31
  };
42
32
 
43
33
  function shouldBreakline(path) {
34
+ if (isNewlineBetweenStatements(path))
35
+ return true;
36
+
44
37
  if (isLast(path) || isParentLast(path))
45
38
  return false;
46
39
 
@@ -53,12 +46,6 @@ function shouldBreakline(path) {
53
46
  if (isStrictMode(path))
54
47
  return true;
55
48
 
56
- if (isNext(path) && isNextDifferent(path) && path.parentPath.node.body?.length > 2)
57
- return true;
58
-
59
- if (isCoupleLines(path) || isNextCoupleLines(path))
60
- return true;
61
-
62
49
  return false;
63
50
  }
64
51
 
@@ -1,18 +1,10 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- hasPrevNewline,
5
- markAfter,
6
- } = require('../mark');
3
+ const {markAfter} = require('../mark');
7
4
 
8
- const {isFirst} = require('../is');
9
5
  const isEmptyConsequent = (path) => path.get('consequent').isEmptyStatement();
10
6
 
11
7
  module.exports.IfStatement = {
12
- before: (path, {print}) => {
13
- print.linebreak();
14
- },
15
- beforeIf: shouldAddNewlineBefore,
16
8
  print: (path, {indent, print, maybe, write, traverse}) => {
17
9
  indent();
18
10
  print('if (');
@@ -66,10 +58,3 @@ module.exports.IfStatement = {
66
58
  markAfter(path);
67
59
  },
68
60
  };
69
-
70
- function shouldAddNewlineBefore(path) {
71
- if (path.parentPath.isIfStatement())
72
- return false;
73
-
74
- return !isFirst(path) && !hasPrevNewline(path);
75
- }
@@ -3,6 +3,7 @@
3
3
  const {
4
4
  isNext,
5
5
  isCoupleLines,
6
+ isNewlineBetweenStatements,
6
7
  } = require('../is');
7
8
 
8
9
  const {
@@ -67,6 +68,9 @@ function shouldAddNewlineAfter(path) {
67
68
  if (prev.isVariableDeclaration() && !next.isVariableDeclaration())
68
69
  return true;
69
70
 
71
+ if (isNewlineBetweenStatements(path))
72
+ return true;
73
+
70
74
  return false;
71
75
  }
72
76
  const isLast = (path) => path.parentPath.isProgram() && !isNext(path);
@@ -7,9 +7,11 @@ module.exports.TSTypeAliasDeclaration = {
7
7
  print(path, {print, maybe, store}) {
8
8
  print('type ');
9
9
  print('__id');
10
+
10
11
  print.space();
11
12
  print('=');
12
13
  print.space();
14
+
13
15
  print('__typeAnnotation');
14
16
  print(';');
15
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.50.0",
3
+ "version": "1.51.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",