@putout/printer 1.6.12 → 1.7.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.03.23, v1.7.0
2
+
3
+ feature:
4
+ - 789d21b @putout/printer: improve support of AssignmentPattern
5
+
6
+ 2023.03.22, v1.6.13
7
+
8
+ feature:
9
+ - 3e3905a @putout/printer: AssignmentExpression: simplify
10
+
1
11
  2023.03.22, v1.6.12
2
12
 
3
13
  feature:
@@ -2,12 +2,10 @@
2
2
 
3
3
  module.exports.AssignmentExpression = (path, {print}) => {
4
4
  const {operator} = path.node;
5
- const left = path.get('left');
6
- const right = path.get('right');
7
5
 
8
- print(left);
6
+ print('__left');
9
7
  print(' ');
10
8
  print(operator);
11
9
  print(' ');
12
- print(right);
10
+ print('__right');
13
11
  };
@@ -1,6 +1,9 @@
1
1
  'use strict';
2
2
 
3
- module.exports.AssignmentPattern = (path, {print}) => {
3
+ const isArg = (path) => path.parentPath.isFunction();
4
+
5
+ module.exports.AssignmentPattern = (path, {print, maybe}) => {
6
+ maybe.print(isArg(path), '__left');
4
7
  print(' = ');
5
8
  print('__right');
6
9
  };
@@ -26,21 +26,23 @@ module.exports.BlockStatement = (path, {indent, maybe, print}) => {
26
26
  print(',');
27
27
  }
28
28
 
29
- const shouldAddNewLine = !isNewLineAdded(path);
29
+ const shouldAddNewLine = isAddNewLineAfter(path);
30
30
 
31
- maybe.print.newline(shouldAddNewLine);
31
+ if (shouldAddNewLine) {
32
+ print.newline();
33
+ }
32
34
  };
33
35
 
34
- function isNewLineAdded(path) {
36
+ function isAddNewLineAfter(path) {
35
37
  const {parentPath} = path;
36
38
 
37
39
  if (isTry(path) || /FunctionExpression/.test(path.parentPath.type))
38
- return true;
40
+ return false;
39
41
 
40
42
  if (parentPath.isIfStatement() && parentPath.get('consequent').node === path.node && parentPath.node.alternate)
41
- return true;
43
+ return false;
42
44
 
43
- return false;
45
+ return true;
44
46
  }
45
47
 
46
48
  function isTry({parentPath}) {
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {hasPrevNewline} = require('../mark');
3
+ const {hasPrevNewline, markAfter} = require('../mark');
4
4
 
5
5
  const {isFirst} = require('../is');
6
6
 
@@ -25,10 +25,18 @@ module.exports.ForOfStatement = (path, {indent, print}) => {
25
25
  print(bodyPath);
26
26
  indent.dec();
27
27
  print.newline();
28
+
29
+ return;
28
30
  }
29
31
 
30
32
  if (bodyPath.isBlockStatement()) {
31
33
  print(' ');
32
34
  print(bodyPath);
33
35
  }
36
+
37
+ if (path.getNextSibling().node) {
38
+ print.indent();
39
+ print.newline();
40
+ markAfter(path);
41
+ }
34
42
  };
@@ -1,25 +1,25 @@
1
1
  'use strict';
2
2
 
3
3
  const {isPrevBody} = require('../is');
4
+ const {hasPrevNewline} = require('../mark');
4
5
  const isBodyLength = ({parentPath}) => parentPath.node?.body?.length > 2;
5
6
 
6
- module.exports.ReturnStatement = (path, {indent, write, traverse}) => {
7
- if (isBodyLength(path) || isPrevBody(path)) {
8
- write.indent();
9
- write.newline();
7
+ module.exports.ReturnStatement = (path, {indent, traverse, print}) => {
8
+ if (!hasPrevNewline(path) && isBodyLength(path) || isPrevBody(path)) {
9
+ print.indent();
10
+ print.newline();
10
11
  }
11
12
 
12
13
  indent();
13
- write('return');
14
+ print('return');
14
15
 
15
16
  const argPath = path.get('argument');
16
17
 
17
18
  if (argPath.node) {
18
- write(' ');
19
+ print(' ');
19
20
  traverse(argPath);
20
21
  }
21
22
 
22
- write(';');
23
- write.newline();
23
+ print(';');
24
+ print.newline();
24
25
  };
25
-
@@ -19,8 +19,9 @@ const isNextAssign = (path) => {
19
19
  };
20
20
 
21
21
  module.exports.VariableDeclaration = (path, {maybe, print}) => {
22
- if (!isFirst(path) && shouldAddNewLineBefore(path) && !hasPrevNewline(path))
22
+ if (!isFirst(path) && isNewLineBefore(path) && !hasPrevNewline(path)) {
23
23
  print.breakline();
24
+ }
24
25
 
25
26
  const isParentBlock = /Program|BlockStatement/.test(path.parentPath.type);
26
27
 
@@ -64,7 +65,7 @@ function shouldAddNewLineAfter(path) {
64
65
 
65
66
  const isLast = (path) => path.parentPath.isProgram() && !isNext(path);
66
67
 
67
- function shouldAddNewLineBefore(path) {
68
+ function isNewLineBefore(path) {
68
69
  const prevPath = path.getPrevSibling();
69
70
 
70
71
  if (prevPath.isStatement() && !prevPath.isExpressionStatement() && !prevPath.isBlockStatement())
@@ -7,6 +7,7 @@ const statements = require('./statements');
7
7
  const literals = require('./literals');
8
8
  const {TYPES} = require('../types');
9
9
  const {createDebug} = require('./debug');
10
+
10
11
  const {
11
12
  maybeMarkAfter,
12
13
  maybeMarkBefore,
@@ -15,6 +16,7 @@ const {
15
16
  const {parseComments} = require('./comments');
16
17
  const isString = (a) => typeof a === 'string';
17
18
  const {assign} = Object;
19
+
18
20
  const traversers = {
19
21
  ...expressions,
20
22
  ...statements,
@@ -35,6 +37,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
35
37
  const format = initFormat(overrides.format);
36
38
  const tokens = [];
37
39
  const debug = createDebug(tokens);
40
+
38
41
  const write = (value) => {
39
42
  tokens.push({
40
43
  type: TYPES.TOKEN,
@@ -47,10 +50,10 @@ module.exports.tokenize = (ast, overrides = {}) => {
47
50
  const maybeIndentDec = (a) => a && indent.dec();
48
51
  const maybeNewline = (a) => a && newline();
49
52
  const maybeBreakline = (a) => a && breakline();
50
-
51
53
  let i = 0;
52
54
  const incIndent = () => ++i;
53
55
  const decIndent = () => --i;
56
+
54
57
  const indent = () => {
55
58
  tokens.push({
56
59
  type: TYPES.INDENT,
@@ -132,8 +135,13 @@ module.exports.tokenize = (ast, overrides = {}) => {
132
135
  if (!path.node)
133
136
  return;
134
137
 
135
- const print = createPrint(path, {write, traverse});
138
+ const print = createPrint(path, {
139
+ write,
140
+ traverse,
141
+ });
142
+
136
143
  assign(print, write);
144
+
137
145
  assign(printer, {
138
146
  print,
139
147
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.6.12",
3
+ "version": "1.7.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",