@putout/printer 2.21.0 → 2.22.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,8 @@
1
+ 2023.06.16, v2.22.0
2
+
3
+ feature:
4
+ - f092cf8 @putout/printer: ObjectPattern: add maxPropertiesInOneLine
5
+
1
6
  2023.06.16, v2.21.0
2
7
 
3
8
  feature:
package/README.md CHANGED
@@ -87,6 +87,7 @@ write(ast, {
87
87
  maxSpecifiersInOneLine: 2,
88
88
  maxElementsInOneLine: 3,
89
89
  maxVariablesInOneLine: 4,
90
+ maxPropertiesInOneLine: 2,
90
91
  },
91
92
  visitors: {
92
93
  AssignmentPattern(path, {write}) {
@@ -16,6 +16,7 @@ module.exports.ObjectMethod = {
16
16
  generator,
17
17
  computed,
18
18
  } = path.node;
19
+
19
20
  const notMethod = kind !== 'method';
20
21
 
21
22
  maybe.print(notMethod, `${kind} `);
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ module.exports.checkMaxPropertiesInOneLine = (path, {maxPropertiesInOneLine}) => {
4
+ const {parentPath} = path;
5
+
6
+ if (parentPath.isObjectProperty())
7
+ return false;
8
+
9
+ const n = path.node.properties.length;
10
+
11
+ return maxPropertiesInOneLine >= n;
12
+ };
13
+
@@ -12,19 +12,23 @@ const {
12
12
  isCoupleLines,
13
13
  exists,
14
14
  } = require('../../is');
15
+ const {checkMaxPropertiesInOneLine} = require('./max-properties-in-one-line');
15
16
 
16
17
  const isTwoLevelsDeep = ({parentPath}) => parentPath.parentPath.parentPath.isObjectProperty();
17
18
 
18
19
  const isOneParentProperty = ({parentPath}) => parentPath.parentPath.node.properties?.length === 1;
19
20
 
20
21
  module.exports.ObjectPattern = {
21
- print(path, {indent, print, maybe}) {
22
+ print(path, {indent, print, maybe}, semantics) {
23
+ const {maxPropertiesInOneLine} = semantics;
22
24
  indent.inc();
23
25
  print('{');
24
26
 
25
27
  const properties = path.get('properties');
26
28
  const n = properties.length - 1;
27
- const is = shouldAddNewline(path);
29
+ const is = shouldAddNewline(path, {
30
+ maxPropertiesInOneLine,
31
+ });
28
32
  const hasObject = n && hasObjectPattern(properties);
29
33
 
30
34
  maybe.print.newline(is);
@@ -120,13 +124,19 @@ function hasObjectPattern(properties) {
120
124
  return false;
121
125
  }
122
126
 
123
- function shouldAddNewline(path) {
127
+ const ONE_LINE = false;
128
+ const COUPLE_LINES = true;
129
+
130
+ function shouldAddNewline(path, {maxPropertiesInOneLine}) {
124
131
  const {parentPath} = path;
125
132
  const properties = path.get('properties');
126
133
  const n = properties.length - 1;
127
134
 
135
+ if (checkMaxPropertiesInOneLine(path, {maxPropertiesInOneLine}))
136
+ return ONE_LINE;
137
+
128
138
  if (!isForOf(path) && !path.parentPath.isFunction() && n && checkLength(properties))
129
- return true;
139
+ return COUPLE_LINES;
130
140
 
131
141
  return parentPath.isObjectProperty();
132
142
  }
@@ -29,6 +29,7 @@ function initFormat(format) {
29
29
  function initSemantics(semantics = {}) {
30
30
  return {
31
31
  comments: true,
32
+ maxPropertiesInOneLine: 2,
32
33
  maxSpecifiersInOneLine: 2,
33
34
  maxElementsInOneLine: 5,
34
35
  maxVariablesInOneLine: 4,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "2.21.0",
3
+ "version": "2.22.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",