@putout/printer 18.51.0 → 18.52.1

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
+ 2026.11.22, v18.52.1
2
+
3
+ feature:
4
+ - 8b19aaa1 VariableDeclarator: comments: improve
5
+
6
+ 2026.11.22, v18.52.0
7
+
8
+ feature:
9
+ - df1a9dd0 @putout/printer: comments: improve
10
+
1
11
  2026.11.21, v18.51.0
2
12
 
3
13
  feature:
@@ -1,5 +1,8 @@
1
+ import {isCommentOnPreviousLine} from './is-comment-on-previous-line.js';
2
+ import {printCommentBlock} from './print-comment-block.js';
3
+
1
4
  const createPrintCommentLine = (fn, value) => () => fn(`//${value}`);
2
- const createPrintCommentBlock = (fn, value) => () => fn(`/*${value}*/\n`);
5
+ const createPrintCommentBlock = (printer, value) => () => printCommentBlock(value, printer, '\n');
3
6
 
4
7
  const isCommentBlock = (node) => node.type === 'CommentBlock';
5
8
  const isCommentLine = (node) => node.type === 'CommentLine';
@@ -58,6 +61,7 @@ export const printLeadingComments = (path, printer, semantics, {currentTraverse}
58
61
  index,
59
62
  printComment: createPrintCommentLine(print, value),
60
63
  isLast: index === n,
64
+ isPreviousLine: () => isCommentOnPreviousLine(path),
61
65
  });
62
66
  continue;
63
67
  }
@@ -65,7 +69,7 @@ export const printLeadingComments = (path, printer, semantics, {currentTraverse}
65
69
  if (isCommentBlock(comment))
66
70
  printLeadingCommentBlock?.(path, printer, semantics, {
67
71
  index,
68
- printComment: createPrintCommentBlock(print, value),
72
+ printComment: createPrintCommentBlock(printer, value),
69
73
  });
70
74
  }
71
75
  };
@@ -99,7 +103,7 @@ export const printTrailingComments = (path, printer, semantics, {currentTraverse
99
103
  if (isCommentBlock(comment))
100
104
  printTrailingCommentBlock?.(path, printer, semantics, {
101
105
  index,
102
- printComment: createPrintCommentBlock(print, value),
106
+ printComment: createPrintCommentBlock(printer, value),
103
107
  isLast: index === n,
104
108
  isSameLine: isSameLine(path, loc),
105
109
  });
@@ -0,0 +1,20 @@
1
+ const isCommentOfPrevious = (path) => {
2
+ const [comment] = path.node.leadingComments;
3
+ const prev = path.getPrevSibling();
4
+
5
+ return prev.node.loc.start.line === comment.loc.start.line;
6
+ };
7
+
8
+ export const isCommentOnPreviousLine = (path) => {
9
+ const {
10
+ loc,
11
+ leadingComments,
12
+ } = path.node;
13
+
14
+ const [comment] = leadingComments;
15
+
16
+ if (isCommentOfPrevious(path))
17
+ return false;
18
+
19
+ return comment.loc.start.line <= loc.start.line - 1;
20
+ };
@@ -0,0 +1,38 @@
1
+ const isStarLine = (line) => line
2
+ .trim()
3
+ .startsWith('*');
4
+
5
+ const isBlank = (line) => !line.trim();
6
+
7
+ export const printCommentBlock = (value, {print, indent}, end = '') => {
8
+ const [first, ...rest] = value.split('\n');
9
+
10
+ if (!rest.length || !indent.getLevel()) {
11
+ print(`/*${value}*/${end}`);
12
+ return;
13
+ }
14
+
15
+ const last = rest.at(-1);
16
+ const lines = isBlank(last) ? rest.slice(0, -1) : rest;
17
+
18
+ if (!lines.every(isStarLine)) {
19
+ print(`/*${value}*/${end}`);
20
+ return;
21
+ }
22
+
23
+ print(`/*${first}`);
24
+
25
+ for (const line of lines) {
26
+ print.breakline();
27
+
28
+ print(` ${line.trimStart()}`);
29
+ }
30
+
31
+ if (isBlank(last)) {
32
+ print.breakline();
33
+
34
+ print(' ');
35
+ }
36
+
37
+ print(`*/${end}`);
38
+ };
@@ -25,7 +25,6 @@ const {
25
25
 
26
26
  const isProperty = satisfy([
27
27
  isObjectProperty,
28
- isVariableDeclarator,
29
28
  isClassProperty,
30
29
  isTSPropertySignature,
31
30
  isSpreadElement,
@@ -1,5 +1,21 @@
1
+ const {stringify} = JSON;
2
+ const escapeValue = (value) => stringify(value)
3
+ .slice(1, -1)
4
+ .replaceAll(`'`, `\\'`);
5
+
6
+ const getRaw = ({value, raw, extra}) => {
7
+ if (raw)
8
+ return raw;
9
+
10
+ if (extra?.raw && extra.rawValue === value)
11
+ return extra.raw;
12
+
13
+ return `'${escapeValue(value)}'`;
14
+ };
15
+
1
16
  export const StringLiteral = (path, {write}, semantics) => {
2
- const {value, raw = `'${value}'`} = path.node;
17
+ const {value} = path.node;
18
+ const raw = getRaw(path.node);
3
19
 
4
20
  if (path.parentPath.isJSXAttribute()) {
5
21
  write(`"${value}"`);
@@ -19,6 +19,7 @@ import {
19
19
  } from './after-if.js';
20
20
  import {maybeSpaceAfterKeyword} from './maybe-space-after-keyword.js';
21
21
  import {beforeIf} from './before-if.js';
22
+ import {VariableDeclarator} from './variable-declarator.js';
22
23
  import {
23
24
  isInsideParentLike,
24
25
  isNeedBreaklineAfterComma,
@@ -95,7 +96,9 @@ export const VariableDeclaration = {
95
96
  write(',');
96
97
 
97
98
  if (hasLeadingComment(next)) {
98
- parseLeadingComments(next, printer, semantics);
99
+ parseLeadingComments(next, printer, semantics, {
100
+ currentTraverse: VariableDeclarator,
101
+ });
99
102
  continue;
100
103
  }
101
104
 
@@ -0,0 +1,26 @@
1
+ export const VariableDeclarator = {};
2
+
3
+ VariableDeclarator.printLeadingCommentLine = (path, printer, semantics, {printComment, isLast, isPreviousLine}) => {
4
+ const {print} = printer;
5
+
6
+ if (isPreviousLine())
7
+ print.breakline();
8
+ else
9
+ print.space();
10
+
11
+ printComment();
12
+
13
+ if (isLast) {
14
+ print.breakline();
15
+ return;
16
+ }
17
+
18
+ print.newline();
19
+ };
20
+
21
+ VariableDeclarator.printLeadingCommentBlock = (path, printer, semantics, {index}) => {
22
+ const {print} = printer;
23
+ const {value} = path.node.leadingComments[index];
24
+
25
+ print(`/*${value}*/`);
26
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "18.51.0",
3
+ "version": "18.52.1",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Simplest possible opinionated Babel AST printer for 🐊Putout",