@putout/printer 1.8.3 → 1.8.5

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,14 @@
1
+ 2023.03.27, v1.8.5
2
+
3
+ feature:
4
+ - f44a060 @putout/printer: drop newlines at end of file
5
+ - 524d6e1 @putout/printer: drop newline after BlockStatement when there is no next sibling
6
+
7
+ 2023.03.27, v1.8.4
8
+
9
+ feature:
10
+ - c117645 @putout/printer: add support of ForInStatement
11
+
1
12
  2023.03.26, v1.8.3
2
13
 
3
14
  feature:
package/README.md CHANGED
@@ -60,7 +60,7 @@ print(ast, {
60
60
  });
61
61
 
62
62
  // returns
63
- 'const {a /* [hello world] */= 5} = b;\n\n';
63
+ 'const {a /* [hello world] */= 5} = b;';
64
64
  ```
65
65
 
66
66
  ## License
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ const {isNext} = require('../is');
3
4
  const isFirstStatement = (path) => path.get('body.0')?.isStatement();
4
5
 
5
6
  module.exports.BlockStatement = (path, {indent, maybe, print}) => {
@@ -26,16 +27,17 @@ module.exports.BlockStatement = (path, {indent, maybe, print}) => {
26
27
  print(',');
27
28
  }
28
29
 
29
- const shouldAddNewline = isAddNewLineAfter(path);
30
-
31
- if (shouldAddNewline) {
30
+ if (shouldAddNewlineAfter(path)) {
32
31
  print.newline();
33
32
  }
34
33
  };
35
34
 
36
- function isAddNewLineAfter(path) {
35
+ function shouldAddNewlineAfter(path) {
37
36
  const {parentPath} = path;
38
37
 
38
+ if (parentPath.isStatement() && !path.node.body.length && !isNext(parentPath))
39
+ return false;
40
+
39
41
  if (isTry(path) || /FunctionExpression/.test(path.parentPath.type))
40
42
  return false;
41
43
 
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ module.exports.ForInStatement = (path, {print}) => {
4
+ print('for ');
5
+ print('(');
6
+ print('__left');
7
+ print(' in ');
8
+ print('__right');
9
+ print(')');
10
+
11
+ if (path.get('body').isBlockStatement())
12
+ print.space();
13
+
14
+ print('__body');
15
+ };
@@ -13,6 +13,7 @@ const importDeclarations = require('./import-declarations');
13
13
  const exportDeclarations = require('./export-declarations');
14
14
  const {WhileStatement} = require('./while-statement');
15
15
  const {SwitchStatement} = require('./switch-statement');
16
+ const {ForInStatement} = require('./for-in-statement');
16
17
 
17
18
  module.exports = {
18
19
  ...importDeclarations,
@@ -22,6 +23,7 @@ module.exports = {
22
23
  VariableDeclaration,
23
24
  IfStatement,
24
25
  ForStatement,
26
+ ForInStatement,
25
27
  ForOfStatement,
26
28
  ReturnStatement,
27
29
  DebuggerStatement,
@@ -9,13 +9,13 @@ const {
9
9
  markAfter,
10
10
  } = require('../mark');
11
11
 
12
+ const isParentBlock = (path) => /Program|BlockStatement/.test(path.parentPath.type);
13
+
12
14
  module.exports.VariableDeclaration = (path, {maybe, print}) => {
13
15
  if (shouldAddNewlineBefore(path))
14
16
  print.breakline();
15
17
 
16
- const isParentBlock = /Program|BlockStatement/.test(path.parentPath.type);
17
-
18
- maybe.indent(isParentBlock);
18
+ maybe.indent(isParentBlock(path));
19
19
  print(`${path.node.kind} `);
20
20
  print('__declarations.0.id');
21
21
 
@@ -23,22 +23,26 @@ module.exports.VariableDeclaration = (path, {maybe, print}) => {
23
23
 
24
24
  maybe.print(initPath.node, ' = ');
25
25
  print(initPath);
26
- maybe.print(isParentBlock, ';');
27
- maybe.print.newline(isParentBlock);
26
+ maybe.print(isParentBlock(path), ';');
27
+ let wasNewline = false;
28
28
 
29
- const is = shouldAddNewlineAfter(path);
29
+ if (isParentBlock(path) && isNext(path)) {
30
+ print.newline();
31
+ wasNewline = true;
32
+ }
30
33
 
31
- if (is) {
32
- print.linebreak();
34
+ if (shouldAddNewlineAfter(path)) {
35
+ maybe.print.linebreak(wasNewline);
36
+ maybe.print.newline(!wasNewline);
33
37
  markAfter(path);
34
38
  }
35
39
  };
36
40
 
37
41
  function shouldAddNewlineAfter(path) {
38
- if (isLast(path))
42
+ if (!isNext(path) && path.parentPath.isBlockStatement())
39
43
  return true;
40
44
 
41
- if (!isNext(path))
45
+ if (isLast(path))
42
46
  return false;
43
47
 
44
48
  if (isCoupleLines(path))
@@ -61,6 +61,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
61
61
  const maybeIndentDec = (a) => a && indent.dec();
62
62
  const maybeNewline = (a) => a && newline();
63
63
  const maybeBreakline = (a) => a && breakline();
64
+ const maybeLinebreak = (a) => a && linebreak();
64
65
  let i = 0;
65
66
  const incIndent = () => ++i;
66
67
  const decIndent = () => --i;
@@ -168,6 +169,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
168
169
  assign(maybePrint, {
169
170
  newline: maybeNewline,
170
171
  breakline: maybeBreakline,
172
+ linebreak: maybeLinebreak,
171
173
  });
172
174
 
173
175
  assign(printer.maybe, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.8.3",
3
+ "version": "1.8.5",
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",