@putout/printer 5.36.0 → 5.38.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.10.25, v5.38.0
2
+
3
+ feature:
4
+ - 983412b comments: leading: improve
5
+
6
+ 2023.10.24, v5.37.0
7
+
8
+ feature:
9
+ - f01afa3 @putout/printer: TSInterfaceBody: simplify
10
+
1
11
  2023.10.24, v5.36.0
2
12
 
3
13
  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
 
@@ -51,6 +71,9 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
51
71
 
52
72
  print(`/*${value}*/`);
53
73
 
74
+ if (isTSPropertySignature(path) && !isPrev(path))
75
+ print.breakline();
76
+
54
77
  if (path.isStatement() || path.isTSEnumMember() || looksLikeDirective || looksLikeMethod || looksLikeProp || looksLikeSwitchCase) {
55
78
  print.newline();
56
79
  markBefore(path);
@@ -1,7 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  const {isDecorator} = require('@putout/babel').types;
4
- const {isLast, isCoupleLines} = require('../is');
4
+ const {
5
+ isLast,
6
+ isCoupleLines,
7
+ isNext,
8
+ } = require('../is');
5
9
 
6
10
  const hasBody = (path) => path.node.body;
7
11
 
@@ -30,7 +34,7 @@ module.exports.parseTrailingComments = (path, {write, maybe}, semantics) => {
30
34
 
31
35
  if (hasBody(path)) {
32
36
  write.breakline();
33
- write.breakline();
37
+ maybe.write.breakline(!isNext(path));
34
38
  }
35
39
 
36
40
  write(`//${value}`);
@@ -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
  };
@@ -35,6 +35,8 @@ const {
35
35
  maybeParenClose,
36
36
  } = require('../expressions/unary-expression/parens');
37
37
 
38
+ const {maybePrintTypeAnnotation} = require('../maybe/maybe-type-annotation');
39
+
38
40
  module.exports = {
39
41
  TSAsExpression,
40
42
  TSTypeLiteral,
@@ -180,13 +182,14 @@ module.exports = {
180
182
  print('__typeAnnotation');
181
183
  },
182
184
  TSConstructSignatureDeclaration,
183
- TSIndexSignature(path, {print}) {
185
+ TSIndexSignature(path, printer) {
186
+ const {print} = printer;
184
187
  print('[');
185
188
  print('__parameters.0');
186
189
  print(']');
187
- print(':');
188
- print.space();
189
- print('__typeAnnotation');
190
+ maybePrintTypeAnnotation(path, printer);
191
+ print(';');
192
+ print.newline();
190
193
  },
191
194
  TSExpressionWithTypeArguments(path, {print}) {
192
195
  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.38.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",