@putout/printer 2.5.0 → 2.7.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.
package/ChangeLog CHANGED
@@ -1,3 +1,14 @@
1
+ 2023.06.13, v2.7.0
2
+
3
+ feature:
4
+ - 7f3fd61 @putout/printer: ObjectPattern: couple levels deep
5
+ - 7922ba6 @putout/printer: ObjectPattern: move out
6
+
7
+ 2023.06.13, v2.6.0
8
+
9
+ fix:
10
+ - 30e2aa1 @putout/printer: ObjectExpression: used as argument call
11
+
1
12
  2023.06.12, v2.5.0
2
13
 
3
14
  feature:
@@ -19,7 +19,7 @@ const {NewExpression} = require('./new-expression');
19
19
 
20
20
  const {ObjectExpression} = require('./object-expression/object-expression');
21
21
  const {ObjectProperty} = require('./object-expression/object-property');
22
- const {ObjectPattern} = require('./object-pattern');
22
+ const {ObjectPattern} = require('./object-pattern/object-pattern');
23
23
 
24
24
  const {
25
25
  ClassProperty,
@@ -12,6 +12,7 @@ const {
12
12
  } = require('../../is');
13
13
 
14
14
  const {parseComments} = require('../../comments');
15
+ const {isObjectExpression} = require('@babel/types');
15
16
 
16
17
  const isBodyOfArrow = (path) => path.parentPath.node.body === path.node;
17
18
  const isLogical = (path) => path.get('argument').isLogicalExpression();
@@ -28,9 +29,7 @@ module.exports.ObjectExpression = (path, {print, maybe, indent, write}, semantic
28
29
 
29
30
  maybe.print(parens, '(');
30
31
  print('{');
31
- parseComments(path, {
32
- write,
33
- }, semantics);
32
+ parseComments(path, {write}, semantics);
34
33
  maybe.print.newline(manyLines);
35
34
 
36
35
  for (const property of properties) {
@@ -85,6 +84,13 @@ const notLastArgInsideCall = (path) => {
85
84
  if (!parentPath.isCallExpression())
86
85
  return false;
87
86
 
87
+ const {properties} = path.node;
88
+
89
+ for (const property of properties) {
90
+ if (isObjectExpression(property.value))
91
+ return false;
92
+ }
93
+
88
94
  return path !== parentPath
89
95
  .get('arguments')
90
96
  .at(-1);
@@ -5,17 +5,17 @@ const {
5
5
  isObjectPattern,
6
6
  } = require('@babel/types');
7
7
 
8
+ const {wrongShorthand} = require('./wrong-shortand');
9
+
8
10
  const {
9
11
  isForOf,
10
12
  isCoupleLines,
11
13
  exists,
12
- } = require('../is');
14
+ } = require('../../is');
13
15
 
14
- const wrongShorthand = ({computed, isAssign, keyPath, valuePath}) => {
15
- return !computed && !isAssign && keyPath.node.name !== valuePath.node.name;
16
- };
16
+ const isTwoLevelsDeep = ({parentPath}) => parentPath.parentPath.parentPath.isObjectProperty();
17
17
 
18
- const isOneParentProperty = ({parentPath}) => parentPath.parentPath.node.properties.length === 1;
18
+ const isOneParentProperty = ({parentPath}) => parentPath.parentPath.node.properties?.length === 1;
19
19
 
20
20
  module.exports.ObjectPattern = {
21
21
  print(path, {indent, print, maybe}) {
@@ -27,7 +27,7 @@ module.exports.ObjectPattern = {
27
27
  const is = shouldAddNewline(path);
28
28
  const hasObject = n && hasObjectPattern(properties);
29
29
 
30
- maybe.print(is, '\n');
30
+ maybe.print.newline(is);
31
31
 
32
32
  for (const [i, property] of properties.entries()) {
33
33
  if (property.isRestElement()) {
@@ -46,7 +46,7 @@ module.exports.ObjectPattern = {
46
46
  computed,
47
47
  } = property.node;
48
48
 
49
- const couple = isCoupleLines(valuePath) && !exists(property.getPrevSibling());
49
+ const couple = isCoupleLines(valuePath) && !exists(property.getPrevSibling()) && !path.parentPath.isObjectProperty();
50
50
 
51
51
  maybe.indent(is);
52
52
  maybe.print.breakline(couple);
@@ -87,10 +87,10 @@ module.exports.ObjectPattern = {
87
87
  if (!path.parentPath.isObjectProperty())
88
88
  return false;
89
89
 
90
- if (isOneParentProperty(path))
91
- return true;
90
+ if (isTwoLevelsDeep(path))
91
+ return false;
92
92
 
93
- return false;
93
+ return isOneParentProperty(path);
94
94
  },
95
95
  after(path, {print}) {
96
96
  print.newline();
@@ -121,13 +121,14 @@ function hasObjectPattern(properties) {
121
121
  }
122
122
 
123
123
  function shouldAddNewline(path) {
124
+ const {parentPath} = path;
124
125
  const properties = path.get('properties');
125
126
  const n = properties.length - 1;
126
127
 
127
128
  if (!isForOf(path) && !path.parentPath.isFunction() && n && checkLength(properties))
128
129
  return true;
129
130
 
130
- if (path.parentPath.isObjectProperty())
131
+ if (parentPath.isObjectProperty())
131
132
  return true;
132
133
 
133
134
  return false;
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+
3
+ module.exports.wrongShorthand = ({computed, isAssign, keyPath, valuePath}) => {
4
+ return !computed && !isAssign && keyPath.node.name !== valuePath.node.name;
5
+ };
6
+
@@ -35,9 +35,7 @@ module.exports.BlockStatement = {
35
35
  print(element);
36
36
  }
37
37
 
38
- parseComments(path, {
39
- write,
40
- }, semantics);
38
+ parseComments(path, {write}, semantics);
41
39
 
42
40
  indent.dec();
43
41
  maybe.indent(body.length);
@@ -61,11 +61,7 @@ module.exports.VariableDeclaration = {
61
61
  continue;
62
62
  }
63
63
 
64
- parseLeadingComments(next, {
65
- print,
66
- maybe,
67
- indent,
68
- }, semantics);
64
+ parseLeadingComments(next, {print, maybe, indent}, semantics);
69
65
  }
70
66
  }
71
67
 
@@ -30,4 +30,3 @@ module.exports.WhileStatement = {
30
30
  markAfter(path);
31
31
  },
32
32
  };
33
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "2.5.0",
3
+ "version": "2.7.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Simplest possible opinionated Babel AST printer for 🐊Putout",