@putout/printer 2.6.0 → 2.8.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.8.0
2
+
3
+ feature:
4
+ - 93b580e @putout/printer: improve node support
5
+
6
+ 2023.06.13, v2.7.0
7
+
8
+ feature:
9
+ - 7f3fd61 @putout/printer: ObjectPattern: couple levels deep
10
+ - 7922ba6 @putout/printer: ObjectPattern: move out
11
+
1
12
  2023.06.13, v2.6.0
2
13
 
3
14
  fix:
@@ -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,
@@ -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,5 @@
1
+ 'use strict';
2
+
3
+ module.exports.wrongShorthand = ({computed, isAssign, keyPath, valuePath}) => {
4
+ return !computed && !isAssign && keyPath.node.name !== valuePath.node.name;
5
+ };
@@ -9,6 +9,7 @@ const {
9
9
  File,
10
10
  ExpressionStatement,
11
11
  Program,
12
+ isStatement,
12
13
  } = require('@babel/types');
13
14
 
14
15
  const isFn = (a) => typeof a === 'function';
@@ -23,8 +24,10 @@ module.exports.maybeThrow = (a, path, b) => {
23
24
  }));
24
25
  };
25
26
 
27
+ const maybeStatement = (ast) => isStatement(ast) ? ast : ExpressionStatement(ast);
28
+
26
29
  const maybeProgram = (ast) => isProgram(ast) ? ast : Program([
27
- ExpressionStatement(ast),
30
+ maybeStatement(ast),
28
31
  ]);
29
32
 
30
33
  module.exports.maybeFile = (ast) => isFile(ast) ? ast : File(maybeProgram(ast));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "2.6.0",
3
+ "version": "2.8.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",