@putout/printer 1.1.3 → 1.3.0

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,34 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const {entries} = Object;
4
- const isForOf = ({parentPath}) => parentPath.parentPath.parentPath.isForOfStatement();
5
-
6
- module.exports.ArrayPattern = (path, {write, indent, maybe, traverse}) => {
7
- write('[');
8
-
9
- const elements = path.get('elements');
10
-
11
- indent.inc();
12
- const isNewLine = !isForOf(path) && elements.length > 2;
13
- const n = elements.length - 1;
14
-
15
- maybe.write(isNewLine && elements.length, '\n');
16
-
17
- for (const [index, element] of entries(elements)) {
18
- maybe.indent(isNewLine);
19
-
20
- traverse(element);
21
-
22
- maybe.write(isNewLine, ',\n');
23
- maybe.write(!isNewLine && index < n, ', ');
24
- }
25
-
26
- indent.dec();
27
-
28
- maybe.indent(elements.length && isNewLine);
29
-
30
- write(']');
31
- };
4
+ const isForOf = ({parentPath}) => parentPath.isForOfStatement();
32
5
 
33
6
  module.exports.ArrayExpression = (path, {write, indent, maybe, traverse}) => {
34
7
  write('[');
@@ -36,7 +9,8 @@ module.exports.ArrayExpression = (path, {write, indent, maybe, traverse}) => {
36
9
  const elements = path.get('elements');
37
10
 
38
11
  indent.inc();
39
- const isNewLine = !isNumbers(elements);
12
+
13
+ const isNewLine = !isNumbers(elements) && !isForOf(path);
40
14
  const n = elements.length - 1;
41
15
 
42
16
  maybe.write(isNewLine && elements.length, '\n');
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+
3
+ const {entries} = Object;
4
+ const isForOf = ({parentPath}) => parentPath.parentPath.parentPath.isForOfStatement();
5
+
6
+ module.exports.ArrayPattern = (path, {write, indent, maybe, traverse}) => {
7
+ write('[');
8
+
9
+ const elements = path.get('elements');
10
+
11
+ indent.inc();
12
+ const isNewLine = !isForOf(path) && elements.length > 2;
13
+ const n = elements.length - 1;
14
+
15
+ maybe.write(isNewLine && elements.length, '\n');
16
+
17
+ for (const [index, element] of entries(elements)) {
18
+ maybe.indent(isNewLine);
19
+
20
+ traverse(element);
21
+
22
+ maybe.write(isNewLine, ',\n');
23
+ maybe.write(!isNewLine && index < n, ', ');
24
+ }
25
+
26
+ indent.dec();
27
+
28
+ maybe.indent(elements.length && isNewLine);
29
+
30
+ write(']');
31
+ };
32
+
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+
3
+ module.exports.AssignmentPattern = (path, {write, traverse}) => {
4
+ write(' = ');
5
+ traverse(path.get('right'));
6
+ };
@@ -1,27 +1,36 @@
1
1
  'use strict';
2
2
 
3
3
  const functions = require('./functions');
4
- const arrays = require('./arrays');
5
4
  const unaryExpressions = require('./unary-expressions');
6
5
  const memberExpressions = require('./member-expressions');
6
+
7
7
  const {ClassDeclaration} = require('./class-declaration');
8
8
  const {CallExpression} = require('./call-expression');
9
9
  const {NewExpression} = require('./new-expression');
10
10
  const {ObjectExpression} = require('./object-expression');
11
11
  const {ObjectPattern} = require('./object-pattern');
12
12
  const {AssignmentExpression} = require('./assignment-expression');
13
+ const {ArrayExpression} = require('./array-expression');
14
+ const {ArrayPattern} = require('./array-pattern');
15
+ const {AssignmentPattern} = require('./assignment-pattern');
16
+ const {RestElement} = require('./RestElement');
17
+ const {SpreadElement} = require('./SpreadElement');
13
18
 
14
19
  module.exports = {
15
20
  ...functions,
16
21
  ...unaryExpressions,
17
- ...arrays,
18
22
  ...memberExpressions,
23
+ ArrayPattern,
24
+ ArrayExpression,
19
25
  AssignmentExpression,
26
+ AssignmentPattern,
20
27
  CallExpression,
21
28
  ClassDeclaration,
22
29
  NewExpression,
23
30
  ObjectExpression,
24
31
  ObjectPattern,
32
+ RestElement,
33
+ SpreadElement,
25
34
  BinaryExpression(path, {traverse, write}) {
26
35
  traverse(path.get('left'));
27
36
  write(` ${path.node.operator} `);
@@ -1,6 +1,15 @@
1
1
  'use strict';
2
2
 
3
3
  const isBodyOfArrow = (path) => path.parentPath.node.body === path.node;
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
+ };
4
13
 
5
14
  module.exports.ObjectExpression = (path, {traverse, write, maybeWrite, maybeIndent, incIndent, decIndent}) => {
6
15
  incIndent();
@@ -8,7 +17,7 @@ module.exports.ObjectExpression = (path, {traverse, write, maybeWrite, maybeInde
8
17
  const properties = path.get('properties');
9
18
  const parens = isBodyOfArrow(path);
10
19
  const isCall = path.parentPath.isCallExpression();
11
- const isOneLine = isCall && properties.length < 2;
20
+ const isOneLine = isCall && properties.length < 2 || isForOf(path);
12
21
 
13
22
  maybeWrite(parens, '(');
14
23
  write('{');
@@ -16,6 +25,11 @@ module.exports.ObjectExpression = (path, {traverse, write, maybeWrite, maybeInde
16
25
  maybeWrite(!isOneLine, '\n');
17
26
 
18
27
  for (const property of properties) {
28
+ if (property.isSpreadElement()) {
29
+ traverse(property);
30
+ continue;
31
+ }
32
+
19
33
  const {shorthand, computed} = property.node;
20
34
 
21
35
  maybeIndent(!isOneLine);
@@ -11,19 +11,30 @@ module.exports.ObjectPattern = (path, {traverse, maybeIndent, write, maybeWrite,
11
11
  const properties = path.get('properties');
12
12
  const n = properties.length - 1;
13
13
 
14
- const is = !isForOf(path) && !path.parentPath.isFunction() && properties.length > 1 && checkLength(properties);
14
+ const is = !isForOf(path) && !path.parentPath.isFunction() && n && checkLength(properties);
15
15
  maybeWrite(is, '\n');
16
16
 
17
17
  for (const [i, property] of entries(properties)) {
18
+ if (property.isRestElement()) {
19
+ traverse(property);
20
+ continue;
21
+ }
22
+
23
+ const valuePath = property.get('value');
24
+ const keyPath = property.get('key');
18
25
  const {shorthand} = property.node;
26
+
19
27
  maybeIndent(is);
20
- traverse(property.get('key'));
28
+ traverse(keyPath);
21
29
 
22
30
  if (!shorthand) {
23
31
  write(': ');
24
- traverse(property.get('value'));
32
+ traverse(valuePath);
25
33
  }
26
34
 
35
+ if (valuePath.isAssignmentPattern())
36
+ traverse(valuePath);
37
+
27
38
  if (is) {
28
39
  write(',\n');
29
40
  continue;
@@ -1,3 +1,5 @@
1
1
  'use strict';
2
2
 
3
3
  module.exports.isPrevBody = (path) => path.getPrevSibling().isBlockStatement();
4
+ module.exports.isNext = (path) => path.getNextSibling().node;
5
+
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ const {isNext} = require('../is');
3
4
  const isNextAssign = (path) => {
4
5
  const nextPath = path.getNextSibling();
5
6
 
@@ -26,7 +27,7 @@ module.exports.VariableDeclaration = (path, {write, maybe, maybeIndent, traverse
26
27
  traverse(initPath);
27
28
  maybe.write(isParentBlock, ';\n');
28
29
 
29
- const is = isCoupleLinesExpression(path) && !isNextAssign(path);
30
+ const is = isNext(path) && isCoupleLinesExpression(path) && !isNextAssign(path);
30
31
 
31
32
  maybe.indent(is);
32
33
  maybe.write(is, '\n');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.1.3",
3
+ "version": "1.3.0",
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",
@@ -17,6 +17,7 @@
17
17
  "url": "git://github.com/putoutjs/printer.git"
18
18
  },
19
19
  "scripts": {
20
+ "wisdom": "madrun wisdom",
20
21
  "test": "madrun test",
21
22
  "watch:test": "madrun watch:test",
22
23
  "lint": "madrun lint",