@putout/printer 1.5.2 → 1.5.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,8 @@
1
+ 2023.03.20, v1.5.3
2
+
3
+ feature:
4
+ - 9dc85e4 @putout/printer: IfStatement: add support of alternate
5
+
1
6
  2023.03.19, v1.5.2
2
7
 
3
8
  feature:
@@ -29,8 +29,6 @@ module.exports.hasPrevNewline = (path) => {
29
29
  return isMarkedAfter(path.getPrevSibling());
30
30
  };
31
31
 
32
- module.exports.maybeMarkAfter = (a, path) => a && markAfter(path);
33
-
34
32
  module.exports.isMarkedParentBefore = (path) => {
35
33
  return isMarkedBefore(path.parentPath);
36
34
  };
@@ -2,35 +2,45 @@
2
2
 
3
3
  const isFirstStatement = (path) => path.get('body.0')?.isStatement();
4
4
 
5
- module.exports.BlockStatement = (path, {write, indent, traverse, maybe}) => {
5
+ module.exports.BlockStatement = (path, {indent, maybe, print}) => {
6
6
  const body = path.get('body');
7
7
 
8
8
  if (path.parentPath.isBlockStatement())
9
9
  indent();
10
10
 
11
11
  indent.inc();
12
- write('{');
12
+ print('{');
13
13
 
14
14
  if (body.length > 1 || isFirstStatement(path))
15
- write.newline();
15
+ print.newline();
16
16
 
17
- body.forEach(traverse);
17
+ body.forEach(print);
18
18
  indent.dec();
19
19
 
20
- if (body.length)
21
- indent();
22
-
23
- write('}');
20
+ maybe.indent(body.length);
21
+ print('}');
24
22
 
25
23
  if (path.parentPath.isObjectMethod()) {
26
- write(',');
24
+ print(',');
27
25
  }
28
26
 
29
- const shouldAddNewLine = !isTry(path) && !/FunctionExpression/.test(path.parentPath.type);
27
+ const shouldAddNewLine = !isNewLineAdded(path);
30
28
 
31
29
  maybe.print.newline(shouldAddNewLine);
32
30
  };
33
31
 
32
+ function isNewLineAdded(path) {
33
+ const {parentPath} = path;
34
+
35
+ if (isTry(path) || /FunctionExpression/.test(path.parentPath.type))
36
+ return true;
37
+
38
+ if (parentPath.isIfStatement() && parentPath.get('consequent').node === path.node && parentPath.node.alternate)
39
+ return true;
40
+
41
+ return false;
42
+ }
43
+
34
44
  function isTry({parentPath}) {
35
45
  if (parentPath.isTryStatement())
36
46
  return true;
@@ -40,3 +50,4 @@ function isTry({parentPath}) {
40
50
 
41
51
  return false;
42
52
  }
53
+
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ module.exports.DebuggerStatement = (path, {print, indent}) => {
4
+ indent();
5
+ print('debugger;');
6
+ print.newline();
7
+ };
8
+
@@ -1,39 +1,50 @@
1
1
  'use strict';
2
2
 
3
- const {hasPrevNewline} = require('../mark');
3
+ const {hasPrevNewline, markAfter} = require('../mark');
4
4
  const {isFirst} = require('../is');
5
5
 
6
- module.exports.IfStatement = (path, {write, indent, traverse, incIndent, decIndent}) => {
6
+ module.exports.IfStatement = (path, {indent, print}) => {
7
7
  if (!isFirst(path) && !hasPrevNewline(path)) {
8
- indent();
9
- write('\n');
8
+ print.indent();
9
+ print.newline();
10
10
  }
11
11
 
12
12
  indent();
13
- write('if (');
14
- traverse(path.get('test'));
15
- write(')');
13
+ print('if (');
14
+ print(path.get('test'));
15
+ print(')');
16
16
 
17
17
  const consequent = path.get('consequent');
18
+ const alternate = path.get('alternate');
18
19
 
19
20
  if (consequent.isBlockStatement()) {
20
- write(' ');
21
- traverse(consequent);
22
-
23
- return;
21
+ print(' ');
22
+ print(consequent);
23
+ } else {
24
+ print.newline();
25
+ indent.inc();
26
+ print(consequent);
27
+ indent.dec();
24
28
  }
25
29
 
26
- write('\n');
27
- incIndent();
28
- traverse(consequent);
29
- decIndent();
30
+ if (alternate.isBlockStatement()) {
31
+ print(' else ');
32
+ print(alternate);
33
+ } else if (alternate.node) {
34
+ print('else');
35
+ print.newline();
36
+ indent.inc();
37
+ print(alternate);
38
+ indent.dec();
39
+ }
30
40
 
31
41
  const next = path.getNextSibling();
32
42
 
33
- if (!next.node || next.isIfStatement() || next.isExpressionStatement() || next.isReturnStatement())
43
+ if (!next.node || next.isExpressionStatement() || next.isReturnStatement())
34
44
  return;
35
45
 
36
- indent();
37
- write('\n');
46
+ print.indent();
47
+ print.newline();
48
+ markAfter(path);
38
49
  };
39
50
 
@@ -7,6 +7,7 @@ const {ForOfStatement} = require('./for-of-statement');
7
7
  const {BlockStatement} = require('./block-statement');
8
8
  const {ReturnStatement} = require('./return-statement');
9
9
  const TryStatements = require('./try-statements');
10
+ const {DebuggerStatement} = require('./debugger-statement');
10
11
 
11
12
  module.exports = {
12
13
  BlockStatement,
@@ -15,6 +16,7 @@ module.exports = {
15
16
  IfStatement,
16
17
  ForOfStatement,
17
18
  ReturnStatement,
19
+ DebuggerStatement,
18
20
  Program(path, {traverse}) {
19
21
  path.get('body').forEach(traverse);
20
22
  },
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const {isNext, isCoupleLines} = require('../is');
4
+ const {hasPrevNewline, markAfter} = require('../mark');
4
5
  const isNextAssign = (path) => {
5
6
  const nextPath = path.getNextSibling();
6
7
 
@@ -10,28 +11,30 @@ const isNextAssign = (path) => {
10
11
  return nextPath.get('expression').isAssignmentExpression();
11
12
  };
12
13
 
13
- module.exports.VariableDeclaration = (path, {write, maybe, maybeIndent, traverse}) => {
14
- if (!isFirst(path) && shouldAddNewLine(path)) {
15
- write.linebreak();
16
- }
14
+ module.exports.VariableDeclaration = (path, {maybe, print}) => {
15
+ if (!isFirst(path) && shouldAddNewLine(path) && !hasPrevNewline(path))
16
+ print.linebreak();
17
17
 
18
18
  const isParentBlock = /Program|BlockStatement/.test(path.parentPath.type);
19
19
 
20
- maybeIndent(isParentBlock);
21
- write(`${path.node.kind} `);
22
- traverse(path.get('declarations.0.id'));
20
+ maybe.indent(isParentBlock);
21
+ print(`${path.node.kind} `);
22
+ print(path.get('declarations.0.id'));
23
23
 
24
24
  const initPath = path.get('declarations.0.init');
25
25
 
26
- maybe.write(initPath.node, ' = ');
27
- traverse(initPath);
28
- maybe.write(isParentBlock, ';\n');
26
+ maybe.print(initPath.node, ' = ');
27
+ print(initPath);
28
+ maybe.print(isParentBlock, ';');
29
+ maybe.print.newline(isParentBlock);
29
30
 
30
31
  const is = isNext(path) && isCoupleLines(path) && !isNextAssign(path);
31
32
 
32
- maybe.indent(is);
33
- maybe.write(is, '\n');
34
- maybe.markAfter(is, path);
33
+ if (is) {
34
+ print.indent();
35
+ print.newline();
36
+ markAfter(path);
37
+ }
35
38
  };
36
39
 
37
40
  function shouldAddNewLine(path) {
@@ -60,3 +63,4 @@ function shouldAddNewLine(path) {
60
63
  function isFirst(path) {
61
64
  return path.node === path.parentPath.node.body[0];
62
65
  }
66
+
@@ -37,7 +37,7 @@ module.exports.tokenize = (ast) => {
37
37
  const maybeIndentInc = (a) => a && indent.inc();
38
38
  const maybeIndentDec = (a) => a && indent.dec();
39
39
  const maybeNewline = (a) => a && newline();
40
- const maybeLinebreak = (a) => a && writeEmptyLine();
40
+ const maybeLinebreak = (a) => a && linebreak();
41
41
 
42
42
  let i = 0;
43
43
  const incIndent = () => ++i;
@@ -54,7 +54,7 @@ module.exports.tokenize = (ast) => {
54
54
  dec: decIndent,
55
55
  });
56
56
 
57
- const writeEmptyLine = () => {
57
+ const linebreak = () => {
58
58
  tokens.push({
59
59
  type: TYPES.NEWLINE,
60
60
  value: `\n${printIndent(i)}`,
@@ -71,7 +71,7 @@ module.exports.tokenize = (ast) => {
71
71
  assign(write, {
72
72
  indent,
73
73
  newline,
74
- linebreak: writeEmptyLine,
74
+ linebreak,
75
75
  });
76
76
  const print = (maybeLine) => {
77
77
  if (isString(maybeLine))
@@ -110,7 +110,6 @@ module.exports.tokenize = (ast) => {
110
110
  debug,
111
111
  maybeIndent,
112
112
  traverse,
113
- writeEmptyLine,
114
113
  maybe,
115
114
  print,
116
115
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.5.2",
3
+ "version": "1.5.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",