@putout/printer 5.36.0 → 5.37.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,8 @@
1
+ 2023.10.24, v5.37.0
2
+
3
+ feature:
4
+ - f01afa3 @putout/printer: TSInterfaceBody: simplify
5
+
1
6
  2023.10.24, v5.36.0
2
7
 
3
8
  feature:
@@ -1,10 +1,29 @@
1
1
  'use strict';
2
2
 
3
- const {hasTrailingComment} = require('../is');
3
+ const {types} = require('@putout/babel');
4
+ const {
5
+ hasTrailingComment,
6
+ satisfy,
7
+ isPrev,
8
+ } = require('../is');
4
9
 
5
10
  const {markBefore} = require('../mark');
6
11
  const {maybeInsideFn} = require('./maybe-inside-fn');
7
12
 
13
+ const {
14
+ isObjectProperty,
15
+ isVariableDeclarator,
16
+ isClassProperty,
17
+ isTSPropertySignature,
18
+ } = types;
19
+
20
+ const isProperty = satisfy([
21
+ isObjectProperty,
22
+ isVariableDeclarator,
23
+ isClassProperty,
24
+ isTSPropertySignature,
25
+ ]);
26
+
8
27
  module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics) => {
9
28
  if (!semantics.comments)
10
29
  return;
@@ -20,8 +39,9 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
20
39
  return;
21
40
 
22
41
  const insideFn = path.parentPath.isFunction();
23
- const isProperty = path.isObjectProperty() || path.isVariableDeclarator() || path.isClassProperty();
24
- const isIndent = !looksLikeSwitchCase && !path.isClassMethod() && !insideFn && !isProperty;
42
+
43
+ const propIs = isProperty(path);
44
+ const isIndent = !looksLikeSwitchCase && !path.isClassMethod() && !insideFn && !propIs;
25
45
 
26
46
  for (const {type, value} of leadingComments) {
27
47
  maybe.indent(isIndent);
@@ -32,12 +52,12 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
32
52
  indent,
33
53
  });
34
54
 
35
- maybe.print.space(isProperty && !path.isClassProperty());
55
+ maybe.print.space(propIs && !path.isClassProperty());
36
56
  print(`//${value}`);
37
57
 
38
58
  const isParentSwitch = path.parentPath.isSwitchCase();
39
- maybe.print.breakline(isProperty || isParentSwitch);
40
- maybe.print.newline(!isProperty && !isParentSwitch);
59
+ maybe.print.breakline(propIs || isParentSwitch);
60
+ maybe.print.newline(!propIs && !isParentSwitch);
41
61
  continue;
42
62
  }
43
63
 
@@ -46,11 +66,15 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
46
66
  const looksLikeDirective = path.isDirective();
47
67
  const looksLikeProp = path.isObjectProperty();
48
68
 
69
+ // || path.isTSPropertySignature();
49
70
  if (looksLikeProp)
50
71
  print.breakline();
51
72
 
52
73
  print(`/*${value}*/`);
53
74
 
75
+ if (isTSPropertySignature(path) && !isPrev(path))
76
+ print.breakline();
77
+
54
78
  if (path.isStatement() || path.isTSEnumMember() || looksLikeDirective || looksLikeMethod || looksLikeProp || looksLikeSwitchCase) {
55
79
  print.newline();
56
80
  markBefore(path);
@@ -23,12 +23,18 @@ const isNext = (path) => {
23
23
  return !next.isEmptyStatement();
24
24
  };
25
25
 
26
+ const isPrev = (path) => {
27
+ const next = path.getPrevSibling();
28
+ return next.node;
29
+ };
30
+
26
31
  const isNextParent = (path) => isNext(path.parentPath);
27
32
  const isLast = (path) => isParentProgram(path) && !isNext(path);
28
33
 
29
34
  module.exports.isFirst = (path) => path.node === path.parentPath.node.body?.[0];
30
35
  module.exports.isPrevBody = (path) => path.getPrevSibling().isBlockStatement();
31
36
  module.exports.isNext = isNext;
37
+ module.exports.isPrev = isPrev;
32
38
  module.exports.isNextParent = isNextParent;
33
39
  module.exports.isParentProgram = isParentProgram;
34
40
  module.exports.isParentBlock = isParentBlock;
@@ -9,4 +9,6 @@ module.exports.TSCallSignatureDeclaration = (path, printer, semantics) => {
9
9
  print(':');
10
10
  print.space();
11
11
  printReturnType(path, printer);
12
+ print(';');
13
+ print.newline();
12
14
  };
@@ -18,4 +18,7 @@ module.exports.TSConstructSignatureDeclaration = (path, printer, semantics) => {
18
18
  write.space();
19
19
  printReturnType(path, printer);
20
20
  }
21
+
22
+ write(';');
23
+ write.newline();
21
24
  };
@@ -18,4 +18,7 @@ module.exports.TSMethodSignature = (path, printer, semantics) => {
18
18
  write.space();
19
19
  printReturnType(path, printer);
20
20
  }
21
+
22
+ write(';');
23
+ write.newline();
21
24
  };
