@putout/printer 1.3.0 → 1.3.1

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.
@@ -1,13 +1,13 @@
1
1
  'use strict';
2
2
 
3
- const {hasPrevNewline} = require('../mark');
3
+ const {hasPrevNewline, isMarkedParent} = require('../mark');
4
4
 
5
5
  const {entries} = Object;
6
6
 
7
7
  module.exports.CallExpression = (path, {traverse, indent, write, incIndent, decIndent, maybeWrite}) => {
8
8
  const isParentCall = toLong(path) && path.parentPath.isCallExpression();
9
9
 
10
- if (shouldAddNewLine(path) && !hasPrevNewline(path.parentPath)) {
10
+ if (shouldAddNewLine(path) && !hasPrevNewline(path.parentPath) && !isMarkedParent(path)) {
11
11
  write.newline();
12
12
  write.indent();
13
13
  }
@@ -11,48 +11,56 @@ const isForOf = (path) => {
11
11
  return false;
12
12
  };
13
13
 
14
- module.exports.ObjectExpression = (path, {traverse, write, maybeWrite, maybeIndent, incIndent, decIndent}) => {
15
- incIndent();
14
+ module.exports.ObjectExpression = (path, {traverse, write, maybe, indent}) => {
15
+ indent.inc();
16
16
 
17
17
  const properties = path.get('properties');
18
18
  const parens = isBodyOfArrow(path);
19
19
  const isCall = path.parentPath.isCallExpression();
20
- const isOneLine = isCall && properties.length < 2 || isForOf(path);
20
+ const {length} = properties;
21
+ const isOneLine = !length || (isCall && length < 2 || isForOf(path));
21
22
 
22
- maybeWrite(parens, '(');
23
+ maybe.write(parens, '(');
23
24
  write('{');
24
25
 
25
- maybeWrite(!isOneLine, '\n');
26
+ maybe.write(!isOneLine, '\n');
26
27
 
27
28
  for (const property of properties) {
28
29
  if (property.isSpreadElement()) {
30
+ maybe.indent(properties.length > 1);
29
31
  traverse(property);
32
+
33
+ if (properties.length > 1) {
34
+ write(',');
35
+ write.newline();
36
+ }
37
+
30
38
  continue;
31
39
  }
32
40
 
33
41
  const {shorthand, computed} = property.node;
34
42
 
35
- maybeIndent(!isOneLine);
43
+ maybe.indent(!isOneLine);
36
44
 
37
45
  if (property.isObjectMethod()) {
38
46
  traverse(property);
39
47
  continue;
40
48
  }
41
49
 
42
- maybeWrite(computed, '[');
50
+ maybe.write(computed, '[');
43
51
  traverse(property.get('key'));
44
- maybeWrite(computed, ']');
52
+ maybe.write(computed, ']');
45
53
 
46
54
  if (!shorthand) {
47
55
  write(': ');
48
56
  traverse(property.get('value'));
49
57
  }
50
58
 
51
- maybeWrite(!isOneLine, ',\n');
59
+ maybe.write(!isOneLine, ',\n');
52
60
  }
53
61
 
54
- decIndent();
55
- maybeIndent(!isOneLine);
62
+ indent.dec();
63
+ maybe.indent(!isOneLine);
56
64
  write('}');
57
- maybeWrite(parens, ')');
65
+ maybe.write(parens, ')');
58
66
  };
@@ -2,4 +2,3 @@
2
2
 
3
3
  module.exports.isPrevBody = (path) => path.getPrevSibling().isBlockStatement();
4
4
  module.exports.isNext = (path) => path.getNextSibling().node;
5
-
@@ -19,3 +19,7 @@ module.exports.hasPrevNewline = (path) => {
19
19
  };
20
20
 
21
21
  module.exports.maybeMark = (a, path) => a && mark(path);
22
+
23
+ module.exports.isMarkedParent = (path) => {
24
+ return isMarked(path.parentPath);
25
+ };
@@ -1,8 +1,10 @@
1
1
  'use strict';
2
2
 
3
+ const {mark} = require('../mark');
3
4
  module.exports.ExpressionStatement = (path, {write, indent, traverse}) => {
4
5
  if (isCoupleLinesExpression(path) && !isFirst(path) && shouldAddNewLine(path)) {
5
6
  write.linebreak();
7
+ mark(path);
6
8
  }
7
9
 
8
10
  const expressionPath = path.get('expression');
@@ -1,12 +1,13 @@
1
1
  'use strict';
2
2
 
3
+ const {isMarked} = require('../mark');
3
4
  const isPrevNewLine = (path) => {
4
5
  const prev = path.getPrevSibling();
5
6
 
6
7
  if (!prev.node)
7
8
  return true;
8
9
 
9
- return prev.__putout_newline;
10
+ return isMarked(prev);
10
11
  };
11
12
 
12
13
  module.exports.ForOfStatement = (path, {write, indent, traverse}) => {
@@ -36,9 +37,4 @@ module.exports.ForOfStatement = (path, {write, indent, traverse}) => {
36
37
  write(' ');
37
38
  traverse(bodyPath);
38
39
  }
39
-
40
- if (path.getNextSibling().node) {
41
- //write.newline();
42
- //path.__putout_newline = true;
43
- }
44
40
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
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",