@putout/printer 18.50.6 → 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,13 @@
1
+ 2026.11.22, v18.52.0
2
+
3
+ feature:
4
+ - df1a9dd0 @putout/printer: comments: improve
5
+
6
+ 2026.11.21, v18.51.0
7
+
8
+ feature:
9
+ - 53e29be3 @putout/printer: type-checker: instrument: add stackIndex
10
+
1
11
  2026.11.17, v18.50.6
2
12
 
3
13
  fix:
@@ -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}"`);
@@ -10,6 +10,7 @@ type InstrumentOverride = {
10
10
  };
11
11
  coverage?: Map<string, CoverageEntry>;
12
12
  instrumentName?: string;
13
+ stackIndex?: number;
13
14
  [key: string]: unknown;
14
15
  };
15
16
 
@@ -2,7 +2,7 @@ import {env as _env} from 'node:process';
2
2
 
3
3
  const STACK_INDEX = 3;
4
4
 
5
- const parseCallLocation = ({stack}) => stack.split('\n')[STACK_INDEX];
5
+ const parseCallLocation = ({stack}, stackIndex) => stack.split('\n')[stackIndex];
6
6
 
7
7
  const Coverage = new Map();
8
8
 
@@ -11,10 +11,11 @@ export const instrument = (typeNames, fn, overrides = {}) => {
11
11
  env = _env,
12
12
  coverage = Coverage,
13
13
  instrumentName = 'TYPE_CHECK',
14
+ stackIndex = STACK_INDEX,
14
15
  } = overrides;
15
16
 
16
17
  const error = Error();
17
- const location = parseCallLocation(error);
18
+ const location = parseCallLocation(error, stackIndex);
18
19
  const on = env[instrumentName];
19
20
  const covered = new Set();
20
21
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "18.50.6",
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",