@putout/printer 8.23.0 → 8.25.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,13 @@
1
+ 2024.05.01, v8.25.0
2
+
3
+ feature:
4
+ - 095a130 @putout/printer: ImportDeclaration: maxPropertiesLengthInOneLine
5
+
6
+ 2024.04.30, v8.24.0
7
+
8
+ feature:
9
+ - 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)
10
+
1
11
  2024.04.30, v8.23.0
2
12
 
3
13
  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,
@@ -12,7 +12,15 @@ const {
12
12
 
13
13
  module.exports.ImportAttribute = ImportAttribute;
14
14
  module.exports.ImportDeclaration = {
15
- print(path, {print, maybe, write, traverse, indent}, options) {
15
+ print(path, printer, semantics) {
16
+ const {
17
+ print,
18
+ maybe,
19
+ write,
20
+ traverse,
21
+ indent,
22
+ } = printer;
23
+
16
24
  const {phase} = path.node;
17
25
  const isType = path.node.importKind === 'type';
18
26
  const specifiers = path.get('specifiers');
@@ -44,7 +52,7 @@ module.exports.ImportDeclaration = {
44
52
  traverse(spec.get('local'));
45
53
  }
46
54
 
47
- const maxSpecifiersInOneLine = parseMaxSpecifiers(imports, options);
55
+ const maxSpecifiersInOneLine = parseMaxSpecifiers(imports, semantics);
48
56
  const importsCount = imports.length - 1;
49
57
 
50
58
  for (const [index, spec] of imports.entries()) {
@@ -75,9 +83,9 @@ module.exports.ImportDeclaration = {
75
83
  maybe.write(n, ',');
76
84
 
77
85
  const last = index === n;
78
- const penult = index === n - 1;
86
+ const penulty = index === n - 1;
79
87
 
80
- maybe.write.newline(penult && defaults.length);
88
+ maybe.write.newline(penulty && defaults.length);
81
89
  maybe.write.newline(last);
82
90
  }
83
91
 
@@ -110,12 +118,18 @@ module.exports.ImportDeclaration = {
110
118
  },
111
119
  };
112
120
 
113
- function parseMaxSpecifiers(imports, options) {
114
- const {maxSpecifiersInOneLine} = options;
121
+ function parseMaxSpecifiers(imports, semantics) {
122
+ const {
123
+ maxSpecifiersInOneLine,
124
+ maxPropertiesLengthInOneLine,
125
+ } = semantics;
115
126
 
116
127
  for (const {node} of imports) {
117
128
  if (node.imported.name !== node.local.name)
118
129
  return 1;
130
+
131
+ if (node.imported.name.length >= maxPropertiesLengthInOneLine)
132
+ return 1;
119
133
  }
120
134
 
121
135
  return maxSpecifiersInOneLine;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "8.23.0",
3
+ "version": "8.25.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",