@putout/printer 1.64.0 → 1.66.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.04.21, v1.66.0
2
+
3
+ feature:
4
+ - 31a3e52 @putout/printer: add support for const type parameters
5
+
6
+ 2023.04.21, v1.65.0
7
+
8
+ feature:
9
+ - a67785f @putout/printer: add support of TSConditionalType
10
+
1
11
  2023.04.21, v1.64.0
2
12
 
3
13
  feature:
@@ -11,6 +11,7 @@ module.exports.FunctionDeclaration = {
11
11
 
12
12
  print('function ');
13
13
  print('__id');
14
+ print('__typeParameters');
14
15
  print('(');
15
16
 
16
17
  const params = path.get('params');
@@ -23,9 +23,7 @@ module.exports.maybeThrow = (a, path, b) => {
23
23
  }));
24
24
  };
25
25
 
26
- const maybeProgram = (ast) => isProgram(ast) ? ast : Program([
27
- ExpressionStatement(ast),
28
- ]);
26
+ const maybeProgram = (ast) => isProgram(ast) ? ast : Program([ExpressionStatement(ast)]);
29
27
 
30
28
  module.exports.maybeFile = (ast) => isFile(ast) ? ast : File(maybeProgram(ast));
31
29
 
@@ -4,11 +4,42 @@ const {exists} = require('../is');
4
4
  const {TSTypeLiteral} = require('./ts-type-literal');
5
5
  const {TSTypeAliasDeclaration} = require('./ts-type-alias-declaration');
6
6
  const {TSMappedType} = require('./ts-mapped-type');
7
+ const {TSConditionalType} = require('./ts-conditional-type');
8
+ const {TSTypeParameter} = require('./ts-type-parameter');
7
9
 
8
10
  module.exports = {
9
11
  TSTypeLiteral,
10
12
  TSTypeAliasDeclaration,
13
+ TSTypeParameter,
11
14
  TSMappedType,
15
+ TSConditionalType,
16
+ TSNeverKeyword(path, {write}) {
17
+ write('never');
18
+ },
19
+ TSUnknownKeyword(path, {write}) {
20
+ write('unknown');
21
+ },
22
+ TSTupleType(path, {write, traverse, maybe}) {
23
+ const elementTypes = path.get('elementTypes');
24
+ const n = elementTypes.length - 1;
25
+
26
+ write('[');
27
+
28
+ for (const [i, elementType] of elementTypes.entries()) {
29
+ traverse(elementType);
30
+ maybe.write(i < n, ', ');
31
+ }
32
+
33
+ write(']');
34
+ },
35
+ TSInferType(path, {print}) {
36
+ print('infer ');
37
+ print('__typeParameter');
38
+ },
39
+ TSRestType(path, {print}) {
40
+ print('...');
41
+ print('__typeAnnotation');
42
+ },
12
43
  TSTypeParameterDeclaration(path, {print}) {
13
44
  print('<');
14
45
 
@@ -35,21 +66,6 @@ module.exports = {
35
66
  print('__elementType');
36
67
  print('[]');
37
68
  },
38
- TSTypeParameter(path, {write, traverse}) {
39
- const constraint = path.get('constraint');
40
-
41
- if (path.node.in)
42
- write('in ');
43
- else if (path.node.out)
44
- write('out ');
45
-
46
- write(path.node.name);
47
-
48
- if (exists(constraint)) {
49
- write(' in ');
50
- traverse(constraint);
51
- }
52
- },
53
69
  TSTypeOperator(path, {write, print}) {
54
70
  const {operator} = path.node;
55
71
  write(`${operator} `);
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ module.exports.TSConditionalType = (path, {print, indent}) => {
4
+ indent.inc();
5
+ print.breakline();
6
+ print('__checkType');
7
+ print(' extends ');
8
+ print('__extendsType');
9
+
10
+ indent.inc();
11
+ print.breakline();
12
+
13
+ print('? ');
14
+ print('__trueType');
15
+
16
+ print.breakline();
17
+
18
+ print(': ');
19
+ print('__falseType');
20
+
21
+ indent.dec();
22
+ indent.dec();
23
+ };
@@ -6,13 +6,16 @@ const isNextType = (a) => a.getNextSibling().isTSTypeAliasDeclaration();
6
6
 
7
7
  module.exports.TSTypeAliasDeclaration = {
8
8
  print(path, {print, maybe, store}) {
9
+ const typeAnnotation = path.get('typeAnnotation');
10
+ const isConditional = typeAnnotation.isTSConditionalType();
11
+
9
12
  print('type ');
10
13
  print('__id');
11
14
  print('__typeParameters');
12
15
 
13
16
  print.space();
14
17
  print('=');
15
- print.space();
18
+ maybe.print.space(!isConditional);
16
19
 
17
20
  print('__typeAnnotation');
18
21
  print(';');
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ const {exists} = require('../is');
4
+
5
+ module.exports.TSTypeParameter = (path, {write, traverse}) => {
6
+ const constraint = path.get('constraint');
7
+
8
+ if (path.node.in)
9
+ write('in ');
10
+ else if (path.node.out)
11
+ write('out ');
12
+ else if (path.node.const)
13
+ write('const ');
14
+
15
+ write(path.node.name);
16
+
17
+ if (!exists(constraint))
18
+ return;
19
+
20
+ if (constraint.isTSTypeOperator()) {
21
+ write(' in ');
22
+ } else {
23
+ write(' extends ');
24
+ }
25
+
26
+ traverse(constraint);
27
+ };
28
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.64.0",
3
+ "version": "1.66.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Easiest possible opinionated Babel AST printer made with ❤️ to use in 🐊Putout",