@putout/printer 1.38.0 → 1.40.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.13, v1.40.0
2
+
3
+ feature:
4
+ - 4cd3b37 @putout/printer: add support of TSStringKeyword
5
+
6
+ 2023.04.12, v1.39.0
7
+
8
+ feature:
9
+ - b14f85d @putout/printer: add support of TSQualifiedName
10
+
1
11
  2023.04.12, v1.38.0
2
12
 
3
13
  feature:
@@ -106,13 +106,13 @@ module.exports.FunctionDeclaration = (path, {print, maybe}) => {
106
106
  print('__body');
107
107
  };
108
108
 
109
- module.exports.ClassMethod = (path, {print}) => {
109
+ module.exports.ClassMethod = (path, {print, maybe}) => {
110
110
  const {kind} = path.node;
111
+ const notMethod = kind !== 'method';
112
+ const notConstructor = kind !== 'constructor';
111
113
 
112
- if (kind !== 'method')
113
- print(`${kind} `);
114
-
115
- print('__key');
114
+ maybe.print(notMethod, `${kind} `);
115
+ maybe.print(notConstructor, '__key');
116
116
  print('(');
117
117
 
118
118
  const params = path.get('params');
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const isFn = (a) => typeof a === 'function';
3
+ const rendy = require('rendy');
4
4
 
5
5
  const {
6
6
  isProgram,
@@ -10,6 +10,18 @@ const {
10
10
  Program,
11
11
  } = require('@babel/types');
12
12
 
13
+ const isFn = (a) => typeof a === 'function';
14
+
15
+ module.exports.maybeThrow = (a, path, b) => {
16
+ if (!a)
17
+ return;
18
+
19
+ throw Error(rendy(b, {
20
+ path,
21
+ type: path.type,
22
+ }));
23
+ };
24
+
13
25
  const maybeProgram = (ast) => isProgram(ast) ? ast : Program([
14
26
  ExpressionStatement(ast),
15
27
  ]);
@@ -1,7 +1,17 @@
1
1
  'use strict';
2
2
 
3
- module.exports.ExportDefaultDeclaration = (path, {print}) => {
4
- print('export default ');
5
- print('__declaration');
6
- print(';');
3
+ module.exports.ExportDefaultDeclaration = {
4
+ /*
5
+ beforeIf(path) {
6
+ return path.getPrevSibling().isTSTypeAliasDeclaration();
7
+ },
8
+ before(path, {print}) {
9
+ print.breakline();
10
+ },
11
+ */print(path, {print, traverse, maybe}) {
12
+ const declaration = path.get('declaration');
13
+ print('export default ');
14
+ traverse(declaration);
15
+ maybe.print(!declaration.isClassDeclaration(), ';');
16
+ },
7
17
  };
@@ -14,6 +14,7 @@ const {TYPES} = require('../types');
14
14
  const {
15
15
  maybeFile,
16
16
  maybePlugin,
17
+ maybeThrow,
17
18
  } = require('./maybe');
18
19
 
19
20
  const {
@@ -138,6 +139,13 @@ module.exports.tokenize = (ast, overrides = {}) => {
138
139
  space,
139
140
  });
140
141
 
142
+ assign(maybeWrite, {
143
+ newline: maybeNewline,
144
+ breakline: maybeBreakline,
145
+ linebreak: maybeLinebreak,
146
+ space: maybeSpace,
147
+ });
148
+
141
149
  const maybe = {
142
150
  indent: maybeIndent,
143
151
  markBefore: maybeMarkBefore,
@@ -206,12 +214,13 @@ module.exports.tokenize = (ast, overrides = {}) => {
206
214
  print: maybePrint,
207
215
  });
208
216
 
209
- if (!currentTraverse)
210
- throw Error(`Node type '${type}' is not supported yet: '${path}'`);
217
+ maybeThrow(!currentTraverse, path, `Node type '{{ type }}' is not supported yet: '{{ path }}'`);
211
218
 
219
+ const currentIndent = i;
212
220
  parseLeadingComments(path, printer);
213
221
  maybePlugin(currentTraverse, path, printer);
214
222
  parseTrailingComments(path, printer);
223
+ maybeThrow(i !== currentIndent, path, `☝️Looks like indent level changed after token visitor: '{{ type }}', for code: '{{ path }}'`);
215
224
  debug(path.type);
216
225
  }
217
226
 
@@ -2,9 +2,11 @@
2
2
 
3
3
  const {exists} = require('../is');
4
4
  const {TSTypeLiteral} = require('./ts-type-literal');
5
+ const {TSTypeAliasDeclaration} = require('./ts-type-alias-declaration');
5
6
 
6
7
  module.exports = {
7
8
  TSTypeLiteral,
9
+ TSTypeAliasDeclaration,
8
10
  TSTypeParameterDeclaration(path, {print}) {
9
11
  print('<');
10
12
 
@@ -32,6 +34,9 @@ module.exports = {
32
34
  TSNumberKeyword(path, {write}) {
33
35
  write('number');
34
36
  },
37
+ TSStringKeyword(path, {write}) {
38
+ write('string');
39
+ },
35
40
  TSInstantiationExpression(path, {print}) {
36
41
  print('__expression');
37
42
  print('__typeParameters');
@@ -50,6 +55,16 @@ module.exports = {
50
55
  print.space();
51
56
  print('__typeAnnotation');
52
57
  },
58
+ TSQualifiedName(path, {print}) {
59
+ print('__left');
60
+ print('.');
61
+ print('__right');
62
+ },
63
+ TSParameterProperty(path, {write, print}) {
64
+ write(path.node.accessibility);
65
+ write.space();
66
+ print('__parameter');
67
+ },
53
68
  TSTypeAnnotation(path, {print}) {
54
69
  if (path.parentPath.isIdentifier()) {
55
70
  print(':');
@@ -62,15 +77,6 @@ module.exports = {
62
77
  print('__expression');
63
78
  print('__typeParameters');
64
79
  },
65
- TSTypeAliasDeclaration(path, {print}) {
66
- print('type ');
67
- print('__id');
68
- print.space();
69
- print('=');
70
- print.space();
71
- print('__typeAnnotation');
72
- print(';');
73
- },
74
80
  TSPropertySignature(path, {print, maybe, traverse}) {
75
81
  const {optional} = path.node;
76
82
  const typeAnnotation = path.get('typeAnnotation');
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ const {isLast} = require('../is');
4
+ const isNextType = (a) => a.getNextSibling().isTSTypeAliasDeclaration();
5
+
6
+ module.exports.TSTypeAliasDeclaration = {
7
+ print(path, {print, maybe, store}) {
8
+ print('type ');
9
+ print('__id');
10
+ print.space();
11
+ print('=');
12
+ print.space();
13
+ print('__typeAnnotation');
14
+ print(';');
15
+
16
+ const is = store(isLast(path));
17
+ maybe.print.newline(!is);
18
+ },
19
+ afterIf(path, {store}) {
20
+ const last = store();
21
+
22
+ if (last)
23
+ return false;
24
+
25
+ if (isNextType(path))
26
+ return false;
27
+
28
+ return true;
29
+ },
30
+ after(path, {print}) {
31
+ print.newline();
32
+ },
33
+ };
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const {isNext} = require('../is');
4
+
3
5
  module.exports.TSTypeLiteral = (path, {indent, traverse, write, maybe}) => {
4
6
  const members = path.get('members');
5
7
  write('{');
@@ -15,11 +17,14 @@ module.exports.TSTypeLiteral = (path, {indent, traverse, write, maybe}) => {
15
17
  indent();
16
18
  traverse(member);
17
19
  maybe.write(is, ';');
20
+
21
+ if (is && isNext(member))
22
+ write.newline(is);
18
23
  }
19
24
 
20
25
  if (is) {
21
26
  write.newline();
22
- indent.inc();
27
+ indent.dec();
23
28
  }
24
29
 
25
30
  write('}');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.38.0",
3
+ "version": "1.40.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",
@@ -32,7 +32,8 @@
32
32
  "@babel/types": "^7.21.3",
33
33
  "@putout/compare": "^9.13.0",
34
34
  "fullstore": "^3.0.0",
35
- "just-snake-case": "^3.2.0"
35
+ "just-snake-case": "^3.2.0",
36
+ "rendy": "^3.1.1"
36
37
  },
37
38
  "keywords": [
38
39
  "putout",