@putout/printer 1.50.0 → 1.52.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,15 @@
1
+ 2023.04.17, v1.52.0
2
+
3
+ feature:
4
+ - bf2b9a3 @putout/printer: add support of afterSatisfy
5
+
6
+ 2023.04.17, v1.51.0
7
+
8
+ feature:
9
+ - 0fc4fb0 @putout/printer: add ability to preserve newline between ExpressionStatements
10
+ - 8073d64 @putout/printer: CallExpression: simplify
11
+ - b94009b @putout/printer: add ability to preserve newline
12
+
1
13
  2023.04.14, v1.50.0
2
14
 
3
15
  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
+ };
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ module.exports = (plugin) => {
4
+ if (!plugin.afterSatisfy && !plugin.beforeSatisfy)
5
+ return plugin;
6
+
7
+ return {
8
+ afterIf: createIf(plugin.afterSatisfy),
9
+ beforeIf: createIf(plugin.beforeSatisfy),
10
+ ...plugin,
11
+ };
12
+ };
13
+
14
+ const createIf = (getConditions) => (path) => {
15
+ const conditions = getConditions?.() || [];
16
+
17
+ for (const condition of conditions) {
18
+ if (condition(path))
19
+ return true;
20
+ }
21
+
22
+ return false;
23
+ };
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const rendy = require('rendy');
4
+ const maybeSatisfy = require('./maybe/satisfy');
4
5
 
5
6
  const {
6
7
  isProgram,
@@ -22,9 +23,7 @@ module.exports.maybeThrow = (a, path, b) => {
22
23
  }));
23
24
  };
24
25
 
25
- const maybeProgram = (ast) => isProgram(ast) ? ast : Program([
26
- ExpressionStatement(ast),
27
- ]);
26
+ const maybeProgram = (ast) => isProgram(ast) ? ast : Program([ExpressionStatement(ast)]);
28
27
 
29
28
  module.exports.maybeFile = (ast) => isFile(ast) ? ast : File(maybeProgram(ast));
30
29
 
@@ -44,7 +43,7 @@ function objectPlugin(plugin, path, printer) {
44
43
  beforeIf = condition,
45
44
  after = split,
46
45
  afterIf = condition,
47
- } = plugin;
46
+ } = maybeSatisfy(plugin);
48
47
 
49
48
  if (beforeIf?.(path, printer)) {
50
49
  before(path, printer);
@@ -2,23 +2,12 @@
2
2
 
3
3
  const {
4
4
  isNext,
5
- isParentProgram,
6
5
  isLast,
7
6
  isParentBlock,
8
- isCoupleLines,
9
- isNextCoupleLines,
10
7
  isParentLast,
8
+ isNewlineBetweenStatements,
11
9
  } = require('../is');
12
10
 
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
11
  module.exports.ExpressionStatement = {
23
12
  print(path, {indent, print, maybe, store}) {
24
13
  indent();
@@ -31,9 +20,12 @@ module.exports.ExpressionStatement = {
31
20
  store(true);
32
21
  }
33
22
  },
34
- afterIf(path) {
35
- return shouldAddNewLineAfter(path);
36
- },
23
+ afterSatisfy: () => [
24
+ isParentOrNotLastORParentLast,
25
+ isParentBlock,
26
+ isNext,
27
+ isNextUp,
28
+ ],
37
29
  after(path, {print, maybe, store}) {
38
30
  print.newline();
39
31
  maybe.markAfter(store(), path);
@@ -41,6 +33,9 @@ module.exports.ExpressionStatement = {
41
33
  };
42
34
 
43
35
  function shouldBreakline(path) {
36
+ if (isNewlineBetweenStatements(path))
37
+ return true;
38
+
44
39
  if (isLast(path) || isParentLast(path))
45
40
  return false;
46
41
 
@@ -53,25 +48,14 @@ function shouldBreakline(path) {
53
48
  if (isStrictMode(path))
54
49
  return true;
55
50
 
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
51
  return false;
63
52
  }
64
53
 
65
- function shouldAddNewLineAfter(path) {
66
- if (!isParentBlock(path) && (isLast(path) || isParentLast(path)))
67
- return false;
68
-
69
- if (isParentBlock(path) && !isParentProgram(path))
70
- return true;
71
-
72
- if (isNext(path))
73
- return true;
74
-
54
+ function isParentOrNotLastORParentLast(path) {
55
+ return isParentBlock(path) || !(isLast(path) || isParentLast(path));
56
+ }
57
+
58
+ function isNextUp(path) {
75
59
  return path.findParent(isNext);
76
60
  }
77
61
 
@@ -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.52.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",