@putout/printer 1.6.10 → 1.6.12

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.03.22, v1.6.12
2
+
3
+ feature:
4
+ - 59b18a0 @putout/printer: simplify mark
5
+
6
+ 2023.03.22, v1.6.11
7
+
8
+ feature:
9
+ - 552e272 @putout/printer: improve support of ArrayExpression
10
+
1
11
  2023.03.22, v1.6.10
2
12
 
3
13
  feature:
@@ -1,8 +1,10 @@
1
1
  'use strict';
2
2
 
3
3
  const {isObjectProperty} = require('@babel/types');
4
+ const {isCoupleLines} = require('../is');
4
5
  const {entries} = Object;
5
6
  const isForOf = ({parentPath}) => parentPath.isForOfStatement();
7
+ const SECOND = 1;
6
8
 
7
9
  module.exports.ArrayExpression = (path, {print, maybe}) => {
8
10
  const elements = path.get('elements');
@@ -11,23 +13,20 @@ module.exports.ArrayExpression = (path, {print, maybe}) => {
11
13
  print('[');
12
14
  maybe.indent.inc(!elementIsObject);
13
15
 
14
- const isNewLine = !isNumbers(elements) && !isForOf(path) && isLastArg(path) && !isParentProperty(path);
16
+ const isNewLine = !elementIsObject && isNewLineBefore(path, {elements});
15
17
  const n = elements.length - 1;
16
18
 
17
19
  maybe.print(isNewLine && elements.length, '\n');
18
20
 
19
21
  for (const [index, element] of entries(elements)) {
20
22
  maybe.indent(isNewLine);
21
-
22
23
  print(element);
23
-
24
24
  maybe.print(isNewLine, ',\n');
25
25
  maybe.print(!isNewLine && index < n, ', ');
26
26
  }
27
27
 
28
28
  maybe.indent.dec(!elementIsObject);
29
29
  maybe.indent(elements.length && isNewLine);
30
-
31
30
  print(']');
32
31
  };
33
32
 
@@ -62,5 +61,32 @@ function isElementObject(path) {
62
61
  if (!elements.length)
63
62
  return false;
64
63
 
65
- return elements[0].isObjectExpression();
64
+ if (elements[0].isObjectExpression())
65
+ return true;
66
+
67
+ if (elements.length > 1 && elements[SECOND].isObjectExpression())
68
+ return true;
69
+
70
+ return false;
71
+ }
72
+
73
+ function tooLong(path) {
74
+ const elements = path.get('elements');
75
+
76
+ if (elements.length < 2)
77
+ return false;
78
+
79
+ for (const el of path.get('elements')) {
80
+ if (el.isStringLiteral() && el.node.value.length > 4)
81
+ return true;
82
+ }
83
+
84
+ return false;
85
+ }
86
+
87
+ function isNewLineBefore(path, {elements}) {
88
+ if (tooLong(path) || isCoupleLines(path) || !isNumbers(elements) && !isForOf(path) && isLastArg(path) && !isParentProperty(path))
89
+ return true;
90
+
91
+ return false;
66
92
  }
@@ -5,27 +5,9 @@ module.exports.AssignmentExpression = (path, {print}) => {
5
5
  const left = path.get('left');
6
6
  const right = path.get('right');
7
7
 
8
- if (shouldAddNewLine(path))
9
- print('\n');
10
-
11
8
  print(left);
12
9
  print(' ');
13
10
  print(operator);
14
11
  print(' ');
15
12
  print(right);
16
13
  };
17
-
18
- function shouldAddNewLine({parentPath}) {
19
- const prevPath = parentPath.getPrevSibling();
20
- const prevPrevPath = prevPath.getPrevSibling();
21
-
22
- const twoPrev = prevPath.node && prevPrevPath.node;
23
-
24
- if (!twoPrev)
25
- return false;
26
-
27
- if (prevPath.isExpressionStatement() && prevPath.get('expression').isAssignmentExpression())
28
- return false;
29
-
30
- return true;
31
- }
@@ -2,7 +2,7 @@
2
2
 
3
3
  const {
4
4
  isMarkedParentBefore,
5
- isMarkedPrevAfter,
5
+ hasPrevNewline,
6
6
  } = require('../mark');
7
7
 
8
8
  module.exports.OptionalCallExpression = (path, {indent, print, maybe}) => {
@@ -20,7 +20,7 @@ module.exports.CallExpression = CallExpression;
20
20
  function CallExpression(path, {indent, print, maybe}) {
21
21
  const isParentCall = toLong(path) && path.parentPath.isCallExpression();
22
22
 
23
- if (shouldAddNewLine(path) && !isMarkedParentBefore(path) && !isMarkedPrevAfter(path.parentPath))
23
+ if (isNewLineBefore(path) && !isMarkedParentBefore(path) && !hasPrevNewline(path.parentPath))
24
24
  print.breakline();
25
25
 
26
26
  print(path.get('callee'));
@@ -56,7 +56,7 @@ function CallExpression(path, {indent, print, maybe}) {
56
56
  print(')');
57
57
  }
58
58
 
59
- function shouldAddNewLine({parentPath}) {
59
+ function isNewLineBefore({parentPath}) {
60
60
  if (!parentPath.isExpressionStatement())
61
61
  return false;
62
62
 
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const {isMarkedPrevAfter} = require('../mark');
3
+ const {hasPrevNewline} = require('../mark');
4
+
4
5
  const isFirst = (path) => !path.getPrevSibling().node;
5
6
 
6
7
  module.exports.FunctionExpression = (path, {print, maybe}) => {
@@ -31,10 +32,10 @@ module.exports.FunctionExpression = (path, {print, maybe}) => {
31
32
  };
32
33
 
33
34
  module.exports.ArrowFunctionExpression = ArrowFunctionExpression;
35
+
34
36
  function ArrowFunctionExpression(path, {print, maybe}) {
35
37
  const {async} = path.node;
36
38
  maybe.print(async, 'async ');
37
-
38
39
  print('(');
39
40
 
40
41
  const params = path.get('params');
@@ -72,11 +73,10 @@ module.exports.ObjectMethod = (path, {print}) => {
72
73
  module.exports.FunctionDeclaration = (path, {print, maybe}) => {
73
74
  const {async} = path.node;
74
75
 
75
- if (!isFirst(path) && !isMarkedPrevAfter(path) && !path.parentPath.isExportDeclaration())
76
+ if (!isFirst(path) && !hasPrevNewline(path) && !path.parentPath.isExportDeclaration())
76
77
  print('\n');
77
78
 
78
79
  maybe.print(async, 'async ');
79
-
80
80
  print('function ');
81
81
  print(path.get('id'));
82
82
  print('(');
@@ -112,4 +112,3 @@ module.exports.ClassMethod = (path, {print}) => {
112
112
  print(') ');
113
113
  print(path.get('body'));
114
114
  };
115
-
@@ -19,6 +19,7 @@ const {AssignmentPattern} = require('./assignment-pattern');
19
19
  const {RestElement} = require('./rest-element');
20
20
  const {SpreadElement} = require('./spread-element');
21
21
  const {SequenceExpression} = require('./sequence-expression');
22
+ const {TaggedTemplateExpression} = require('./tagged-template-expression');
22
23
 
23
24
  module.exports = {
24
25
  ...functions,
@@ -37,6 +38,7 @@ module.exports = {
37
38
  RestElement,
38
39
  SpreadElement,
39
40
  SequenceExpression,
41
+ TaggedTemplateExpression,
40
42
  BinaryExpression(path, {traverse, write}) {
41
43
  traverse(path.get('left'));
42
44
  write(` ${path.node.operator} `);
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+
3
+ module.exports.TaggedTemplateExpression = (path, {print}) => {
4
+ print('__tag');
5
+ print('__quasi');
6
+ };
@@ -13,7 +13,6 @@ function markBefore(path) {
13
13
  function markAfter(path) {
14
14
  path[WATER_MARK_AFTER] = true;
15
15
  }
16
-
17
16
  module.exports.isMarkedBefore = isMarkedBefore;
18
17
  module.exports.isMarkedAfter = isMarkedAfter;
19
18
 
@@ -32,7 +31,3 @@ module.exports.hasPrevNewline = (path) => {
32
31
  module.exports.isMarkedParentBefore = (path) => {
33
32
  return isMarkedBefore(path.parentPath);
34
33
  };
35
-
36
- module.exports.isMarkedPrevAfter = (path) => {
37
- return isMarkedAfter(path.getPrevSibling());
38
- };
@@ -3,12 +3,13 @@
3
3
  const {
4
4
  markBefore,
5
5
  markAfter,
6
- isMarkedPrevAfter,
6
+ hasPrevNewline,
7
7
  } = require('../mark');
8
+
8
9
  const {isNext} = require('../is');
9
10
 
10
11
  module.exports.ExpressionStatement = (path, {write, indent, traverse, print}) => {
11
- if (isCoupleLinesExpression(path) && !isFirst(path) && shouldAddNewLine(path) && !isMarkedPrevAfter(path)) {
12
+ if (isCoupleLinesExpression(path) && !isFirst(path) && shouldAddNewLine(path) && !hasPrevNewline(path)) {
12
13
  print.breakline();
13
14
  markBefore(path);
14
15
  }
@@ -27,8 +28,8 @@ module.exports.ExpressionStatement = (path, {write, indent, traverse, print}) =>
27
28
  };
28
29
 
29
30
  function isCoupleLinesExpression(path) {
30
- const start = path.node.loc?.start.line;
31
- const end = path.node.loc?.end.line;
31
+ const start = path.node.loc?.start?.line;
32
+ const end = path.node.loc?.end?.line;
32
33
 
33
34
  return end > start;
34
35
  }
@@ -62,4 +63,3 @@ function shouldAddNewLine(path) {
62
63
 
63
64
  return true;
64
65
  }
65
-
@@ -1,10 +1,11 @@
1
1
  'use strict';
2
2
 
3
- const {isMarkedPrevAfter} = require('../mark');
3
+ const {hasPrevNewline} = require('../mark');
4
+
4
5
  const {isFirst} = require('../is');
5
6
 
6
7
  module.exports.ForOfStatement = (path, {indent, print}) => {
7
- if (!isFirst(path) && !isMarkedPrevAfter(path)) {
8
+ if (!isFirst(path) && !hasPrevNewline(path)) {
8
9
  print.indent();
9
10
  print.newline();
10
11
  }
@@ -31,4 +32,3 @@ module.exports.ForOfStatement = (path, {indent, print}) => {
31
32
  print(bodyPath);
32
33
  }
33
34
  };
34
-
@@ -1,7 +1,14 @@
1
1
  'use strict';
2
2
 
3
- const {isNext, isCoupleLines} = require('../is');
4
- const {hasPrevNewline, markAfter} = require('../mark');
3
+ const {
4
+ isNext,
5
+ isCoupleLines,
6
+ } = require('../is');
7
+ const {
8
+ hasPrevNewline,
9
+ markAfter,
10
+ } = require('../mark');
11
+
5
12
  const isNextAssign = (path) => {
6
13
  const nextPath = path.getNextSibling();
7
14
 
@@ -12,7 +19,7 @@ const isNextAssign = (path) => {
12
19
  };
13
20
 
14
21
  module.exports.VariableDeclaration = (path, {maybe, print}) => {
15
- if (!isFirst(path) && shouldAddNewLine(path) && !hasPrevNewline(path))
22
+ if (!isFirst(path) && shouldAddNewLineBefore(path) && !hasPrevNewline(path))
16
23
  print.breakline();
17
24
 
18
25
  const isParentBlock = /Program|BlockStatement/.test(path.parentPath.type);
@@ -28,18 +35,36 @@ module.exports.VariableDeclaration = (path, {maybe, print}) => {
28
35
  maybe.print(isParentBlock, ';');
29
36
  maybe.print.newline(isParentBlock);
30
37
 
31
- const is = isNext(path) && isCoupleLines(path) && !isNextAssign(path);
38
+ const is = shouldAddNewLineAfter(path);
32
39
 
33
40
  if (is) {
34
41
  print.linebreak();
35
42
  markAfter(path);
36
43
  }
37
-
38
- if (path.parentPath.isProgram() && !isNext(path))
39
- print.linebreak();
40
44
  };
41
45
 
42
- function shouldAddNewLine(path) {
46
+ function shouldAddNewLineAfter(path) {
47
+ if (isLast(path))
48
+ return true;
49
+
50
+ if (!isNext(path))
51
+ return false;
52
+
53
+ if (isCoupleLines(path))
54
+ return true;
55
+
56
+ if (isNextAssign(path))
57
+ return true;
58
+
59
+ if (isCoupleLines(path.getNextSibling()))
60
+ return true;
61
+
62
+ return false;
63
+ }
64
+
65
+ const isLast = (path) => path.parentPath.isProgram() && !isNext(path);
66
+
67
+ function shouldAddNewLineBefore(path) {
43
68
  const prevPath = path.getPrevSibling();
44
69
 
45
70
  if (prevPath.isStatement() && !prevPath.isExpressionStatement() && !prevPath.isBlockStatement())
@@ -53,7 +78,6 @@ function shouldAddNewLine(path) {
53
78
 
54
79
  const nextPath = path.getNextSibling();
55
80
  const nextNextPath = nextPath.getNextSibling();
56
-
57
81
  const twoNext = nextPath.node && nextNextPath.node;
58
82
 
59
83
  if (!twoNext)
@@ -65,4 +89,3 @@ function shouldAddNewLine(path) {
65
89
  function isFirst(path) {
66
90
  return path.node === path.parentPath.node.body[0];
67
91
  }
68
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.6.10",
3
+ "version": "1.6.12",
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",