@putout/printer 8.22.0 → 8.23.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
+ 2024.04.30, v8.23.0
2
+
3
+ feature:
4
+ - a0dc84e @putout/printer: improve support of Decorators
5
+
1
6
  2024.04.29, v8.22.0
2
7
 
3
8
  feature:
@@ -26,6 +26,8 @@ const isProperty = satisfy([
26
26
  isSpreadElement,
27
27
  ]);
28
28
 
29
+ const hasDecorators = ({parentPath}) => parentPath.node.decorators?.length;
30
+
29
31
  function isCommentOfPrevious(path) {
30
32
  const [comment] = path.node.leadingComments;
31
33
  const {line} = comment.loc.start;
@@ -62,6 +64,9 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
62
64
  if (!leadingComments?.length)
63
65
  return;
64
66
 
67
+ if (hasDecorators(path))
68
+ return;
69
+
65
70
  const looksLikeSwitchCase = path.isSwitchCase();
66
71
 
67
72
  if (!looksLikeSwitchCase && hasTrailingComment(path.getPrevSibling()))
@@ -1,14 +1,16 @@
1
1
  'use strict';
2
2
 
3
+ const {isFunction} = require('@putout/babel').types;
3
4
  const {isNext} = require('../../is');
4
5
  const {markAfter} = require('../../mark');
5
6
 
6
- //const {maybeDecorators} = require('../../maybe-get');
7
7
  const {maybeDeclare} = require('../../maybe/maybe-declare');
8
8
  const {parseComments} = require('../../comment/comment');
9
9
  const {maybeDecorators} = require('../../maybe/maybe-decorators');
10
10
 
11
11
  const isInsideExport = ({parentPath}) => parentPath.isExportDeclaration();
12
+ const isFunctionLike = (path) => isFunction(path.parentPath.parentPath);
13
+ const hasBody = ({node}) => node.body.body.length;
12
14
 
13
15
  const classVisitor = maybeDecorators((path, printer, semantics) => {
14
16
  const {id, abstract} = path.node;
@@ -70,6 +72,9 @@ module.exports.ClassDeclaration = {
70
72
  classVisitor(path, printer, semantics);
71
73
  }),
72
74
  afterIf(path) {
75
+ if (isFunctionLike(path))
76
+ return true;
77
+
73
78
  if (!isNext(path))
74
79
  return false;
75
80
 
@@ -78,7 +83,7 @@ module.exports.ClassDeclaration = {
78
83
  after(path, {write}) {
79
84
  write.newline();
80
85
 
81
- if (path.node.body.body.length) {
86
+ if (!isFunctionLike(path) && hasBody(path)) {
82
87
  write.newline();
83
88
  markAfter(path);
84
89
  }
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ const {isMemberExpression} = require('@putout/babel').types;
4
+
5
+ module.exports.Decorator = (path, {print, maybe}) => {
6
+ const {expression} = path.node;
7
+ const isMember = isMemberExpression(expression);
8
+
9
+ print('@');
10
+
11
+ maybe.print(isMember, '(');
12
+ print('__expression');
13
+ maybe.print(isMember, ')');
14
+ };
@@ -3,6 +3,8 @@
3
3
  const {TemplateLiteral} = require('./template-literal');
4
4
  const {Identifier} = require('./identifier');
5
5
 
6
+ const {Decorator} = require('../expressions/decorator');
7
+
6
8
  const maybeEncode = (value, {encodeDoubleQuote, encodeSingleQuote}) => {
7
9
  if (encodeSingleQuote && !value.includes('\\'))
8
10
  return value.replaceAll(`'`, `\\'`);
@@ -15,14 +17,11 @@ const maybeEncode = (value, {encodeDoubleQuote, encodeSingleQuote}) => {
15
17
 
16
18
  module.exports = {
17
19
  Identifier,
20
+ Decorator,
18
21
  TemplateLiteral,
19
22
  BigIntLiteral(path, {write}) {
20
23
  write(path.node.raw);
21
24
  },
22
- Decorator(path, {print}) {
23
- print('@');
24
- print('__expression');
25
- },
26
25
  NumericLiteral(path, {write}) {
27
26
  const {
28
27
  raw,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "8.22.0",
3
+ "version": "8.23.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",