@putout/printer 9.1.0 → 9.3.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.06.10, v9.3.0
2
+
3
+ feature:
4
+ - 960565f @putout/printer: roundBraces: add ability to split to: arrow, sequence and assign
5
+
6
+ 2024.06.09, v9.2.0
7
+
8
+ feature:
9
+ - 6fae5d0 @putout/printer: SequenceExpressions: braces: add support of ConditinalExpressions (@putoutjs/minify#15)
10
+
1
11
  2024.06.09, v9.1.0
2
12
 
3
13
  feature:
package/README.md CHANGED
@@ -102,7 +102,11 @@ print(ast, {
102
102
  trailingComma: true,
103
103
  encodeSingleQuote: true,
104
104
  encodeDoubleQuote: false,
105
- roundBraces: true,
105
+ roundBraces: {
106
+ arrow: true,
107
+ sequence: true,
108
+ assign: false,
109
+ },
106
110
  },
107
111
  visitors: {
108
112
  AssignmentPattern(path, {print}) {
@@ -175,9 +179,9 @@ Options used to configure logic of output, similar to ESLint rules:
175
179
  - ✅ `maxPropertiesInOneLine` - count of `ObjectProperties` in one line.
176
180
  - ✅ `maxPropertiesLengthInOneLine` - maximum length of `Object Property`, when violated splits event if `maxPropertiesInOneLine` satisfies;
177
181
  - ✅ `roundBraces` to output braces or not
178
- - In a single argument arrow function expressions `(a) => {}` or not `a => {}`;
179
- - In sequence expressions: `for(let e of l) (a(), b())` or not `for(let e of l) a(), b()`;
180
- - In assignment expressions: `(e.o=w(e.o)` or not `e.o=w(e.o)`;
182
+ - `arrow`: In a single argument arrow function expressions enabled: `(a) => {}`, disabled: `a => {}`;
183
+ - `sequence`: In sequence expressions: enabled: `for(let e of l) (a(), b())`, disabled: `for(let e of l) a(), b()`;
184
+ - `assign`: In assignment expressions: enabled: `(e.o=w(e.o)`, disabled: `e.o=w(e.o)`;
181
185
 
182
186
  ## Visitors API
183
187
 
@@ -30,7 +30,7 @@ function maybeWriteBrace(path, printer, semantics, {brace}) {
30
30
  return;
31
31
  }
32
32
 
33
- if (!roundBraces)
33
+ if (!roundBraces.assign)
34
34
  return;
35
35
 
36
36
  if (!isParens(path))
@@ -52,14 +52,14 @@ module.exports.printParams = (path, printer, semantics, customization = {}) => {
52
52
  };
53
53
 
54
54
  function printBraceOpen(path, {print, braceOpen}, semantics) {
55
- if (isOneArgArrow(path) && !semantics.roundBraces)
55
+ if (isOneArgArrow(path) && !semantics.roundBraces.arrow)
56
56
  return;
57
57
 
58
58
  return print(braceOpen);
59
59
  }
60
60
 
61
61
  function printBraceClose(path, {print, braceClose}, semantics) {
62
- if (isOneArgArrow(path) && !semantics.roundBraces)
62
+ if (isOneArgArrow(path) && !semantics.roundBraces.arrow)
63
63
  return;
64
64
 
65
65
  print(braceClose);
@@ -23,12 +23,17 @@ function maybeWriteBrace(path, printer, semantics, {brace}) {
23
23
  return;
24
24
  }
25
25
 
26
+ if (type === 'ConditionalExpression' && path !== parentPath.get('test')) {
27
+ write(brace);
28
+ return;
29
+ }
30
+
26
31
  if (type === 'LogicalExpression') {
27
32
  write(brace);
28
33
  return;
29
34
  }
30
35
 
31
- if (!roundBraces)
36
+ if (!roundBraces.sequence)
32
37
  return;
33
38
 
34
39
  write(brace);
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const {parseRoundBraces} = require('./parse-round-braces');
4
+
3
5
  module.exports.parseOverrides = (overrides = {}) => {
4
6
  const {
5
7
  format,
@@ -37,7 +39,7 @@ function initSemantics(semantics = {}) {
37
39
  trailingComma: true,
38
40
  encodeSingleQuote: true,
39
41
  encodeDoubleQuote: false,
40
- roundBraces: true,
41
42
  ...semantics,
43
+ roundBraces: parseRoundBraces(semantics),
42
44
  };
43
45
  }
@@ -0,0 +1,42 @@
1
+ 'use strict';
2
+
3
+ const isObject = (a) => a && typeof a === 'object';
4
+ const isBool = (a) => typeof a === 'boolean';
5
+
6
+ const ROUND_BRACES_DEFAULTS = {
7
+ arrow: true,
8
+ sequence: true,
9
+ assign: false,
10
+ };
11
+
12
+ const ROUND_BRACES_ENABLED = {
13
+ arrow: true,
14
+ sequence: true,
15
+ assign: true,
16
+ };
17
+
18
+ const ROUND_BRACES_DISABLED = {
19
+ arrow: false,
20
+ sequence: false,
21
+ assign: false,
22
+ };
23
+
24
+ module.exports.parseRoundBraces = ({roundBraces}) => {
25
+ if (isObject(roundBraces))
26
+ return {
27
+ ...ROUND_BRACES_DEFAULTS,
28
+ ...roundBraces,
29
+ };
30
+
31
+ if (isBool(roundBraces) && roundBraces)
32
+ return ROUND_BRACES_ENABLED;
33
+
34
+ if (isBool(roundBraces) && !roundBraces)
35
+ return ROUND_BRACES_DISABLED;
36
+
37
+ return ROUND_BRACES_DEFAULTS;
38
+ };
39
+
40
+ module.exports.ROUND_BRACES_DISABLED = ROUND_BRACES_DISABLED;
41
+ module.exports.ROUND_BRACES_ENABLED = ROUND_BRACES_ENABLED;
42
+ module.exports.ROUND_BRACES_DEFAULTS = ROUND_BRACES_DEFAULTS;
@@ -9,7 +9,7 @@ module.exports.maybeSpaceAfterKeyword = (path, {print}, semantics) => {
9
9
 
10
10
  const {type} = argument;
11
11
 
12
- if (type === 'SequenceExpression' && roundBraces)
12
+ if (type === 'SequenceExpression' && roundBraces.sequence)
13
13
  return print.space();
14
14
 
15
15
  if (type === 'StringLiteral' || type === 'TemplateLiteral')
@@ -21,7 +21,7 @@ module.exports.maybeSpaceAfterKeyword = (path, {print}, semantics) => {
21
21
  if (type === 'UnaryExpression' && argument.operator === '!')
22
22
  return print.space();
23
23
 
24
- if (type === 'ArrowFunctionExpression' && roundBraces)
24
+ if (type === 'ArrowFunctionExpression' && roundBraces.arrow)
25
25
  return print.space();
26
26
 
27
27
  print(' ');
@@ -21,7 +21,7 @@ const {
21
21
  parseTrailingComments,
22
22
  } = require('./comment/comment');
23
23
 
24
- const {parseOverrides} = require('./overrides');
24
+ const {parseOverrides} = require('./overrides/overrides');
25
25
 
26
26
  const isString = (a) => typeof a === 'string';
27
27
  const {assign, freeze} = Object;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "9.1.0",
3
+ "version": "9.3.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",