@putout/printer 18.51.0 → 18.52.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
+ 2026.11.22, v18.52.0
2
+
3
+ feature:
4
+ - df1a9dd0 @putout/printer: comments: improve
5
+
1
6
  2026.11.21, v18.51.0
2
7
 
3
8
  feature:
@@ -1,5 +1,7 @@
1
+ import {printCommentBlock} from './print-comment-block.js';
2
+
1
3
  const createPrintCommentLine = (fn, value) => () => fn(`//${value}`);
2
- const createPrintCommentBlock = (fn, value) => () => fn(`/*${value}*/\n`);
4
+ const createPrintCommentBlock = (printer, value) => () => printCommentBlock(value, printer, '\n');
3
5
 
4
6
  const isCommentBlock = (node) => node.type === 'CommentBlock';
5
7
  const isCommentLine = (node) => node.type === 'CommentLine';
@@ -65,7 +67,7 @@ export const printLeadingComments = (path, printer, semantics, {currentTraverse}
65
67
  if (isCommentBlock(comment))
66
68
  printLeadingCommentBlock?.(path, printer, semantics, {
67
69
  index,
68
- printComment: createPrintCommentBlock(print, value),
70
+ printComment: createPrintCommentBlock(printer, value),
69
71
  });
70
72
  }
71
73
  };
@@ -99,7 +101,7 @@ export const printTrailingComments = (path, printer, semantics, {currentTraverse
99
101
  if (isCommentBlock(comment))
100
102
  printTrailingCommentBlock?.(path, printer, semantics, {
101
103
  index,
102
- printComment: createPrintCommentBlock(print, value),
104
+ printComment: createPrintCommentBlock(printer, value),
103
105
  isLast: index === n,
104
106
  isSameLine: isSameLine(path, loc),
105
107
  });
@@ -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
+ };
@@ -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}"`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "18.51.0",
3
+ "version": "18.52.0",
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",