@@ -34,6 +34,7 @@ const {
34
34
  maybeParenOpen,
35
35
  maybeParenClose,
36
36
  } = require('../expressions/unary-expression/parens');
37
+ const {maybePrintTypeAnnotation} = require('../maybe/maybe-type-annotation');
37
38
 
38
39
  module.exports = {
39
40
  TSAsExpression,
@@ -180,13 +181,14 @@ module.exports = {
180
181
  print('__typeAnnotation');
181
182
  },
182
183
  TSConstructSignatureDeclaration,
183
- TSIndexSignature(path, {print}) {
184
+ TSIndexSignature(path, printer) {
185
+ const {print} = printer;
184
186
  print('[');
185
187
  print('__parameters.0');
186
188
  print(']');
187
- print(':');
188
- print.space();
189
- print('__typeAnnotation');
189
+ maybePrintTypeAnnotation(path, printer);
190
+ print(';');
191
+ print.newline();
190
192
  },
191
193
  TSExpressionWithTypeArguments(path, {print}) {
192
194
  print('__expression');
@@ -10,12 +10,17 @@ module.exports.TSInterfaceBody = (path, {traverse, write, indent, maybe}) => {
10
10
  for (const item of body) {
11
11
  indent();
12
12
  traverse(item);
13
- write(';');
14
- write.newline();
15
13
  }
16
14
 
17
15
  indent.dec();
18
16
  indent();
19
17
  write('}');
20
- maybe.write.newline(path.parentPath.parentPath.isTSModuleBlock());
18
+ maybe.write.newline(findTSModuleBlock(path));
21
19
  };
20
+
21
+ function findTSModuleBlock(path) {
22
+ if (path.parentPath.parentPath.isTSModuleBlock())
23
+ return true;
24
+
25
+ return path.parentPath.parentPath.parentPath?.isTSModuleBlock();
26
+ }
@@ -9,10 +9,10 @@ const {isNext, isNextParent} = require('../../is');
9
9
  const {maybeDeclare} = require('../../maybe/maybe-declare');
10
10
 
11
11
  module.exports.TSInterfaceDeclaration = {
12
- print: maybeDeclare((path, {print, indent}) => {
13
- const {node} = path;
12
+ print: maybeDeclare((path, {print, maybe}) => {
13
+ const {node, parentPath} = path;
14
14
 
15
- indent();
15
+ maybe.indent(!isExportDeclaration(parentPath));
16
16
  print('interface ');
17
17
  print('__id');
18
18
 
@@ -28,10 +28,10 @@ module.exports.TSInterfaceDeclaration = {
28
28
  }),
29
29
  afterSatisfy: () => [isNext, isNextParent],
30
30
  after(path, {print}) {
31
- const next = path.parentPath.getNextSibling();
32
-
33
31
  print.breakline();
34
32
 
33
+ const next = path.parentPath.getNextSibling();
34
+
35
35
  if (!isTSTypeAliasDeclaration(next) && !isExportDeclaration(next))
36
36
  print.breakline();
37
37
  },
@@ -1,11 +1,32 @@
1
1
  'use strict';
2
2
 
3
- const {maybeTypeAnnotation} = require('../maybe/maybe-type-annotation');
3
+ const {maybePrintTypeAnnotation} = require('../maybe/maybe-type-annotation');
4
4
 
5
- module.exports.TSPropertySignature = maybeTypeAnnotation((path, {print, maybe}) => {
5
+ const {
6
+ hasTrailingComment,
7
+ isNext,
8
+ } = require('../is');
9
+
10
+ module.exports.TSPropertySignature = (path, printer) => {
11
+ const {
12
+ print,
13
+ maybe,
14
+ write,
15
+ } = printer;
16
+
6
17
  const {optional, readonly} = path.node;
7
18
 
8
19
  maybe.print(readonly, 'readonly ');
9
20
  print('__key');
10
21
  maybe.print(optional, '?');
11
- });
22
+
23
+ maybePrintTypeAnnotation(path, printer);
24
+
25
+ if (!path.parentPath.parentPath.isTSTypeParameterInstantiation()) {
26
+ write(';');
27
+ write.newline();
28
+ }
29
+
30
+ if (isNext(path) && hasTrailingComment(path))
31
+ write.newline();
32
+ };
@@ -1,8 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {isNext} = require('../../is');
4
-
5
- module.exports.TSTypeLiteral = (path, {indent, traverse, write, maybe}) => {
3
+ module.exports.TSTypeLiteral = (path, {indent, traverse, write}) => {
6
4
  const members = path.get('members');
7
5
  write('{');
8
6
 
@@ -16,15 +14,11 @@ module.exports.TSTypeLiteral = (path, {indent, traverse, write, maybe}) => {
16
14
  for (const member of members) {
17
15
  indent();
18
16
  traverse(member);
19
- maybe.write(is, ';');
20
-
21
- if (is && isNext(member))
22
- write.newline();
23
17
  }
24
18
 
25
19
  if (is) {
26
20
  indent.dec();
27
- write.breakline();
21
+ write.indent();
28
22
  }
29
23
 
30
24
  write('}');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "5.36.0",
3
+ "version": "5.37.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",