@aforemendude/prettier-plugin-wrap-comments 1.0.5 → 1.1.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.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +50 -14
  3. package/dist/comments/comment-body.d.ts +4 -0
  4. package/dist/comments/comment-body.js +36 -0
  5. package/dist/comments/comment-directives.d.ts +2 -0
  6. package/dist/comments/comment-directives.js +48 -0
  7. package/dist/comments/comment-eligibility.d.ts +4 -0
  8. package/dist/comments/comment-eligibility.js +20 -0
  9. package/dist/comments/comment-location.d.ts +9 -0
  10. package/dist/comments/comment-location.js +21 -0
  11. package/dist/comments/comment-ranges.d.ts +24 -0
  12. package/dist/comments/comment-ranges.js +36 -0
  13. package/dist/comments/embedded-expression-ranges.d.ts +13 -0
  14. package/dist/comments/embedded-expression-ranges.js +105 -0
  15. package/dist/comments/jsx-expression-layout.d.ts +10 -0
  16. package/dist/comments/jsx-expression-layout.js +117 -0
  17. package/dist/comments/line-comment-groups.d.ts +6 -0
  18. package/dist/comments/line-comment-groups.js +28 -0
  19. package/dist/comments/prettier-ignore.d.ts +8 -0
  20. package/dist/comments/prettier-ignore.js +145 -0
  21. package/dist/comments/printer-layout.d.ts +17 -0
  22. package/dist/comments/printer-layout.js +64 -0
  23. package/dist/comments/{block.d.ts → wrap-block-comment.d.ts} +9 -1
  24. package/dist/comments/{block.js → wrap-block-comment.js} +30 -14
  25. package/dist/comments/wrap-comments.d.ts +9 -0
  26. package/dist/comments/wrap-comments.js +101 -0
  27. package/dist/comments/wrap-line-comment-group.d.ts +4 -0
  28. package/dist/comments/wrap-line-comment-group.js +39 -0
  29. package/dist/comments/wrap-trailing-line-comment.d.ts +9 -0
  30. package/dist/comments/wrap-trailing-line-comment.js +78 -0
  31. package/dist/index.d.ts +2 -2
  32. package/dist/index.js +4 -4
  33. package/dist/plugin/create-parsers.d.ts +2 -0
  34. package/dist/plugin/create-parsers.js +57 -0
  35. package/dist/plugin/create-printers.d.ts +2 -0
  36. package/dist/plugin/{printers.js → create-printers.js} +9 -10
  37. package/dist/plugin/get-printer-layout-source.d.ts +4 -0
  38. package/dist/plugin/get-printer-layout-source.js +27 -0
  39. package/dist/plugin/jsx-comment-rewrite-metadata.d.ts +8 -0
  40. package/dist/plugin/jsx-comment-rewrite-metadata.js +40 -0
  41. package/dist/plugin/parser-names.d.ts +2 -0
  42. package/dist/plugin/parser-names.js +1 -0
  43. package/dist/utils/ast.d.ts +6 -0
  44. package/dist/utils/ast.js +52 -0
  45. package/dist/utils/display-width.d.ts +2 -0
  46. package/dist/utils/display-width.js +19 -0
  47. package/dist/{shared/markdown.d.ts → utils/format-markdown.d.ts} +1 -1
  48. package/dist/{shared/markdown.js → utils/format-markdown.js} +12 -2
  49. package/dist/utils/indentation.d.ts +4 -0
  50. package/dist/utils/indentation.js +21 -0
  51. package/dist/utils/replacements.d.ts +6 -0
  52. package/dist/utils/replacements.js +21 -0
  53. package/dist/utils/source-lines.d.ts +6 -0
  54. package/dist/utils/source-lines.js +40 -0
  55. package/dist/utils/type-guards.d.ts +2 -0
  56. package/dist/utils/type-guards.js +6 -0
  57. package/dist/utils/whitespace.d.ts +2 -0
  58. package/dist/utils/whitespace.js +22 -0
  59. package/dist/{shared/options.d.ts → utils/wrap-options.d.ts} +2 -1
  60. package/package.json +17 -9
  61. package/dist/comments/core.d.ts +0 -8
  62. package/dist/comments/core.js +0 -114
  63. package/dist/comments/line.d.ts +0 -6
  64. package/dist/comments/line.js +0 -108
  65. package/dist/comments/wrap.d.ts +0 -3
  66. package/dist/comments/wrap.js +0 -375
  67. package/dist/plugin/parsers.d.ts +0 -2
  68. package/dist/plugin/parsers.js +0 -35
  69. package/dist/plugin/printers.d.ts +0 -2
  70. package/dist/shared/text.d.ts +0 -15
  71. package/dist/shared/text.js +0 -99
  72. package/dist/shared/types.d.ts +0 -30
  73. package/dist/shared/types.js +0 -1
  74. /package/dist/{shared/options.js → utils/wrap-options.js} +0 -0
