@putout/printer 1.2.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.
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+
3
+ module.exports.RestElement = (path, {write, traverse}) => {
4
+ write('...');
5
+ traverse(path.get('argument'));
6
+ };
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+
3
+ module.exports.SpreadElement = (path, {write, traverse}) => {
4
+ write('...');
5
+ traverse(path.get('argument'));
6
+ };
@@ -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
  }
@@ -13,6 +13,8 @@ const {AssignmentExpression} = require('./assignment-expression');
13
13
  const {ArrayExpression} = require('./array-expression');
14
14
  const {ArrayPattern} = require('./array-pattern');
15
15
  const {AssignmentPattern} = require('./assignment-pattern');
16
+ const {RestElement} = require('./RestElement');
17
+ const {SpreadElement} = require('./SpreadElement');
16
18
 
17
19
  module.exports = {
18
20
  ...functions,
@@ -27,6 +29,8 @@ module.exports = {
27
29
  NewExpression,
28
30
  ObjectExpression,
29
31
  ObjectPattern,
32
+ RestElement,
33
+ SpreadElement,
30
34
  BinaryExpression(path, {traverse, write}) {
31
35
  traverse(path.get('left'));
32
36
  write(` ${path.node.operator} `);
@@ -1,45 +1,66 @@
1
1
  'use strict';
2
2
 
3
3
  const isBodyOfArrow = (path) => path.parentPath.node.body === path.node;
4
- const isForOf = (path) => path.parentPath?.parentPath?.isForOfStatement();
4
+ const isForOf = (path) => {
5
+ if (path.parentPath.isForOfStatement())
6
+ return true;
7
+
8
+ if (path.parentPath?.parentPath?.isForOfStatement())
9
+ return true;
10
+
11
+ return false;
12
+ };
5
13
 
6
- module.exports.ObjectExpression = (path, {traverse, write, maybeWrite, maybeIndent, incIndent, decIndent}) => {
7
- incIndent();
14
+ module.exports.ObjectExpression = (path, {traverse, write, maybe, indent}) => {
15
+ indent.inc();
8
16
 
9
17
  const properties = path.get('properties');
10
18
  const parens = isBodyOfArrow(path);
11
19
  const isCall = path.parentPath.isCallExpression();
12
- const isOneLine = isCall && properties.length < 2 || isForOf(path);
20
+ const {length} = properties;
21
+ const isOneLine = !length || (isCall && length < 2 || isForOf(path));
13
22
 
14
- maybeWrite(parens, '(');
23
+ maybe.write(parens, '(');
15
24
  write('{');
16
25
 
17
- maybeWrite(!isOneLine, '\n');
26
+ maybe.write(!isOneLine, '\n');
18
27
 
19
28
  for (const property of properties) {
29
+ if (property.isSpreadElement()) {
30
+ maybe.indent(properties.length > 1);
31
+ traverse(property);
32
+
33
+ if (properties.length > 1) {
34
+ write(',');
35
+ write.newline();
36
+ }
37
+
38
+ continue;
39
+ }
40
+
20
41
  const {shorthand, computed} = property.node;
21
42
 
22
- maybeIndent(!isOneLine);
43
+ maybe.indent(!isOneLine);
23
44
 
24
45
  if (property.isObjectMethod()) {
25
46
  traverse(property);
26
47
  continue;
27
48
  }
28
49
 
29
- maybeWrite(computed, '[');
50
+ maybe.write(computed, '[');
30
51
  traverse(property.get('key'));
31
- maybeWrite(computed, ']');
52
+ maybe.write(computed, ']');
32
53
 
33
54
  if (!shorthand) {
34
55
  write(': ');
35
56
  traverse(property.get('value'));
36
57
  }
37
58
 
38
- maybeWrite(!isOneLine, ',\n');
59
+ maybe.write(!isOneLine, ',\n');
39
60
  }
40
61
 
41
- decIndent();
42
- maybeIndent(!isOneLine);
62
+ indent.dec();
63
+ maybe.indent(!isOneLine);
43
64
  write('}');
44
- maybeWrite(parens, ')');
65
+ maybe.write(parens, ')');
45
66
  };
@@ -2,7 +2,6 @@
2
2
 
3
3
  const {entries} = Object;
4
4
  const isForOf = (path) => path.parentPath?.parentPath?.parentPath?.isForOfStatement();
5
- const isAssign = (path) => path.isAssignmentPattern();
6
5
 
7
6
  module.exports.ObjectPattern = (path, {traverse, maybeIndent, write, maybeWrite, incIndent, decIndent}) => {
8
7
  incIndent();
@@ -16,6 +15,11 @@ module.exports.ObjectPattern = (path, {traverse, maybeIndent, write, maybeWrite,
16
15
  maybeWrite(is, '\n');
17
16
 
18
17
  for (const [i, property] of entries(properties)) {
18
+ if (property.isRestElement()) {
19
+ traverse(property);
20
+ continue;
21
+ }
22
+
19
23
  const valuePath = property.get('value');
20
24
  const keyPath = property.get('key');
21
25
  const {shorthand} = property.node;
@@ -28,7 +32,7 @@ module.exports.ObjectPattern = (path, {traverse, maybeIndent, write, maybeWrite,
28
32
  traverse(valuePath);
29
33
  }
30
34
 
31
- if (isAssign(valuePath))
35
+ if (valuePath.isAssignmentPattern())
32
36
  traverse(valuePath);
33
37
 
34
38
  if (is) {
@@ -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.2.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",