@putout/printer 1.20.0 → 1.22.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.04, v1.22.0
2
+
3
+ feature:
4
+ - c8fc7b7 @putout/printer: improve support of tuples
5
+ - 599bd93 @putout/printer: set @putout/printer as putout printer
6
+
7
+ 2023.04.04, v1.21.0
8
+
9
+ feature:
10
+ - d5716be @putout/printer: ForOfStatement: convert
11
+ - 5bd0fe5 @putout/printer: improve newlines formatting
12
+
1
13
  2023.04.03, v1.20.0
2
14
 
3
15
  feature:
@@ -52,8 +52,10 @@ module.exports.createLog = ({newline = '\n', store = createStore()} = {}) => ({t
52
52
  function createStore() {
53
53
  let chunks = [];
54
54
 
55
- return (chunk) => {
56
- if (chunk) {
55
+ return (...args) => {
56
+ const [chunk] = args;
57
+
58
+ if (args.length) {
57
59
  chunks.push(chunk);
58
60
  return;
59
61
  }
@@ -8,6 +8,7 @@ const isForOf = ({parentPath}) => parentPath.isForOfStatement();
8
8
  const SECOND = 1;
9
9
 
10
10
  const isStringAndArray = ([a, b]) => a?.isStringLiteral() && b?.isArrayExpression();
11
+ const isStringAndIdentifier = ([a, b]) => a?.isStringLiteral() && b?.isIdentifier();
11
12
  const isArrayParent = (path) => path.parentPath.isArrayExpression();
12
13
  const isTwoLongStrings = ([a, b]) => {
13
14
  const LONG_STRING = 20;
@@ -27,9 +28,8 @@ module.exports.ArrayExpression = (path, {print, maybe, indent}) => {
27
28
 
28
29
  print('[');
29
30
 
30
- if (!isTwoLongStrings(elements) && !shouldIncreaseIndent) {
31
- indent.inc();
32
- }
31
+ if (!isTwoLongStrings(elements))
32
+ maybe.indent.inc(!shouldIncreaseIndent);
33
33
 
34
34
  const isNewLine = isNewlineBetweenElements(path, {elements});
35
35
  const n = elements.length - 1;
@@ -133,13 +133,21 @@ function isNewlineBetweenElements(path, {elements}) {
133
133
  if (isStringAndArray(elements))
134
134
  return false;
135
135
 
136
+ if (isStringAndIdentifier(elements))
137
+ return false;
138
+
136
139
  if (tooLong(path) || isCoupleLines(path) || !isNumbers(elements) && !isForOf(path) && isLastArg(path) && !isParentProperty(path))
137
140
  return true;
138
141
 
139
142
  return false;
140
143
  }
141
144
 
142
- function isTwoStringsDifferentLength([a, b]) {
145
+ function isTwoStringsDifferentLength(strings) {
146
+ const [a, b] = strings;
147
+
148
+ if (strings.length > 2)
149
+ return false;
150
+
143
151
  if (!a?.isStringLiteral() || !b?.isStringLiteral())
144
152
  return false;
145
153
 
@@ -6,7 +6,6 @@ module.exports.ArrayPattern = (path, {indent, maybe, print}) => {
6
6
  print('[');
7
7
 
8
8
  const elements = path.get('elements');
9
-
10
9
  indent.inc();
11
10
 
12
11
  const isNewLine = !isForOf(path) && elements.length > 2;
@@ -1,17 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- isMarkedParentBefore,
5
- hasPrevNewline,
6
- } = require('../mark');
7
-
8
3
  const CallExpression = {
9
- beforeIf(path) {
10
- return isNewLineBefore(path) && !isMarkedParentBefore(path) && !hasPrevNewline(path.parentPath);
11
- },
12
- before(path, {print}) {
13
- print.breakline();
14
- },
15
4
  print(path, {indent, print, maybe}) {
16
5
  const isParentCall = toLong(path) && path.parentPath.isCallExpression();
17
6
  print('__callee');
@@ -57,27 +46,6 @@ const CallExpression = {
57
46
  module.exports.OptionalCallExpression = CallExpression;
58
47
  module.exports.CallExpression = CallExpression;
59
48
 
60
- function isNewLineBefore({parentPath}) {
61
- if (!parentPath.isExpressionStatement())
62
- return false;
63
-
64
- const prevPath = parentPath.getPrevSibling();
65
- const prevPrevPath = prevPath.getPrevSibling();
66
-
67
- if (prevPath.isStatement() && !prevPath.isExpressionStatement() && !prevPath.isVariableDeclaration())
68
- return true;
69
-
70
- const twoPrev = prevPath.node && prevPrevPath.node;
71
-
72
- if (!twoPrev)
73
- return false;
74
-
75
- if (prevPath.isExpressionStatement() && prevPath.get('expression').isCallExpression())
76
- return false;
77
-
78
- return true;
79
- }
80
-
81
49
  function toLong(path) {
82
50
  const args = path.get('arguments');
83
51
 
@@ -5,6 +5,7 @@ module.exports.ClassDeclaration = classVisitor;
5
5
 
6
6
  module.exports.ClassDeclaration = (path, {print, indent}) => {
7
7
  indent();
8
+
8
9
  classVisitor(path, {
9
10
  print,
10
11
  indent,
@@ -16,7 +16,6 @@ module.exports.FunctionExpression = (path, {print, maybe, write, traverse}) => {
16
16
  maybe.print(generator, '*');
17
17
 
18
18
  const id = path.get('id');
19
-
20
19
  write.space();
21
20
 
22
21
  if (id.node) {
@@ -6,7 +6,6 @@ module.exports.NewExpression = (path, {indent, print, maybe}) => {
6
6
  print('__callee');
7
7
 
8
8
  const args = path.get('arguments');
9
-
10
9
  print('(');
11
10
 
12
11
  const n = args.length - 1;
@@ -52,10 +52,16 @@ module.exports.ObjectExpression = (path, {print, maybe, indent}) => {
52
52
  indent.dec();
53
53
  maybe.indent(manyLines);
54
54
  print('}');
55
- maybe.print.newline(path.parentPath.isLogicalExpression());
55
+ maybe.print.newline(shouldAddNewline(path));
56
56
  maybe.print(parens, ')');
57
57
  };
58
58
 
59
+ function shouldAddNewline(path) {
60
+ if (!path.parentPath.isLogicalExpression())
61
+ return false;
62
+
63
+ return path.parentPath.parentPath.isSpreadElement();
64
+ }
59
65
  module.exports.ObjectProperty = (path, {print, maybe}) => {
60
66
  const {
61
67
  shorthand,
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ const {isMarkedAfter} = require('./mark');
3
4
  const isParentProgram = (path) => path.parentPath?.isProgram();
4
5
  const isParentBlock = (path) => path.parentPath.isBlockStatement();
5
6
 
@@ -9,6 +10,9 @@ const isNext = (path) => {
9
10
  if (!next.node)
10
11
  return false;
11
12
 
13
+ if (isMarkedAfter(path.get('body')))
14
+ return false;
15
+
12
16
  return !next.isEmptyStatement();
13
17
  };
14
18
 
@@ -14,20 +14,11 @@ function markBefore(path) {
14
14
  function markAfter(path) {
15
15
  path[WATER_MARK_AFTER] = true;
16
16
  }
17
- module.exports.isMarkedBefore = isMarkedBefore;
18
17
  module.exports.isMarkedAfter = isMarkedAfter;
19
18
 
20
- function isMarkedBefore(path) {
21
- return path[WATER_MARK_BEFORE];
22
- }
23
-
24
19
  function isMarkedAfter(path) {
25
20
  return path[WATER_MARK_AFTER];
26
21
  }
27
22
  module.exports.hasPrevNewline = (path) => {
28
23
  return isMarkedAfter(path.getPrevSibling());
29
24
  };
30
-
31
- module.exports.isMarkedParentBefore = (path) => {
32
- return isMarkedBefore(path.parentPath);
33
- };
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  const {
4
- isNext,
5
4
  isParentBlock,
6
5
  isNextParent,
7
6
  } = require('../is');
@@ -15,9 +14,12 @@ module.exports.BreakStatement = {
15
14
  print('break;');
16
15
  },
17
16
  afterIf(path) {
18
- if (!isNext(path) && !isParentBlock(path) && !isNextParent(path))
19
- return false;
17
+ if (isParentBlock(path))
18
+ return true;
20
19
 
21
- return true;
20
+ if (isNextParent(path))
21
+ return true;
22
+
23
+ return false;
22
24
  },
23
25
  };
@@ -72,7 +72,7 @@ function shouldAddNewLineAfter(path) {
72
72
  if (isNext(path))
73
73
  return true;
74
74
 
75
- return false;
75
+ return path.findParent(isNext);
76
76
  }
77
77
 
78
78
  function isStrictMode(path) {
@@ -11,40 +11,44 @@ const {
11
11
  isNext,
12
12
  } = require('../is');
13
13
 
14
- module.exports.ForOfStatement = (path, {indent, print, maybe}) => {
15
- if (!isFirst(path) && !hasPrevNewline(path)) {
14
+ module.exports.ForOfStatement = {
15
+ beforeIf(path) {
16
+ return !isFirst(path) && !hasPrevNewline(path);
17
+ },
18
+ before(path, {print}) {
16
19
  print.indent();
17
20
  print.newline();
18
21
  markBefore(path);
19
- }
20
-
21
- indent();
22
- print('for (');
23
- print('__left');
24
- print(' of ');
25
- print('__right');
26
- print(')');
27
-
28
- const bodyPath = path.get('body');
29
-
30
- if (bodyPath.isExpressionStatement()) {
31
- indent.inc();
32
- print.newline();
33
- print('__body');
34
- indent.dec();
35
- maybe.print.newline(isNext(path));
22
+ },
23
+ print(path, {indent, print, maybe}) {
24
+ indent();
25
+ print('for (');
26
+ print('__left');
27
+ print(' of ');
28
+ print('__right');
29
+ print(')');
30
+
31
+ const bodyPath = path.get('body');
32
+
33
+ if (bodyPath.isExpressionStatement()) {
34
+ indent.inc();
35
+ print.newline();
36
+ print('__body');
37
+ indent.dec();
38
+ maybe.print.newline(isNext(path));
39
+
40
+ return;
41
+ }
36
42
 
37
- return;
38
- }
39
-
40
- if (bodyPath.isBlockStatement()) {
41
- print(' ');
42
- print('__body');
43
- }
44
-
45
- if (isNext(path)) {
43
+ if (bodyPath.isBlockStatement()) {
44
+ print(' ');
45
+ print('__body');
46
+ }
47
+ },
48
+ afterIf: isNext,
49
+ after(path, {print}) {
46
50
  print.indent();
47
51
  print.newline();
48
52
  markAfter(path);
49
- }
53
+ },
50
54
  };
@@ -37,23 +37,17 @@ module.exports.IfStatement = {
37
37
  write(' else ');
38
38
  traverse(alternate);
39
39
  } else if (alternate.isIfStatement()) {
40
- print.space();
40
+ write.space();
41
41
  write('else');
42
- print.space();
42
+ write.space();
43
43
  traverse(alternate);
44
44
  } else if (alternate.node) {
45
- print.breakline();
46
45
  write('else');
47
46
  print.newline();
48
47
  indent.inc();
49
48
  traverse(alternate);
50
49
  indent.dec();
51
50
  }
52
-
53
- const next = path.getNextSibling();
54
-
55
- if (next.isExpressionStatement() && !next.get('expression').isCallExpression())
56
- print.newline();
57
51
  },
58
52
  afterIf: (path) => {
59
53
  const next = path.getNextSibling();
@@ -18,7 +18,6 @@ module.exports.SwitchStatement = (path, {print, maybe}) => {
18
18
  print(':');
19
19
 
20
20
  const isBlock = switchCase.get('consequent.0').isBlockStatement();
21
-
22
21
  maybe.indent.inc(!isBlock);
23
22
  maybe.print.newline(!isBlock);
24
23
 
@@ -14,9 +14,9 @@ module.exports.TryStatement = (path, {print, maybe}) => {
14
14
  print('finally');
15
15
  print(' ');
16
16
  print(finalizer);
17
- print.newline();
18
17
  }
19
18
 
19
+ print.newline();
20
20
  maybe.print.breakline(isNext(path));
21
21
  };
22
22
 
@@ -23,9 +23,8 @@ module.exports.VariableDeclaration = {
23
23
  print('__declarations.0.id');
24
24
 
25
25
  const initPath = path.get('declarations.0.init');
26
-
27
26
  maybe.print(initPath.node, ' = ');
28
- print(initPath);
27
+ print('__declarations.0.init');
29
28
  maybe.print(isParentBlock(path), ';');
30
29
 
31
30
  let wasNewline = false;
@@ -60,10 +59,14 @@ function shouldAddNewlineAfter(path) {
60
59
  return true;
61
60
 
62
61
  const next = path.getNextSibling();
62
+ const prev = path.getPrevSibling();
63
63
 
64
64
  if (isCoupleLines(next))
65
65
  return true;
66
66
 
67
+ if (prev.isVariableDeclaration() && !next.isVariableDeclaration())
68
+ return true;
69
+
67
70
  return false;
68
71
  }
69
72
  const isLast = (path) => path.parentPath.isProgram() && !isNext(path);
@@ -243,4 +243,3 @@ const computePath = (path, maybeLine) => {
243
243
 
244
244
  return maybeLine;
245
245
  };
246
-
@@ -59,6 +59,7 @@ module.exports = {
59
59
  write('{');
60
60
  write.newline();
61
61
  indent.inc();
62
+
62
63
  for (const member of path.get('members')) {
63
64
  indent();
64
65
  traverse(member);
@@ -78,4 +79,3 @@ module.exports = {
78
79
  print('__typeAnnotation');
79
80
  },
80
81
  };
81
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.20.0",
3
+ "version": "1.22.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",