@putout/printer 1.6.13 → 1.7.1

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.1
2
+
3
+ feature:
4
+ - f1db3bd @putout/printer: print: path.get(__a) => __a
5
+
6
+ 2023.03.23, v1.7.0
7
+
8
+ feature:
9
+ - 789d21b @putout/printer: improve support of AssignmentPattern
10
+
1
11
  2023.03.22, v1.6.13
2
12
 
3
13
  feature:
@@ -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
  };
@@ -23,7 +23,7 @@ function CallExpression(path, {indent, print, maybe}) {
23
23
  if (isNewLineBefore(path) && !isMarkedParentBefore(path) && !hasPrevNewline(path.parentPath))
24
24
  print.breakline();
25
25
 
26
- print(path.get('callee'));
26
+ print('__callee');
27
27
  print('(');
28
28
 
29
29
  const args = path.get('arguments');
@@ -11,7 +11,7 @@ module.exports.ClassDeclaration = (path, {maybe, print, indent}) => {
11
11
 
12
12
  indent();
13
13
  print('class ');
14
- print(path.get('id'));
14
+ print('__id');
15
15
  print(' {\n');
16
16
  indent.inc();
17
17
 
@@ -53,7 +53,7 @@ function ArrowFunctionExpression(path, {print, maybe}) {
53
53
  }
54
54
 
55
55
  module.exports.ObjectMethod = (path, {print}) => {
56
- print(path.get('key'));
56
+ print('__key');
57
57
  print('(');
58
58
 
59
59
  const params = path.get('params');
@@ -67,7 +67,7 @@ module.exports.ObjectMethod = (path, {print}) => {
67
67
  }
68
68
 
69
69
  print(') ');
70
- print(path.get('body'));
70
+ print('__body');
71
71
  };
72
72
 
73
73
  module.exports.FunctionDeclaration = (path, {print, maybe}) => {
@@ -78,7 +78,7 @@ module.exports.FunctionDeclaration = (path, {print, maybe}) => {
78
78
 
79
79
  maybe.print(async, 'async ');
80
80
  print('function ');
81
- print(path.get('id'));
81
+ print('__id');
82
82
  print('(');
83
83
 
84
84
  const params = path.get('params');
@@ -92,11 +92,11 @@ module.exports.FunctionDeclaration = (path, {print, maybe}) => {
92
92
  }
93
93
 
94
94
  print(') ');
95
- print(path.get('body'));
95
+ print('__body');
96
96
  };
97
97
 
98
98
  module.exports.ClassMethod = (path, {print}) => {
99
- print(path.get('key'));
99
+ print('__key');
100
100
  print('(');
101
101
 
102
102
  const params = path.get('params');
@@ -110,5 +110,5 @@ module.exports.ClassMethod = (path, {print}) => {
110
110
  }
111
111
 
112
112
  print(') ');
113
- print(path.get('body'));
113
+ print('__body');
114
114
  };
@@ -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
  };
@@ -11,7 +11,7 @@ module.exports.IfStatement = (path, {indent, print}) => {
11
11
 
12
12
  indent();
13
13
  print('if (');
14
- print(path.get('test'));
14
+ print('__test');
15
15
  print(')');
16
16
 
17
17
  const consequent = path.get('consequent');
@@ -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
-
@@ -5,8 +5,8 @@ module.exports.TryStatement = (path, {print, maybe}) => {
5
5
  const finalizer = path.get('finalizer');
6
6
 
7
7
  print('try ');
8
- print(path.get('block'));
9
- print(path.get('handler'));
8
+ print('__block');
9
+ print('__handler');
10
10
 
11
11
  if (finalizer.node) {
12
12
  print(' ');
@@ -19,14 +19,15 @@ 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
 
27
28
  maybe.indent(isParentBlock);
28
29
  print(`${path.node.kind} `);
29
- print(path.get('declarations.0.id'));
30
+ print('__declarations.0.id');
30
31
 
31
32
  const initPath = path.get('declarations.0.init');
32
33
 
@@ -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.13",
3
+ "version": "1.7.1",
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",
@@ -41,6 +41,7 @@
41
41
  "generate"
42
42
  ],
43
43
  "devDependencies": {
44
+ "@putout/test": "^6.0.1",
44
45
  "c8": "^7.5.0",
45
46
  "eslint": "^8.0.1",
46
47
  "eslint-plugin-n": "^15.2.4",
@@ -0,0 +1,2 @@
1
+ print('__block');
2
+ print('__handler');
@@ -0,0 +1,2 @@
1
+ print(path.get('block'));
2
+ print(path.get('handler'));
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => `Use print('__path') instead of path.get(__path)`;
4
+
5
+ module.exports.replace = () => ({
6
+ 'print(path.get(__a))': ({__a}) => {
7
+ __a.value = '__' + __a.value;
8
+ return 'print(__a)';
9
+ },
10
+ });
11
+