@putout/printer 1.1.1 → 1.1.2

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.
@@ -0,0 +1,3 @@
1
+ 'use strict';
2
+
3
+ module.exports.isPrevBody = (path) => path.getPrevSibling().isBlockStatement();
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ const WATER_MARK = '__putout_newline';
4
+
5
+ module.exports.mark = mark;
6
+
7
+ function mark(path) {
8
+ path[WATER_MARK] = true;
9
+ }
10
+
11
+ module.exports.isMarked = isMarked;
12
+
13
+ function isMarked(path) {
14
+ return path[WATER_MARK];
15
+ }
16
+
17
+ module.exports.hasPrevNewline = (path) => {
18
+ return isMarked(path.getPrevSibling());
19
+ };
20
+
21
+ module.exports.maybeMark = (a, path) => a && mark(path);
@@ -5,6 +5,9 @@ const isFirstStatement = (path) => path.get('body.0')?.isStatement();
5
5
  module.exports.BlockStatement = (path, {write, indent, traverse}) => {
6
6
  const body = path.get('body');
7
7
 
8
+ if (path.parentPath.isBlockStatement())
9
+ indent();
10
+
8
11
  indent.inc();
9
12
  write('{');
10
13
 
@@ -1,7 +1,9 @@
1
1
  'use strict';
2
2
 
3
+ const {hasPrevNewline} = require('../mark');
4
+
3
5
  module.exports.IfStatement = (path, {write, indent, traverse, incIndent, decIndent}) => {
4
- if (!isFirst(path)) {
6
+ if (!isFirst(path) && !hasPrevNewline(path)) {
5
7
  indent();
6
8
  write('\n');
7
9
  }
@@ -5,6 +5,7 @@ const {VariableDeclaration} = require('./variable-declaration');
5
5
  const {IfStatement} = require('./if-statement');
6
6
  const {ForOfStatement} = require('./for-of-statement');
7
7
  const {BlockStatement} = require('./block-statement');
8
+ const {ReturnStatement} = require('./return-statement');
8
9
 
9
10
  module.exports = {
10
11
  BlockStatement,
@@ -12,6 +13,7 @@ module.exports = {
12
13
  VariableDeclaration,
13
14
  IfStatement,
14
15
  ForOfStatement,
16
+ ReturnStatement,
15
17
  Program(path, {traverse}) {
16
18
  path.get('body').forEach(traverse);
17
19
  },
@@ -19,24 +21,5 @@ module.exports = {
19
21
  indent();
20
22
  write('continue;\n');
21
23
  },
22
- ReturnStatement(path, {indent, write, traverse}) {
23
- if (path?.parentPath?.node?.body?.length > 2) {
24
- write.indent();
25
- write.newline();
26
- }
27
-
28
- indent();
29
- write('return');
30
-
31
- const argPath = path.get('argument');
32
-
33
- if (argPath.node) {
34
- write(' ');
35
- traverse(argPath);
36
- }
37
-
38
- write(';');
39
- write.newline();
40
- },
41
24
  };
42
25
 
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+
3
+ const {isPrevBody} = require('../is');
4
+ const isBodyLength = ({parentPath}) => parentPath.node?.body?.length > 2;
5
+
6
+ module.exports.ReturnStatement = (path, {indent, write, traverse}) => {
7
+ if (isBodyLength(path) || isPrevBody(path)) {
8
+ write.indent();
9
+ write.newline();
10
+ }
11
+
12
+ indent();
13
+ write('return');
14
+
15
+ const argPath = path.get('argument');
16
+
17
+ if (argPath.node) {
18
+ write(' ');
19
+ traverse(argPath);
20
+ }
21
+
22
+ write(';');
23
+ write.newline();
24
+ };
25
+
@@ -30,6 +30,7 @@ module.exports.VariableDeclaration = (path, {write, maybe, maybeIndent, traverse
30
30
 
31
31
  maybe.indent(is);
32
32
  maybe.write(is, '\n');
33
+ maybe.mark(is, path);
33
34
  };
34
35
  function isCoupleLinesExpression(path) {
35
36
  const start = path.node?.loc?.start.line;
@@ -6,6 +6,7 @@ const statements = require('./statements');
6
6
  const literals = require('./literals');
7
7
  const {TYPES} = require('../types');
8
8
  const {createDebug} = require('./debug');
9
+ const {maybeMark} = require('./mark');
9
10
 
10
11
  const {assign} = Object;
11
12
 
@@ -67,6 +68,7 @@ module.exports.tokenize = (ast) => {
67
68
  const maybe = {
68
69
  write: maybeWrite,
69
70
  indent: maybeIndent,
71
+ mark: maybeMark,
70
72
  };
71
73
 
72
74
  const printer = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
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",