@@ -1,108 +0,0 @@
1
- import { hasPreserveCommentMarker, isDirectiveComment, normalizeLineCommentBody } from './core.js';
2
- import { formatMarkdownLines } from '../shared/markdown.js';
3
- import { getAvailableContentWidth, getPrintWidth, getTabWidth } from '../shared/options.js';
4
- import { getColumnAt, getColumns, getContinuationIndent, getLineEnd, getLinePrefix, getLineStart, getPreferredNewline, makeIndent, } from '../shared/text.js';
5
- export async function wrapLineCommentGroup(text, comments, options) {
6
- const firstComment = comments[0];
7
- if (firstComment === undefined) {
8
- return undefined;
9
- }
10
- const lastComment = comments.at(-1) ?? firstComment;
11
- const bodyLines = comments.map((comment) => normalizeLineCommentBody(text.slice(comment.start + 2, comment.end)));
12
- if (bodyLines.every((line) => line.trim() === '')) {
13
- return undefined;
14
- }
15
- const tabWidth = getTabWidth(options);
16
- const markerColumn = getColumnAt(text, firstComment.start, tabWidth);
17
- const availableWidth = getAvailableContentWidth(options, markerColumn + 3);
18
- const formattedLines = await formatMarkdownLines(bodyLines.join('\n'), availableWidth, options);
19
- const newline = getPreferredNewline(text, options);
20
- const continuationIndent = getContinuationIndent(text, firstComment.start, markerColumn, options);
21
- const replacementText = formattedLines
22
- .map((line, index) => {
23
- const commentText = line.length === 0 ? '//' : `// ${line}`;
24
- return index === 0 ? commentText : `${newline}${continuationIndent}${commentText}`;
25
- })
26
- .join('');
27
- const start = firstComment.start;
28
- const end = lastComment.end;
29
- if (replacementText === text.slice(start, end)) {
30
- return undefined;
31
- }
32
- return {
33
- end,
34
- start,
35
- text: replacementText,
36
- };
37
- }
38
- export async function wrapTrailingLineComment(text, comment, options) {
39
- if (isTrailingLineCommentWithinPrintWidth(text, comment, options)) {
40
- return undefined;
41
- }
42
- const lineStart = getLineStart(text, comment.start);
43
- const lineEnd = getLineEnd(text, comment.end);
44
- const linePrefix = text.slice(lineStart, comment.start);
45
- const codeText = linePrefix.replace(/[ \t]+$/u, '');
46
- if (codeText.trim() === '') {
47
- return undefined;
48
- }
49
- const body = normalizeLineCommentBody(text.slice(comment.start + 2, comment.end));
50
- if (body.trim() === '') {
51
- return undefined;
52
- }
53
- const tabWidth = getTabWidth(options);
54
- const indent = getTrailingCommentIndent(codeText, linePrefix, options);
55
- const availableWidth = getAvailableContentWidth(options, getColumns(indent, tabWidth) + 3);
56
- const formattedLines = await formatMarkdownLines(body, availableWidth, options);
57
- const newline = getPreferredNewline(text, options);
58
- const leadingCommentText = formattedLines
59
- .map((line) => `${indent}${line.length === 0 ? '//' : `// ${line}`}`)
60
- .join(newline);
61
- const codeEnd = lineStart + codeText.length;
62
- return [
63
- {
64
- end: lineStart,
65
- start: lineStart,
66
- text: `${leadingCommentText}${newline}`,
67
- },
68
- {
69
- end: lineEnd,
70
- start: codeEnd,
71
- text: '',
72
- },
73
- ];
74
- }
75
- export function shouldSkipLineComment(text, comment) {
76
- const raw = text.slice(comment.start, comment.end);
77
- if (raw.startsWith('///') || hasPreserveCommentMarker(raw)) {
78
- return true;
79
- }
80
- return isDirectiveComment(normalizeLineCommentBody(raw.slice(2)));
81
- }
82
- export function isStandaloneLineComment(text, comment) {
83
- return /^[ \t]*$/u.test(getLinePrefix(text, comment.start));
84
- }
85
- export function areAdjacentLineComments(text, previous, next) {
86
- return /^(?:\r\n|\n|\r)[ \t]*$/u.test(text.slice(previous.end, next.start));
87
- }
88
- function isTrailingLineCommentWithinPrintWidth(text, comment, options) {
89
- const tabWidth = getTabWidth(options);
90
- const lineStart = getLineStart(text, comment.start);
91
- const lineEnd = getLineEnd(text, comment.end);
92
- const lineText = text.slice(lineStart, lineEnd).replace(/[ \t]+$/u, '');
93
- return getColumns(lineText, tabWidth) <= getPrintWidth(options);
94
- }
95
- function getLineIndent(linePrefix) {
96
- return /^[ \t]*/u.exec(linePrefix)?.[0] ?? '';
97
- }
98
- function getTrailingCommentIndent(codeText, linePrefix, options) {
99
- const indent = getLineIndent(linePrefix);
100
- if (!isClosingDelimiterLine(codeText)) {
101
- return indent;
102
- }
103
- const tabWidth = getTabWidth(options);
104
- return makeIndent(getColumns(indent, tabWidth) + tabWidth, options);
105
- }
106
- function isClosingDelimiterLine(codeText) {
107
- return /^[ \t]*[\])}]+[\])};,]*[ \t]*$/u.test(codeText);
108
- }
@@ -1,3 +0,0 @@
1
- import type { WrapOptions } from '../shared/types.js';
2
- export declare function wrapComments<T>(text: string, ast: T, options: WrapOptions): Promise<string>;
3
- export declare function neutralizePrettierIgnoreForIgnoredBlockComments<T>(text: string, ast: T): T;
@@ -1,375 +0,0 @@
1
- import { wrapBlockComment } from './block.js';
2
- import { collectComments, hasPreserveCommentMarker, isDirectiveComment, isPrettierIgnoreComment, normalizeBlockCommentBody, normalizeLineCommentBody, toCommentRange, } from './core.js';
3
- import { areAdjacentLineComments, isStandaloneLineComment, shouldSkipLineComment, wrapLineCommentGroup, wrapTrailingLineComment, } from './line.js';
4
- import { getTabWidth } from '../shared/options.js';
5
- import { applyReplacements, getColumnAt, getColumns, getLineEnd, getLinePrefix, getLineStart, isStandaloneBlockComment, } from '../shared/text.js';
6
- const NEUTRALIZED_PRETTIER_IGNORE_COMMENT = 'prettier-ignore wrap-comments';
7
- const AST_TRAVERSAL_SKIP_KEYS = new Set([
8
- 'comments',
9
- 'errors',
10
- 'innerComments',
11
- 'leadingComments',
12
- 'loc',
13
- 'parent',
14
- 'range',
15
- 'tokens',
16
- 'trailingComments',
17
- ]);
18
- export async function wrapComments(text, ast, options) {
19
- const commentEntries = collectSortedCommentEntries(ast, text);
20
- const comments = commentEntries.map((entry) => entry.range);
21
- const jsxExpressionContainers = collectJsxExpressionContainerRanges(ast);
22
- const ignoredLineRanges = collectPrettierIgnoredLineRanges(text, ast, commentEntries);
23
- if (comments.length === 0) {
24
- return text;
25
- }
26
- const replacements = [];
27
- const tabWidth = getTabWidth(options);
28
- for (let index = 0; index < comments.length; index += 1) {
29
- const comment = comments[index];
30
- if (comment === undefined) {
31
- continue;
32
- }
33
- if (isCommentInIgnoredLineRange(comment, ignoredLineRanges)) {
34
- continue;
35
- }
36
- if (comment.kind === 'block') {
37
- if (isPrettierIgnoredBlockComment(text, commentEntries, index)) {
38
- continue;
39
- }
40
- const jsxLayout = getJsxExpressionBlockCommentLayout(text, comment, jsxExpressionContainers, tabWidth);
41
- if (jsxLayout?.placement === 'inline') {
42
- continue;
43
- }
44
- const replacement = await wrapBlockComment(text, comment, options, jsxLayout);
45
- if (Array.isArray(replacement)) {
46
- replacements.push(...replacement);
47
- }
48
- else if (replacement !== undefined) {
49
- replacements.push(replacement);
50
- }
51
- continue;
52
- }
53
- if (shouldSkipLineComment(text, comment)) {
54
- continue;
55
- }
56
- if (!isStandaloneLineComment(text, comment)) {
57
- if (isPrettierIgnoredTrailingLineComment(text, commentEntries, index)) {
58
- continue;
59
- }
60
- const replacement = await wrapTrailingLineComment(text, comment, options);
61
- if (replacement !== undefined) {
62
- replacements.push(...replacement);
63
- }
64
- continue;
65
- }
66
- const group = [comment];
67
- let previousComment = comment;
68
- while (index + 1 < comments.length) {
69
- const nextComment = comments[index + 1];
70
- if (nextComment === undefined) {
71
- break;
72
- }
73
- if (nextComment.kind !== 'line' ||
74
- !isStandaloneLineComment(text, nextComment) ||
75
- shouldSkipLineComment(text, nextComment) ||
76
- !areAdjacentLineComments(text, previousComment, nextComment) ||
77
- getColumnAt(text, comment.start, tabWidth) !== getColumnAt(text, nextComment.start, tabWidth)) {
78
- break;
79
- }
80
- group.push(nextComment);
81
- previousComment = nextComment;
82
- index += 1;
83
- }
84
- const replacement = await wrapLineCommentGroup(text, group, options);
85
- if (replacement !== undefined) {
86
- replacements.push(replacement);
87
- }
88
- }
89
- return applyReplacements(text, replacements);
90
- }
91
- export function neutralizePrettierIgnoreForIgnoredBlockComments(text, ast) {
92
- const comments = collectSortedCommentEntries(ast, text);
93
- for (let index = 0; index < comments.length; index += 1) {
94
- const entry = comments[index];
95
- const previousEntry = comments[index - 1];
96
- if (entry !== undefined &&
97
- previousEntry !== undefined &&
98
- isPrettierIgnoredBlockComment(text, comments, index) &&
99
- !isBlockCommentNormallyIgnored(text, entry.range)) {
100
- previousEntry.raw.value = NEUTRALIZED_PRETTIER_IGNORE_COMMENT;
101
- }
102
- }
103
- return ast;
104
- }
105
- function collectSortedCommentEntries(ast, text) {
106
- return collectComments(ast)
107
- .map((raw) => ({ range: toCommentRange(raw, text), raw }))
108
- .filter((entry) => entry.range !== undefined)
109
- .sort((left, right) => left.range.start - right.range.start);
110
- }
111
- function collectPrettierIgnoredLineRanges(text, ast, comments) {
112
- const nodeRanges = collectAstNodeRanges(ast);
113
- const ignoredLineRanges = [];
114
- for (let index = 0; index < comments.length; index += 1) {
115
- const comment = comments[index]?.range;
116
- if (comment === undefined ||
117
- !isStandaloneComment(text, comment) ||
118
- !isPrettierIgnoreComment(getCommentBody(text, comment))) {
119
- continue;
120
- }
121
- const targetStart = getPrettierIgnoreTargetStart(text, comments, index);
122
- if (targetStart === undefined) {
123
- continue;
124
- }
125
- const targetRange = nodeRanges.find((range) => range.start === targetStart);
126
- if (targetRange === undefined) {
127
- continue;
128
- }
129
- ignoredLineRanges.push({
130
- end: getLineEnd(text, targetRange.end),
131
- start: getLineStart(text, targetRange.start),
132
- });
133
- }
134
- return ignoredLineRanges;
135
- }
136
- function collectAstNodeRanges(ast) {
137
- const ranges = [];
138
- const seen = new Set();
139
- visit(ast);
140
- return ranges.sort((left, right) => left.start - right.start || right.end - left.end);
141
- function visit(value) {
142
- if (!isRecord(value) || seen.has(value)) {
143
- return;
144
- }
145
- seen.add(value);
146
- const range = getAstNodeRange(value);
147
- if (range !== undefined && typeof value['type'] === 'string') {
148
- ranges.push(range);
149
- }
150
- for (const [key, child] of Object.entries(value)) {
151
- if (AST_TRAVERSAL_SKIP_KEYS.has(key)) {
152
- continue;
153
- }
154
- if (Array.isArray(child)) {
155
- for (const item of child) {
156
- visit(item);
157
- }
158
- }
159
- else {
160
- visit(child);
161
- }
162
- }
163
- }
164
- }
165
- function collectJsxExpressionContainerRanges(ast) {
166
- const ranges = [];
167
- const seen = new Set();
168
- visit(ast);
169
- return ranges.sort((left, right) => left.start - right.start || right.end - left.end);
170
- function visit(value) {
171
- if (!isRecord(value) || seen.has(value)) {
172
- return;
173
- }
174
- seen.add(value);
175
- if (value['type'] === 'JSXExpressionContainer') {
176
- const range = getAstNodeRange(value);
177
- if (range !== undefined) {
178
- ranges.push(range);
179
- }
180
- }
181
- for (const [key, child] of Object.entries(value)) {
182
- if (AST_TRAVERSAL_SKIP_KEYS.has(key)) {
183
- continue;
184
- }
185
- if (Array.isArray(child)) {
186
- for (const item of child) {
187
- visit(item);
188
- }
189
- }
190
- else {
191
- visit(child);
192
- }
193
- }
194
- }
195
- }
196
- function getAstNodeRange(node) {
197
- const start = numberOrUndefined(node['start']) ?? getRangeNumber(node['range'], 0);
198
- const end = numberOrUndefined(node['end']) ?? getRangeNumber(node['range'], 1);
199
- if (start === undefined || end === undefined || start >= end) {
200
- return undefined;
201
- }
202
- return { end, start };
203
- }
204
- function getRangeNumber(range, index) {
205
- if (!Array.isArray(range)) {
206
- return undefined;
207
- }
208
- return numberOrUndefined(range[index]);
209
- }
210
- function getPrettierIgnoreTargetStart(text, comments, ignoreCommentIndex) {
211
- const ignoreComment = comments[ignoreCommentIndex]?.range;
212
- if (ignoreComment === undefined) {
213
- return undefined;
214
- }
215
- let cursor = ignoreComment.end;
216
- for (let index = ignoreCommentIndex + 1; index < comments.length; index += 1) {
217
- cursor = skipWhitespace(text, cursor);
218
- const comment = comments[index]?.range;
219
- if (comment === undefined || comment.start !== cursor) {
220
- break;
221
- }
222
- if (!isStandaloneComment(text, comment) || !isCommentNormallyIgnored(text, comment)) {
223
- return undefined;
224
- }
225
- cursor = comment.end;
226
- }
227
- const targetStart = skipWhitespace(text, cursor);
228
- return targetStart >= text.length ? undefined : targetStart;
229
- }
230
- function skipWhitespace(text, index) {
231
- let cursor = index;
232
- while (cursor < text.length) {
233
- const character = text[cursor];
234
- if (character === undefined || !/\s/u.test(character)) {
235
- break;
236
- }
237
- cursor += 1;
238
- }
239
- return cursor;
240
- }
241
- function isCommentInIgnoredLineRange(comment, ignoredLineRanges) {
242
- return ignoredLineRanges.some((range) => comment.start >= range.start && comment.start < range.end);
243
- }
244
- function getJsxExpressionBlockCommentLayout(text, comment, jsxExpressionContainers, tabWidth) {
245
- const container = getSmallestContainingRange(comment, jsxExpressionContainers);
246
- if (container === undefined || text[container.start] !== '{' || text[container.end - 1] !== '}') {
247
- return undefined;
248
- }
249
- const expressionTextBeforeComment = text.slice(container.start + 1, comment.start).trim();
250
- const expressionTextAfterComment = text.slice(comment.end, container.end - 1).trim();
251
- const containerOutputColumn = getJsxExpressionContainerOutputColumn(text, container, tabWidth);
252
- if (expressionTextBeforeComment === '' && expressionTextAfterComment === '') {
253
- return { contentColumn: containerOutputColumn + tabWidth + 3, multilineIndent: '', placement: 'standalone' };
254
- }
255
- if (expressionTextBeforeComment !== '' && expressionTextAfterComment === '') {
256
- const expressionStart = skipWhitespace(text, container.start + 1);
257
- const expressionEnd = trimWhitespaceEnd(text, container.start + 1, comment.start);
258
- const removalEnd = Math.min(skipWhitespace(text, comment.end), container.end - 1);
259
- return {
260
- contentColumn: containerOutputColumn + tabWidth + 3,
261
- multilineIndent: '',
262
- placement: 'trailing',
263
- trailingMove: {
264
- insertAt: expressionStart,
265
- removeEnd: removalEnd,
266
- removeStart: expressionEnd,
267
- },
268
- };
269
- }
270
- return { placement: 'inline' };
271
- }
272
- function getJsxExpressionContainerOutputColumn(text, container, tabWidth) {
273
- const linePrefix = getLinePrefix(text, container.start);
274
- if (/^[ \t]*$/u.test(linePrefix)) {
275
- return getColumns(linePrefix, tabWidth);
276
- }
277
- const lineIndent = /^[ \t]*/u.exec(linePrefix)?.[0] ?? '';
278
- return getColumns(lineIndent, tabWidth) + tabWidth;
279
- }
280
- function trimWhitespaceEnd(text, start, end) {
281
- let cursor = end;
282
- while (cursor > start) {
283
- const character = text[cursor - 1];
284
- if (character === undefined || !/\s/u.test(character)) {
285
- break;
286
- }
287
- cursor -= 1;
288
- }
289
- return cursor;
290
- }
291
- function getSmallestContainingRange(comment, ranges) {
292
- let containingRange;
293
- for (const range of ranges) {
294
- if (comment.start <= range.start || comment.end >= range.end) {
295
- continue;
296
- }
297
- if (containingRange === undefined || range.end - range.start < containingRange.end - containingRange.start) {
298
- containingRange = range;
299
- }
300
- }
301
- return containingRange;
302
- }
303
- function isPrettierIgnoredBlockComment(text, comments, index) {
304
- const comment = comments[index]?.range;
305
- const previousComment = comments[index - 1]?.range;
306
- if (comment === undefined || comment.kind !== 'block' || previousComment === undefined) {
307
- return false;
308
- }
309
- if (!isStandaloneComment(text, previousComment) || !isAdjacentPreviousComment(text, previousComment, comment)) {
310
- return false;
311
- }
312
- return isPrettierIgnoreComment(getCommentBody(text, previousComment));
313
- }
314
- function isPrettierIgnoredTrailingLineComment(text, comments, index) {
315
- const comment = comments[index]?.range;
316
- if (comment === undefined || comment.kind !== 'line' || isStandaloneLineComment(text, comment)) {
317
- return false;
318
- }
319
- let cursor = getLineStart(text, comment.start);
320
- for (let previousIndex = index - 1; previousIndex >= 0; previousIndex -= 1) {
321
- const previousComment = comments[previousIndex]?.range;
322
- if (previousComment !== undefined && previousComment.end > cursor) {
323
- continue;
324
- }
325
- if (previousComment === undefined || !isStandaloneComment(text, previousComment)) {
326
- return false;
327
- }
328
- if (!isAdjacentCommentBeforeIndex(text, previousComment, cursor)) {
329
- return false;
330
- }
331
- const body = getCommentBody(text, previousComment);
332
- if (previousComment.kind === 'line' && isPrettierIgnoreComment(body)) {
333
- return true;
334
- }
335
- if (!isCommentNormallyIgnored(text, previousComment)) {
336
- return false;
337
- }
338
- cursor = getLineStart(text, previousComment.start);
339
- }
340
- return false;
341
- }
342
- function isCommentNormallyIgnored(text, comment) {
343
- if (comment.kind === 'line') {
344
- const raw = text.slice(comment.start, comment.end);
345
- return shouldSkipLineComment(text, comment) && !isPrettierIgnoreComment(normalizeLineCommentBody(raw.slice(2)));
346
- }
347
- return isBlockCommentNormallyIgnored(text, comment);
348
- }
349
- function isBlockCommentNormallyIgnored(text, comment) {
350
- const raw = text.slice(comment.start, comment.end);
351
- if (raw.startsWith('/**') || hasPreserveCommentMarker(raw)) {
352
- return true;
353
- }
354
- const body = normalizeBlockCommentBody(raw);
355
- return body.trim() === '' || isDirectiveComment(body);
356
- }
357
- function isStandaloneComment(text, comment) {
358
- return comment.kind === 'line' ? isStandaloneLineComment(text, comment) : isStandaloneBlockComment(text, comment);
359
- }
360
- function isAdjacentPreviousComment(text, previousComment, comment) {
361
- return /^(?:\r\n|\n|\r)[ \t]*$/u.test(text.slice(previousComment.end, comment.start));
362
- }
363
- function isAdjacentCommentBeforeIndex(text, comment, index) {
364
- return /^(?:\r\n|\n|\r)[ \t]*$/u.test(text.slice(comment.end, index));
365
- }
366
- function getCommentBody(text, comment) {
367
- const raw = text.slice(comment.start, comment.end);
368
- return comment.kind === 'line' ? normalizeLineCommentBody(raw.slice(2)) : normalizeBlockCommentBody(raw);
369
- }
370
- function isRecord(value) {
371
- return typeof value === 'object' && value !== null;
372
- }
373
- function numberOrUndefined(value) {
374
- return typeof value === 'number' ? value : undefined;
375
- }
@@ -1,2 +0,0 @@
1
- import type { Plugin } from 'prettier';
2
- export declare function buildParsers(): Plugin['parsers'];
@@ -1,35 +0,0 @@
1
- import * as babelPlugin from 'prettier/plugins/babel';
2
- import * as typescriptPlugin from 'prettier/plugins/typescript';
3
- import { neutralizePrettierIgnoreForIgnoredBlockComments, wrapComments } from '../comments/wrap.js';
4
- const parserNames = ['babel', 'babel-ts', 'typescript'];
5
- function wrapParser(parser) {
6
- return {
7
- ...parser,
8
- async parse(text, options) {
9
- const ast = await parser.parse(text, options);
10
- return neutralizePrettierIgnoreForIgnoredBlockComments(text, ast);
11
- },
12
- async preprocess(text, options) {
13
- const preprocessed = parser.preprocess === undefined ? text : await parser.preprocess(text, options);
14
- let ast;
15
- try {
16
- ast = await parser.parse(preprocessed, options);
17
- }
18
- catch {
19
- return preprocessed;
20
- }
21
- return wrapComments(preprocessed, ast, options);
22
- },
23
- };
24
- }
25
- export function buildParsers() {
26
- const parsers = {};
27
- for (const parserName of parserNames) {
28
- const sourceParsers = parserName === 'typescript' ? typescriptPlugin.parsers : babelPlugin.parsers;
29
- const parser = sourceParsers[parserName];
30
- if (parser !== undefined) {
31
- parsers[parserName] = wrapParser(parser);
32
- }
33
- }
34
- return parsers;
35
- }
@@ -1,2 +0,0 @@
1
- import type { Plugin } from 'prettier';
2
- export declare function buildPrinters(): Plugin['printers'];
@@ -1,15 +0,0 @@
1
- import type { Replacement, WrapOptions } from './types.js';
2
- export declare function applyReplacements(text: string, replacements: Replacement[]): string;
3
- export declare function getPreferredNewline(text: string, options: WrapOptions): string;
4
- export declare function getContinuationIndent(text: string, commentStart: number, markerColumn: number, options: WrapOptions): string;
5
- export declare function getLinePrefix(text: string, index: number): string;
6
- export declare function getLineStart(text: string, index: number): number;
7
- export declare function getLineEnd(text: string, index: number): number;
8
- export declare function getColumnAt(text: string, index: number, tabWidth: number): number;
9
- export declare function getColumns(text: string, tabWidth: number): number;
10
- export declare function isStandaloneBlockComment(text: string, comment: {
11
- end: number;
12
- start: number;
13
- }): boolean;
14
- export declare function trimBlankEdges(markdown: string): string;
15
- export declare function makeIndent(column: number, options: WrapOptions): string;
@@ -1,99 +0,0 @@
1
- import { getTabWidth } from './options.js';
2
- export function applyReplacements(text, replacements) {
3
- let result = text;
4
- for (const replacement of getNonOverlappingReplacements(replacements).sort((left, right) => right.start - left.start)) {
5
- result = result.slice(0, replacement.start) + replacement.text + result.slice(replacement.end);
6
- }
7
- return result;
8
- }
9
- function getNonOverlappingReplacements(replacements) {
10
- return [...replacements]
11
- .sort((left, right) => left.start - right.start || right.end - left.end)
12
- .reduce((accepted, replacement) => {
13
- const previous = accepted.at(-1);
14
- if (previous === undefined || !rangesOverlap(previous, replacement)) {
15
- accepted.push(replacement);
16
- return accepted;
17
- }
18
- return accepted;
19
- }, []);
20
- }
21
- function rangesOverlap(left, right) {
22
- return left.start < right.end && right.start < left.end;
23
- }
24
- export function getPreferredNewline(text, options) {
25
- if (options.endOfLine === 'crlf') {
26
- return '\r\n';
27
- }
28
- if (options.endOfLine === 'cr') {
29
- return '\r';
30
- }
31
- if (options.endOfLine === 'auto') {
32
- const match = /\r\n|\n|\r/u.exec(text);
33
- return match?.[0] ?? '\n';
34
- }
35
- return '\n';
36
- }
37
- export function getContinuationIndent(text, commentStart, markerColumn, options) {
38
- const linePrefix = getLinePrefix(text, commentStart);
39
- if (/^[ \t]*$/u.test(linePrefix)) {
40
- return linePrefix;
41
- }
42
- return makeIndent(markerColumn, options);
43
- }
44
- export function getLinePrefix(text, index) {
45
- return text.slice(getLineStart(text, index), index);
46
- }
47
- export function getLineStart(text, index) {
48
- const newlineIndex = text.lastIndexOf('\n', index - 1);
49
- return newlineIndex === -1 ? 0 : newlineIndex + 1;
50
- }
51
- export function getLineEnd(text, index) {
52
- const newlineIndex = text.indexOf('\n', index);
53
- if (newlineIndex === -1) {
54
- return text.length;
55
- }
56
- return text[newlineIndex - 1] === '\r' ? newlineIndex - 1 : newlineIndex;
57
- }
58
- export function getColumnAt(text, index, tabWidth) {
59
- return getColumns(text.slice(getLineStart(text, index), index), tabWidth);
60
- }
61
- export function getColumns(text, tabWidth) {
62
- let column = 0;
63
- for (const character of text) {
64
- if (character === '\t') {
65
- column += tabWidth - (column % tabWidth);
66
- }
67
- else {
68
- column += 1;
69
- }
70
- }
71
- return column;
72
- }
73
- export function isStandaloneBlockComment(text, comment) {
74
- const before = text.slice(getLineStart(text, comment.start), comment.start);
75
- const after = text.slice(comment.end, getLineEnd(text, comment.end));
76
- return /^[ \t]*$/u.test(before) && /^[ \t]*$/u.test(after);
77
- }
78
- export function trimBlankEdges(markdown) {
79
- const lines = markdown.split('\n');
80
- while (isBlankLine(lines[0])) {
81
- lines.shift();
82
- }
83
- while (isBlankLine(lines.at(-1))) {
84
- lines.pop();
85
- }
86
- return lines.join('\n');
87
- }
88
- function isBlankLine(line) {
89
- return line !== undefined && line.trim() === '';
90
- }
91
- export function makeIndent(column, options) {
92
- const tabWidth = getTabWidth(options);
93
- if (options.useTabs === true) {
94
- const tabs = Math.floor(column / tabWidth);
95
- const spaces = column % tabWidth;
96
- return `${'\t'.repeat(tabs)}${' '.repeat(spaces)}`;
97
- }
98
- return ' '.repeat(column);
99
- }
@@ -1,30 +0,0 @@
1
- import type { ParserOptions } from 'prettier';
2
- export type AstWithComments = {
3
- comments?: unknown;
4
- program?: {
5
- comments?: unknown;
6
- };
7
- };
8
- export type RawComment = {
9
- end?: unknown;
10
- loc?: {
11
- start?: {
12
- column?: unknown;
13
- };
14
- };
15
- range?: unknown;
16
- start?: unknown;
17
- type?: unknown;
18
- value?: unknown;
19
- };
20
- export type CommentRange = {
21
- end: number;
22
- kind: 'block' | 'line';
23
- start: number;
24
- };
25
- export type Replacement = {
26
- end: number;
27
- start: number;
28
- text: string;
29
- };
30
- export type WrapOptions = Pick<ParserOptions, 'endOfLine' | 'printWidth' | 'tabWidth' | 'useTabs'>;
@@ -1 +0,0 @@
1
- export {};