@putout/printer 2.94.0 → 2.96.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.
Files changed (30) hide show
  1. package/ChangeLog +11 -0
  2. package/lib/tokenize/expressions/assignment-expression.js +3 -2
  3. package/lib/tokenize/expressions/binary-expression/binary-expression.js +2 -3
  4. package/lib/tokenize/expressions/conditional-expression.js +1 -1
  5. package/lib/tokenize/expressions/{functions → function}/params.js +10 -1
  6. package/lib/tokenize/expressions/index.js +3 -3
  7. package/lib/tokenize/expressions/unary-expression/parens.js +13 -0
  8. package/lib/tokenize/expressions/{unary-expressions.js → unary-expression/unary-expressions.js} +11 -4
  9. package/lib/tokenize/literals/identifier.js +35 -0
  10. package/lib/tokenize/literals/index.js +3 -18
  11. package/lib/tokenize/typescript/function/print-return-type.js +19 -0
  12. package/lib/tokenize/typescript/function/ts-call-signature-declaration.js +12 -0
  13. package/lib/tokenize/typescript/function/ts-construct-signature-declaration.js +21 -0
  14. package/lib/tokenize/typescript/{ts-constructor-type.js → function/ts-constructor-type.js} +4 -6
  15. package/lib/tokenize/typescript/function/ts-declare-function.js +1 -1
  16. package/lib/tokenize/typescript/function/ts-declare-method.js +1 -1
  17. package/lib/tokenize/typescript/function/ts-function-type.js +14 -0
  18. package/lib/tokenize/typescript/function/ts-method-signature.js +21 -0
  19. package/lib/tokenize/typescript/index.js +22 -45
  20. package/lib/tokenize/typescript/type/ts-type-parameter.js +7 -1
  21. package/package.json +1 -1
  22. package/lib/tokenize/typescript/ts-function-type.js +0 -15
  23. /package/lib/tokenize/expressions/{functions → function}/arrow-function-expression.js +0 -0
  24. /package/lib/tokenize/expressions/{functions → function}/class-method.js +0 -0
  25. /package/lib/tokenize/expressions/{functions → function}/function-declaration.js +0 -0
  26. /package/lib/tokenize/expressions/{functions → function}/function-expression.js +0 -0
  27. /package/lib/tokenize/expressions/{functions → function}/functions.js +0 -0
  28. /package/lib/tokenize/expressions/{functions → function}/object-method.js +0 -0
  29. /package/lib/tokenize/expressions/{member-expressions → member-expression}/chain.js +0 -0
  30. /package/lib/tokenize/expressions/{member-expressions → member-expression}/member-expressions.js +0 -0
