@putout/printer 2.95.0 → 3.0.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,20 @@
1
+ 2023.08.05, v3.0.0
2
+
3
+ feature:
4
+ - 90898aa @putout/printer: drop support of Babel < 8
5
+ - 95e659c package: @putout/plugin-minify v2.4.0
6
+ - 4025615 package: @putout/operate v10.0.2
7
+ - df5945b package: @putout/compare v12.0.4
8
+ - 6092ae6 package: nodemon v3.0.1
9
+ - b7428a5 package: estree-to-babel v6.0.0
10
+ - 2a6decd package: eslint-plugin-putout v19.0.2
11
+ - e17d03f package: putout v31.0.3
12
+
13
+ 2023.08.03, v2.96.0
14
+
15
+ feature:
16
+ - 205200b @putout/printer: UpdateExpression: parens (coderaiser/minify#112)
17
+
1
18
  2023.08.02, v2.95.0
2
19
 
3
20
  feature:
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [NPMIMGURL]: https://img.shields.io/npm/v/@putout/printer.svg?style=flat&longCache=true
4
4
  [NPMURL]: https://npmjs.org/package/@putout/printer "npm"
5
5
 
6
- Prints [**Babel AST**](https://github.com/coderaiser/estree-to-babel) to readable **JavaScript**.
6
+ Prints [**Babel AST**](https://github.com/coderaiser/estree-to-babel) to readable **JavaScript**. For Babel 7 use `@putout/printer` v2.
7
7
 
8
8
  - ☝️ Similar to **Recast**, but [twice faster](#speed-comparison), also simpler and easier in maintenance, since it supports only **Babel**.
9
9
  - ☝️ As opinionated as **Prettier**, but has more user-friendly output and works directly with **AST**.
@@ -1,15 +1,16 @@
1
1
  'use strict';
2
2
 
3
3
  const {isObjectPattern} = require('@babel/types');
4
+ const {isParens} = require('./unary-expression/parens');
4
5
 
5
6
  module.exports.AssignmentExpression = {
6
7
  condition: (path) => {
7
- const {left, extra} = path.node;
8
+ const {left} = path.node;
8
9
 
9
10
  if (isObjectPattern(left))
10
11
  return true;
11
12
 
12
- if (extra?.parenthesized)
13
+ if (isParens(path))
13
14
  return true;
14
15
 
15
16
  return path.parentPath.isLogicalExpression();
@@ -6,12 +6,11 @@ const {
6
6
  } = require('./concatanate');
7
7
 
8
8
  const {maybeSpace} = require('./maybe-space');
9
+ const {isParens} = require('../unary-expression/parens');
9
10
 
10
11
  const BinaryExpression = {
11
12
  condition(path) {
12
- const parens = path.node.extra?.parenthesized;
13
-
14
- if (parens)
13
+ if (isParens(path))
15
14
  return true;
16
15
 
17
16
  return path.parentPath.isAwaitExpression();
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const isParens = (path) => path.node.extra?.parenthesized;
3
+ const {isParens} = require('./unary-expression/parens');
4
4
 
5
5
  module.exports.ConditionalExpression = {
6
6
  condition: isParens,
@@ -2,12 +2,7 @@
2
2
 
3
3
  const {parseComments} = require('../../comment/comment');
4
4
 
5
- const isBabel7 = (path) => path.node.parameters;
6
-
7
5
  function parseParams(path) {
8
- if (isBabel7(path))
9
- return path.get('parameters');
10
-
11
6
  return path.get('params');
12
7
  }
13
8
 
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const functions = require('./function/functions');
4
- const unaryExpressions = require('./unary-expressions');
4
+ const unaryExpressions = require('./unary-expression/unary-expressions');
5
5
  const memberExpressions = require('./member-expression/member-expressions');
6
6
 
7
7
  const {
@@ -12,7 +12,6 @@ const {
12
12
  } = require('../../is');
13
13
 
14
14
  const {parseComments} = require('../../comment/comment');
15
- const {isSpreadElement} = require('@babel/types');
16
15
 
17
16
  const isBodyOfArrow = (path) => path.parentPath.node.body === path.node;
18
17
  const isLogical = (path) => path.get('argument').isLogicalExpression();
@@ -94,13 +93,6 @@ const notLastArgInsideCall = (path) => {
94
93
  const ONE_LINE = true;
95
94
  const MANY_LINES = false;
96
95
 
97
- const isSpreadFirst = (path) => {
98
- const {properties} = path.node;
99
- const {length} = properties;
100
-
101
- return length > 1 && isSpreadElement(properties[0]);
102
- };
103
-
104
96
  module.exports.isOneLine = isOneLine;
105
97
  function isOneLine(path) {
106
98
  const {length} = path.get('properties');
@@ -120,9 +112,6 @@ function isOneLine(path) {
120
112
  if (isCoupleLines(path))
121
113
  return MANY_LINES;
122
114
 
123
- if (isSpreadFirst(path))
124
- return MANY_LINES;
125
-
126
115
  return !isValue(path);
127
116
  }
128
117
 
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ const isParens = (path) => path.node.extra?.parenthesized;
4
+
5
+ module.exports.isParens = isParens;
6
+
7
+ module.exports.maybeParenOpen = (path, {maybe}) => {
8
+ maybe.write(isParens(path), '(');
9
+ };
10
+
11
+ module.exports.maybeParenClose = (path, {maybe}) => {
12
+ maybe.write(isParens(path), ')');
13
+ };
@@ -1,6 +1,10 @@
1
1
  'use strict';
2
2
 
3
- const {isLast, isNext} = require('../is');
3
+ const {isLast, isNext} = require('../../is');
4
+ const {
5
+ maybeParenOpen,
6
+ maybeParenClose,
7
+ } = require('./parens');
4
8
 
5
9
  module.exports.UnaryExpression = unaryExpression;
6
10
  module.exports.UpdateExpression = unaryExpression;
@@ -39,14 +43,17 @@ function printUnary(path, name, {print}) {
39
43
 
40
44
  const isWord = (a) => /^(delete|typeof|void|throw)$/.test(a);
41
45
 
42
- function unaryExpression(path, {maybe, traverse}) {
46
+ function unaryExpression(path, printer) {
47
+ const {maybe, traverse} = printer;
43
48
  const {prefix, operator} = path.node;
44
-
45
49
  const argPath = path.get('argument');
46
50
 
51
+ maybeParenOpen(path, printer);
52
+
47
53
  maybe.print(prefix, operator);
48
54
  maybe.print(isWord(operator), ' ');
49
-
50
55
  traverse(argPath);
51
56
  maybe.print(!prefix, operator);
57
+
58
+ maybeParenClose(path, printer);
52
59
  }
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ maybeParenOpen,
5
+ maybeParenClose,
6
+ } = require('../expressions/unary-expression/parens');
7
+
8
+ const {maybeDecorators} = require('../maybe-get');
9
+
10
+ module.exports.Identifier = (path, printer) => {
11
+ const {
12
+ write,
13
+ maybe,
14
+ traverse,
15
+ print,
16
+ } = printer;
17
+
18
+ const {node} = path;
19
+ const {name, optional} = node;
20
+
21
+ const typeAnnotation = path.get('typeAnnotation');
22
+
23
+ maybeParenOpen(path, printer);
24
+
25
+ for (const decorator of maybeDecorators(path)) {
26
+ traverse(decorator);
27
+ print(' ');
28
+ }
29
+
30
+ write(name);
31
+ maybe.write(optional, '?');
32
+ traverse(typeAnnotation);
33
+
34
+ maybeParenClose(path, printer);
35
+ };
@@ -1,9 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  const {TemplateLiteral} = require('./template-literal');
4
- const {maybeDecorators} = require('../maybe-get');
4
+
5
+ const {Identifier} = require('./identifier');
5
6
 
6
7
  module.exports = {
8
+ Identifier,
7
9
  TemplateLiteral,
8
10
  BigIntLiteral(path, {write}) {
9
11
  write(path.node.raw);
@@ -39,23 +41,6 @@ module.exports = {
39
41
 
40
42
  write(raw || `'${value}'`);
41
43
  },
42
- Identifier(path, {write, maybe, traverse, print}) {
43
- const {node} = path;
44
- const {name, optional} = node;
45
- const parenthesized = node.extra?.parenthesized;
46
- const typeAnnotation = path.get('typeAnnotation');
47
-
48
- maybe.write(parenthesized, '(');
49
- for (const decorator of maybeDecorators(path)) {
50
- traverse(decorator);
51
- print(' ');
52
- }
53
-
54
- write(name);
55
- maybe.write(optional, '?');
56
- traverse(typeAnnotation);
57
- maybe.write(parenthesized, ')');
58
- },
59
44
  RegExpLiteral(path, {print}) {
60
45
  print(path.node.raw);
61
46
  },
@@ -1,19 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const isBabel7 = (path) => path.node.typeAnnotation;
4
-
5
- module.exports.hasReturnType = (path) => {
6
- if (isBabel7(path))
7
- return path.node.typeAnnotation;
8
-
9
- return path.node.returnType;
10
- };
3
+ module.exports.hasReturnType = (path) => path.node.returnType;
11
4
 
12
5
  module.exports.printReturnType = (path, {traverse}) => {
13
- if (isBabel7(path)) {
14
- traverse(path.get('typeAnnotation'));
15
- return;
16
- }
17
-
18
6
  traverse(path.get('returnType'));
19
7
  };
@@ -30,6 +30,11 @@ const {TSCallSignatureDeclaration} = require('./function/ts-call-signature-decla
30
30
  const {TSConstructSignatureDeclaration} = require('./function/ts-construct-signature-declaration');
31
31
  const {TSMethodSignature} = require('./function/ts-method-signature');
32
32
 
33
+ const {
34
+ maybeParenOpen,
35
+ maybeParenClose,
36
+ } = require('../expressions/unary-expression/parens');
37
+
33
38
  module.exports = {
34
39
  TSAsExpression,
35
40
  TSTypeLiteral,
@@ -108,22 +113,19 @@ module.exports = {
108
113
  print('>');
109
114
  print('__expression');
110
115
  },
111
- TSParenthesizedType(path, {print}) {
112
- print('(');
113
- print('__typeAnnotation');
114
- print(')');
115
- },
116
116
  TSUndefinedKeyword(path, {write}) {
117
117
  write('undefined');
118
118
  },
119
119
  TSBooleanKeyword(path, {write}) {
120
120
  write('boolean');
121
121
  },
122
- TSUnionType(path, {traverse, write, maybe}) {
122
+ TSUnionType(path, printer) {
123
+ const {traverse, write} = printer;
124
+
123
125
  const types = path.get('types');
124
126
  const n = types.length - 1;
125
127
 
126
- maybe.write(path.node.extra?.parenthesized, '(');
128
+ maybeParenOpen(path, printer);
127
129
 
128
130
  for (const [i, type] of types.entries()) {
129
131
  traverse(type);
@@ -135,7 +137,7 @@ module.exports = {
135
137
  }
136
138
  }
137
139
 
138
- maybe.write(path.node.extra?.parenthesized, ')');
140
+ maybeParenClose(path, printer);
139
141
  },
140
142
  TSNumberKeyword(path, {write}) {
141
143
  write('number');
@@ -1,10 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const isString = (a) => typeof a === 'string';
4
3
  const {exists} = require('../../is');
5
4
 
6
- const isBabel7 = (path) => isString(path.node.name);
7
-
8
5
  module.exports.TSTypeParameter = (path, {write, traverse}) => {
9
6
  const constraint = path.get('constraint');
10
7
 
@@ -15,10 +12,7 @@ module.exports.TSTypeParameter = (path, {write, traverse}) => {
15
12
  else if (path.node.const)
16
13
  write('const ');
17
14
 
18
- if (isBabel7(path))
19
- write(path.node.name);
20
- else
21
- write(path.node.name.name);
15
+ write(path.node.name.name);
22
16
 
23
17
  if (!exists(constraint))
24
18
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "2.95.0",
3
+ "version": "3.0.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",
@@ -30,8 +30,8 @@
30
30
  "@babel/parser": "^7.19.0",
31
31
  "@babel/traverse": "^7.21.2",
32
32
  "@babel/types": "^7.21.3",
33
- "@putout/compare": "^11.0.0",
34
- "@putout/operate": "^8.11.0",
33
+ "@putout/compare": "^12.0.4",
34
+ "@putout/operate": "^10.0.2",
35
35
  "fullstore": "^3.0.0",
36
36
  "just-snake-case": "^3.2.0",
37
37
  "parse-import-specifiers": "^1.0.1",
@@ -48,7 +48,7 @@
48
48
  ],
49
49
  "devDependencies": {
50
50
  "@babel/plugin-codemod-object-assign-to-object-spread": "^7.10.4",
51
- "@putout/plugin-minify": "^1.8.0",
51
+ "@putout/plugin-minify": "^2.4.0",
52
52
  "@putout/plugin-printer": "^1.0.0",
53
53
  "@putout/plugin-promises": "^11.0.0",
54
54
  "@putout/plugin-react-hook-form": "^3.4.1",
@@ -58,14 +58,14 @@
58
58
  "escover": "^3.0.0",
59
59
  "eslint": "^8.0.1",
60
60
  "eslint-plugin-n": "^16.0.0",
61
- "eslint-plugin-putout": "^18.0.0",
62
- "estree-to-babel": "^5.0.1",
61
+ "eslint-plugin-putout": "^19.0.2",
62
+ "estree-to-babel": "^6.0.0",
63
63
  "just-kebab-case": "^4.2.0",
64
64
  "madrun": "^9.0.0",
65
65
  "mock-require": "^3.0.3",
66
66
  "montag": "^1.0.0",
67
- "nodemon": "^2.0.1",
68
- "putout": "^30.0.0",
67
+ "nodemon": "^3.0.1",
68
+ "putout": "^31.0.3",
69
69
  "supertape": "^8.0.0",
70
70
  "try-catch": "^3.0.0"
71
71
  },