@putout/printer 8.23.0 → 8.24.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
+ 2024.04.30, v8.24.0
2
+
3
+ feature:
4
+ - dd056f5 @putout/printer: semantics: add maxPropertiesLengthInOneLine to align with eslint-plugin-putout: long-properties-destructuring (https://github.com/coderaiser/putout/tree/master/packages/eslint-plugin-putout/lib/long-properties-destructuring#readme)
5
+
1
6
  2024.04.30, v8.23.0
2
7
 
3
8
  feature:
package/README.md CHANGED
@@ -98,6 +98,7 @@ print(ast, {
98
98
  maxElementsInOneLine: 3,
99
99
  maxVariablesInOneLine: 4,
100
100
  maxPropertiesInOneLine: 2,
101
+ maxPropertiesLengthInOneLine: 15,
101
102
  trailingComma: true,
102
103
  encodeSingleQuote: true,
103
104
  encodeDoubleQuote: false,
@@ -172,6 +173,7 @@ Options used to configure logic of output, similar to ESLint rules:
172
173
  - ✅ `maxElementsInOneLine` - count of `ArrayExpression` and `ArrayPattern` elements placed in one line.
173
174
  - ✅ `maxVariablesInOneLine` - count of `VariableDeclarators` in one line.
174
175
  - ✅ `maxPropertiesInOneLine` - count of `ObjectProperties` in one line.
176
+ - ✅ `maxPropertiesLengthInOneLine` - maximum length of `Object Property`, when violated splits event if `maxPropertiesInOneLine` satisfies;
175
177
  - ✅ `roundBraces` - to output braces in a single argument arrow function expressions: `(a) => {}` or not `a => {}`.
176
178
 
177
179
  ## Visitors API
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- module.exports.checkMaxPropertiesInOneLine = (path, {maxPropertiesInOneLine}) => {
3
+ module.exports.moreThenMaxPropertiesInOneLine = (path, {maxPropertiesInOneLine}) => {
4
4
  const {parentPath} = path;
5
5
 
6
6
  if (parentPath.isObjectProperty())
@@ -0,0 +1,19 @@
1
+ 'use strict';
2
+
3
+ const {isIdentifier} = require('@putout/babel').types;
4
+
5
+ module.exports.moreThenMaxPropertiesLengthInOneLine = (path, {maxPropertiesLengthInOneLine}) => {
6
+ const {properties} = path.node;
7
+
8
+ for (const {key} of properties) {
9
+ if (!isIdentifier(key))
10
+ continue;
11
+
12
+ const {name} = key;
13
+
14
+ if (name.length >= maxPropertiesLengthInOneLine)
15
+ return true;
16
+ }
17
+
18
+ return false;
19
+ };
@@ -14,11 +14,12 @@ const {
14
14
  exists,
15
15
  } = require('../../is');
16
16
 
17
- const {checkMaxPropertiesInOneLine} = require('./max-properties-in-one-line');
17
+ const {moreThenMaxPropertiesInOneLine} = require('./more-then-max-properties-in-one-line');
18
+
18
19
  const {maybeTypeAnnotation} = require('../../maybe/maybe-type-annotation');
20
+ const {moreThenMaxPropertiesLengthInOneLine} = require('./more-then-max-properties-length-in-one-line');
19
21
 
20
22
  const isTwoLevelsDeep = ({parentPath}) => parentPath.parentPath.parentPath.isObjectProperty();
21
-
22
23
  const isOneParentProperty = ({parentPath}) => parentPath.parentPath.node.properties?.length === 1;
23
24
 
24
25
  function isIndent(path) {
@@ -27,8 +28,11 @@ function isIndent(path) {
27
28
 
28
29
  module.exports.ObjectPattern = {
29
30
  print: maybeTypeAnnotation((path, {print, maybe}, semantics) => {
30
- const {maxPropertiesInOneLine} = semantics;
31
31
  const shouldIndent = isIndent(path);
32
+ const {
33
+ maxPropertiesInOneLine,
34
+ maxPropertiesLengthInOneLine,
35
+ } = semantics;
32
36
 
33
37
  maybe.indent.inc(shouldIndent);
34
38
  print('{');
@@ -38,6 +42,7 @@ module.exports.ObjectPattern = {
38
42
 
39
43
  const is = shouldAddNewline(path, {
40
44
  maxPropertiesInOneLine,
45
+ maxPropertiesLengthInOneLine,
41
46
  });
42
47
 
43
48
  const hasObject = n && hasObjectPattern(properties);
@@ -150,12 +155,24 @@ function hasObjectPattern(properties) {
150
155
  const ONE_LINE = false;
151
156
  const COUPLE_LINES = true;
152
157
 
153
- function shouldAddNewline(path, {maxPropertiesInOneLine}) {
158
+ function shouldAddNewline(path, semantics) {
154
159
  const {parentPath} = path;
155
160
  const properties = path.get('properties');
156
161
  const n = properties.length - 1;
157
162
 
158
- if (checkMaxPropertiesInOneLine(path, {maxPropertiesInOneLine}))
163
+ const {
164
+ maxPropertiesInOneLine,
165
+ maxPropertiesLengthInOneLine,
166
+ } = semantics;
167
+
168
+ const moreLength = moreThenMaxPropertiesLengthInOneLine(path, {
169
+ maxPropertiesLengthInOneLine,
170
+ });
171
+ const moreCount = moreThenMaxPropertiesInOneLine(path, {
172
+ maxPropertiesInOneLine,
173
+ });
174
+
175
+ if (moreCount && !moreLength)
159
176
  return ONE_LINE;
160
177
 
161
178
  if (!isForOf(path) && !path.parentPath.isFunction() && n && checkLength(properties))
@@ -30,6 +30,7 @@ function initSemantics(semantics = {}) {
30
30
  return {
31
31
  comments: true,
32
32
  maxPropertiesInOneLine: 2,
33
+ maxPropertiesLengthInOneLine: 15,
33
34
  maxSpecifiersInOneLine: 2,
34
35
  maxElementsInOneLine: 5,
35
36
  maxVariablesInOneLine: 4,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "8.23.0",
3
+ "version": "8.24.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",