@putout/printer 2.56.0 → 2.57.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.07.08, v2.57.0
2
+
3
+ feature:
4
+ - 0e980f4 @putout/printer: ObjectProperty: improve support of multiline comments
5
+
1
6
  2023.07.08, v2.56.0
2
7
 
3
8
  feature:
@@ -1,129 +1,10 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- hasTrailingComment,
5
- isLast,
6
- } = require('../is');
3
+ const {parseLeadingComments} = require('./parse-leading-comments');
4
+ const {parseTrailingComments} = require('./parse-trailing-comments');
5
+ const {parseComments} = require('./parse-comments');
7
6
 
8
- const {markBefore} = require('../mark');
9
- const {isVariableDeclarator} = require('@babel/types');
7
+ module.exports.parseLeadingComments = parseLeadingComments;
8
+ module.exports.parseTrailingComments = parseTrailingComments;
9
+ module.exports.parseComments = parseComments;
10
10
 
11
- module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics) => {
12
- if (!semantics.comments)
13
- return;
14
-
15
- const {leadingComments} = path.node;
16
-
17
- if (!leadingComments?.length)
18
- return;
19
-
20
- if (hasTrailingComment(path.getPrevSibling()))
21
- return;
22
-
23
- const insideFn = path.parentPath.isFunction();
24
- const isProperty = path.isObjectProperty() || isVariableDeclarator(path);
25
- const isIndent = !path.isClassMethod() && !insideFn && !isProperty;
26
-
27
- for (const {type, value} of leadingComments) {
28
- maybe.indent(isIndent);
29
-
30
- if (type === 'CommentLine') {
31
- maybeInsideFn(insideFn, {
32
- print,
33
- indent,
34
- });
35
-
36
- maybe.print.space(isProperty);
37
- print(`//${value}`);
38
-
39
- maybe.print.breakline(isProperty);
40
- maybe.print.newline(!isProperty);
41
- continue;
42
- }
43
-
44
- if (type === 'CommentBlock') {
45
- print(`/*${value}*/`);
46
-
47
- if (path.isStatement() || path.isClassMethod() || path.isDirective()) {
48
- print.newline();
49
- markBefore(path);
50
- maybe.indent(path.isClassMethod());
51
- }
52
-
53
- continue;
54
- }
55
- }
56
- };
57
-
58
- module.exports.parseTrailingComments = (path, {write, maybe}, semantics) => {
59
- if (!semantics.comments)
60
- return;
61
-
62
- const {trailingComments} = path.node;
63
-
64
- if (!trailingComments?.length)
65
- return;
66
-
67
- if (path.isDirective())
68
- return;
69
-
70
- for (const {type, value, loc} of trailingComments) {
71
- const sameLine = isSameLine(path, loc);
72
-
73
- if (type === 'CommentLine') {
74
- maybe.write.space(sameLine);
75
- maybe.indent(!sameLine);
76
-
77
- write(`//${value}`);
78
- maybe.write.newline(!isLast(path));
79
- continue;
80
- }
81
-
82
- if (type === 'CommentBlock') {
83
- maybe.write.space(sameLine);
84
- write(`/*${value}*/`);
85
- maybe.write.newline(!sameLine);
86
- }
87
- }
88
- };
89
-
90
- module.exports.parseComments = (path, {write}, semantics) => {
91
- if (!semantics.comments)
92
- return;
93
-
94
- const comments = path.node.comments || path.node.innerComments;
95
-
96
- if (!comments)
97
- return;
98
-
99
- for (const {type, value} of comments) {
100
- if (type === 'CommentLine') {
101
- write.breakline();
102
- write('//');
103
- write(value);
104
- write.newline();
105
- continue;
106
- }
107
-
108
- if (type === 'CommentBlock') {
109
- write('/*');
110
- write(value);
111
- write('*/');
112
- }
113
- }
114
- };
115
-
116
- function isSameLine(path, loc) {
117
- return path.node.loc?.start.line === loc.start.line || path.node.loc?.end.line === loc.end.line;
118
- }
119
-
120
- function maybeInsideFn(insideFn, {print, indent}) {
121
- if (!insideFn)
122
- return;
123
-
124
- indent.inc();
125
- indent.inc();
126
- print.breakline();
127
- indent.dec();
128
- indent.dec();
129
- }
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ module.exports.maybeInsideFn = (insideFn, {print, indent}) => {
4
+ if (!insideFn)
5
+ return;
6
+
7
+ indent.inc();
8
+ indent.inc();
9
+ print.breakline();
10
+ indent.dec();
11
+ indent.dec();
12
+ };
13
+
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ module.exports.parseComments = (path, {write}, semantics) => {
4
+ if (!semantics.comments)
5
+ return;
6
+
7
+ const comments = path.node.comments || path.node.innerComments;
8
+
9
+ if (!comments)
10
+ return;
11
+
12
+ for (const {type, value} of comments) {
13
+ if (type === 'CommentLine') {
14
+ write.breakline();
15
+ write('//');
16
+ write(value);
17
+ write.newline();
18
+ continue;
19
+ }
20
+
21
+ if (type === 'CommentBlock') {
22
+ write('/*');
23
+ write(value);
24
+ write('*/');
25
+ }
26
+ }
27
+ };
28
+
@@ -0,0 +1,61 @@
1
+ 'use strict';
2
+
3
+ const {hasTrailingComment} = require('../is');
4
+ const {isVariableDeclarator} = require('@babel/types');
5
+ const {markBefore} = require('../mark');
6
+ const {maybeInsideFn} = require('./maybe-inside-fn');
7
+
8
+ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics) => {
9
+ if (!semantics.comments)
10
+ return;
11
+
12
+ const {leadingComments} = path.node;
13
+
14
+ if (!leadingComments?.length)
15
+ return;
16
+
17
+ if (hasTrailingComment(path.getPrevSibling()))
18
+ return;
19
+
20
+ const insideFn = path.parentPath.isFunction();
21
+ const isProperty = path.isObjectProperty() || isVariableDeclarator(path);
22
+ const isIndent = !path.isClassMethod() && !insideFn && !isProperty;
23
+
24
+ for (const {type, value} of leadingComments) {
25
+ maybe.indent(isIndent);
26
+
27
+ if (type === 'CommentLine') {
28
+ maybeInsideFn(insideFn, {
29
+ print,
30
+ indent,
31
+ });
32
+
33
+ maybe.print.space(isProperty);
34
+ print(`//${value}`);
35
+
36
+ maybe.print.breakline(isProperty);
37
+ maybe.print.newline(!isProperty);
38
+ continue;
39
+ }
40
+
41
+ if (type === 'CommentBlock') {
42
+ const looksLikeMethod = path.isClassMethod();
43
+ const looksLikeDirective = path.isDirective();
44
+ const looksLikeProp = path.isObjectProperty();
45
+
46
+ if (looksLikeProp)
47
+ print.breakline();
48
+
49
+ print(`/*${value}*/`);
50
+
51
+ if (path.isStatement() || looksLikeDirective || looksLikeMethod || looksLikeProp) {
52
+ print.newline();
53
+ markBefore(path);
54
+ maybe.indent(looksLikeMethod || looksLikeProp);
55
+ }
56
+
57
+ continue;
58
+ }
59
+ }
60
+ };
61
+
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ const {isLast} = require('../is');
4
+
5
+ function isSameLine(path, loc) {
6
+ return path.node.loc?.start.line === loc.start.line || path.node.loc?.end.line === loc.end.line;
7
+ }
8
+
9
+ module.exports.parseTrailingComments = (path, {write, maybe}, semantics) => {
10
+ if (!semantics.comments)
11
+ return;
12
+
13
+ const {trailingComments} = path.node;
14
+
15
+ if (!trailingComments?.length)
16
+ return;
17
+
18
+ if (path.isDirective())
19
+ return;
20
+
21
+ for (const {type, value, loc} of trailingComments) {
22
+ const sameLine = isSameLine(path, loc);
23
+
24
+ if (type === 'CommentLine') {
25
+ maybe.write.space(sameLine);
26
+ maybe.indent(!sameLine);
27
+
28
+ write(`//${value}`);
29
+ maybe.write.newline(!isLast(path));
30
+ continue;
31
+ }
32
+
33
+ if (type === 'CommentBlock') {
34
+ maybe.write.space(sameLine);
35
+ write(`/*${value}*/`);
36
+ maybe.write.newline(!sameLine);
37
+ }
38
+ }
39
+ };
40
+
@@ -72,4 +72,3 @@ module.exports = {
72
72
  write('super');
73
73
  },
74
74
  };
75
-
@@ -1,4 +1,3 @@
1
1
  'use strict';
2
2
 
3
3
  module.exports.getDirectives = (path) => !path.node.directives ? [] : path.get('directives');
4
-
@@ -24,4 +24,3 @@ module.exports.Program = (path, {print, write, traverse, maybe}, semantics) => {
24
24
 
25
25
  print.newline();
26
26
  };
27
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "2.56.0",
3
+ "version": "2.57.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",