@kongyo2/ts-comment-scanner 1.9.0 → 1.10.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/README.en.md +3 -1
- package/README.md +3 -1
- package/dist/jsx.d.ts +54 -0
- package/dist/jsx.d.ts.map +1 -0
- package/dist/jsx.js +193 -0
- package/dist/jsx.js.map +1 -0
- package/dist/ranges.d.ts +10 -0
- package/dist/ranges.d.ts.map +1 -0
- package/dist/ranges.js +19 -0
- package/dist/ranges.js.map +1 -0
- package/dist/remove.d.ts +15 -4
- package/dist/remove.d.ts.map +1 -1
- package/dist/remove.js +325 -187
- package/dist/remove.js.map +1 -1
- package/dist/scanner.d.ts +7 -0
- package/dist/scanner.d.ts.map +1 -1
- package/dist/scanner.js +9 -1
- package/dist/scanner.js.map +1 -1
- package/dist/splice.d.ts +48 -0
- package/dist/splice.d.ts.map +1 -0
- package/dist/splice.js +279 -0
- package/dist/splice.js.map +1 -0
- package/package.json +1 -1
- package/src/jsx.ts +263 -0
- package/src/ranges.ts +23 -0
- package/src/remove.ts +394 -192
- package/src/scanner.ts +16 -1
- package/src/splice.ts +317 -0
package/src/remove.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { activePositionalDirectives, bunMarkerOffset, isLegalComment, type DirectivePlacement } from "./directives.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import type { JsxContainer } from "./jsx.js";
|
|
3
|
+
import { LINE_TERMINATOR, isBlank, lineStartOffsets } from "./lines.js";
|
|
4
|
+
import { firstIndexAtOrAfter, type Range } from "./ranges.js";
|
|
5
|
+
import { analyzeSource } from "./scanner.js";
|
|
6
|
+
import { applyEdits, blankLineEdits, type Edit } from "./splice.js";
|
|
4
7
|
import type { Comment } from "./types.js";
|
|
5
8
|
|
|
6
9
|
export interface RemoveOptions {
|
|
@@ -20,10 +23,10 @@ export interface RemoveResult {
|
|
|
20
23
|
/**
|
|
21
24
|
* Comments kept because they are protected: directives, license headers,
|
|
22
25
|
* comments whose removal would shift code under a next-line directive or
|
|
23
|
-
* that sit between a directive and the code it reaches past them for
|
|
24
|
-
* comments whose removal would move a
|
|
25
|
-
* (prettier's first-comment pragma, Deno's
|
|
26
|
-
* file-start marker) into effect.
|
|
26
|
+
* that sit between a directive and the code it reaches past them for (or
|
|
27
|
+
* the JSX sibling it applies to), and comments whose removal would move a
|
|
28
|
+
* position-dependent directive (prettier's first-comment pragma, Deno's
|
|
29
|
+
* coverage file pragma, Bun's file-start marker) into effect.
|
|
27
30
|
*/
|
|
28
31
|
kept: Comment[];
|
|
29
32
|
/** Comments excluded from removal by the `shouldRemove` predicate. */
|
|
@@ -45,6 +48,17 @@ export interface RemoveResult {
|
|
|
45
48
|
* shebang line), the leading comment is kept instead.
|
|
46
49
|
* - A space is inserted where removing a block comment would merge two tokens,
|
|
47
50
|
* and a line break is kept where the comment acted as one (ASI stays intact).
|
|
51
|
+
* - A JSX comment goes with its braces (`{/* note */}`), unless that would
|
|
52
|
+
* change what the element renders: the braces split the text around them
|
|
53
|
+
* into separate strings, so where they stand between text that renders,
|
|
54
|
+
* `{" "}` takes their place when that keeps the strings identical, and
|
|
55
|
+
* otherwise the braces stay as `{}`. The output is re-parsed to prove every
|
|
56
|
+
* element renders the same strings, under the whitespace rules of
|
|
57
|
+
* TypeScript, esbuild and Babel alike.
|
|
58
|
+
* - Blank lines that only separated a removed comment from the code around it
|
|
59
|
+
* go with it: no pile-up of blank lines where a comment stood between two
|
|
60
|
+
* of them, and at the start or end of the file or of a block only the blank
|
|
61
|
+
* lines already against that edge stay.
|
|
48
62
|
* - A leading byte-order mark is preserved. Reported positions index into the
|
|
49
63
|
* `source` string exactly as `scanComments(source)` reports them (a BOM
|
|
50
64
|
* counts as its first character).
|
|
@@ -53,7 +67,8 @@ export interface RemoveResult {
|
|
|
53
67
|
*/
|
|
54
68
|
export function removeComments(source: string, options: RemoveOptions = {}): RemoveResult {
|
|
55
69
|
const jsx = options.jsx === true;
|
|
56
|
-
const
|
|
70
|
+
const analysis = analyzeSource(source, { jsx });
|
|
71
|
+
const comments = analysis.comments;
|
|
57
72
|
|
|
58
73
|
let removed: Comment[] = [];
|
|
59
74
|
const kept: Comment[] = [];
|
|
@@ -68,18 +83,45 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
|
|
|
68
83
|
}
|
|
69
84
|
}
|
|
70
85
|
|
|
86
|
+
const containers = attachContainers(source, analysis.jsx.containers, comments);
|
|
87
|
+
const containerOf = new Map<Comment, Container>();
|
|
88
|
+
for (const container of containers) {
|
|
89
|
+
for (const comment of container.comments) containerOf.set(comment, container);
|
|
90
|
+
}
|
|
91
|
+
const context: ShieldContext = { containerOf, skippable: skippableRanges(comments, containerOf, new Set(removed)) };
|
|
92
|
+
const search = containerSearch(containers);
|
|
93
|
+
// Collapsing blank lines can only move a surviving comment to the very
|
|
94
|
+
// start of the file (or of the line after a shebang), where Bun reads its
|
|
95
|
+
// marker; when that changes a survivor's meaning, the attempt is repeated
|
|
96
|
+
// without the collapse before any removal is given up.
|
|
97
|
+
let tidyBlankLines = true;
|
|
98
|
+
|
|
71
99
|
for (;;) {
|
|
72
|
-
|
|
100
|
+
const shielding = shieldNextLineDirectives(source, removed, kept, skipped, context);
|
|
101
|
+
removed = shielding.removed;
|
|
73
102
|
|
|
74
103
|
if (removed.length === 0) {
|
|
75
104
|
return { code: source, removed, kept, skipped, changed: false };
|
|
76
105
|
}
|
|
77
106
|
|
|
78
|
-
const
|
|
79
|
-
|
|
107
|
+
const removing = new Set(removed);
|
|
108
|
+
search.track(new Set(containers.filter((container) => container.comments.every((c) => removing.has(c)))));
|
|
109
|
+
const edits = removalEdits(removed, containerOf, search.droppable);
|
|
110
|
+
const deletions = edits.filter((edit) => edit.replacement === undefined);
|
|
111
|
+
const blankEdits = tidyBlankLines ? blankLineEdits(source, deletions, comments, shielding.lines, analysis.jsx) : [];
|
|
112
|
+
const code = editKeepingBom(source, [...edits, ...blankEdits].sort(byStart));
|
|
113
|
+
const output = analyzeSource(code, { jsx });
|
|
114
|
+
|
|
115
|
+
if (search.adjust(changedSegments(analysis.jsx.segments, output.jsx.segments))) continue;
|
|
116
|
+
|
|
117
|
+
const survivor = firstChangedSurvivor(source, code, output.comments, kept, skipped);
|
|
80
118
|
if (survivor === undefined) {
|
|
81
119
|
return { code, removed, kept, skipped, changed: true };
|
|
82
120
|
}
|
|
121
|
+
if (blankEdits.length > 0) {
|
|
122
|
+
tidyBlankLines = false;
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
83
125
|
|
|
84
126
|
// A surviving comment changed meaning: with everything before it deleted
|
|
85
127
|
// it became the file's first comment (or its very first bytes), turning a
|
|
@@ -95,6 +137,7 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
|
|
|
95
137
|
removed = removed.filter((comment) => comment !== blocker);
|
|
96
138
|
kept.push(blocker);
|
|
97
139
|
kept.sort(byStart);
|
|
140
|
+
tidyBlankLines = true;
|
|
98
141
|
}
|
|
99
142
|
}
|
|
100
143
|
|
|
@@ -102,8 +145,8 @@ function unexpectedResult(): Error {
|
|
|
102
145
|
return new Error("comment removal produced an unexpected result; refusing to continue");
|
|
103
146
|
}
|
|
104
147
|
|
|
105
|
-
/** Sort comparator for document order:
|
|
106
|
-
function byStart(a:
|
|
148
|
+
/** Sort comparator for document order: ranges are ordered by where they start. */
|
|
149
|
+
function byStart(a: Range, b: Range): number {
|
|
107
150
|
return a.start - b.start;
|
|
108
151
|
}
|
|
109
152
|
|
|
@@ -170,19 +213,18 @@ function closestRemovalBefore(removals: readonly Comment[], comment: Comment): C
|
|
|
170
213
|
}
|
|
171
214
|
|
|
172
215
|
/**
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
216
|
+
* Applies the edits, restoring the byte-order mark when dropping a
|
|
217
|
+
* comment-only first line took it along: the mark is an encoding artifact,
|
|
218
|
+
* not line content.
|
|
176
219
|
*/
|
|
177
|
-
function
|
|
178
|
-
if (!source.startsWith("
|
|
179
|
-
// The
|
|
220
|
+
function editKeepingBom(source: string, edits: readonly Edit[]): string {
|
|
221
|
+
if (!source.startsWith("")) return applyEdits(source, edits);
|
|
222
|
+
// The editing itself works on the BOM-less body (with ranges shifted to
|
|
180
223
|
// match) so its line handling never sees the mark, then the mark is
|
|
181
|
-
// re-attached; this keeps
|
|
224
|
+
// re-attached; this keeps `// gone\n...` from losing the mark with
|
|
182
225
|
// the dropped line.
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
return `\uFEFF${splice(body, shifted)}`;
|
|
226
|
+
const shifted = edits.map((edit) => ({ ...edit, start: edit.start - 1, end: edit.end - 1 }));
|
|
227
|
+
return `${applyEdits(source.slice(1), shifted)}`;
|
|
186
228
|
}
|
|
187
229
|
|
|
188
230
|
function isProtected(comment: Comment, options: RemoveOptions): boolean {
|
|
@@ -191,6 +233,239 @@ function isProtected(comment: Comment, options: RemoveOptions): boolean {
|
|
|
191
233
|
return false;
|
|
192
234
|
}
|
|
193
235
|
|
|
236
|
+
/**
|
|
237
|
+
* How a comment-only JSX container goes once all its comments do: braces
|
|
238
|
+
* and all; replaced by `{" "}` together with the spaces that held it off its
|
|
239
|
+
* neighbour; or with its braces kept, only the comments going.
|
|
240
|
+
*/
|
|
241
|
+
type ContainerMode = "drop" | "space" | "keep";
|
|
242
|
+
|
|
243
|
+
/** A comment-only JSX container (`{/* note */}`) with the comments in it. */
|
|
244
|
+
interface Container extends Range {
|
|
245
|
+
segment: number;
|
|
246
|
+
suspect: boolean;
|
|
247
|
+
/** The comment-only container right after it among the siblings, if any. */
|
|
248
|
+
next: Container | undefined;
|
|
249
|
+
comments: Comment[];
|
|
250
|
+
mode: ContainerMode;
|
|
251
|
+
/** What `{" "}` would replace, when the container shares its line and is held off by spaces. */
|
|
252
|
+
spaceRange: Range | undefined;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function attachContainers(source: string, layouts: readonly JsxContainer[], comments: readonly Comment[]): Container[] {
|
|
256
|
+
const containers = new Map<JsxContainer, Container>();
|
|
257
|
+
for (const layout of layouts) {
|
|
258
|
+
const inside: Comment[] = [];
|
|
259
|
+
let index = firstIndexAtOrAfter(comments, layout.start, (comment) => comment.start);
|
|
260
|
+
for (let comment = comments[index]; comment !== undefined && comment.end < layout.end; comment = comments[index]) {
|
|
261
|
+
inside.push(comment);
|
|
262
|
+
index += 1;
|
|
263
|
+
}
|
|
264
|
+
containers.set(layout, {
|
|
265
|
+
start: layout.start,
|
|
266
|
+
end: layout.end,
|
|
267
|
+
segment: layout.segment,
|
|
268
|
+
suspect: layout.suspect,
|
|
269
|
+
next: undefined,
|
|
270
|
+
comments: inside,
|
|
271
|
+
mode: "drop",
|
|
272
|
+
spaceRange: spaceRange(source, layout),
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
for (const [layout, container] of containers) {
|
|
276
|
+
if (layout.next !== undefined) container.next = containers.get(layout.next);
|
|
277
|
+
}
|
|
278
|
+
return [...containers.values()];
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const isHorizontalSpace = (char: string | undefined): boolean => char === " " || char === "\t";
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* The range `{" "}` would take the place of: a one-line container and the
|
|
285
|
+
* spaces before it when code precedes it on its line (`<b /> {/* x */}`), or
|
|
286
|
+
* the spaces after it when it opens its line and code follows. Undefined when
|
|
287
|
+
* no spaces hold it off anything on its line.
|
|
288
|
+
*/
|
|
289
|
+
function spaceRange(source: string, container: Range): Range | undefined {
|
|
290
|
+
if (LINE_TERMINATOR.test(source.slice(container.start, container.end))) return undefined;
|
|
291
|
+
let lineStart = container.start;
|
|
292
|
+
while (lineStart > 0 && !LINE_TERMINATOR.test(source[lineStart - 1] as string)) lineStart -= 1;
|
|
293
|
+
let before = container.start;
|
|
294
|
+
while (isHorizontalSpace(source[before - 1])) before -= 1;
|
|
295
|
+
let after = container.end;
|
|
296
|
+
while (isHorizontalSpace(source[after])) after += 1;
|
|
297
|
+
|
|
298
|
+
if (!isBlank(source.slice(lineStart, container.start))) {
|
|
299
|
+
return before < container.start ? { start: before, end: container.end } : undefined;
|
|
300
|
+
}
|
|
301
|
+
const codeAfter = after < source.length && !LINE_TERMINATOR.test(source[after] as string);
|
|
302
|
+
return codeAfter && after > container.end ? { start: container.start, end: after } : undefined;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Comments, and the comment-only containers that may go along with theirs,
|
|
307
|
+
* as sorted ranges: what the search for the code after a directive skips.
|
|
308
|
+
* A container counts as skippable when all its comments are removal
|
|
309
|
+
* candidates, even if some get protected later — skipping it then only moves
|
|
310
|
+
* the found code further down, which shields more, not less.
|
|
311
|
+
*/
|
|
312
|
+
function skippableRanges(
|
|
313
|
+
comments: readonly Comment[],
|
|
314
|
+
containerOf: ReadonlyMap<Comment, Container>,
|
|
315
|
+
candidates: ReadonlySet<Comment>,
|
|
316
|
+
): Range[] {
|
|
317
|
+
const ranges: Range[] = [];
|
|
318
|
+
for (const comment of comments) {
|
|
319
|
+
const container = containerOf.get(comment);
|
|
320
|
+
if (container === undefined || !container.comments.every((inside) => candidates.has(inside))) {
|
|
321
|
+
ranges.push(comment);
|
|
322
|
+
} else if (ranges[ranges.length - 1] !== container) {
|
|
323
|
+
ranges.push(container);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return ranges;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** The edits that remove the comments: one per comment, or one per container going with its braces. */
|
|
330
|
+
function removalEdits(
|
|
331
|
+
removed: readonly Comment[],
|
|
332
|
+
containerOf: ReadonlyMap<Comment, Container>,
|
|
333
|
+
droppable: ReadonlySet<Container>,
|
|
334
|
+
): Edit[] {
|
|
335
|
+
const edits: Edit[] = [];
|
|
336
|
+
for (const comment of removed) {
|
|
337
|
+
const container = containerOf.get(comment);
|
|
338
|
+
if (container === undefined || !droppable.has(container) || container.mode === "keep") {
|
|
339
|
+
edits.push({ start: comment.start, end: comment.end });
|
|
340
|
+
} else if (comment === container.comments[0]) {
|
|
341
|
+
edits.push(
|
|
342
|
+
container.mode === "drop"
|
|
343
|
+
? { start: container.start, end: container.end, jsx: true }
|
|
344
|
+
: { ...(container.spaceRange as Range), replacement: '{" "}' },
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return edits;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** Indexes of the JSX stretches whose rendering differs; throws when the structure itself does. */
|
|
352
|
+
function changedSegments(before: readonly string[], after: readonly string[]): Set<number> {
|
|
353
|
+
if (before.length !== after.length) throw unexpectedResult();
|
|
354
|
+
const changed = new Set<number>();
|
|
355
|
+
for (const [index, rendering] of before.entries()) {
|
|
356
|
+
if (rendering !== after[index]) changed.add(index);
|
|
357
|
+
}
|
|
358
|
+
return changed;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
interface ContainerSearch {
|
|
362
|
+
/** Containers whose comments all go, so their braces can too. */
|
|
363
|
+
readonly droppable: ReadonlySet<Container>;
|
|
364
|
+
/** Records which containers can go; when that changes (comments got protected), the search starts over. */
|
|
365
|
+
track(droppable: ReadonlySet<Container>): void;
|
|
366
|
+
/** Adjusts the modes after an attempt rendered the given stretches differently; true when anything changed. */
|
|
367
|
+
adjust(changed: ReadonlySet<number>): boolean;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Decides how each comment-only container goes. Dropping the braces is tried
|
|
372
|
+
* everywhere first; where the re-parsed output then renders a stretch of JSX
|
|
373
|
+
* children differently, that stretch's suspect containers are settled one
|
|
374
|
+
* at a time, in document order — each tries dropping, then `{" "}`, then
|
|
375
|
+
* keeping its braces, while the suspects after it keep theirs — so braces
|
|
376
|
+
* stay only where nothing less keeps the rendering intact. Keeping every
|
|
377
|
+
* suspect's braces always does: the other containers sit between text that
|
|
378
|
+
* renders nothing, which stays so however it is merged or trimmed.
|
|
379
|
+
*/
|
|
380
|
+
// The most suspects of one stretch settled one at a time; beyond that they all keep their braces.
|
|
381
|
+
const MAX_SUSPECTS_SETTLED = 16;
|
|
382
|
+
|
|
383
|
+
function containerSearch(containers: readonly Container[]): ContainerSearch {
|
|
384
|
+
let droppable: ReadonlySet<Container> = new Set();
|
|
385
|
+
/** Per stretch that rendered differently: its suspects still to settle, the first being tried. */
|
|
386
|
+
const pending = new Map<number, Container[]>();
|
|
387
|
+
|
|
388
|
+
const advance = (container: Container): boolean => {
|
|
389
|
+
if (container.mode === "keep") return false;
|
|
390
|
+
container.mode = container.mode === "drop" && container.spaceRange !== undefined ? "space" : "keep";
|
|
391
|
+
return true;
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
return {
|
|
395
|
+
get droppable() {
|
|
396
|
+
return droppable;
|
|
397
|
+
},
|
|
398
|
+
track(now) {
|
|
399
|
+
// Protection only ever takes comments out of the removal, so the set
|
|
400
|
+
// only shrinks and its size tells whether it changed.
|
|
401
|
+
if (now.size === droppable.size) return;
|
|
402
|
+
droppable = now;
|
|
403
|
+
pending.clear();
|
|
404
|
+
for (const container of containers) container.mode = "drop";
|
|
405
|
+
},
|
|
406
|
+
adjust(changed) {
|
|
407
|
+
let adjusted = false;
|
|
408
|
+
for (const segment of changed) {
|
|
409
|
+
let queue = pending.get(segment);
|
|
410
|
+
if (queue === undefined) {
|
|
411
|
+
// The stretch's first difference: from now on its suspects are
|
|
412
|
+
// settled one at a time, the first trying to drop its braces while
|
|
413
|
+
// the rest keep theirs. Every try costs a parse, so a stretch with
|
|
414
|
+
// a great many of them keeps all their braces straight away.
|
|
415
|
+
queue = containers.filter((c) => c.segment === segment && c.suspect && droppable.has(c));
|
|
416
|
+
const giveUp = queue.length > MAX_SUSPECTS_SETTLED;
|
|
417
|
+
for (const container of giveUp ? queue : queue.slice(1)) container.mode = "keep";
|
|
418
|
+
pending.set(segment, giveUp ? [] : queue);
|
|
419
|
+
if (giveUp || queue.length > 1) {
|
|
420
|
+
adjusted = true;
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
// The container being tried still changes the rendering: it tries
|
|
425
|
+
// the next mode. Keeping its braces cannot fail, so running out of
|
|
426
|
+
// modes (or of suspects to blame) means the output is not trustworthy.
|
|
427
|
+
const current = queue[0];
|
|
428
|
+
if (current === undefined || !advance(current)) throw unexpectedResult();
|
|
429
|
+
adjusted = true;
|
|
430
|
+
}
|
|
431
|
+
for (const [segment, queue] of pending) {
|
|
432
|
+
if (changed.has(segment) || queue.length === 0) continue;
|
|
433
|
+
// The stretch renders as before: the container being tried is
|
|
434
|
+
// settled, and the next suspect tries dropping its braces.
|
|
435
|
+
queue.shift();
|
|
436
|
+
const next = queue[0];
|
|
437
|
+
if (next !== undefined) {
|
|
438
|
+
next.mode = "drop";
|
|
439
|
+
adjusted = true;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return adjusted;
|
|
443
|
+
},
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
interface ShieldContext {
|
|
448
|
+
/** The comment-only JSX container each comment in one sits in. */
|
|
449
|
+
containerOf: ReadonlyMap<Comment, Container>;
|
|
450
|
+
/** Comments and the containers that may go with theirs, sorted: what code starts after. */
|
|
451
|
+
skippable: readonly Range[];
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
interface Shielding {
|
|
455
|
+
removed: Comment[];
|
|
456
|
+
/** Lines (1-based) that a surviving next-line directive reaches. */
|
|
457
|
+
lines: Set<number>;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* What deleting part of the removal takes out of the source: a single
|
|
462
|
+
* comment, or a comment-only JSX container going braces and all.
|
|
463
|
+
*/
|
|
464
|
+
interface RemovalUnit extends Range {
|
|
465
|
+
comments: readonly Comment[];
|
|
466
|
+
container?: Container;
|
|
467
|
+
}
|
|
468
|
+
|
|
194
469
|
/**
|
|
195
470
|
* Re-protects comments whose removal would delete a physical line sitting
|
|
196
471
|
* under a surviving next-line directive (`@ts-expect-error`,
|
|
@@ -198,27 +473,36 @@ function isProtected(comment: Comment, options: RemoveOptions): boolean {
|
|
|
198
473
|
* following code up, silently changing which line the directive applies to —
|
|
199
474
|
* and with it the compiler or linter behaviour. Directives whose tools reach
|
|
200
475
|
* further get more: below a tsc suppression the first line each compiler
|
|
201
|
-
* cannot step over stays,
|
|
476
|
+
* cannot step over stays, between a directive that only counts as the
|
|
202
477
|
* last comment before its target (`@deno-types`, ...) and that target every
|
|
203
|
-
* comment stays
|
|
478
|
+
* comment stays, and a JSX comment container right after a directive in a
|
|
479
|
+
* container (`{/* prettier-ignore */}`, which applies to the next sibling)
|
|
480
|
+
* stays too.
|
|
204
481
|
*
|
|
205
482
|
* The line checks work on whole lines, not single comments: a line holding
|
|
206
483
|
* several removable comments (`/* one */ /* two */`) vanishes just the same,
|
|
207
|
-
* so every comment on it has to stay
|
|
208
|
-
*
|
|
209
|
-
* lines (when directives
|
|
484
|
+
* so every comment on it has to stay — and a comment-only JSX container takes
|
|
485
|
+
* its braces along. Runs to a fixpoint because a re-protected comment can
|
|
486
|
+
* itself be a next-line directive shielding further lines (when directives
|
|
487
|
+
* are being removed selectively).
|
|
210
488
|
*/
|
|
211
|
-
function shieldNextLineDirectives(
|
|
489
|
+
function shieldNextLineDirectives(
|
|
490
|
+
source: string,
|
|
491
|
+
removed: Comment[],
|
|
492
|
+
kept: Comment[],
|
|
493
|
+
skipped: Comment[],
|
|
494
|
+
context: ShieldContext,
|
|
495
|
+
): Shielding {
|
|
212
496
|
const lineOffsets = lineStartOffsets(source);
|
|
213
497
|
const totalLines = lineOffsets.length;
|
|
214
|
-
// Where the code after a directive starts does not depend on which comments
|
|
215
|
-
// go, so it is looked up among all of them.
|
|
216
|
-
const allComments = [...removed, ...kept, ...skipped].sort(byStart);
|
|
217
498
|
const shieldedLines = new Set<number>();
|
|
218
499
|
const blockingLines = new Set<number>();
|
|
219
500
|
const shieldedGaps: Array<readonly [number, number]> = [];
|
|
501
|
+
const siblingTargets = new Set<Container>();
|
|
220
502
|
const processed = new Set<Comment>();
|
|
221
503
|
let stillRemoved = removed;
|
|
504
|
+
let units: RemovalUnit[] | undefined;
|
|
505
|
+
const currentUnits = (): RemovalUnit[] => (units ??= removalUnits(stillRemoved, context.containerOf));
|
|
222
506
|
|
|
223
507
|
const collectShields = (): boolean => {
|
|
224
508
|
let added = false;
|
|
@@ -228,7 +512,7 @@ function shieldNextLineDirectives(source: string, removed: Comment[], kept: Comm
|
|
|
228
512
|
const directive = comment.directive;
|
|
229
513
|
if (directive === undefined) continue;
|
|
230
514
|
if (LAST_COMMENT_DIRECTIVES.has(directive)) {
|
|
231
|
-
const codeStart = codeStartAfter(source, comment.end,
|
|
515
|
+
const codeStart = codeStartAfter(source, comment.end, context.skippable);
|
|
232
516
|
if (codeStart > comment.end && codeStart < source.length) {
|
|
233
517
|
shieldedGaps.push([comment.end, codeStart]);
|
|
234
518
|
added = true;
|
|
@@ -236,17 +520,27 @@ function shieldNextLineDirectives(source: string, removed: Comment[], kept: Comm
|
|
|
236
520
|
continue;
|
|
237
521
|
}
|
|
238
522
|
if (TSC_SUPPRESSIONS.has(directive)) {
|
|
239
|
-
for (const line of firstBlockingLines(source, comment, lineOffsets,
|
|
523
|
+
for (const line of firstBlockingLines(source, comment, lineOffsets, context.skippable)) {
|
|
240
524
|
if (blockingLines.has(line)) continue;
|
|
241
525
|
blockingLines.add(line);
|
|
242
526
|
added = true;
|
|
243
527
|
}
|
|
244
528
|
}
|
|
245
529
|
if (!isNextLineDirective(directive)) continue;
|
|
530
|
+
// In JSX, such a directive in braces applies to the sibling after it,
|
|
531
|
+
// whatever line that is on (prettier leaves the element after
|
|
532
|
+
// `{/* prettier-ignore */}` as is): a comment-only container standing
|
|
533
|
+
// between them has to stay. Range and file pragmas target no sibling.
|
|
534
|
+
const holder = context.containerOf.get(comment);
|
|
535
|
+
if (holder?.next !== undefined && !siblingTargets.has(holder.next)) {
|
|
536
|
+
siblingTargets.add(holder.next);
|
|
537
|
+
added = true;
|
|
538
|
+
}
|
|
246
539
|
// A trailing formatter or scanner suppression (`a = [1]; // oxfmt-ignore`,
|
|
247
540
|
// `secret(); // nosemgrep`) targets its own line, so it shields nothing
|
|
248
|
-
// below it.
|
|
249
|
-
|
|
541
|
+
// below it. In JSX, the container holding the comment is what has to
|
|
542
|
+
// stand on lines of its own.
|
|
543
|
+
if (TRAILING_TARGETS_OWN_LINE.has(directive) && !occupiesWholeLines(source, holder ?? comment)) {
|
|
250
544
|
continue;
|
|
251
545
|
}
|
|
252
546
|
// Counted pragmas like `c8 ignore next 3` cover several lines.
|
|
@@ -266,18 +560,19 @@ function shieldNextLineDirectives(source: string, removed: Comment[], kept: Comm
|
|
|
266
560
|
const grouped = new Set(group);
|
|
267
561
|
stillRemoved = stillRemoved.filter((comment) => !grouped.has(comment));
|
|
268
562
|
kept.push(...group);
|
|
563
|
+
units = undefined;
|
|
269
564
|
};
|
|
270
565
|
|
|
271
566
|
const protectShields = (): boolean => {
|
|
272
567
|
let changed = false;
|
|
273
568
|
for (const line of shieldedLines) {
|
|
274
|
-
const group = vanishingLineGroup(source, line, lineOffsets,
|
|
569
|
+
const group = vanishingLineGroup(source, line, lineOffsets, currentUnits());
|
|
275
570
|
if (group === undefined) continue;
|
|
276
571
|
protect(group);
|
|
277
572
|
changed = true;
|
|
278
573
|
}
|
|
279
574
|
for (const line of blockingLines) {
|
|
280
|
-
const group = lineOpeningGroup(source, line, lineOffsets,
|
|
575
|
+
const group = lineOpeningGroup(source, line, lineOffsets, currentUnits());
|
|
281
576
|
if (group === undefined) continue;
|
|
282
577
|
protect(group);
|
|
283
578
|
changed = true;
|
|
@@ -288,6 +583,12 @@ function shieldNextLineDirectives(source: string, removed: Comment[], kept: Comm
|
|
|
288
583
|
protect(group);
|
|
289
584
|
changed = true;
|
|
290
585
|
}
|
|
586
|
+
for (const target of siblingTargets) {
|
|
587
|
+
const group = stillRemoved.filter((comment) => target.comments.includes(comment));
|
|
588
|
+
if (group.length === 0) continue;
|
|
589
|
+
protect(group);
|
|
590
|
+
changed = true;
|
|
591
|
+
}
|
|
291
592
|
return changed;
|
|
292
593
|
};
|
|
293
594
|
|
|
@@ -298,24 +599,47 @@ function shieldNextLineDirectives(source: string, removed: Comment[], kept: Comm
|
|
|
298
599
|
if (stillRemoved.length !== removed.length) {
|
|
299
600
|
kept.sort(byStart);
|
|
300
601
|
}
|
|
301
|
-
return stillRemoved;
|
|
602
|
+
return { removed: stillRemoved, lines: shieldedLines };
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* The removals as the edits delete them: a comment-only container whose
|
|
607
|
+
* comments all go — and that is to go braces and all — is one unit spanning
|
|
608
|
+
* its braces, every other comment a unit of its own. Sorted, as the comments.
|
|
609
|
+
*/
|
|
610
|
+
function removalUnits(removals: readonly Comment[], containerOf: ReadonlyMap<Comment, Container>): RemovalUnit[] {
|
|
611
|
+
const removing = new Set(removals);
|
|
612
|
+
const units: RemovalUnit[] = [];
|
|
613
|
+
for (const comment of removals) {
|
|
614
|
+
const container = containerOf.get(comment);
|
|
615
|
+
if (
|
|
616
|
+
container === undefined ||
|
|
617
|
+
container.mode !== "drop" ||
|
|
618
|
+
!container.comments.every((inside) => removing.has(inside))
|
|
619
|
+
) {
|
|
620
|
+
units.push({ start: comment.start, end: comment.end, comments: [comment] });
|
|
621
|
+
} else if (units[units.length - 1]?.container !== container) {
|
|
622
|
+
units.push({ start: container.start, end: container.end, comments: container.comments, container });
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
return units;
|
|
302
626
|
}
|
|
303
627
|
|
|
304
628
|
/**
|
|
305
629
|
* Offset of the first character at or after `from` that is neither
|
|
306
|
-
* whitespace nor inside one of the (sorted, non-overlapping)
|
|
307
|
-
* source length when only
|
|
308
|
-
* least one
|
|
630
|
+
* whitespace nor inside one of the (sorted, non-overlapping) ranges, or the
|
|
631
|
+
* source length when only ranges follow. `from` lies past the start of at
|
|
632
|
+
* least one range (the directive the search is for); when the closest of
|
|
309
633
|
* them runs past `from` — a block comment opened on the directive's line —
|
|
310
634
|
* the search resumes at its end.
|
|
311
635
|
*/
|
|
312
|
-
function codeStartAfter(source: string, from: number,
|
|
313
|
-
let next = firstIndexAtOrAfter(
|
|
314
|
-
let index = Math.max(from, (
|
|
636
|
+
function codeStartAfter(source: string, from: number, ranges: readonly Range[]): number {
|
|
637
|
+
let next = firstIndexAtOrAfter(ranges, from, (range) => range.start);
|
|
638
|
+
let index = Math.max(from, (ranges[next - 1] as Range).end);
|
|
315
639
|
while (index < source.length) {
|
|
316
|
-
const
|
|
317
|
-
if (
|
|
318
|
-
index =
|
|
640
|
+
const range = ranges[next];
|
|
641
|
+
if (range !== undefined && range.start === index) {
|
|
642
|
+
index = range.end;
|
|
319
643
|
next += 1;
|
|
320
644
|
} else if (isBlank(source[index] as string)) {
|
|
321
645
|
index += 1;
|
|
@@ -326,18 +650,6 @@ function codeStartAfter(source: string, from: number, comments: readonly Comment
|
|
|
326
650
|
return source.length;
|
|
327
651
|
}
|
|
328
652
|
|
|
329
|
-
/** Index of the first item whose key is at least `value`, by binary search over ascending keys. */
|
|
330
|
-
function firstIndexAtOrAfter<T>(items: readonly T[], value: number, key: (item: T) => number): number {
|
|
331
|
-
let low = 0;
|
|
332
|
-
let high = items.length;
|
|
333
|
-
while (low < high) {
|
|
334
|
-
const middle = (low + high) >> 1;
|
|
335
|
-
if (key(items[middle] as T) < value) low = middle + 1;
|
|
336
|
-
else high = middle;
|
|
337
|
-
}
|
|
338
|
-
return low;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
653
|
/**
|
|
342
654
|
* For each TypeScript compiler, the first line between a tsc suppression and
|
|
343
655
|
* the code on the lines below it that the compiler does not step over, if
|
|
@@ -354,11 +666,11 @@ function firstBlockingLines(
|
|
|
354
666
|
source: string,
|
|
355
667
|
comment: Comment,
|
|
356
668
|
lineOffsets: readonly number[],
|
|
357
|
-
|
|
669
|
+
ranges: readonly Range[],
|
|
358
670
|
): number[] {
|
|
359
671
|
// A suppression applies from the line below it on, so code sharing its line
|
|
360
672
|
// (`/* @ts-ignore */ foo();`) does not end the search.
|
|
361
|
-
const codeStart = codeStartAfter(source, lineOffsets[comment.endLine] ?? source.length,
|
|
673
|
+
const codeStart = codeStartAfter(source, lineOffsets[comment.endLine] ?? source.length, ranges);
|
|
362
674
|
if (codeStart >= source.length) return [];
|
|
363
675
|
const codeLine = firstIndexAtOrAfter(lineOffsets, codeStart + 1, (offset) => offset);
|
|
364
676
|
const found: number[] = [];
|
|
@@ -374,48 +686,50 @@ function firstBlockingLines(
|
|
|
374
686
|
}
|
|
375
687
|
|
|
376
688
|
/**
|
|
377
|
-
* The
|
|
378
|
-
*
|
|
379
|
-
*
|
|
380
|
-
*
|
|
689
|
+
* The removals on the given line when one of them opens it (holds its first
|
|
690
|
+
* non-whitespace character), so that deleting them would change how the
|
|
691
|
+
* line starts. Returns undefined when something that survives opens the line
|
|
692
|
+
* instead, or nothing on it is to be removed.
|
|
381
693
|
*/
|
|
382
694
|
function lineOpeningGroup(
|
|
383
695
|
source: string,
|
|
384
696
|
line: number,
|
|
385
697
|
lineOffsets: readonly number[],
|
|
386
|
-
|
|
698
|
+
units: readonly RemovalUnit[],
|
|
387
699
|
): Comment[] | undefined {
|
|
388
700
|
// A blocking line always lies above the code line, so the next offset exists.
|
|
389
701
|
const lineStart = lineOffsets[line - 1] as number;
|
|
390
702
|
const lineEnd = lineOffsets[line] as number;
|
|
391
|
-
const group =
|
|
703
|
+
const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
|
|
392
704
|
let first = lineStart;
|
|
393
705
|
while (first < lineEnd && isBlank(source[first] as string)) first += 1;
|
|
394
|
-
return group.some((
|
|
706
|
+
return group.some((unit) => unit.start <= first && first < unit.end)
|
|
707
|
+
? group.flatMap((unit) => unit.comments)
|
|
708
|
+
: undefined;
|
|
395
709
|
}
|
|
396
710
|
|
|
397
711
|
/**
|
|
398
|
-
* The
|
|
399
|
-
* non-whitespace character of the line lies inside one of them, so
|
|
400
|
-
* them
|
|
401
|
-
*
|
|
712
|
+
* The removals that would make the given line disappear: every
|
|
713
|
+
* non-whitespace character of the line lies inside one of them, so deleting
|
|
714
|
+
* them deletes the physical line. Returns undefined when the line survives
|
|
715
|
+
* (it holds code, a kept comment, or nothing to remove at all).
|
|
402
716
|
*/
|
|
403
717
|
function vanishingLineGroup(
|
|
404
718
|
source: string,
|
|
405
719
|
line: number,
|
|
406
720
|
lineOffsets: readonly number[],
|
|
407
|
-
|
|
721
|
+
units: readonly RemovalUnit[],
|
|
408
722
|
): Comment[] | undefined {
|
|
409
723
|
// collectShields never shields a line beyond the last one, so the offset exists.
|
|
410
724
|
const lineStart = lineOffsets[line - 1] as number;
|
|
411
725
|
const lineEnd = lineOffsets[line] ?? source.length;
|
|
412
|
-
const group =
|
|
726
|
+
const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
|
|
413
727
|
if (group.length === 0) return undefined;
|
|
414
728
|
for (let index = lineStart; index < lineEnd; index += 1) {
|
|
415
729
|
if (isBlank(source[index] as string)) continue;
|
|
416
|
-
if (!group.some((
|
|
730
|
+
if (!group.some((unit) => unit.start <= index && index < unit.end)) return undefined;
|
|
417
731
|
}
|
|
418
|
-
return group;
|
|
732
|
+
return group.flatMap((unit) => unit.comments);
|
|
419
733
|
}
|
|
420
734
|
|
|
421
735
|
// These suppressions only apply to the following node/line when they stand on
|
|
@@ -534,132 +848,20 @@ function shieldedLineCount(comment: Comment): number {
|
|
|
534
848
|
}
|
|
535
849
|
|
|
536
850
|
/**
|
|
537
|
-
* True when nothing but whitespace shares the
|
|
851
|
+
* True when nothing but whitespace shares the range's first and last lines.
|
|
538
852
|
* Lines are delimited by any ECMAScript line terminator, matching how the
|
|
539
853
|
* scanner numbers them (a lone `\r`, U+2028 and U+2029 count too).
|
|
540
854
|
*/
|
|
541
|
-
function occupiesWholeLines(source: string,
|
|
542
|
-
for (let index =
|
|
855
|
+
function occupiesWholeLines(source: string, range: Range): boolean {
|
|
856
|
+
for (let index = range.start - 1; index >= 0; index -= 1) {
|
|
543
857
|
const char = source[index] as string;
|
|
544
858
|
if (LINE_TERMINATOR.test(char)) break;
|
|
545
859
|
if (!isBlank(char)) return false;
|
|
546
860
|
}
|
|
547
|
-
for (let index =
|
|
861
|
+
for (let index = range.end; index < source.length; index += 1) {
|
|
548
862
|
const char = source[index] as string;
|
|
549
863
|
if (LINE_TERMINATOR.test(char)) break;
|
|
550
864
|
if (!isBlank(char)) return false;
|
|
551
865
|
}
|
|
552
866
|
return true;
|
|
553
867
|
}
|
|
554
|
-
|
|
555
|
-
const isHorizontalSpace = (char: string | undefined): boolean => char === " " || char === "\t";
|
|
556
|
-
|
|
557
|
-
// A global copy of the line-break matcher, so exec() can resume from a removal's end offset.
|
|
558
|
-
const NEXT_LINE_BREAK = new RegExp(LINE_BREAK.source, "g");
|
|
559
|
-
|
|
560
|
-
/** End offset (exclusive) of the last line break in the chunk, or -1 when it has none. */
|
|
561
|
-
function lastLineBreakEnd(chunk: string): number {
|
|
562
|
-
for (let index = chunk.length - 1; index >= 0; index -= 1) {
|
|
563
|
-
if (LINE_TERMINATOR.test(chunk[index] as string)) return index + 1;
|
|
564
|
-
}
|
|
565
|
-
return -1;
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
/**
|
|
569
|
-
* Splices sorted, non-overlapping comment ranges out of the source, tidying lines.
|
|
570
|
-
* Output is accumulated as segments with incremental line-state tracking, so the
|
|
571
|
-
* work stays linear in the source size even for comment-dense files.
|
|
572
|
-
*/
|
|
573
|
-
function splice(source: string, removals: readonly Comment[]): string {
|
|
574
|
-
const segments: string[] = [];
|
|
575
|
-
let cursor = 0;
|
|
576
|
-
/** Whether the output's current (unterminated) line is all whitespace so far. */
|
|
577
|
-
let lineIsBlank = true;
|
|
578
|
-
/** Last character of the output so far ("" while empty). */
|
|
579
|
-
let lastChar = "";
|
|
580
|
-
|
|
581
|
-
const push = (chunk: string): void => {
|
|
582
|
-
if (chunk === "") return;
|
|
583
|
-
segments.push(chunk);
|
|
584
|
-
const breakEnd = lastLineBreakEnd(chunk);
|
|
585
|
-
lineIsBlank = breakEnd === -1 ? lineIsBlank && isBlank(chunk) : isBlank(chunk.slice(breakEnd));
|
|
586
|
-
lastChar = chunk.slice(-1);
|
|
587
|
-
};
|
|
588
|
-
|
|
589
|
-
/** Deletes everything the output holds after its last line break. */
|
|
590
|
-
const dropCurrentLine = (): void => {
|
|
591
|
-
while (segments.length > 0) {
|
|
592
|
-
const last = segments[segments.length - 1] as string;
|
|
593
|
-
const breakEnd = lastLineBreakEnd(last);
|
|
594
|
-
if (breakEnd === -1) {
|
|
595
|
-
segments.pop();
|
|
596
|
-
continue;
|
|
597
|
-
}
|
|
598
|
-
segments[segments.length - 1] = last.slice(0, breakEnd);
|
|
599
|
-
lineIsBlank = true;
|
|
600
|
-
lastChar = last.charAt(breakEnd - 1);
|
|
601
|
-
return;
|
|
602
|
-
}
|
|
603
|
-
lineIsBlank = true;
|
|
604
|
-
lastChar = "";
|
|
605
|
-
};
|
|
606
|
-
|
|
607
|
-
/** Deletes trailing spaces/tabs of the output's current line. */
|
|
608
|
-
const trimLineEnd = (): void => {
|
|
609
|
-
while (segments.length > 0) {
|
|
610
|
-
const last = segments[segments.length - 1] as string;
|
|
611
|
-
let end = last.length;
|
|
612
|
-
while (end > 0 && isHorizontalSpace(last[end - 1])) end -= 1;
|
|
613
|
-
if (end === 0) {
|
|
614
|
-
segments.pop();
|
|
615
|
-
continue;
|
|
616
|
-
}
|
|
617
|
-
if (end < last.length) segments[segments.length - 1] = last.slice(0, end);
|
|
618
|
-
lastChar = (segments[segments.length - 1] as string).slice(-1);
|
|
619
|
-
return;
|
|
620
|
-
}
|
|
621
|
-
lastChar = "";
|
|
622
|
-
};
|
|
623
|
-
|
|
624
|
-
for (const comment of removals) {
|
|
625
|
-
push(source.slice(cursor, comment.start));
|
|
626
|
-
cursor = comment.end;
|
|
627
|
-
|
|
628
|
-
NEXT_LINE_BREAK.lastIndex = cursor;
|
|
629
|
-
const lineBreak = NEXT_LINE_BREAK.exec(source);
|
|
630
|
-
const contentEnd = lineBreak === null ? source.length : lineBreak.index;
|
|
631
|
-
const lineEnd = lineBreak === null ? source.length : lineBreak.index + lineBreak[0].length;
|
|
632
|
-
const restContent = source.slice(cursor, contentEnd);
|
|
633
|
-
|
|
634
|
-
if (lineIsBlank && isBlank(restContent)) {
|
|
635
|
-
// The comment occupied whole line(s): drop them entirely.
|
|
636
|
-
dropCurrentLine();
|
|
637
|
-
cursor = lineEnd;
|
|
638
|
-
} else if (isBlank(restContent)) {
|
|
639
|
-
// Trailing comment after code: trim the gap, keep the line break.
|
|
640
|
-
trimLineEnd();
|
|
641
|
-
push(source.slice(contentEnd, lineEnd));
|
|
642
|
-
cursor = lineEnd;
|
|
643
|
-
} else if (!lineIsBlank && LINE_TERMINATOR.test(comment.text)) {
|
|
644
|
-
// Code on both sides of a block comment that spans lines. Per ECMA-262
|
|
645
|
-
// such a comment acts as a line terminator for semicolon insertion, so
|
|
646
|
-
// replace it with a real line break (matching the file's style) instead
|
|
647
|
-
// of merging the lines.
|
|
648
|
-
trimLineEnd();
|
|
649
|
-
push(comment.text.includes("\r\n") ? "\r\n" : "\n");
|
|
650
|
-
} else {
|
|
651
|
-
// Inline comment with code after it on the same line.
|
|
652
|
-
const next = source.charAt(cursor);
|
|
653
|
-
if (lastChar !== "" && !isBlank(lastChar) && !isBlank(next)) {
|
|
654
|
-
// `a/* x */b` would merge tokens: keep them apart.
|
|
655
|
-
push(" ");
|
|
656
|
-
} else if (lastChar === "" || isBlank(lastChar)) {
|
|
657
|
-
// Collapse the horizontal whitespace that followed the comment.
|
|
658
|
-
while (cursor < lineEnd && isHorizontalSpace(source[cursor])) cursor += 1;
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
segments.push(source.slice(cursor));
|
|
664
|
-
return segments.join("");
|
|
665
|
-
}
|