@putout/printer 1.5.2 → 1.5.4

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.20, v1.5.4
2
+
3
+ feature:
4
+ - 96d7056 @putout/printer: split linebreak(\s\n) and breakline(\n\s)
5
+
6
+ 2023.03.20, v1.5.3
7
+
8
+ feature:
9
+ - 9dc85e4 @putout/printer: IfStatement: add support of alternate
10
+
1
11
  2023.03.19, v1.5.2
2
12
 
3
13
  feature:
@@ -11,7 +11,7 @@ module.exports.CallExpression = (path, {indent, print, maybe}) => {
11
11
  const isParentCall = toLong(path) && path.parentPath.isCallExpression();
12
12
 
13
13
  if (shouldAddNewLine(path) && !isMarkedParentBefore(path) && !isMarkedPrevAfter(path.parentPath))
14
- print.linebreak();
14
+ print.breakline();
15
15
 
16
16
  print(path.get('callee'));
17
17
  print('(');
@@ -6,7 +6,7 @@ const isFirst = (path) => path.node === path.parentPath.node.body[0];
6
6
 
7
7
  module.exports.ClassDeclaration = (path, {maybe, print, indent}) => {
8
8
  if (!isFirst(path)) {
9
- print.linebreak();
9
+ print.breakline();
10
10
  }
11
11
 
12
12
  indent();
@@ -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
+
@@ -7,9 +7,9 @@ const {
7
7
  } = require('../mark');
8
8
  const {isNext} = require('../is');
9
9
 
10
- module.exports.ExpressionStatement = (path, {write, indent, traverse}) => {
10
+ module.exports.ExpressionStatement = (path, {write, indent, traverse, print}) => {
11
11
  if (isCoupleLinesExpression(path) && !isFirst(path) && shouldAddNewLine(path) && !isMarkedPrevAfter(path)) {
12
- write.linebreak();
12
+ print.breakline();
13
13
  markBefore(path);
14
14
  }
15
15
 
@@ -62,3 +62,4 @@ function shouldAddNewLine(path) {
62
62
 
63
63
  return true;
64
64
  }
65
+
@@ -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
  },
@@ -16,7 +16,7 @@ module.exports.TryStatement = (path, {print, maybe}) => {
16
16
  print.newline();
17
17
  }
18
18
 
19
- maybe.print.linebreak(isNext(path));
19
+ maybe.print.breakline(isNext(path));
20
20
  };
21
21
 
22
22
  module.exports.CatchClause = (path, {print}) => {
@@ -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,29 @@ 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.breakline();
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.linebreak();
35
+ markAfter(path);
36
+ }
35
37
  };
36
38
 
37
39
  function shouldAddNewLine(path) {
@@ -60,3 +62,4 @@ function shouldAddNewLine(path) {
60
62
  function isFirst(path) {
61
63
  return path.node === path.parentPath.node.body[0];
62
64
  }
65
+
@@ -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 maybeBreakline = (a) => a && breakline();
41
41
 
42
42
  let i = 0;
43
43
  const incIndent = () => ++i;
@@ -54,7 +54,14 @@ module.exports.tokenize = (ast) => {
54
54
  dec: decIndent,
55
55
  });
56
56
 
57
- const writeEmptyLine = () => {
57
+ const linebreak = () => {
58
+ tokens.push({
59
+ type: TYPES.NEWLINE,
60
+ value: `${printIndent(i)}\n`,
61
+ });
62
+ };
63
+
64
+ const breakline = () => {
58
65
  tokens.push({
59
66
  type: TYPES.NEWLINE,
60
67
  value: `\n${printIndent(i)}`,
@@ -71,7 +78,8 @@ module.exports.tokenize = (ast) => {
71
78
  assign(write, {
72
79
  indent,
73
80
  newline,
74
- linebreak: writeEmptyLine,
81
+ linebreak,
82
+ breakline,
75
83
  });
76
84
  const print = (maybeLine) => {
77
85
  if (isString(maybeLine))
@@ -98,7 +106,7 @@ module.exports.tokenize = (ast) => {
98
106
  assign(print, write);
99
107
  assign(maybePrint, {
100
108
  newline: maybeNewline,
101
- linebreak: maybeLinebreak,
109
+ breakline: maybeBreakline,
102
110
  });
103
111
 
104
112
  const printer = {
@@ -110,7 +118,6 @@ module.exports.tokenize = (ast) => {
110
118
  debug,
111
119
  maybeIndent,
112
120
  traverse,
113
- writeEmptyLine,
114
121
  maybe,
115
122
  print,
116
123
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.5.2",
3
+ "version": "1.5.4",
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",