@putout/printer 2.21.0 → 2.23.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
+ 2023.06.16, v2.23.0
2
+
3
+ feature:
4
+ - 8dd9ddf @putout/printer: ArrayExpression: newline
5
+
6
+ 2023.06.16, v2.22.0
7
+
8
+ feature:
9
+ - f092cf8 @putout/printer: ObjectPattern: add maxPropertiesInOneLine
10
+
1
11
  2023.06.16, v2.21.0
2
12
 
3
13
  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}) {
@@ -113,10 +113,7 @@ const isForOf = ({parentPath}) => parentPath.isForOfStatement();
113
113
  const isStringAndString = ([a, b]) => isStringLiteral(a) && isStringLiteral(b);
114
114
 
115
115
  const isShortTwoSimplesInsideCall = (path, short) => {
116
- const {
117
- node,
118
- parentPath,
119
- } = path;
116
+ const {node, parentPath} = path;
120
117
 
121
118
  const {elements} = node;
122
119
  const {length} = elements;
@@ -139,7 +136,7 @@ function isOneSimple(path) {
139
136
 
140
137
  const [first] = elements;
141
138
 
142
- if (first.isIdentifier() && first.node.name.length < 5)
139
+ if (first.isIdentifier() && first.node.name.length < 15)
143
140
  return true;
144
141
 
145
142
  if (first.isStringLiteral() && first.node.value.length > 10)
@@ -4,10 +4,7 @@ const {isObjectPattern} = require('@babel/types');
4
4
 
5
5
  module.exports.AssignmentExpression = {
6
6
  condition: (path) => {
7
- const {
8
- left,
9
- extra,
10
- } = path.node;
7
+ const {left, extra} = path.node;
11
8
 
12
9
  if (isObjectPattern(left))
13
10
  return true;
@@ -2,10 +2,7 @@
2
2
 
3
3
  const {markAfter} = require('../../mark');
4
4
 
5
- const {
6
- isNext,
7
- isNextParent,
8
- } = require('../../is');
5
+ const {isNext, isNextParent} = require('../../is');
9
6
 
10
7
  const {printParams} = require('./params');
11
8
 
@@ -6,10 +6,7 @@ const {printParams} = require('./params');
6
6
  module.exports.FunctionExpression = (path, {print, maybe, write, traverse}) => {
7
7
  const {node} = path;
8
8
 
9
- const {
10
- generator,
11
- async,
12
- } = node;
9
+ const {generator, async} = node;
13
10
 
14
11
  maybe.write(async, 'async ');
15
12
  write('function');
@@ -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} `);
@@ -41,10 +41,7 @@ module.exports.MemberExpression = (path, {print, maybe, traverse}) => {
41
41
  };
42
42
 
43
43
  module.exports.OptionalMemberExpression = (path, {print, maybe}) => {
44
- const {
45
- computed,
46
- optional,
47
- } = path.node;
44
+ const {computed, optional} = path.node;
48
45
 
49
46
  print('__object');
50
47
 
@@ -4,10 +4,7 @@ const {isConcatenation} = require('../binary-expression/concatanate');
4
4
  const {isOneLine} = require('./object-expression');
5
5
 
6
6
  module.exports.ObjectProperty = (path, {maybe, traverse, write}) => {
7
- const {
8
- shorthand,
9
- computed,
10
- } = path.node;
7
+ const {shorthand, computed} = path.node;
11
8
 
12
9
  const key = path.get('key');
13
10
  const value = path.get('value');
@@ -0,0 +1,12 @@
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,18 +13,25 @@ const {
13
13
  exists,
14
14
  } = require('../../is');
15
15
 
16
+ const {checkMaxPropertiesInOneLine} = require('./max-properties-in-one-line');
17
+
16
18
  const isTwoLevelsDeep = ({parentPath}) => parentPath.parentPath.parentPath.isObjectProperty();
17
19
 
18
20
  const isOneParentProperty = ({parentPath}) => parentPath.parentPath.node.properties?.length === 1;
19
21
 
20
22
  module.exports.ObjectPattern = {
21
- print(path, {indent, print, maybe}) {
23
+ print(path, {indent, print, maybe}, semantics) {
24
+ const {maxPropertiesInOneLine} = semantics;
22
25
  indent.inc();
23
26
  print('{');
24
27
 
25
28
  const properties = path.get('properties');
26
29
  const n = properties.length - 1;
27
- const is = shouldAddNewline(path);
30
+
31
+ const is = shouldAddNewline(path, {
32
+ maxPropertiesInOneLine,
33
+ });
34
+
28
35
  const hasObject = n && hasObjectPattern(properties);
29
36
 
30
37
  maybe.print.newline(is);
@@ -41,10 +48,7 @@ module.exports.ObjectPattern = {
41
48
  const keyPath = property.get('key');
42
49
  const isAssign = valuePath.isAssignmentPattern();
43
50
 
44
- const {
45
- shorthand,
46
- computed,
47
- } = property.node;
51
+ const {shorthand, computed} = property.node;
48
52
 
49
53
  const couple = isCoupleLines(valuePath) && !exists(property.getPrevSibling()) && !path.parentPath.isObjectProperty();
50
54
 
@@ -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
  }
@@ -1,9 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- isLast,
5
- isNext,
6
- } = require('../is');
3
+ const {isLast, isNext} = require('../is');
7
4
 
8
5
  module.exports.UnaryExpression = unaryExpression;
9
6
  module.exports.UpdateExpression = unaryExpression;
@@ -40,10 +37,7 @@ function printUnary(path, name, {print}) {
40
37
  const isWord = (a) => /^(delete|typeof|void)$/.test(a);
41
38
 
42
39
  function unaryExpression(path, {maybe, traverse}) {
43
- const {
44
- prefix,
45
- operator,
46
- } = path.node;
40
+ const {prefix, operator} = path.node;
47
41
 
48
42
  const argPath = path.get('argument');
49
43
 
@@ -20,10 +20,7 @@ module.exports = {
20
20
  write(path.node.value);
21
21
  },
22
22
  StringLiteral(path, {write}) {
23
- const {
24
- raw,
25
- value,
26
- } = path.node;
23
+ const {raw, value} = path.node;
27
24
 
28
25
  write(raw || `'${value}'`);
29
26
  },
@@ -31,10 +28,7 @@ module.exports = {
31
28
  const {node} = path;
32
29
  const parenthesized = node.extra?.parenthesized;
33
30
 
34
- const {
35
- name,
36
- optional,
37
- } = node;
31
+ const {name, optional} = node;
38
32
 
39
33
  maybe.write(parenthesized, '(');
40
34
  write(name);
@@ -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,
@@ -1,9 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- isParentBlock,
5
- isNextParent,
6
- } = require('../is');
3
+ const {isParentBlock, isNextParent} = require('../is');
7
4
 
8
5
  const insideCase = (path) => path.parentPath.isSwitchCase();
9
6
 
@@ -7,10 +7,7 @@ const {
7
7
  isLast,
8
8
  } = require('../../is');
9
9
 
10
- const {
11
- markAfter,
12
- isMarkedAfter,
13
- } = require('../../mark');
10
+ const {markAfter, isMarkedAfter} = require('../../mark');
14
11
 
15
12
  const {isExportNamespaceSpecifier} = require('@babel/types');
16
13
 
@@ -23,10 +20,7 @@ const options = {
23
20
  };
24
21
 
25
22
  module.exports.ExportSpecifier = (path, {print}) => {
26
- const {
27
- local,
28
- exported,
29
- } = path.node;
23
+ const {local, exported} = path.node;
30
24
 
31
25
  print('__local');
32
26
 
@@ -2,10 +2,7 @@
2
2
 
3
3
  const {markAfter} = require('../../mark');
4
4
 
5
- const {
6
- isLast,
7
- isNext,
8
- } = require('../../is');
5
+ const {isLast, isNext} = require('../../is');
9
6
 
10
7
  const {parseImportSpecifiers} = require('parse-import-specifiers');
11
8
 
@@ -53,10 +50,7 @@ module.exports.ImportDeclaration = {
53
50
  const last = index === importsCount;
54
51
  const notLast = !last;
55
52
 
56
- const {
57
- imported,
58
- local,
59
- } = spec.node;
53
+ const {imported, local} = spec.node;
60
54
 
61
55
  indent.inc();
62
56
 
@@ -17,10 +17,7 @@ const {
17
17
  maybeThrow,
18
18
  } = require('./maybe');
19
19
 
20
- const {
21
- createDebug,
22
- createLog,
23
- } = require('./debug');
20
+ const {createDebug, createLog} = require('./debug');
24
21
 
25
22
  const {
26
23
  maybeMarkAfter,
@@ -1,10 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  module.exports.TSMappedType = (path, {print, write, indent, maybe}) => {
4
- const {
5
- readonly,
6
- optional,
7
- } = path.node;
4
+ const {readonly, optional} = path.node;
8
5
 
9
6
  write('{');
10
7
  write.newline();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "2.21.0",
3
+ "version": "2.23.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",