@putout/printer 2.93.0 → 2.95.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 (24) hide show
  1. package/ChangeLog +11 -0
  2. package/lib/tokenize/expressions/{functions → function}/params.js +10 -1
  3. package/lib/tokenize/expressions/index.js +2 -2
  4. package/lib/tokenize/typescript/function/print-return-type.js +19 -0
  5. package/lib/tokenize/typescript/function/ts-call-signature-declaration.js +12 -0
  6. package/lib/tokenize/typescript/function/ts-construct-signature-declaration.js +21 -0
  7. package/lib/tokenize/typescript/{ts-constructor-type.js → function/ts-constructor-type.js} +4 -6
  8. package/lib/tokenize/typescript/function/ts-declare-function.js +1 -1
  9. package/lib/tokenize/typescript/function/ts-declare-method.js +1 -1
  10. package/lib/tokenize/typescript/function/ts-function-type.js +14 -0
  11. package/lib/tokenize/typescript/function/ts-method-signature.js +21 -0
  12. package/lib/tokenize/typescript/index.js +15 -45
  13. package/lib/tokenize/typescript/interface/ts-interface-declaration.js +8 -3
  14. package/lib/tokenize/typescript/type/ts-type-parameter.js +7 -1
  15. package/package.json +1 -1
  16. package/lib/tokenize/typescript/ts-function-type.js +0 -15
  17. /package/lib/tokenize/expressions/{functions → function}/arrow-function-expression.js +0 -0
  18. /package/lib/tokenize/expressions/{functions → function}/class-method.js +0 -0
  19. /package/lib/tokenize/expressions/{functions → function}/function-declaration.js +0 -0
  20. /package/lib/tokenize/expressions/{functions → function}/function-expression.js +0 -0
  21. /package/lib/tokenize/expressions/{functions → function}/functions.js +0 -0
  22. /package/lib/tokenize/expressions/{functions → function}/object-method.js +0 -0
  23. /package/lib/tokenize/expressions/{member-expressions → member-expression}/chain.js +0 -0
  24. /package/lib/tokenize/expressions/{member-expressions → member-expression}/member-expressions.js +0 -0
package/ChangeLog CHANGED
@@ -1,3 +1,14 @@
1
+ 2023.08.02, v2.95.0
2
+
3
+ feature:
4
+ - 8c3016c @putout/printer: isBabel7
5
+ - 79da1c9 @putout/printer: add support of babel 8 (http://next.babeljs.io/docs/v8-migration-api)
6
+
7
+ 2023.08.01, v2.94.0
8
+
9
+ feature:
10
+ - b12d1d8 @putout/printer: TSInterfaceDeclaration: newlines
11
+
1
12
  2023.08.01, v2.93.0
2
13
 
3
14
  feature:
@@ -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');
3
+ const functions = require('./function/functions');
4
4
  const unaryExpressions = require('./unary-expressions');
5
- const memberExpressions = require('./member-expressions/member-expressions');
5
+ const memberExpressions = require('./member-expression/member-expressions');
6
6
 
7
7
  const {
8
8
  ClassExpression,
@@ -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,16 @@ 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');
29
32
 
30
33
  module.exports = {
31
34
  TSAsExpression,
@@ -116,10 +119,12 @@ module.exports = {
116
119
  TSBooleanKeyword(path, {write}) {
117
120
  write('boolean');
118
121
  },
119
- TSUnionType(path, {traverse, write}) {
122
+ TSUnionType(path, {traverse, write, maybe}) {
120
123
  const types = path.get('types');
121
124
  const n = types.length - 1;
122
125
 
126
+ maybe.write(path.node.extra?.parenthesized, '(');
127
+
123
128
  for (const [i, type] of types.entries()) {
124
129
  traverse(type);
125
130
 
@@ -129,6 +134,8 @@ module.exports = {
129
134
  write.space();
130
135
  }
131
136
  }
137
+
138
+ maybe.write(path.node.extra?.parenthesized, ')');
132
139
  },
133
140
  TSNumberKeyword(path, {write}) {
134
141
  write('number');
@@ -170,22 +177,7 @@ module.exports = {
170
177
 
171
178
  print('__typeAnnotation');
172
179
  },
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
- },
180
+ TSConstructSignatureDeclaration,
189
181
  TSIndexSignature(path, {print}) {
190
182
  print('[');
191
183
  print('__parameters.0');
@@ -194,30 +186,6 @@ module.exports = {
194
186
  print.space();
195
187
  print('__typeAnnotation');
196
188
  },
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
189
  TSExpressionWithTypeArguments(path, {print}) {
222
190
  print('__expression');
223
191
  print('__typeParameters');
@@ -253,6 +221,8 @@ module.exports = {
253
221
  TSDeclareMethod,
254
222
  TSNamedTupleMember,
255
223
  TSConstructorType,
224
+ TSMethodSignature,
225
+ TSCallSignatureDeclaration,
256
226
  TSThisType(path, {print}) {
257
227
  print('this');
258
228
  },
@@ -1,7 +1,10 @@
1
1
  'use strict';
2
2
 
3
3
  const {isNext, isNextParent} = require('../../is');
4
- const {isTSTypeAliasDeclaration} = require('@babel/types');
4
+ const {
5
+ isTSTypeAliasDeclaration,
6
+ isExportDeclaration,
7
+ } = require('@babel/types');
5
8
 
6
9
  module.exports.TSInterfaceDeclaration = {
7
10
  print(path, {print}) {
@@ -10,10 +13,12 @@ module.exports.TSInterfaceDeclaration = {
10
13
  print('__body');
11
14
  },
12
15
  afterSatisfy: () => [isNext, isNextParent],
13
- after(path, {print, maybe}) {
16
+ after(path, {print}) {
14
17
  const next = path.parentPath.getNextSibling();
15
18
 
16
19
  print.breakline();
17
- maybe.print.breakline(!isTSTypeAliasDeclaration(next));
20
+
21
+ if (!isTSTypeAliasDeclaration(next) && !isExportDeclaration(next))
22
+ print.breakline();
18
23
  },
19
24
  };
@@ -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.93.0",
3
+ "version": "2.95.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
- };