@kongyo2/ts-comment-scanner 1.8.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 +14 -9
- package/README.md +14 -9
- package/dist/directives.d.ts +27 -9
- package/dist/directives.d.ts.map +1 -1
- package/dist/directives.js +351 -153
- package/dist/directives.js.map +1 -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 +18 -5
- package/dist/remove.d.ts.map +1 -1
- package/dist/remove.js +460 -169
- 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 +26 -5
- 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/directives.ts +379 -155
- package/src/jsx.ts +263 -0
- package/src/ranges.ts +23 -0
- package/src/remove.ts +539 -174
- package/src/scanner.ts +33 -5
- package/src/splice.ts +317 -0
package/dist/remove.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { activePositionalDirectives, isLegalComment } from "./directives.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { activePositionalDirectives, bunMarkerOffset, isLegalComment } from "./directives.js";
|
|
2
|
+
import { LINE_TERMINATOR, isBlank, lineStartOffsets } from "./lines.js";
|
|
3
|
+
import { firstIndexAtOrAfter } from "./ranges.js";
|
|
4
|
+
import { analyzeSource } from "./scanner.js";
|
|
5
|
+
import { applyEdits, blankLineEdits } from "./splice.js";
|
|
4
6
|
/**
|
|
5
7
|
* Removes comments from TypeScript source without touching code.
|
|
6
8
|
*
|
|
@@ -11,10 +13,21 @@ import { scanComments } from "./scanner.js";
|
|
|
11
13
|
* - Surviving comments keep their directive semantics: when deleting a leading
|
|
12
14
|
* comment would newly activate a position-dependent directive in a kept
|
|
13
15
|
* comment (prettier's `@format` docblock pragma counts only as the file's
|
|
14
|
-
* first comment, Bun's `// @bun` only at the very start
|
|
15
|
-
* comment is kept instead.
|
|
16
|
+
* first comment, Bun's `// @bun` only at the very start or right after a
|
|
17
|
+
* shebang line), the leading comment is kept instead.
|
|
16
18
|
* - A space is inserted where removing a block comment would merge two tokens,
|
|
17
19
|
* and a line break is kept where the comment acted as one (ASI stays intact).
|
|
20
|
+
* - A JSX comment goes with its braces (`{/* note */}`), unless that would
|
|
21
|
+
* change what the element renders: the braces split the text around them
|
|
22
|
+
* into separate strings, so where they stand between text that renders,
|
|
23
|
+
* `{" "}` takes their place when that keeps the strings identical, and
|
|
24
|
+
* otherwise the braces stay as `{}`. The output is re-parsed to prove every
|
|
25
|
+
* element renders the same strings, under the whitespace rules of
|
|
26
|
+
* TypeScript, esbuild and Babel alike.
|
|
27
|
+
* - Blank lines that only separated a removed comment from the code around it
|
|
28
|
+
* go with it: no pile-up of blank lines where a comment stood between two
|
|
29
|
+
* of them, and at the start or end of the file or of a block only the blank
|
|
30
|
+
* lines already against that edge stay.
|
|
18
31
|
* - A leading byte-order mark is preserved. Reported positions index into the
|
|
19
32
|
* `source` string exactly as `scanComments(source)` reports them (a BOM
|
|
20
33
|
* counts as its first character).
|
|
@@ -23,7 +36,8 @@ import { scanComments } from "./scanner.js";
|
|
|
23
36
|
*/
|
|
24
37
|
export function removeComments(source, options = {}) {
|
|
25
38
|
const jsx = options.jsx === true;
|
|
26
|
-
const
|
|
39
|
+
const analysis = analyzeSource(source, { jsx });
|
|
40
|
+
const comments = analysis.comments;
|
|
27
41
|
let removed = [];
|
|
28
42
|
const kept = [];
|
|
29
43
|
const skipped = [];
|
|
@@ -38,16 +52,42 @@ export function removeComments(source, options = {}) {
|
|
|
38
52
|
removed.push(comment);
|
|
39
53
|
}
|
|
40
54
|
}
|
|
55
|
+
const containers = attachContainers(source, analysis.jsx.containers, comments);
|
|
56
|
+
const containerOf = new Map();
|
|
57
|
+
for (const container of containers) {
|
|
58
|
+
for (const comment of container.comments)
|
|
59
|
+
containerOf.set(comment, container);
|
|
60
|
+
}
|
|
61
|
+
const context = { containerOf, skippable: skippableRanges(comments, containerOf, new Set(removed)) };
|
|
62
|
+
const search = containerSearch(containers);
|
|
63
|
+
// Collapsing blank lines can only move a surviving comment to the very
|
|
64
|
+
// start of the file (or of the line after a shebang), where Bun reads its
|
|
65
|
+
// marker; when that changes a survivor's meaning, the attempt is repeated
|
|
66
|
+
// without the collapse before any removal is given up.
|
|
67
|
+
let tidyBlankLines = true;
|
|
41
68
|
for (;;) {
|
|
42
|
-
|
|
69
|
+
const shielding = shieldNextLineDirectives(source, removed, kept, skipped, context);
|
|
70
|
+
removed = shielding.removed;
|
|
43
71
|
if (removed.length === 0) {
|
|
44
72
|
return { code: source, removed, kept, skipped, changed: false };
|
|
45
73
|
}
|
|
46
|
-
const
|
|
47
|
-
|
|
74
|
+
const removing = new Set(removed);
|
|
75
|
+
search.track(new Set(containers.filter((container) => container.comments.every((c) => removing.has(c)))));
|
|
76
|
+
const edits = removalEdits(removed, containerOf, search.droppable);
|
|
77
|
+
const deletions = edits.filter((edit) => edit.replacement === undefined);
|
|
78
|
+
const blankEdits = tidyBlankLines ? blankLineEdits(source, deletions, comments, shielding.lines, analysis.jsx) : [];
|
|
79
|
+
const code = editKeepingBom(source, [...edits, ...blankEdits].sort(byStart));
|
|
80
|
+
const output = analyzeSource(code, { jsx });
|
|
81
|
+
if (search.adjust(changedSegments(analysis.jsx.segments, output.jsx.segments)))
|
|
82
|
+
continue;
|
|
83
|
+
const survivor = firstChangedSurvivor(source, code, output.comments, kept, skipped);
|
|
48
84
|
if (survivor === undefined) {
|
|
49
85
|
return { code, removed, kept, skipped, changed: true };
|
|
50
86
|
}
|
|
87
|
+
if (blankEdits.length > 0) {
|
|
88
|
+
tidyBlankLines = false;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
51
91
|
// A surviving comment changed meaning: with everything before it deleted
|
|
52
92
|
// it became the file's first comment (or its very first bytes), turning a
|
|
53
93
|
// previously inert positional directive live. Re-protect the removal
|
|
@@ -62,12 +102,13 @@ export function removeComments(source, options = {}) {
|
|
|
62
102
|
removed = removed.filter((comment) => comment !== blocker);
|
|
63
103
|
kept.push(blocker);
|
|
64
104
|
kept.sort(byStart);
|
|
105
|
+
tidyBlankLines = true;
|
|
65
106
|
}
|
|
66
107
|
}
|
|
67
108
|
function unexpectedResult() {
|
|
68
109
|
return new Error("comment removal produced an unexpected result; refusing to continue");
|
|
69
110
|
}
|
|
70
|
-
/** Sort comparator for document order:
|
|
111
|
+
/** Sort comparator for document order: ranges are ordered by where they start. */
|
|
71
112
|
function byStart(a, b) {
|
|
72
113
|
return a.start - b.start;
|
|
73
114
|
}
|
|
@@ -105,17 +146,17 @@ function firstChangedSurvivor(source, code, actual, kept, skipped) {
|
|
|
105
146
|
* The placement bits the position-dependent directives care about, derived
|
|
106
147
|
* without a parse: the comment is the file's first comment when nothing but
|
|
107
148
|
* whitespace (or a shebang) precedes it — any earlier comment's text keeps
|
|
108
|
-
* the slice non-blank — and it sits at the file start when it begins
|
|
109
|
-
*
|
|
110
|
-
* comments never moves a comment across the first token, so the
|
|
111
|
-
* check does not consult it.
|
|
149
|
+
* the slice non-blank — and it sits at the file start when it begins where
|
|
150
|
+
* Bun reads its marker. Header status needs the token structure, but
|
|
151
|
+
* removing comments never moves a comment across the first token, so the
|
|
152
|
+
* positional check does not consult it.
|
|
112
153
|
*/
|
|
113
154
|
function positionalPlacement(text, comment) {
|
|
114
155
|
const shebangLength = /^#!.*/.exec(text)?.[0].length ?? 0;
|
|
115
156
|
return {
|
|
116
157
|
header: false,
|
|
117
158
|
firstComment: isBlank(text.slice(shebangLength, comment.start)),
|
|
118
|
-
fileStart: comment.start ===
|
|
159
|
+
fileStart: comment.start === bunMarkerOffset(text),
|
|
119
160
|
};
|
|
120
161
|
}
|
|
121
162
|
/** The removal candidate that starts closest before the comment, if any. */
|
|
@@ -128,20 +169,19 @@ function closestRemovalBefore(removals, comment) {
|
|
|
128
169
|
return closest;
|
|
129
170
|
}
|
|
130
171
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
172
|
+
* Applies the edits, restoring the byte-order mark when dropping a
|
|
173
|
+
* comment-only first line took it along: the mark is an encoding artifact,
|
|
174
|
+
* not line content.
|
|
134
175
|
*/
|
|
135
|
-
function
|
|
136
|
-
if (!source.startsWith("
|
|
137
|
-
return
|
|
138
|
-
// The
|
|
176
|
+
function editKeepingBom(source, edits) {
|
|
177
|
+
if (!source.startsWith(""))
|
|
178
|
+
return applyEdits(source, edits);
|
|
179
|
+
// The editing itself works on the BOM-less body (with ranges shifted to
|
|
139
180
|
// match) so its line handling never sees the mark, then the mark is
|
|
140
|
-
// re-attached; this keeps
|
|
181
|
+
// re-attached; this keeps `// gone\n...` from losing the mark with
|
|
141
182
|
// the dropped line.
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
return `\uFEFF${splice(body, shifted)}`;
|
|
183
|
+
const shifted = edits.map((edit) => ({ ...edit, start: edit.start - 1, end: edit.end - 1 }));
|
|
184
|
+
return `${applyEdits(source.slice(1), shifted)}`;
|
|
145
185
|
}
|
|
146
186
|
function isProtected(comment, options) {
|
|
147
187
|
if (comment.directive !== undefined && options.removeDirectives !== true)
|
|
@@ -150,37 +190,255 @@ function isProtected(comment, options) {
|
|
|
150
190
|
return true;
|
|
151
191
|
return false;
|
|
152
192
|
}
|
|
193
|
+
function attachContainers(source, layouts, comments) {
|
|
194
|
+
const containers = new Map();
|
|
195
|
+
for (const layout of layouts) {
|
|
196
|
+
const inside = [];
|
|
197
|
+
let index = firstIndexAtOrAfter(comments, layout.start, (comment) => comment.start);
|
|
198
|
+
for (let comment = comments[index]; comment !== undefined && comment.end < layout.end; comment = comments[index]) {
|
|
199
|
+
inside.push(comment);
|
|
200
|
+
index += 1;
|
|
201
|
+
}
|
|
202
|
+
containers.set(layout, {
|
|
203
|
+
start: layout.start,
|
|
204
|
+
end: layout.end,
|
|
205
|
+
segment: layout.segment,
|
|
206
|
+
suspect: layout.suspect,
|
|
207
|
+
next: undefined,
|
|
208
|
+
comments: inside,
|
|
209
|
+
mode: "drop",
|
|
210
|
+
spaceRange: spaceRange(source, layout),
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
for (const [layout, container] of containers) {
|
|
214
|
+
if (layout.next !== undefined)
|
|
215
|
+
container.next = containers.get(layout.next);
|
|
216
|
+
}
|
|
217
|
+
return [...containers.values()];
|
|
218
|
+
}
|
|
219
|
+
const isHorizontalSpace = (char) => char === " " || char === "\t";
|
|
220
|
+
/**
|
|
221
|
+
* The range `{" "}` would take the place of: a one-line container and the
|
|
222
|
+
* spaces before it when code precedes it on its line (`<b /> {/* x */}`), or
|
|
223
|
+
* the spaces after it when it opens its line and code follows. Undefined when
|
|
224
|
+
* no spaces hold it off anything on its line.
|
|
225
|
+
*/
|
|
226
|
+
function spaceRange(source, container) {
|
|
227
|
+
if (LINE_TERMINATOR.test(source.slice(container.start, container.end)))
|
|
228
|
+
return undefined;
|
|
229
|
+
let lineStart = container.start;
|
|
230
|
+
while (lineStart > 0 && !LINE_TERMINATOR.test(source[lineStart - 1]))
|
|
231
|
+
lineStart -= 1;
|
|
232
|
+
let before = container.start;
|
|
233
|
+
while (isHorizontalSpace(source[before - 1]))
|
|
234
|
+
before -= 1;
|
|
235
|
+
let after = container.end;
|
|
236
|
+
while (isHorizontalSpace(source[after]))
|
|
237
|
+
after += 1;
|
|
238
|
+
if (!isBlank(source.slice(lineStart, container.start))) {
|
|
239
|
+
return before < container.start ? { start: before, end: container.end } : undefined;
|
|
240
|
+
}
|
|
241
|
+
const codeAfter = after < source.length && !LINE_TERMINATOR.test(source[after]);
|
|
242
|
+
return codeAfter && after > container.end ? { start: container.start, end: after } : undefined;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Comments, and the comment-only containers that may go along with theirs,
|
|
246
|
+
* as sorted ranges: what the search for the code after a directive skips.
|
|
247
|
+
* A container counts as skippable when all its comments are removal
|
|
248
|
+
* candidates, even if some get protected later — skipping it then only moves
|
|
249
|
+
* the found code further down, which shields more, not less.
|
|
250
|
+
*/
|
|
251
|
+
function skippableRanges(comments, containerOf, candidates) {
|
|
252
|
+
const ranges = [];
|
|
253
|
+
for (const comment of comments) {
|
|
254
|
+
const container = containerOf.get(comment);
|
|
255
|
+
if (container === undefined || !container.comments.every((inside) => candidates.has(inside))) {
|
|
256
|
+
ranges.push(comment);
|
|
257
|
+
}
|
|
258
|
+
else if (ranges[ranges.length - 1] !== container) {
|
|
259
|
+
ranges.push(container);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return ranges;
|
|
263
|
+
}
|
|
264
|
+
/** The edits that remove the comments: one per comment, or one per container going with its braces. */
|
|
265
|
+
function removalEdits(removed, containerOf, droppable) {
|
|
266
|
+
const edits = [];
|
|
267
|
+
for (const comment of removed) {
|
|
268
|
+
const container = containerOf.get(comment);
|
|
269
|
+
if (container === undefined || !droppable.has(container) || container.mode === "keep") {
|
|
270
|
+
edits.push({ start: comment.start, end: comment.end });
|
|
271
|
+
}
|
|
272
|
+
else if (comment === container.comments[0]) {
|
|
273
|
+
edits.push(container.mode === "drop"
|
|
274
|
+
? { start: container.start, end: container.end, jsx: true }
|
|
275
|
+
: { ...container.spaceRange, replacement: '{" "}' });
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return edits;
|
|
279
|
+
}
|
|
280
|
+
/** Indexes of the JSX stretches whose rendering differs; throws when the structure itself does. */
|
|
281
|
+
function changedSegments(before, after) {
|
|
282
|
+
if (before.length !== after.length)
|
|
283
|
+
throw unexpectedResult();
|
|
284
|
+
const changed = new Set();
|
|
285
|
+
for (const [index, rendering] of before.entries()) {
|
|
286
|
+
if (rendering !== after[index])
|
|
287
|
+
changed.add(index);
|
|
288
|
+
}
|
|
289
|
+
return changed;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Decides how each comment-only container goes. Dropping the braces is tried
|
|
293
|
+
* everywhere first; where the re-parsed output then renders a stretch of JSX
|
|
294
|
+
* children differently, that stretch's suspect containers are settled one
|
|
295
|
+
* at a time, in document order — each tries dropping, then `{" "}`, then
|
|
296
|
+
* keeping its braces, while the suspects after it keep theirs — so braces
|
|
297
|
+
* stay only where nothing less keeps the rendering intact. Keeping every
|
|
298
|
+
* suspect's braces always does: the other containers sit between text that
|
|
299
|
+
* renders nothing, which stays so however it is merged or trimmed.
|
|
300
|
+
*/
|
|
301
|
+
// The most suspects of one stretch settled one at a time; beyond that they all keep their braces.
|
|
302
|
+
const MAX_SUSPECTS_SETTLED = 16;
|
|
303
|
+
function containerSearch(containers) {
|
|
304
|
+
let droppable = new Set();
|
|
305
|
+
/** Per stretch that rendered differently: its suspects still to settle, the first being tried. */
|
|
306
|
+
const pending = new Map();
|
|
307
|
+
const advance = (container) => {
|
|
308
|
+
if (container.mode === "keep")
|
|
309
|
+
return false;
|
|
310
|
+
container.mode = container.mode === "drop" && container.spaceRange !== undefined ? "space" : "keep";
|
|
311
|
+
return true;
|
|
312
|
+
};
|
|
313
|
+
return {
|
|
314
|
+
get droppable() {
|
|
315
|
+
return droppable;
|
|
316
|
+
},
|
|
317
|
+
track(now) {
|
|
318
|
+
// Protection only ever takes comments out of the removal, so the set
|
|
319
|
+
// only shrinks and its size tells whether it changed.
|
|
320
|
+
if (now.size === droppable.size)
|
|
321
|
+
return;
|
|
322
|
+
droppable = now;
|
|
323
|
+
pending.clear();
|
|
324
|
+
for (const container of containers)
|
|
325
|
+
container.mode = "drop";
|
|
326
|
+
},
|
|
327
|
+
adjust(changed) {
|
|
328
|
+
let adjusted = false;
|
|
329
|
+
for (const segment of changed) {
|
|
330
|
+
let queue = pending.get(segment);
|
|
331
|
+
if (queue === undefined) {
|
|
332
|
+
// The stretch's first difference: from now on its suspects are
|
|
333
|
+
// settled one at a time, the first trying to drop its braces while
|
|
334
|
+
// the rest keep theirs. Every try costs a parse, so a stretch with
|
|
335
|
+
// a great many of them keeps all their braces straight away.
|
|
336
|
+
queue = containers.filter((c) => c.segment === segment && c.suspect && droppable.has(c));
|
|
337
|
+
const giveUp = queue.length > MAX_SUSPECTS_SETTLED;
|
|
338
|
+
for (const container of giveUp ? queue : queue.slice(1))
|
|
339
|
+
container.mode = "keep";
|
|
340
|
+
pending.set(segment, giveUp ? [] : queue);
|
|
341
|
+
if (giveUp || queue.length > 1) {
|
|
342
|
+
adjusted = true;
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
// The container being tried still changes the rendering: it tries
|
|
347
|
+
// the next mode. Keeping its braces cannot fail, so running out of
|
|
348
|
+
// modes (or of suspects to blame) means the output is not trustworthy.
|
|
349
|
+
const current = queue[0];
|
|
350
|
+
if (current === undefined || !advance(current))
|
|
351
|
+
throw unexpectedResult();
|
|
352
|
+
adjusted = true;
|
|
353
|
+
}
|
|
354
|
+
for (const [segment, queue] of pending) {
|
|
355
|
+
if (changed.has(segment) || queue.length === 0)
|
|
356
|
+
continue;
|
|
357
|
+
// The stretch renders as before: the container being tried is
|
|
358
|
+
// settled, and the next suspect tries dropping its braces.
|
|
359
|
+
queue.shift();
|
|
360
|
+
const next = queue[0];
|
|
361
|
+
if (next !== undefined) {
|
|
362
|
+
next.mode = "drop";
|
|
363
|
+
adjusted = true;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return adjusted;
|
|
367
|
+
},
|
|
368
|
+
};
|
|
369
|
+
}
|
|
153
370
|
/**
|
|
154
371
|
* Re-protects comments whose removal would delete a physical line sitting
|
|
155
372
|
* under a surviving next-line directive (`@ts-expect-error`,
|
|
156
373
|
* `eslint-disable-next-line`, ...). Dropping such a line would shift the
|
|
157
374
|
* following code up, silently changing which line the directive applies to —
|
|
158
|
-
* and with it the compiler or linter behaviour.
|
|
375
|
+
* and with it the compiler or linter behaviour. Directives whose tools reach
|
|
376
|
+
* further get more: below a tsc suppression the first line each compiler
|
|
377
|
+
* cannot step over stays, between a directive that only counts as the
|
|
378
|
+
* last comment before its target (`@deno-types`, ...) and that target every
|
|
379
|
+
* comment stays, and a JSX comment container right after a directive in a
|
|
380
|
+
* container (`{/* prettier-ignore */}`, which applies to the next sibling)
|
|
381
|
+
* stays too.
|
|
159
382
|
*
|
|
160
|
-
* The
|
|
161
|
-
* removable comments (`/* one */ /* two */`) vanishes just the same,
|
|
162
|
-
* comment on it has to stay
|
|
163
|
-
*
|
|
164
|
-
*
|
|
383
|
+
* The line checks work on whole lines, not single comments: a line holding
|
|
384
|
+
* several removable comments (`/* one */ /* two */`) vanishes just the same,
|
|
385
|
+
* so every comment on it has to stay — and a comment-only JSX container takes
|
|
386
|
+
* its braces along. Runs to a fixpoint because a re-protected comment can
|
|
387
|
+
* itself be a next-line directive shielding further lines (when directives
|
|
388
|
+
* are being removed selectively).
|
|
165
389
|
*/
|
|
166
|
-
function shieldNextLineDirectives(source, removed, kept, skipped) {
|
|
390
|
+
function shieldNextLineDirectives(source, removed, kept, skipped, context) {
|
|
167
391
|
const lineOffsets = lineStartOffsets(source);
|
|
168
392
|
const totalLines = lineOffsets.length;
|
|
169
393
|
const shieldedLines = new Set();
|
|
394
|
+
const blockingLines = new Set();
|
|
395
|
+
const shieldedGaps = [];
|
|
396
|
+
const siblingTargets = new Set();
|
|
170
397
|
const processed = new Set();
|
|
171
398
|
let stillRemoved = removed;
|
|
399
|
+
let units;
|
|
400
|
+
const currentUnits = () => (units ??= removalUnits(stillRemoved, context.containerOf));
|
|
172
401
|
const collectShields = () => {
|
|
173
402
|
let added = false;
|
|
174
403
|
for (const comment of [...kept, ...skipped]) {
|
|
175
404
|
if (processed.has(comment))
|
|
176
405
|
continue;
|
|
177
406
|
processed.add(comment);
|
|
178
|
-
|
|
407
|
+
const directive = comment.directive;
|
|
408
|
+
if (directive === undefined)
|
|
179
409
|
continue;
|
|
410
|
+
if (LAST_COMMENT_DIRECTIVES.has(directive)) {
|
|
411
|
+
const codeStart = codeStartAfter(source, comment.end, context.skippable);
|
|
412
|
+
if (codeStart > comment.end && codeStart < source.length) {
|
|
413
|
+
shieldedGaps.push([comment.end, codeStart]);
|
|
414
|
+
added = true;
|
|
415
|
+
}
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
if (TSC_SUPPRESSIONS.has(directive)) {
|
|
419
|
+
for (const line of firstBlockingLines(source, comment, lineOffsets, context.skippable)) {
|
|
420
|
+
if (blockingLines.has(line))
|
|
421
|
+
continue;
|
|
422
|
+
blockingLines.add(line);
|
|
423
|
+
added = true;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (!isNextLineDirective(directive))
|
|
427
|
+
continue;
|
|
428
|
+
// In JSX, such a directive in braces applies to the sibling after it,
|
|
429
|
+
// whatever line that is on (prettier leaves the element after
|
|
430
|
+
// `{/* prettier-ignore */}` as is): a comment-only container standing
|
|
431
|
+
// between them has to stay. Range and file pragmas target no sibling.
|
|
432
|
+
const holder = context.containerOf.get(comment);
|
|
433
|
+
if (holder?.next !== undefined && !siblingTargets.has(holder.next)) {
|
|
434
|
+
siblingTargets.add(holder.next);
|
|
435
|
+
added = true;
|
|
436
|
+
}
|
|
180
437
|
// A trailing formatter or scanner suppression (`a = [1]; // oxfmt-ignore`,
|
|
181
438
|
// `secret(); // nosemgrep`) targets its own line, so it shields nothing
|
|
182
|
-
// below it.
|
|
183
|
-
|
|
439
|
+
// below it. In JSX, the container holding the comment is what has to
|
|
440
|
+
// stand on lines of its own.
|
|
441
|
+
if (TRAILING_TARGETS_OWN_LINE.has(directive) && !occupiesWholeLines(source, holder ?? comment)) {
|
|
184
442
|
continue;
|
|
185
443
|
}
|
|
186
444
|
// Counted pragmas like `c8 ignore next 3` cover several lines.
|
|
@@ -196,49 +454,169 @@ function shieldNextLineDirectives(source, removed, kept, skipped) {
|
|
|
196
454
|
}
|
|
197
455
|
return added;
|
|
198
456
|
};
|
|
199
|
-
const
|
|
457
|
+
const protect = (group) => {
|
|
458
|
+
const grouped = new Set(group);
|
|
459
|
+
stillRemoved = stillRemoved.filter((comment) => !grouped.has(comment));
|
|
460
|
+
kept.push(...group);
|
|
461
|
+
units = undefined;
|
|
462
|
+
};
|
|
463
|
+
const protectShields = () => {
|
|
200
464
|
let changed = false;
|
|
201
465
|
for (const line of shieldedLines) {
|
|
202
|
-
const group = vanishingLineGroup(source, line, lineOffsets,
|
|
466
|
+
const group = vanishingLineGroup(source, line, lineOffsets, currentUnits());
|
|
467
|
+
if (group === undefined)
|
|
468
|
+
continue;
|
|
469
|
+
protect(group);
|
|
470
|
+
changed = true;
|
|
471
|
+
}
|
|
472
|
+
for (const line of blockingLines) {
|
|
473
|
+
const group = lineOpeningGroup(source, line, lineOffsets, currentUnits());
|
|
203
474
|
if (group === undefined)
|
|
204
475
|
continue;
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
476
|
+
protect(group);
|
|
477
|
+
changed = true;
|
|
478
|
+
}
|
|
479
|
+
for (const [from, to] of shieldedGaps) {
|
|
480
|
+
const group = stillRemoved.filter((comment) => comment.start >= from && comment.start < to);
|
|
481
|
+
if (group.length === 0)
|
|
482
|
+
continue;
|
|
483
|
+
protect(group);
|
|
484
|
+
changed = true;
|
|
485
|
+
}
|
|
486
|
+
for (const target of siblingTargets) {
|
|
487
|
+
const group = stillRemoved.filter((comment) => target.comments.includes(comment));
|
|
488
|
+
if (group.length === 0)
|
|
489
|
+
continue;
|
|
490
|
+
protect(group);
|
|
208
491
|
changed = true;
|
|
209
492
|
}
|
|
210
493
|
return changed;
|
|
211
494
|
};
|
|
212
495
|
collectShields();
|
|
213
|
-
while (
|
|
496
|
+
while (protectShields()) {
|
|
214
497
|
if (!collectShields())
|
|
215
498
|
break;
|
|
216
499
|
}
|
|
217
500
|
if (stillRemoved.length !== removed.length) {
|
|
218
501
|
kept.sort(byStart);
|
|
219
502
|
}
|
|
220
|
-
return stillRemoved;
|
|
503
|
+
return { removed: stillRemoved, lines: shieldedLines };
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* The removals as the edits delete them: a comment-only container whose
|
|
507
|
+
* comments all go — and that is to go braces and all — is one unit spanning
|
|
508
|
+
* its braces, every other comment a unit of its own. Sorted, as the comments.
|
|
509
|
+
*/
|
|
510
|
+
function removalUnits(removals, containerOf) {
|
|
511
|
+
const removing = new Set(removals);
|
|
512
|
+
const units = [];
|
|
513
|
+
for (const comment of removals) {
|
|
514
|
+
const container = containerOf.get(comment);
|
|
515
|
+
if (container === undefined ||
|
|
516
|
+
container.mode !== "drop" ||
|
|
517
|
+
!container.comments.every((inside) => removing.has(inside))) {
|
|
518
|
+
units.push({ start: comment.start, end: comment.end, comments: [comment] });
|
|
519
|
+
}
|
|
520
|
+
else if (units[units.length - 1]?.container !== container) {
|
|
521
|
+
units.push({ start: container.start, end: container.end, comments: container.comments, container });
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
return units;
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* Offset of the first character at or after `from` that is neither
|
|
528
|
+
* whitespace nor inside one of the (sorted, non-overlapping) ranges, or the
|
|
529
|
+
* source length when only ranges follow. `from` lies past the start of at
|
|
530
|
+
* least one range (the directive the search is for); when the closest of
|
|
531
|
+
* them runs past `from` — a block comment opened on the directive's line —
|
|
532
|
+
* the search resumes at its end.
|
|
533
|
+
*/
|
|
534
|
+
function codeStartAfter(source, from, ranges) {
|
|
535
|
+
let next = firstIndexAtOrAfter(ranges, from, (range) => range.start);
|
|
536
|
+
let index = Math.max(from, ranges[next - 1].end);
|
|
537
|
+
while (index < source.length) {
|
|
538
|
+
const range = ranges[next];
|
|
539
|
+
if (range !== undefined && range.start === index) {
|
|
540
|
+
index = range.end;
|
|
541
|
+
next += 1;
|
|
542
|
+
}
|
|
543
|
+
else if (isBlank(source[index])) {
|
|
544
|
+
index += 1;
|
|
545
|
+
}
|
|
546
|
+
else {
|
|
547
|
+
return index;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return source.length;
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* For each TypeScript compiler, the first line between a tsc suppression and
|
|
554
|
+
* the code on the lines below it that the compiler does not step over, if
|
|
555
|
+
* any. Looking for the line a suppression applies to, a compiler skips the
|
|
556
|
+
* lines it reads as blank or as a `//` comment (see TSC_PASSABLE_LINES); any
|
|
557
|
+
* other line — one opening with a block comment, say — ends the search,
|
|
558
|
+
* leaving the suppression without a target for that compiler. Deleting that
|
|
559
|
+
* line would let the suppression reach the code instead, so it has to stay.
|
|
560
|
+
* The two compilers can stop at different lines: a line holding only a
|
|
561
|
+
* vertical tab stops tsc 7, while tsc 6 searches on to the next line it
|
|
562
|
+
* cannot pass.
|
|
563
|
+
*/
|
|
564
|
+
function firstBlockingLines(source, comment, lineOffsets, ranges) {
|
|
565
|
+
// A suppression applies from the line below it on, so code sharing its line
|
|
566
|
+
// (`/* @ts-ignore */ foo();`) does not end the search.
|
|
567
|
+
const codeStart = codeStartAfter(source, lineOffsets[comment.endLine] ?? source.length, ranges);
|
|
568
|
+
if (codeStart >= source.length)
|
|
569
|
+
return [];
|
|
570
|
+
const codeLine = firstIndexAtOrAfter(lineOffsets, codeStart + 1, (offset) => offset);
|
|
571
|
+
const found = [];
|
|
572
|
+
for (const passable of TSC_PASSABLE_LINES) {
|
|
573
|
+
for (let line = comment.endLine + 1; line < codeLine; line += 1) {
|
|
574
|
+
if (!passable.test(source.slice(lineOffsets[line - 1], lineOffsets[line]))) {
|
|
575
|
+
found.push(line);
|
|
576
|
+
break;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
return found;
|
|
221
581
|
}
|
|
222
582
|
/**
|
|
223
|
-
* The
|
|
224
|
-
* non-whitespace character
|
|
225
|
-
*
|
|
226
|
-
*
|
|
583
|
+
* The removals on the given line when one of them opens it (holds its first
|
|
584
|
+
* non-whitespace character), so that deleting them would change how the
|
|
585
|
+
* line starts. Returns undefined when something that survives opens the line
|
|
586
|
+
* instead, or nothing on it is to be removed.
|
|
227
587
|
*/
|
|
228
|
-
function
|
|
588
|
+
function lineOpeningGroup(source, line, lineOffsets, units) {
|
|
589
|
+
// A blocking line always lies above the code line, so the next offset exists.
|
|
590
|
+
const lineStart = lineOffsets[line - 1];
|
|
591
|
+
const lineEnd = lineOffsets[line];
|
|
592
|
+
const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
|
|
593
|
+
let first = lineStart;
|
|
594
|
+
while (first < lineEnd && isBlank(source[first]))
|
|
595
|
+
first += 1;
|
|
596
|
+
return group.some((unit) => unit.start <= first && first < unit.end)
|
|
597
|
+
? group.flatMap((unit) => unit.comments)
|
|
598
|
+
: undefined;
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* The removals that would make the given line disappear: every
|
|
602
|
+
* non-whitespace character of the line lies inside one of them, so deleting
|
|
603
|
+
* them deletes the physical line. Returns undefined when the line survives
|
|
604
|
+
* (it holds code, a kept comment, or nothing to remove at all).
|
|
605
|
+
*/
|
|
606
|
+
function vanishingLineGroup(source, line, lineOffsets, units) {
|
|
229
607
|
// collectShields never shields a line beyond the last one, so the offset exists.
|
|
230
608
|
const lineStart = lineOffsets[line - 1];
|
|
231
609
|
const lineEnd = lineOffsets[line] ?? source.length;
|
|
232
|
-
const group =
|
|
610
|
+
const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
|
|
233
611
|
if (group.length === 0)
|
|
234
612
|
return undefined;
|
|
235
613
|
for (let index = lineStart; index < lineEnd; index += 1) {
|
|
236
614
|
if (isBlank(source[index]))
|
|
237
615
|
continue;
|
|
238
|
-
if (!group.some((
|
|
616
|
+
if (!group.some((unit) => unit.start <= index && index < unit.end))
|
|
239
617
|
return undefined;
|
|
240
618
|
}
|
|
241
|
-
return group;
|
|
619
|
+
return group.flatMap((unit) => unit.comments);
|
|
242
620
|
}
|
|
243
621
|
// These suppressions only apply to the following node/line when they stand on
|
|
244
622
|
// their own line — trailing after code they target (at most) their own line.
|
|
@@ -253,6 +631,33 @@ const TRAILING_TARGETS_OWN_LINE = new Set([
|
|
|
253
631
|
"skipcq",
|
|
254
632
|
"nosemgrep",
|
|
255
633
|
]);
|
|
634
|
+
// The suppressions tsc looks past blank and `//` lines for (see
|
|
635
|
+
// firstBlockingLines). They stay next-line directives as well: Flow, which
|
|
636
|
+
// honours them in TypeScript files, applies them to the very next line.
|
|
637
|
+
const TSC_SUPPRESSIONS = new Set(["@ts-ignore", "@ts-expect-error"]);
|
|
638
|
+
// The lines each compiler steps over on its way up from a diagnostic to a
|
|
639
|
+
// suppression, matched against the whole line, line break included. tsc 6
|
|
640
|
+
// trims the line and accepts nothing or a `//` comment; tsc 7 accepts only
|
|
641
|
+
// spaces and tabs before the line break or the `//`.
|
|
642
|
+
const TSC_PASSABLE_LINES = [/^\s*(?:\/\/|$)/, /^[ \t]*(?:[\r\n]|\/\/)/];
|
|
643
|
+
// Directives that only count while no other comment sits between them and
|
|
644
|
+
// their target: deno_graph reads `@deno-types`/`@ts-types` from the last
|
|
645
|
+
// comment before an import, ts-prune and graphql-tag-pluck from the closest
|
|
646
|
+
// leading comment, and ESLint's no-fallthrough and default-case from the last
|
|
647
|
+
// comment before the next case or the closing brace. Deleting the comments in
|
|
648
|
+
// that gap would newly activate such a directive, so the whole gap is
|
|
649
|
+
// shielded — trailing comments in front of the code included. With no code
|
|
650
|
+
// left there is nothing to target, and the switch-clause comments are only
|
|
651
|
+
// detected in front of the `case`, `default` or `}` their rules read them
|
|
652
|
+
// before.
|
|
653
|
+
const LAST_COMMENT_DIRECTIVES = new Set([
|
|
654
|
+
"@deno-types",
|
|
655
|
+
"@ts-types",
|
|
656
|
+
"ts-prune-ignore-next",
|
|
657
|
+
"GraphQL",
|
|
658
|
+
"fallthrough",
|
|
659
|
+
"no-default",
|
|
660
|
+
]);
|
|
256
661
|
// Directives that target the line or node directly below them. File- and
|
|
257
662
|
// range-scoped pragmas (`c8 ignore start`, `istanbul ignore file`, ...) are
|
|
258
663
|
// absent: they do not target the following line, so they never shield it.
|
|
@@ -283,12 +688,9 @@ const NEXT_LINE_DIRECTIVES = new Set([
|
|
|
283
688
|
// Directives that hang off the node or line directly below them: a
|
|
284
689
|
// whole-line comment between them and their target would change what
|
|
285
690
|
// they apply to, so the line below stays shielded.
|
|
286
|
-
"ts-prune-ignore-next",
|
|
287
691
|
"million-ignore",
|
|
288
692
|
"@million-skip",
|
|
289
693
|
"@million-jsx-skip",
|
|
290
|
-
"@deno-types",
|
|
291
|
-
"@ts-types",
|
|
292
694
|
"deepcode-ignore",
|
|
293
695
|
"skipcq",
|
|
294
696
|
"lgtm",
|
|
@@ -306,7 +708,6 @@ const NEXT_LINE_DIRECTIVES = new Set([
|
|
|
306
708
|
"$FlowIgnore",
|
|
307
709
|
"pragma-allowlist-nextline-secret",
|
|
308
710
|
"cspell:disable-next",
|
|
309
|
-
"GraphQL",
|
|
310
711
|
"HTML",
|
|
311
712
|
]);
|
|
312
713
|
function isNextLineDirective(name) {
|
|
@@ -326,19 +727,19 @@ function shieldedLineCount(comment) {
|
|
|
326
727
|
return match ? Math.max(1, Number(match[1])) : 1;
|
|
327
728
|
}
|
|
328
729
|
/**
|
|
329
|
-
* True when nothing but whitespace shares the
|
|
730
|
+
* True when nothing but whitespace shares the range's first and last lines.
|
|
330
731
|
* Lines are delimited by any ECMAScript line terminator, matching how the
|
|
331
732
|
* scanner numbers them (a lone `\r`, U+2028 and U+2029 count too).
|
|
332
733
|
*/
|
|
333
|
-
function occupiesWholeLines(source,
|
|
334
|
-
for (let index =
|
|
734
|
+
function occupiesWholeLines(source, range) {
|
|
735
|
+
for (let index = range.start - 1; index >= 0; index -= 1) {
|
|
335
736
|
const char = source[index];
|
|
336
737
|
if (LINE_TERMINATOR.test(char))
|
|
337
738
|
break;
|
|
338
739
|
if (!isBlank(char))
|
|
339
740
|
return false;
|
|
340
741
|
}
|
|
341
|
-
for (let index =
|
|
742
|
+
for (let index = range.end; index < source.length; index += 1) {
|
|
342
743
|
const char = source[index];
|
|
343
744
|
if (LINE_TERMINATOR.test(char))
|
|
344
745
|
break;
|
|
@@ -347,114 +748,4 @@ function occupiesWholeLines(source, comment) {
|
|
|
347
748
|
}
|
|
348
749
|
return true;
|
|
349
750
|
}
|
|
350
|
-
const isHorizontalSpace = (char) => char === " " || char === "\t";
|
|
351
|
-
// A global copy of the line-break matcher, so exec() can resume from a removal's end offset.
|
|
352
|
-
const NEXT_LINE_BREAK = new RegExp(LINE_BREAK.source, "g");
|
|
353
|
-
/** End offset (exclusive) of the last line break in the chunk, or -1 when it has none. */
|
|
354
|
-
function lastLineBreakEnd(chunk) {
|
|
355
|
-
for (let index = chunk.length - 1; index >= 0; index -= 1) {
|
|
356
|
-
if (LINE_TERMINATOR.test(chunk[index]))
|
|
357
|
-
return index + 1;
|
|
358
|
-
}
|
|
359
|
-
return -1;
|
|
360
|
-
}
|
|
361
|
-
/**
|
|
362
|
-
* Splices sorted, non-overlapping comment ranges out of the source, tidying lines.
|
|
363
|
-
* Output is accumulated as segments with incremental line-state tracking, so the
|
|
364
|
-
* work stays linear in the source size even for comment-dense files.
|
|
365
|
-
*/
|
|
366
|
-
function splice(source, removals) {
|
|
367
|
-
const segments = [];
|
|
368
|
-
let cursor = 0;
|
|
369
|
-
/** Whether the output's current (unterminated) line is all whitespace so far. */
|
|
370
|
-
let lineIsBlank = true;
|
|
371
|
-
/** Last character of the output so far ("" while empty). */
|
|
372
|
-
let lastChar = "";
|
|
373
|
-
const push = (chunk) => {
|
|
374
|
-
if (chunk === "")
|
|
375
|
-
return;
|
|
376
|
-
segments.push(chunk);
|
|
377
|
-
const breakEnd = lastLineBreakEnd(chunk);
|
|
378
|
-
lineIsBlank = breakEnd === -1 ? lineIsBlank && isBlank(chunk) : isBlank(chunk.slice(breakEnd));
|
|
379
|
-
lastChar = chunk.slice(-1);
|
|
380
|
-
};
|
|
381
|
-
/** Deletes everything the output holds after its last line break. */
|
|
382
|
-
const dropCurrentLine = () => {
|
|
383
|
-
while (segments.length > 0) {
|
|
384
|
-
const last = segments[segments.length - 1];
|
|
385
|
-
const breakEnd = lastLineBreakEnd(last);
|
|
386
|
-
if (breakEnd === -1) {
|
|
387
|
-
segments.pop();
|
|
388
|
-
continue;
|
|
389
|
-
}
|
|
390
|
-
segments[segments.length - 1] = last.slice(0, breakEnd);
|
|
391
|
-
lineIsBlank = true;
|
|
392
|
-
lastChar = last.charAt(breakEnd - 1);
|
|
393
|
-
return;
|
|
394
|
-
}
|
|
395
|
-
lineIsBlank = true;
|
|
396
|
-
lastChar = "";
|
|
397
|
-
};
|
|
398
|
-
/** Deletes trailing spaces/tabs of the output's current line. */
|
|
399
|
-
const trimLineEnd = () => {
|
|
400
|
-
while (segments.length > 0) {
|
|
401
|
-
const last = segments[segments.length - 1];
|
|
402
|
-
let end = last.length;
|
|
403
|
-
while (end > 0 && isHorizontalSpace(last[end - 1]))
|
|
404
|
-
end -= 1;
|
|
405
|
-
if (end === 0) {
|
|
406
|
-
segments.pop();
|
|
407
|
-
continue;
|
|
408
|
-
}
|
|
409
|
-
if (end < last.length)
|
|
410
|
-
segments[segments.length - 1] = last.slice(0, end);
|
|
411
|
-
lastChar = segments[segments.length - 1].slice(-1);
|
|
412
|
-
return;
|
|
413
|
-
}
|
|
414
|
-
lastChar = "";
|
|
415
|
-
};
|
|
416
|
-
for (const comment of removals) {
|
|
417
|
-
push(source.slice(cursor, comment.start));
|
|
418
|
-
cursor = comment.end;
|
|
419
|
-
NEXT_LINE_BREAK.lastIndex = cursor;
|
|
420
|
-
const lineBreak = NEXT_LINE_BREAK.exec(source);
|
|
421
|
-
const contentEnd = lineBreak === null ? source.length : lineBreak.index;
|
|
422
|
-
const lineEnd = lineBreak === null ? source.length : lineBreak.index + lineBreak[0].length;
|
|
423
|
-
const restContent = source.slice(cursor, contentEnd);
|
|
424
|
-
if (lineIsBlank && isBlank(restContent)) {
|
|
425
|
-
// The comment occupied whole line(s): drop them entirely.
|
|
426
|
-
dropCurrentLine();
|
|
427
|
-
cursor = lineEnd;
|
|
428
|
-
}
|
|
429
|
-
else if (isBlank(restContent)) {
|
|
430
|
-
// Trailing comment after code: trim the gap, keep the line break.
|
|
431
|
-
trimLineEnd();
|
|
432
|
-
push(source.slice(contentEnd, lineEnd));
|
|
433
|
-
cursor = lineEnd;
|
|
434
|
-
}
|
|
435
|
-
else if (!lineIsBlank && LINE_TERMINATOR.test(comment.text)) {
|
|
436
|
-
// Code on both sides of a block comment that spans lines. Per ECMA-262
|
|
437
|
-
// such a comment acts as a line terminator for semicolon insertion, so
|
|
438
|
-
// replace it with a real line break (matching the file's style) instead
|
|
439
|
-
// of merging the lines.
|
|
440
|
-
trimLineEnd();
|
|
441
|
-
push(comment.text.includes("\r\n") ? "\r\n" : "\n");
|
|
442
|
-
}
|
|
443
|
-
else {
|
|
444
|
-
// Inline comment with code after it on the same line.
|
|
445
|
-
const next = source.charAt(cursor);
|
|
446
|
-
if (lastChar !== "" && !isBlank(lastChar) && !isBlank(next)) {
|
|
447
|
-
// `a/* x */b` would merge tokens: keep them apart.
|
|
448
|
-
push(" ");
|
|
449
|
-
}
|
|
450
|
-
else if (lastChar === "" || isBlank(lastChar)) {
|
|
451
|
-
// Collapse the horizontal whitespace that followed the comment.
|
|
452
|
-
while (cursor < lineEnd && isHorizontalSpace(source[cursor]))
|
|
453
|
-
cursor += 1;
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
segments.push(source.slice(cursor));
|
|
458
|
-
return segments.join("");
|
|
459
|
-
}
|
|
460
751
|
//# sourceMappingURL=remove.js.map
|