@putout/printer 5.13.0 → 5.14.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,9 @@
1
+ 2023.10.02, v5.14.0
2
+
3
+ feature:
4
+ - 0157a3c @putout/printer: ClassDeclaration: declare
5
+ - 09310b8 @putout/printer: typescript: maybeDeclare
6
+
1
7
  2023.10.02, v5.13.0
2
8
 
3
9
  feature:
@@ -3,13 +3,14 @@
3
3
  const {isNext} = require('../../is');
4
4
  const {markAfter} = require('../../mark');
5
5
  const {maybeDecorators} = require('../../maybe-get');
6
+ const {maybeDeclare} = require('../../typescript/namespace/maybeDeclare');
6
7
 
7
8
  const isInsideExport = ({parentPath}) => parentPath.isExportDeclaration();
8
9
 
9
10
  module.exports.ClassExpression = classVisitor;
10
11
 
11
12
  module.exports.ClassDeclaration = {
12
- print(path, {print, indent, maybe, traverse}) {
13
+ print: maybeDeclare((path, {print, indent, maybe, traverse}) => {
13
14
  indent();
14
15
 
15
16
  classVisitor(path, {
@@ -18,7 +19,7 @@ module.exports.ClassDeclaration = {
18
19
  maybe,
19
20
  traverse,
20
21
  });
21
- },
22
+ }),
22
23
  afterIf(path) {
23
24
  if (!isNext(path))
24
25
  return false;
@@ -14,6 +14,7 @@ const {maybeSpaceAfterKeyword} = require('./maybe-space-after-keyword');
14
14
 
15
15
  const {isConcatenation} = require('../../expressions/binary-expression/concatanate');
16
16
  const {parseLeadingComments} = require('../../comment/comment');
17
+ const {maybeDeclare} = require('../../typescript/namespace/maybeDeclare');
17
18
 
18
19
  const isParentTSModuleBlock = (path) => path.parentPath.isTSModuleBlock();
19
20
  const isParentBlock = (path) => /Program|BlockStatement|Export/.test(path.parentPath.type);
@@ -26,15 +27,11 @@ module.exports.VariableDeclaration = {
26
27
  before(path, {print}) {
27
28
  print.breakline();
28
29
  },
29
- print(path, {maybe, store, write, traverse, print, indent}, semantics) {
30
- const {declare} = path.node;
30
+ print: maybeDeclare((path, {maybe, store, write, traverse, print, indent}, semantics) => {
31
31
  const {maxVariablesInOneLine} = semantics;
32
32
 
33
33
  maybe.indent(isInsideBlock(path));
34
34
 
35
- if (declare)
36
- write('declare ');
37
-
38
35
  write(path.node.kind);
39
36
  maybeSpaceAfterKeyword(path, {
40
37
  write,
@@ -94,7 +91,7 @@ module.exports.VariableDeclaration = {
94
91
  }
95
92
 
96
93
  store(wasNewline);
97
- },
94
+ }),
98
95
  afterSatisfy: () => [
99
96
  noNextParentBlock,
100
97
  notLastCoupleLines,
@@ -8,9 +8,14 @@ const isInsideExport = (path) => {
8
8
  };
9
9
 
10
10
  module.exports.TSDeclareFunction = (path, printer, semantics) => {
11
- const {print, maybe} = printer;
11
+ const {
12
+ print,
13
+ maybe,
14
+ indent,
15
+ } = printer;
12
16
  const {declare} = path.node;
13
17
 
18
+ indent();
14
19
  maybe.print(declare, 'declare ');
15
20
  print('function ');
16
21
  print('__id');
@@ -26,3 +31,4 @@ module.exports.TSDeclareFunction = (path, printer, semantics) => {
26
31
  maybe.print.newline(isNext(path) || isNext(path.parentPath));
27
32
  }
28
33
  };
34
+
@@ -4,7 +4,11 @@ const {printParams} = require('../../expressions/function/params');
4
4
 
5
5
  module.exports.TSDeclareMethod = (path, printer, semantics) => {
6
6
  const {print} = printer;
7
- const {accessibility, abstract} = path.node;
7
+ const {
8
+ accessibility,
9
+ abstract,
10
+ returnType,
11
+ } = path.node;
8
12
 
9
13
  if (accessibility) {
10
14
  print(accessibility);
@@ -20,9 +24,13 @@ module.exports.TSDeclareMethod = (path, printer, semantics) => {
20
24
 
21
25
  printParams(path, printer, semantics);
22
26
 
23
- print(':');
24
- print.space();
25
- print('__returnType');
27
+ if (returnType) {
28
+ print(':');
29
+ print.space();
30
+ print('__returnType');
31
+ }
32
+
26
33
  print(';');
27
34
  print.newline();
28
35
  };
36
+
@@ -12,7 +12,7 @@ const {TSDeclareMethod} = require('./function/ts-declare-method');
12
12
  const {
13
13
  TSModuleDeclaration,
14
14
  TSModuleBlock,
15
- } = require('./ts-module-declaration');
15
+ } = require('./namespace/ts-module-declaration');
16
16
 
17
17
  const {TSInterfaceDeclaration} = require('./interface/ts-interface-declaration');
18
18
  const {TSAsExpression} = require('./ts-as-expression');
@@ -1,23 +1,19 @@
1
1
  'use strict';
2
2
 
3
- const {isNext, isNextParent} = require('../../is');
4
3
  const {
5
4
  isTSTypeAliasDeclaration,
6
5
  isExportDeclaration,
7
6
  } = require('@putout/babel').types;
8
7
 
8
+ const {isNext, isNextParent} = require('../../is');
9
+ const {maybeDeclare} = require('../namespace/maybeDeclare');
10
+
9
11
  module.exports.TSInterfaceDeclaration = {
10
- beforeIf(path) {
11
- return path.node.declare;
12
- },
13
- before(path, {print}) {
14
- print('declare ');
15
- },
16
- print(path, {print}) {
12
+ print: maybeDeclare((path, {print}) => {
17
13
  print('interface ');
18
14
  print('__id');
19
15
  print('__body');
20
- },
16
+ }),
21
17
  afterSatisfy: () => [isNext, isNextParent],
22
18
  after(path, {print}) {
23
19
  const next = path.parentPath.getNextSibling();
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ module.exports.maybeDeclare = (visit) => (path, printer, semantics) => {
4
+ const {maybe} = printer;
5
+ const {declare} = path.node;
6
+
7
+ maybe.print(declare, 'declare ');
8
+ visit(path, printer, semantics);
9
+ };
@@ -1,17 +1,15 @@
1
1
  'use strict';
2
2
 
3
- const {isNext} = require('../is');
3
+ const {isNext} = require('../../is');
4
+ const {maybeDeclare} = require('./maybeDeclare');
4
5
 
5
6
  module.exports.TSModuleDeclaration = {
6
- print(path, {print, maybe}) {
7
- const {declare} = path.node;
8
- maybe.print(declare, 'declare ');
9
-
7
+ print: maybeDeclare((path, {print}) => {
10
8
  print('namespace ');
11
9
  print('__id');
12
10
  print.space();
13
11
  print('__body');
14
- },
12
+ }),
15
13
  afterSatisfy: () => [isNext],
16
14
  after(path, {print}) {
17
15
  print.newline();
@@ -2,16 +2,11 @@
2
2
 
3
3
  const {isLast} = require('../../is');
4
4
  const {markAfter} = require('../../mark');
5
+ const {maybeDeclare} = require('../namespace/maybeDeclare');
5
6
  const isNextType = (a) => a.getNextSibling().isTSTypeAliasDeclaration();
6
7
 
7
8
  module.exports.TSTypeAliasDeclaration = {
8
- beforeIf(path) {
9
- return path.node.declare;
10
- },
11
- before(path, {print}) {
12
- print('declare ');
13
- },
14
- print(path, {print, maybe, store}) {
9
+ print: maybeDeclare((path, {print, maybe, store}) => {
15
10
  const typeAnnotation = path.get('typeAnnotation');
16
11
  const isConditional = typeAnnotation.isTSConditionalType();
17
12
 
@@ -28,7 +23,7 @@ module.exports.TSTypeAliasDeclaration = {
28
23
 
29
24
  const is = store(isLast(path) || isLast(path.parentPath));
30
25
  maybe.print.newline(!is);
31
- },
26
+ }),
32
27
  afterIf(path, {store}) {
33
28
  const last = store();
34
29
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "5.13.0",
3
+ "version": "5.14.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",