@putout/printer 1.14.1 → 1.14.3

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.30, v1.14.3
2
+
3
+ feature:
4
+ - a060299 @putout/printer: improve support EmptyStatement
5
+
6
+ 2023.03.30, v1.14.2
7
+
8
+ feature:
9
+ - 6bf7725 @putout/printer: add support of EmptyStatement
10
+
1
11
  2023.03.30, v1.14.1
2
12
 
3
13
  fix:
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- module.exports.ClassDeclaration = (path, {maybe, print, indent}) => {
3
+ module.exports.ClassDeclaration = (path, {print, indent}) => {
4
4
  indent();
5
5
  print('class ');
6
6
  print('__id');
@@ -17,12 +17,10 @@ module.exports.ClassDeclaration = (path, {maybe, print, indent}) => {
17
17
  indent.inc();
18
18
 
19
19
  const body = path.get('body.body');
20
- const n = body.length - 1;
21
20
 
22
- for (const [i, item] of body.entries()) {
21
+ for (const item of body) {
23
22
  indent();
24
23
  print(item);
25
- maybe.print(i < n, '\n');
26
24
  }
27
25
 
28
26
  indent.dec();
@@ -99,6 +99,11 @@ module.exports.FunctionDeclaration = (path, {print, maybe}) => {
99
99
  };
100
100
 
101
101
  module.exports.ClassMethod = (path, {print}) => {
102
+ const {kind} = path.node;
103
+
104
+ if (kind !== 'method')
105
+ print(`${kind} `);
106
+
102
107
  print('__key');
103
108
  print('(');
104
109
 
@@ -2,8 +2,17 @@
2
2
 
3
3
  const isParentProgram = (path) => path.parentPath?.isProgram();
4
4
  const isParentBlock = (path) => path.parentPath.isBlockStatement();
5
- const isNext = (path) => path.getNextSibling().node;
6
- const isNextParent = (path) => path.parentPath.getNextSibling().node;
5
+
6
+ const isNext = (path) => {
7
+ const next = path.getNextSibling();
8
+
9
+ if (!next.node)
10
+ return false;
11
+
12
+ return !next.isEmptyStatement();
13
+ };
14
+
15
+ const isNextParent = (path) => isNext(path.parentPath);
7
16
  const isLast = (path) => isParentProgram(path) && !isNext(path);
8
17
 
9
18
  module.exports.isFirst = (path) => path.node === path.parentPath.node.body[0];
@@ -3,7 +3,7 @@
3
3
  const {isLast} = require('../is');
4
4
 
5
5
  module.exports.ForStatement = {
6
- print(path, {print, maybe, indent}) {
6
+ print(path, {print, maybe}) {
7
7
  const {
8
8
  test,
9
9
  update,
@@ -24,10 +24,12 @@ module.exports.ForStatement = {
24
24
  print(' ');
25
25
  print('__body');
26
26
  } else {
27
- print.newline();
28
- indent.inc();
27
+ const is = !path.get('body').isEmptyStatement();
28
+
29
+ maybe.print.newline(is);
30
+ maybe.indent.inc(is);
29
31
  print('__body');
30
- indent.dec();
32
+ maybe.indent.dec(is);
31
33
  }
32
34
  },
33
35
  after(path, {print}) {
@@ -3,49 +3,59 @@
3
3
  const {hasPrevNewline, markAfter} = require('../mark');
4
4
  const {isFirst} = require('../is');
5
5
 
6
- module.exports.IfStatement = (path, {indent, print}) => {
7
- if (shouldAddNewlineBefore(path)) {
6
+ const isEmptyConsequent = (path) => path.get('consequent').isEmptyStatement();
7
+
8
+ module.exports.IfStatement = {
9
+ before: (path, {print}) => {
8
10
  print.linebreak();
9
- }
10
-
11
- indent();
12
- print('if (');
13
- print('__test');
14
- print(')');
15
-
16
- const consequent = path.get('consequent');
17
- const alternate = path.get('alternate');
18
-
19
- if (consequent.isBlockStatement()) {
20
- print(' ');
21
- print(consequent);
22
- } else {
23
- print.newline();
24
- indent.inc();
25
- print(consequent);
26
- indent.dec();
27
- }
28
-
29
- if (alternate.isBlockStatement()) {
30
- print(' else ');
31
- print(alternate);
32
- } else if (alternate.node) {
33
- print.breakline();
34
- print('else');
11
+ },
12
+ beforeIf: shouldAddNewlineBefore,
13
+ print: (path, {indent, print, maybe}) => {
14
+ indent();
15
+ print('if (');
16
+ print('__test');
17
+ print(')');
18
+
19
+ const consequent = path.get('consequent');
20
+ const alternate = path.get('alternate');
21
+
22
+ if (consequent.isBlockStatement()) {
23
+ print(' ');
24
+ print(consequent);
25
+ } else {
26
+ const is = !isEmptyConsequent(path);
27
+
28
+ maybe.print.newline(is);
29
+ maybe.indent.inc(is);
30
+ print(consequent);
31
+ maybe.indent.dec(is);
32
+ }
33
+
34
+ if (alternate.isBlockStatement()) {
35
+ print(' else ');
36
+ print(alternate);
37
+ } else if (alternate.node) {
38
+ print.breakline();
39
+ print('else');
40
+ print.newline();
41
+ indent.inc();
42
+ print(alternate);
43
+ indent.dec();
44
+ }
45
+ },
46
+ afterIf: (path) => {
47
+ const next = path.getNextSibling();
48
+
49
+ if (!next.node || next.isExpressionStatement() || next.isReturnStatement())
50
+ return false;
51
+
52
+ return true;
53
+ },
54
+ after: (path, {print}) => {
55
+ print.indent();
35
56
  print.newline();
36
- indent.inc();
37
- print(alternate);
38
- indent.dec();
39
- }
40
-
41
- const next = path.getNextSibling();
42
-
43
- if (!next.node || next.isExpressionStatement() || next.isReturnStatement())
44
- return;
45
-
46
- print.indent();
47
- print.newline();
48
- markAfter(path);
57
+ markAfter(path);
58
+ },
49
59
  };
50
60
 
51
61
  function shouldAddNewlineBefore(path) {
@@ -35,6 +35,9 @@ module.exports = {
35
35
  path.get('body').forEach(print);
36
36
  print.newline();
37
37
  },
38
+ EmptyStatement(path, {write}) {
39
+ write(';');
40
+ },
38
41
  InterpreterDirective(path, {print}) {
39
42
  print(`#!${path.node.value}`);
40
43
  print.newline();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.14.1",
3
+ "version": "1.14.3",
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",