package/ChangeLog CHANGED
@@ -1,3 +1,14 @@
1
+ 2023.08.03, v2.96.0
2
+
3
+ feature:
4
+ - 205200b @putout/printer: UpdateExpression: parens (coderaiser/minify#112)
5
+
6
+ 2023.08.02, v2.95.0
7
+
8
+ feature:
9
+ - 8c3016c @putout/printer: isBabel7
10
+ - 79da1c9 @putout/printer: add support of babel 8 (http://next.babeljs.io/docs/v8-migration-api)
11
+
1
12
  2023.08.01, v2.94.0
2
13
 
3
14
  feature:
@@ -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,9 +2,18 @@
2
2
 
3
3
  const {parseComments} = require('../../comment/comment');
4
4
 
5
+ const isBabel7 = (path) => path.node.parameters;
6
+
7
+ function parseParams(path) {
8
+ if (isBabel7(path))
9
+ return path.get('parameters');
10
+
11
+ return path.get('params');
12
+ }
13
+
5
14
  module.exports.printParams = (path, printer, semantics, customization = {}) => {
6
15
  const {
7
- params = path.get('params'),
16
+ params = parseParams(path),
8
17
  braceOpen = '(',
9
18
  braceClose = ')',
10
19
  } = customization;
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
- const functions = require('./functions/functions');
4
- const unaryExpressions = require('./unary-expressions');
5
- const memberExpressions = require('./member-expressions/member-expressions');
3
+ const functions = require('./function/functions');
4
+ const unaryExpressions = require('./unary-expression/unary-expressions');
5
+ const memberExpressions = require('./member-expression/member-expressions');
6
6
 
7
7
  const {
8
8
  ClassExpression,
@@ -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
  },
@@ -0,0 +1,19 @@
1
+ 'use strict';
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
+ };
11
+
12
+ module.exports.printReturnType = (path, {traverse}) => {
13
+ if (isBabel7(path)) {
14
+ traverse(path.get('typeAnnotation'));
15
+ return;
16
+ }
17
+
18
+ traverse(path.get('returnType'));
19
+ };
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ const {printReturnType} = require('./print-return-type');
4
+ const {printParams} = require('../../expressions/function/params');
5
+
6
+ module.exports.TSCallSignatureDeclaration = (path, printer, semantics) => {
7
+ const {print} = printer;
8
+ printParams(path, printer, semantics);
9
+ print(':');
10
+ print.space();
11
+ printReturnType(path, printer);
12
+ };
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ const {printParams} = require('../../expressions/function/params');
4
+
5
+ const {
6
+ hasReturnType,
7
+ printReturnType,
8
+ } = require('./print-return-type');
9
+
10
+ module.exports.TSConstructSignatureDeclaration = (path, printer, semantics) => {
11
+ const {write} = printer;
12
+
13
+ write('new');
14
+ printParams(path, printer, semantics);
15
+
16
+ if (hasReturnType(path)) {
17
+ write(':');
18
+ write.space();
19
+ printReturnType(path, printer);
20
+ }
21
+ };
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const {printParams} = require('../expressions/functions/params');
3
+ const {printParams} = require('../../expressions/function/params');
4
+ const {printReturnType} = require('./print-return-type');
4
5
 
5
6
  module.exports.TSConstructorType = (path, printer, semantics) => {
6
7
  const {print} = printer;
@@ -8,12 +9,9 @@ module.exports.TSConstructorType = (path, printer, semantics) => {
8
9
  print('new');
9
10
  print(' ');
10
11
 
11
- printParams(path, printer, semantics, {
12
- params: path.get('parameters'),
13
- });
14
-
12
+ printParams(path, printer, semantics);
15
13
  print.space();
16
14
  print('=>');
17
15
  print.space();
18
- print('__typeAnnotation');
16
+ printReturnType(path, printer);
19
17
  };
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {printParams} = require('../../expressions/functions/params');
3
+ const {printParams} = require('../../expressions/function/params');
4
4
  const {isNext} = require('../../is');
5
5
 
6
6
  const isInsideExport = (path) => {
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {printParams} = require('../../expressions/functions/params');
3
+ const {printParams} = require('../../expressions/function/params');
4
4
 
5
5
  module.exports.TSDeclareMethod = (path, printer, semantics) => {
6
6
  const {print} = printer;
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ const {printParams} = require('../../expressions/function/params');
4
+ const {printReturnType} = require('./print-return-type');
5
+
6
+ module.exports.TSFunctionType = (path, printer, semantics) => {
7
+ const {print} = printer;
8
+
9
+ printParams(path, printer, semantics);
10
+ print.space();
11
+ print('=>');
12
+ print.space();
13
+ printReturnType(path, printer);
14
+ };
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ const {printParams} = require('../../expressions/function/params');
4
+
5
+ const {
6
+ hasReturnType,
7
+ printReturnType,
8
+ } = require('./print-return-type');
9
+
10
+ module.exports.TSMethodSignature = (path, printer, semantics) => {
11
+ const {traverse, write} = printer;
12
+
13
+ traverse(path.get('key'));
14
+ printParams(path, printer, semantics);
15
+
16
+ if (hasReturnType(path)) {
17
+ write(':');
18
+ write.space();
19
+ printReturnType(path, printer);
20
+ }
21
+ };
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {exists, isNext} = require('../is');
3
+ const {isNext} = require('../is');
4
4
  const {TSTypeLiteral} = require('./type/ts-type-literal');
5
5
  const {TSTypeAliasDeclaration} = require('./type/ts-type-alias-declaration');
6
6
  const {TSMappedType} = require('./ts-mapped-type');
@@ -19,13 +19,21 @@ const {TSAsExpression} = require('./ts-as-expression');
19
19
  const {TSInterfaceBody} = require('./interface/ts-interface-body');
20
20
  const {TSIntersectionType} = require('./ts-intersection-type');
21
21
  const {TSPropertySignature} = require('./ts-property-signature');
22
- const {TSFunctionType} = require('./ts-function-type');
23
- const {printParams} = require('../expressions/functions/params');
22
+ const {TSFunctionType} = require('./function/ts-function-type');
23
+ const {printParams} = require('../expressions/function/params');
24
24
  const {TSEnumDeclaration} = require('./enum/ts-enum-declaration');
25
25
  const {TSEnumMember} = require('./enum/ts-enum-member');
26
26
  const {TSTupleType} = require('./tuple/ts-tuple-type');
27
27
  const {TSNamedTupleMember} = require('./tuple/ts-named-tuple-member');
28
- const {TSConstructorType} = require('./ts-constructor-type');
28
+ const {TSConstructorType} = require('./function/ts-constructor-type');
29
+ const {TSCallSignatureDeclaration} = require('./function/ts-call-signature-declaration');
30
+ const {TSConstructSignatureDeclaration} = require('./function/ts-construct-signature-declaration');
31
+ const {TSMethodSignature} = require('./function/ts-method-signature');
32
+
33
+ const {
34
+ maybeParenOpen,
35
+ maybeParenClose,
36
+ } = require('../expressions/unary-expression/parens');
29
37
 
30
38
  module.exports = {
31
39
  TSAsExpression,
@@ -116,10 +124,14 @@ module.exports = {
116
124
  TSBooleanKeyword(path, {write}) {
117
125
  write('boolean');
118
126
  },
119
- TSUnionType(path, {traverse, write}) {
127
+ TSUnionType(path, printer) {
128
+ const {traverse, write} = printer;
129
+
120
130
  const types = path.get('types');
121
131
  const n = types.length - 1;
122
132
 
133
+ maybeParenOpen(path, printer);
134
+
123
135
  for (const [i, type] of types.entries()) {
124
136
  traverse(type);
125
137
 
@@ -129,6 +141,8 @@ module.exports = {
129
141
  write.space();
130
142
  }
131
143
  }
144
+
145
+ maybeParenClose(path, printer);
132
146
  },
133
147
  TSNumberKeyword(path, {write}) {
134
148
  write('number');
@@ -170,22 +184,7 @@ module.exports = {
170
184
 
171
185
  print('__typeAnnotation');
172
186
  },
173
- TSConstructSignatureDeclaration(path, printer, semantics) {
174
- const {write, traverse} = printer;
175
-
176
- write('new');
177
- printParams(path, printer, semantics, {
178
- params: path.get('parameters'),
179
- });
180
-
181
- const typeAnnotation = path.get('typeAnnotation');
182
-
183
- if (exists(typeAnnotation)) {
184
- write(':');
185
- write.space();
186
- traverse(typeAnnotation);
187
- }
188
- },
187
+ TSConstructSignatureDeclaration,
189
188
  TSIndexSignature(path, {print}) {
190
189
  print('[');
191
190
  print('__parameters.0');
@@ -194,30 +193,6 @@ module.exports = {
194
193
  print.space();
195
194
  print('__typeAnnotation');
196
195
  },
197
- TSCallSignatureDeclaration(path, {print}) {
198
- print('(');
199
- print('__parameters.0');
200
- print(')');
201
- print(':');
202
- print.space();
203
- print('__typeAnnotation');
204
- },
205
- TSMethodSignature(path, printer, semantics) {
206
- const {traverse, write} = printer;
207
-
208
- traverse(path.get('key'));
209
- printParams(path, printer, semantics, {
210
- params: path.get('parameters'),
211
- });
212
-
213
- const typeAnnotation = path.get('typeAnnotation');
214
-
215
- if (exists(typeAnnotation)) {
216
- write(':');
217
- write.space();
218
- traverse(typeAnnotation);
219
- }
220
- },
221
196
  TSExpressionWithTypeArguments(path, {print}) {
222
197
  print('__expression');
223
198
  print('__typeParameters');
@@ -253,6 +228,8 @@ module.exports = {
253
228
  TSDeclareMethod,
254
229
  TSNamedTupleMember,
255
230
  TSConstructorType,
231
+ TSMethodSignature,
232
+ TSCallSignatureDeclaration,
256
233
  TSThisType(path, {print}) {
257
234
  print('this');
258
235
  },
@@ -1,7 +1,10 @@
1
1
  'use strict';
2
2
 
3
+ const isString = (a) => typeof a === 'string';
3
4
  const {exists} = require('../../is');
4
5
 
6
+ const isBabel7 = (path) => isString(path.node.name);
7
+
5
8
  module.exports.TSTypeParameter = (path, {write, traverse}) => {
6
9
  const constraint = path.get('constraint');
7
10
 
@@ -12,7 +15,10 @@ module.exports.TSTypeParameter = (path, {write, traverse}) => {
12
15
  else if (path.node.const)
13
16
  write('const ');
14
17
 
15
- write(path.node.name);
18
+ if (isBabel7(path))
19
+ write(path.node.name);
20
+ else
21
+ write(path.node.name.name);
16
22
 
17
23
  if (!exists(constraint))
18
24
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "2.94.0",
3
+ "version": "2.96.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",
@@ -1,15 +0,0 @@
1
- 'use strict';
2
-
3
- const {printParams} = require('../expressions/functions/params');
4
-
5
- module.exports.TSFunctionType = (path, printer, semantics) => {
6
- const {print} = printer;
7
-
8
- printParams(path, printer, semantics, {
9
- params: path.get('parameters'),
10
- });
11
- print.space();
12
- print('=>');
13
- print.space();
14
- print('__typeAnnotation');
15
- };