@kongyo2/ts-comment-scanner 1.9.0 → 1.11.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.md +264 -22
- package/dist/args.d.ts +2 -0
- package/dist/args.d.ts.map +1 -1
- package/dist/args.js +25 -0
- package/dist/args.js.map +1 -1
- package/dist/directives.d.ts +41 -14
- package/dist/directives.d.ts.map +1 -1
- package/dist/directives.js +103 -32
- 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 +25 -5
- package/dist/remove.d.ts.map +1 -1
- package/dist/remove.js +385 -192
- package/dist/remove.js.map +1 -1
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +5 -1
- package/dist/run.js.map +1 -1
- package/dist/scanner.d.ts +18 -0
- package/dist/scanner.d.ts.map +1 -1
- package/dist/scanner.js +108 -19
- 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/args.ts +28 -0
- package/src/directives.ts +148 -35
- package/src/jsx.ts +263 -0
- package/src/ranges.ts +23 -0
- package/src/remove.ts +461 -199
- package/src/run.ts +5 -1
- package/src/scanner.ts +122 -19
- package/src/splice.ts +317 -0
- package/README.en.md +0 -239
package/dist/remove.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { activePositionalDirectives, bunMarkerOffset, isLegalComment } from "./directives.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { LINE_TERMINATOR, isBlank, lineStartOffsets } from "./lines.js";
|
|
3
|
+
import { firstIndexAtOrAfter } from "./ranges.js";
|
|
4
|
+
import { analyzeSource, isFileWidePosition, opensFirstRow } from "./scanner.js";
|
|
5
|
+
import { applyEdits, blankLineEdits } from "./splice.js";
|
|
4
6
|
/**
|
|
5
7
|
* Removes comments from TypeScript source without touching code.
|
|
6
8
|
*
|
|
@@ -15,6 +17,17 @@ import { scanComments } from "./scanner.js";
|
|
|
15
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,10 +146,11 @@ 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 —
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
149
|
+
* the slice non-blank — it sits at the file start when it begins where Bun
|
|
150
|
+
* reads its marker, and in ast-grep's file-wide position when it also opens
|
|
151
|
+
* the first line with the next node two or more lines down. Header status
|
|
152
|
+
* needs the token structure, but removing comments never moves a comment
|
|
153
|
+
* across the first token, so the positional check does not consult it.
|
|
112
154
|
*/
|
|
113
155
|
function positionalPlacement(text, comment) {
|
|
114
156
|
const shebangLength = /^#!.*/.exec(text)?.[0].length ?? 0;
|
|
@@ -116,6 +158,7 @@ function positionalPlacement(text, comment) {
|
|
|
116
158
|
header: false,
|
|
117
159
|
firstComment: isBlank(text.slice(shebangLength, comment.start)),
|
|
118
160
|
fileStart: comment.start === bunMarkerOffset(text),
|
|
161
|
+
fileWide: isFileWidePosition(text, comment.start, comment.end),
|
|
119
162
|
};
|
|
120
163
|
}
|
|
121
164
|
/** The removal candidate that starts closest before the comment, if any. */
|
|
@@ -128,28 +171,206 @@ function closestRemovalBefore(removals, comment) {
|
|
|
128
171
|
return closest;
|
|
129
172
|
}
|
|
130
173
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
174
|
+
* Applies the edits, restoring the byte-order mark when dropping a
|
|
175
|
+
* comment-only first line took it along: the mark is an encoding artifact,
|
|
176
|
+
* not line content.
|
|
134
177
|
*/
|
|
135
|
-
function
|
|
136
|
-
if (!source.startsWith("
|
|
137
|
-
return
|
|
138
|
-
// The
|
|
178
|
+
function editKeepingBom(source, edits) {
|
|
179
|
+
if (!source.startsWith(""))
|
|
180
|
+
return applyEdits(source, edits);
|
|
181
|
+
// The editing itself works on the BOM-less body (with ranges shifted to
|
|
139
182
|
// match) so its line handling never sees the mark, then the mark is
|
|
140
|
-
// re-attached; this keeps
|
|
183
|
+
// re-attached; this keeps `// gone\n...` from losing the mark with
|
|
141
184
|
// the dropped line.
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
return `\uFEFF${splice(body, shifted)}`;
|
|
185
|
+
const shifted = edits.map((edit) => ({ ...edit, start: edit.start - 1, end: edit.end - 1 }));
|
|
186
|
+
return `${applyEdits(source.slice(1), shifted)}`;
|
|
145
187
|
}
|
|
146
188
|
function isProtected(comment, options) {
|
|
189
|
+
if (options.keep?.(comment) === true)
|
|
190
|
+
return true;
|
|
147
191
|
if (comment.directive !== undefined && options.removeDirectives !== true)
|
|
148
192
|
return true;
|
|
149
193
|
if (isLegalComment(comment.text) && options.removeLegal !== true)
|
|
150
194
|
return true;
|
|
151
195
|
return false;
|
|
152
196
|
}
|
|
197
|
+
function attachContainers(source, layouts, comments) {
|
|
198
|
+
const containers = new Map();
|
|
199
|
+
for (const layout of layouts) {
|
|
200
|
+
const inside = [];
|
|
201
|
+
let index = firstIndexAtOrAfter(comments, layout.start, (comment) => comment.start);
|
|
202
|
+
for (let comment = comments[index]; comment !== undefined && comment.end < layout.end; comment = comments[index]) {
|
|
203
|
+
inside.push(comment);
|
|
204
|
+
index += 1;
|
|
205
|
+
}
|
|
206
|
+
containers.set(layout, {
|
|
207
|
+
start: layout.start,
|
|
208
|
+
end: layout.end,
|
|
209
|
+
segment: layout.segment,
|
|
210
|
+
suspect: layout.suspect,
|
|
211
|
+
next: undefined,
|
|
212
|
+
comments: inside,
|
|
213
|
+
mode: "drop",
|
|
214
|
+
spaceRange: spaceRange(source, layout),
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
for (const [layout, container] of containers) {
|
|
218
|
+
if (layout.next !== undefined)
|
|
219
|
+
container.next = containers.get(layout.next);
|
|
220
|
+
}
|
|
221
|
+
return [...containers.values()];
|
|
222
|
+
}
|
|
223
|
+
const isHorizontalSpace = (char) => char === " " || char === "\t";
|
|
224
|
+
/**
|
|
225
|
+
* The range `{" "}` would take the place of: a one-line container and the
|
|
226
|
+
* spaces before it when code precedes it on its line (`<b /> {/* x */}`), or
|
|
227
|
+
* the spaces after it when it opens its line and code follows. Undefined when
|
|
228
|
+
* no spaces hold it off anything on its line.
|
|
229
|
+
*/
|
|
230
|
+
function spaceRange(source, container) {
|
|
231
|
+
if (LINE_TERMINATOR.test(source.slice(container.start, container.end)))
|
|
232
|
+
return undefined;
|
|
233
|
+
let lineStart = container.start;
|
|
234
|
+
while (lineStart > 0 && !LINE_TERMINATOR.test(source[lineStart - 1]))
|
|
235
|
+
lineStart -= 1;
|
|
236
|
+
let before = container.start;
|
|
237
|
+
while (isHorizontalSpace(source[before - 1]))
|
|
238
|
+
before -= 1;
|
|
239
|
+
let after = container.end;
|
|
240
|
+
while (isHorizontalSpace(source[after]))
|
|
241
|
+
after += 1;
|
|
242
|
+
if (!isBlank(source.slice(lineStart, container.start))) {
|
|
243
|
+
return before < container.start ? { start: before, end: container.end } : undefined;
|
|
244
|
+
}
|
|
245
|
+
const codeAfter = after < source.length && !LINE_TERMINATOR.test(source[after]);
|
|
246
|
+
return codeAfter && after > container.end ? { start: container.start, end: after } : undefined;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Comments, and the comment-only containers that may go along with theirs,
|
|
250
|
+
* as sorted ranges: what the search for the code after a directive skips.
|
|
251
|
+
* A container counts as skippable when all its comments are removal
|
|
252
|
+
* candidates, even if some get protected later — skipping it then only moves
|
|
253
|
+
* the found code further down, which shields more, not less.
|
|
254
|
+
*/
|
|
255
|
+
function skippableRanges(comments, containerOf, candidates) {
|
|
256
|
+
const ranges = [];
|
|
257
|
+
for (const comment of comments) {
|
|
258
|
+
const container = containerOf.get(comment);
|
|
259
|
+
if (container === undefined || !container.comments.every((inside) => candidates.has(inside))) {
|
|
260
|
+
ranges.push(comment);
|
|
261
|
+
}
|
|
262
|
+
else if (ranges[ranges.length - 1] !== container) {
|
|
263
|
+
ranges.push(container);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return ranges;
|
|
267
|
+
}
|
|
268
|
+
/** The edits that remove the comments: one per comment, or one per container going with its braces. */
|
|
269
|
+
function removalEdits(removed, containerOf, droppable) {
|
|
270
|
+
const edits = [];
|
|
271
|
+
for (const comment of removed) {
|
|
272
|
+
const container = containerOf.get(comment);
|
|
273
|
+
if (container === undefined || !droppable.has(container) || container.mode === "keep") {
|
|
274
|
+
edits.push({ start: comment.start, end: comment.end });
|
|
275
|
+
}
|
|
276
|
+
else if (comment === container.comments[0]) {
|
|
277
|
+
edits.push(container.mode === "drop"
|
|
278
|
+
? { start: container.start, end: container.end, jsx: true }
|
|
279
|
+
: { ...container.spaceRange, replacement: '{" "}' });
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return edits;
|
|
283
|
+
}
|
|
284
|
+
/** Indexes of the JSX stretches whose rendering differs; throws when the structure itself does. */
|
|
285
|
+
function changedSegments(before, after) {
|
|
286
|
+
if (before.length !== after.length)
|
|
287
|
+
throw unexpectedResult();
|
|
288
|
+
const changed = new Set();
|
|
289
|
+
for (const [index, rendering] of before.entries()) {
|
|
290
|
+
if (rendering !== after[index])
|
|
291
|
+
changed.add(index);
|
|
292
|
+
}
|
|
293
|
+
return changed;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Decides how each comment-only container goes. Dropping the braces is tried
|
|
297
|
+
* everywhere first; where the re-parsed output then renders a stretch of JSX
|
|
298
|
+
* children differently, that stretch's suspect containers are settled one
|
|
299
|
+
* at a time, in document order — each tries dropping, then `{" "}`, then
|
|
300
|
+
* keeping its braces, while the suspects after it keep theirs — so braces
|
|
301
|
+
* stay only where nothing less keeps the rendering intact. Keeping every
|
|
302
|
+
* suspect's braces always does: the other containers sit between text that
|
|
303
|
+
* renders nothing, which stays so however it is merged or trimmed.
|
|
304
|
+
*/
|
|
305
|
+
// The most suspects of one stretch settled one at a time; beyond that they all keep their braces.
|
|
306
|
+
const MAX_SUSPECTS_SETTLED = 16;
|
|
307
|
+
function containerSearch(containers) {
|
|
308
|
+
let droppable = new Set();
|
|
309
|
+
/** Per stretch that rendered differently: its suspects still to settle, the first being tried. */
|
|
310
|
+
const pending = new Map();
|
|
311
|
+
const advance = (container) => {
|
|
312
|
+
if (container.mode === "keep")
|
|
313
|
+
return false;
|
|
314
|
+
container.mode = container.mode === "drop" && container.spaceRange !== undefined ? "space" : "keep";
|
|
315
|
+
return true;
|
|
316
|
+
};
|
|
317
|
+
return {
|
|
318
|
+
get droppable() {
|
|
319
|
+
return droppable;
|
|
320
|
+
},
|
|
321
|
+
track(now) {
|
|
322
|
+
// Protection only ever takes comments out of the removal, so the set
|
|
323
|
+
// only shrinks and its size tells whether it changed.
|
|
324
|
+
if (now.size === droppable.size)
|
|
325
|
+
return;
|
|
326
|
+
droppable = now;
|
|
327
|
+
pending.clear();
|
|
328
|
+
for (const container of containers)
|
|
329
|
+
container.mode = "drop";
|
|
330
|
+
},
|
|
331
|
+
adjust(changed) {
|
|
332
|
+
let adjusted = false;
|
|
333
|
+
for (const segment of changed) {
|
|
334
|
+
let queue = pending.get(segment);
|
|
335
|
+
if (queue === undefined) {
|
|
336
|
+
// The stretch's first difference: from now on its suspects are
|
|
337
|
+
// settled one at a time, the first trying to drop its braces while
|
|
338
|
+
// the rest keep theirs. Every try costs a parse, so a stretch with
|
|
339
|
+
// a great many of them keeps all their braces straight away.
|
|
340
|
+
queue = containers.filter((c) => c.segment === segment && c.suspect && droppable.has(c));
|
|
341
|
+
const giveUp = queue.length > MAX_SUSPECTS_SETTLED;
|
|
342
|
+
for (const container of giveUp ? queue : queue.slice(1))
|
|
343
|
+
container.mode = "keep";
|
|
344
|
+
pending.set(segment, giveUp ? [] : queue);
|
|
345
|
+
if (giveUp || queue.length > 1) {
|
|
346
|
+
adjusted = true;
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
// The container being tried still changes the rendering: it tries
|
|
351
|
+
// the next mode. Keeping its braces cannot fail, so running out of
|
|
352
|
+
// modes (or of suspects to blame) means the output is not trustworthy.
|
|
353
|
+
const current = queue[0];
|
|
354
|
+
if (current === undefined || !advance(current))
|
|
355
|
+
throw unexpectedResult();
|
|
356
|
+
adjusted = true;
|
|
357
|
+
}
|
|
358
|
+
for (const [segment, queue] of pending) {
|
|
359
|
+
if (changed.has(segment) || queue.length === 0)
|
|
360
|
+
continue;
|
|
361
|
+
// The stretch renders as before: the container being tried is
|
|
362
|
+
// settled, and the next suspect tries dropping its braces.
|
|
363
|
+
queue.shift();
|
|
364
|
+
const next = queue[0];
|
|
365
|
+
if (next !== undefined) {
|
|
366
|
+
next.mode = "drop";
|
|
367
|
+
adjusted = true;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return adjusted;
|
|
371
|
+
},
|
|
372
|
+
};
|
|
373
|
+
}
|
|
153
374
|
/**
|
|
154
375
|
* Re-protects comments whose removal would delete a physical line sitting
|
|
155
376
|
* under a surviving next-line directive (`@ts-expect-error`,
|
|
@@ -157,27 +378,34 @@ function isProtected(comment, options) {
|
|
|
157
378
|
* following code up, silently changing which line the directive applies to —
|
|
158
379
|
* and with it the compiler or linter behaviour. Directives whose tools reach
|
|
159
380
|
* further get more: below a tsc suppression the first line each compiler
|
|
160
|
-
* cannot step over stays,
|
|
381
|
+
* cannot step over stays, between a directive that only counts as the
|
|
161
382
|
* last comment before its target (`@deno-types`, ...) and that target every
|
|
162
|
-
* comment stays
|
|
383
|
+
* comment stays, and a JSX comment container right after a directive in a
|
|
384
|
+
* container (`{/* prettier-ignore */}`, which applies to the next sibling)
|
|
385
|
+
* stays too.
|
|
163
386
|
*
|
|
164
387
|
* The line checks work on whole lines, not single comments: a line holding
|
|
165
388
|
* several removable comments (`/* one */ /* two */`) vanishes just the same,
|
|
166
|
-
* so every comment on it has to stay
|
|
167
|
-
*
|
|
168
|
-
* lines (when directives
|
|
389
|
+
* so every comment on it has to stay — and a comment-only JSX container takes
|
|
390
|
+
* its braces along. Runs to a fixpoint because a re-protected comment can
|
|
391
|
+
* itself be a next-line directive shielding further lines (when directives
|
|
392
|
+
* are being removed selectively).
|
|
169
393
|
*/
|
|
170
|
-
function shieldNextLineDirectives(source, removed, kept, skipped) {
|
|
394
|
+
function shieldNextLineDirectives(source, removed, kept, skipped, context) {
|
|
171
395
|
const lineOffsets = lineStartOffsets(source);
|
|
172
396
|
const totalLines = lineOffsets.length;
|
|
173
|
-
// Where the code after a directive starts does not depend on which comments
|
|
174
|
-
// go, so it is looked up among all of them.
|
|
175
|
-
const allComments = [...removed, ...kept, ...skipped].sort(byStart);
|
|
176
397
|
const shieldedLines = new Set();
|
|
177
398
|
const blockingLines = new Set();
|
|
178
399
|
const shieldedGaps = [];
|
|
400
|
+
const siblingTargets = new Set();
|
|
401
|
+
/** Directives whose own line, up to where they start, has to stay as it is. */
|
|
402
|
+
const linePrefixes = [];
|
|
403
|
+
/** Directives whose last line, from where they end, has to stay as it is. */
|
|
404
|
+
const lineSuffixes = [];
|
|
179
405
|
const processed = new Set();
|
|
180
406
|
let stillRemoved = removed;
|
|
407
|
+
let units;
|
|
408
|
+
const currentUnits = () => (units ??= removalUnits(stillRemoved, context.containerOf));
|
|
181
409
|
const collectShields = () => {
|
|
182
410
|
let added = false;
|
|
183
411
|
for (const comment of [...kept, ...skipped]) {
|
|
@@ -188,7 +416,7 @@ function shieldNextLineDirectives(source, removed, kept, skipped) {
|
|
|
188
416
|
if (directive === undefined)
|
|
189
417
|
continue;
|
|
190
418
|
if (LAST_COMMENT_DIRECTIVES.has(directive)) {
|
|
191
|
-
const codeStart = codeStartAfter(source, comment.end,
|
|
419
|
+
const codeStart = codeStartAfter(source, comment.end, context.skippable);
|
|
192
420
|
if (codeStart > comment.end && codeStart < source.length) {
|
|
193
421
|
shieldedGaps.push([comment.end, codeStart]);
|
|
194
422
|
added = true;
|
|
@@ -196,21 +424,41 @@ function shieldNextLineDirectives(source, removed, kept, skipped) {
|
|
|
196
424
|
continue;
|
|
197
425
|
}
|
|
198
426
|
if (TSC_SUPPRESSIONS.has(directive)) {
|
|
199
|
-
for (const line of firstBlockingLines(source, comment, lineOffsets,
|
|
427
|
+
for (const line of firstBlockingLines(source, comment, lineOffsets, context.skippable)) {
|
|
200
428
|
if (blockingLines.has(line))
|
|
201
429
|
continue;
|
|
202
430
|
blockingLines.add(line);
|
|
203
431
|
added = true;
|
|
204
432
|
}
|
|
205
433
|
}
|
|
434
|
+
if (SIBLING_SENSITIVE.has(directive)) {
|
|
435
|
+
linePrefixes.push(comment);
|
|
436
|
+
if (opensFirstRow(source, comment.start))
|
|
437
|
+
lineSuffixes.push(comment);
|
|
438
|
+
added = true;
|
|
439
|
+
}
|
|
206
440
|
if (!isNextLineDirective(directive))
|
|
207
441
|
continue;
|
|
442
|
+
// In JSX, such a directive in braces applies to the sibling after it,
|
|
443
|
+
// whatever line that is on (prettier leaves the element after
|
|
444
|
+
// `{/* prettier-ignore */}` as is): a comment-only container standing
|
|
445
|
+
// between them has to stay. Range and file pragmas target no sibling.
|
|
446
|
+
const holder = context.containerOf.get(comment);
|
|
447
|
+
if (holder?.next !== undefined && !siblingTargets.has(holder.next)) {
|
|
448
|
+
siblingTargets.add(holder.next);
|
|
449
|
+
added = true;
|
|
450
|
+
}
|
|
208
451
|
// A trailing formatter or scanner suppression (`a = [1]; // oxfmt-ignore`,
|
|
209
452
|
// `secret(); // nosemgrep`) targets its own line, so it shields nothing
|
|
210
|
-
// below it.
|
|
211
|
-
|
|
453
|
+
// below it. In JSX, the container holding the comment is what has to
|
|
454
|
+
// stand on lines of its own. ast-grep looks only at what precedes the
|
|
455
|
+
// comment on its line: with nothing there it suppresses the next line,
|
|
456
|
+
// even when code follows the comment (`/* ast-grep-ignore */ foo();`).
|
|
457
|
+
const trailing = SIBLING_SENSITIVE.has(directive)
|
|
458
|
+
? !opensItsLine(source, comment)
|
|
459
|
+
: TRAILING_TARGETS_OWN_LINE.has(directive) && !occupiesWholeLines(source, holder ?? comment);
|
|
460
|
+
if (trailing)
|
|
212
461
|
continue;
|
|
213
|
-
}
|
|
214
462
|
// Counted pragmas like `c8 ignore next 3` cover several lines.
|
|
215
463
|
for (let offset = 1; offset <= shieldedLineCount(comment); offset += 1) {
|
|
216
464
|
const line = comment.endLine + offset;
|
|
@@ -228,18 +476,19 @@ function shieldNextLineDirectives(source, removed, kept, skipped) {
|
|
|
228
476
|
const grouped = new Set(group);
|
|
229
477
|
stillRemoved = stillRemoved.filter((comment) => !grouped.has(comment));
|
|
230
478
|
kept.push(...group);
|
|
479
|
+
units = undefined;
|
|
231
480
|
};
|
|
232
481
|
const protectShields = () => {
|
|
233
482
|
let changed = false;
|
|
234
483
|
for (const line of shieldedLines) {
|
|
235
|
-
const group = vanishingLineGroup(source, line, lineOffsets,
|
|
484
|
+
const group = vanishingLineGroup(source, line, lineOffsets, currentUnits());
|
|
236
485
|
if (group === undefined)
|
|
237
486
|
continue;
|
|
238
487
|
protect(group);
|
|
239
488
|
changed = true;
|
|
240
489
|
}
|
|
241
490
|
for (const line of blockingLines) {
|
|
242
|
-
const group = lineOpeningGroup(source, line, lineOffsets,
|
|
491
|
+
const group = lineOpeningGroup(source, line, lineOffsets, currentUnits());
|
|
243
492
|
if (group === undefined)
|
|
244
493
|
continue;
|
|
245
494
|
protect(group);
|
|
@@ -252,6 +501,34 @@ function shieldNextLineDirectives(source, removed, kept, skipped) {
|
|
|
252
501
|
protect(group);
|
|
253
502
|
changed = true;
|
|
254
503
|
}
|
|
504
|
+
for (const target of siblingTargets) {
|
|
505
|
+
const group = stillRemoved.filter((comment) => target.comments.includes(comment));
|
|
506
|
+
if (group.length === 0)
|
|
507
|
+
continue;
|
|
508
|
+
protect(group);
|
|
509
|
+
changed = true;
|
|
510
|
+
}
|
|
511
|
+
for (const directive of linePrefixes) {
|
|
512
|
+
// Whatever ends on the directive's line in front of it: the comments
|
|
513
|
+
// that make it a trailing suppression, or that it trails.
|
|
514
|
+
const lineStart = lineOffsets[directive.line - 1];
|
|
515
|
+
const group = currentUnits().filter((unit) => unit.end > lineStart && unit.end <= directive.start);
|
|
516
|
+
if (group.length === 0)
|
|
517
|
+
continue;
|
|
518
|
+
protect(group.flatMap((unit) => unit.comments));
|
|
519
|
+
changed = true;
|
|
520
|
+
}
|
|
521
|
+
for (const directive of lineSuffixes) {
|
|
522
|
+
// Whatever follows the directive on the line it ends on, when it opens
|
|
523
|
+
// the file: deleting the node after it there can leave the next one two
|
|
524
|
+
// lines down, which makes the suppression file-wide.
|
|
525
|
+
const lineEnd = lineOffsets[directive.endLine] ?? source.length;
|
|
526
|
+
const group = currentUnits().filter((unit) => unit.start >= directive.end && unit.start < lineEnd);
|
|
527
|
+
if (group.length === 0)
|
|
528
|
+
continue;
|
|
529
|
+
protect(group.flatMap((unit) => unit.comments));
|
|
530
|
+
changed = true;
|
|
531
|
+
}
|
|
255
532
|
return changed;
|
|
256
533
|
};
|
|
257
534
|
collectShields();
|
|
@@ -262,23 +539,44 @@ function shieldNextLineDirectives(source, removed, kept, skipped) {
|
|
|
262
539
|
if (stillRemoved.length !== removed.length) {
|
|
263
540
|
kept.sort(byStart);
|
|
264
541
|
}
|
|
265
|
-
return stillRemoved;
|
|
542
|
+
return { removed: stillRemoved, lines: shieldedLines };
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* The removals as the edits delete them: a comment-only container whose
|
|
546
|
+
* comments all go — and that is to go braces and all — is one unit spanning
|
|
547
|
+
* its braces, every other comment a unit of its own. Sorted, as the comments.
|
|
548
|
+
*/
|
|
549
|
+
function removalUnits(removals, containerOf) {
|
|
550
|
+
const removing = new Set(removals);
|
|
551
|
+
const units = [];
|
|
552
|
+
for (const comment of removals) {
|
|
553
|
+
const container = containerOf.get(comment);
|
|
554
|
+
if (container === undefined ||
|
|
555
|
+
container.mode !== "drop" ||
|
|
556
|
+
!container.comments.every((inside) => removing.has(inside))) {
|
|
557
|
+
units.push({ start: comment.start, end: comment.end, comments: [comment] });
|
|
558
|
+
}
|
|
559
|
+
else if (units[units.length - 1]?.container !== container) {
|
|
560
|
+
units.push({ start: container.start, end: container.end, comments: container.comments, container });
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return units;
|
|
266
564
|
}
|
|
267
565
|
/**
|
|
268
566
|
* Offset of the first character at or after `from` that is neither
|
|
269
|
-
* whitespace nor inside one of the (sorted, non-overlapping)
|
|
270
|
-
* source length when only
|
|
271
|
-
* least one
|
|
567
|
+
* whitespace nor inside one of the (sorted, non-overlapping) ranges, or the
|
|
568
|
+
* source length when only ranges follow. `from` lies past the start of at
|
|
569
|
+
* least one range (the directive the search is for); when the closest of
|
|
272
570
|
* them runs past `from` — a block comment opened on the directive's line —
|
|
273
571
|
* the search resumes at its end.
|
|
274
572
|
*/
|
|
275
|
-
function codeStartAfter(source, from,
|
|
276
|
-
let next = firstIndexAtOrAfter(
|
|
277
|
-
let index = Math.max(from,
|
|
573
|
+
function codeStartAfter(source, from, ranges) {
|
|
574
|
+
let next = firstIndexAtOrAfter(ranges, from, (range) => range.start);
|
|
575
|
+
let index = Math.max(from, ranges[next - 1].end);
|
|
278
576
|
while (index < source.length) {
|
|
279
|
-
const
|
|
280
|
-
if (
|
|
281
|
-
index =
|
|
577
|
+
const range = ranges[next];
|
|
578
|
+
if (range !== undefined && range.start === index) {
|
|
579
|
+
index = range.end;
|
|
282
580
|
next += 1;
|
|
283
581
|
}
|
|
284
582
|
else if (isBlank(source[index])) {
|
|
@@ -290,19 +588,6 @@ function codeStartAfter(source, from, comments) {
|
|
|
290
588
|
}
|
|
291
589
|
return source.length;
|
|
292
590
|
}
|
|
293
|
-
/** Index of the first item whose key is at least `value`, by binary search over ascending keys. */
|
|
294
|
-
function firstIndexAtOrAfter(items, value, key) {
|
|
295
|
-
let low = 0;
|
|
296
|
-
let high = items.length;
|
|
297
|
-
while (low < high) {
|
|
298
|
-
const middle = (low + high) >> 1;
|
|
299
|
-
if (key(items[middle]) < value)
|
|
300
|
-
low = middle + 1;
|
|
301
|
-
else
|
|
302
|
-
high = middle;
|
|
303
|
-
}
|
|
304
|
-
return low;
|
|
305
|
-
}
|
|
306
591
|
/**
|
|
307
592
|
* For each TypeScript compiler, the first line between a tsc suppression and
|
|
308
593
|
* the code on the lines below it that the compiler does not step over, if
|
|
@@ -315,10 +600,10 @@ function firstIndexAtOrAfter(items, value, key) {
|
|
|
315
600
|
* vertical tab stops tsc 7, while tsc 6 searches on to the next line it
|
|
316
601
|
* cannot pass.
|
|
317
602
|
*/
|
|
318
|
-
function firstBlockingLines(source, comment, lineOffsets,
|
|
603
|
+
function firstBlockingLines(source, comment, lineOffsets, ranges) {
|
|
319
604
|
// A suppression applies from the line below it on, so code sharing its line
|
|
320
605
|
// (`/* @ts-ignore */ foo();`) does not end the search.
|
|
321
|
-
const codeStart = codeStartAfter(source, lineOffsets[comment.endLine] ?? source.length,
|
|
606
|
+
const codeStart = codeStartAfter(source, lineOffsets[comment.endLine] ?? source.length, ranges);
|
|
322
607
|
if (codeStart >= source.length)
|
|
323
608
|
return [];
|
|
324
609
|
const codeLine = firstIndexAtOrAfter(lineOffsets, codeStart + 1, (offset) => offset);
|
|
@@ -334,41 +619,43 @@ function firstBlockingLines(source, comment, lineOffsets, comments) {
|
|
|
334
619
|
return found;
|
|
335
620
|
}
|
|
336
621
|
/**
|
|
337
|
-
* The
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
*
|
|
622
|
+
* The removals on the given line when one of them opens it (holds its first
|
|
623
|
+
* non-whitespace character), so that deleting them would change how the
|
|
624
|
+
* line starts. Returns undefined when something that survives opens the line
|
|
625
|
+
* instead, or nothing on it is to be removed.
|
|
341
626
|
*/
|
|
342
|
-
function lineOpeningGroup(source, line, lineOffsets,
|
|
627
|
+
function lineOpeningGroup(source, line, lineOffsets, units) {
|
|
343
628
|
// A blocking line always lies above the code line, so the next offset exists.
|
|
344
629
|
const lineStart = lineOffsets[line - 1];
|
|
345
630
|
const lineEnd = lineOffsets[line];
|
|
346
|
-
const group =
|
|
631
|
+
const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
|
|
347
632
|
let first = lineStart;
|
|
348
633
|
while (first < lineEnd && isBlank(source[first]))
|
|
349
634
|
first += 1;
|
|
350
|
-
return group.some((
|
|
635
|
+
return group.some((unit) => unit.start <= first && first < unit.end)
|
|
636
|
+
? group.flatMap((unit) => unit.comments)
|
|
637
|
+
: undefined;
|
|
351
638
|
}
|
|
352
639
|
/**
|
|
353
|
-
* The
|
|
354
|
-
* non-whitespace character of the line lies inside one of them, so
|
|
355
|
-
* them
|
|
356
|
-
*
|
|
640
|
+
* The removals that would make the given line disappear: every
|
|
641
|
+
* non-whitespace character of the line lies inside one of them, so deleting
|
|
642
|
+
* them deletes the physical line. Returns undefined when the line survives
|
|
643
|
+
* (it holds code, a kept comment, or nothing to remove at all).
|
|
357
644
|
*/
|
|
358
|
-
function vanishingLineGroup(source, line, lineOffsets,
|
|
645
|
+
function vanishingLineGroup(source, line, lineOffsets, units) {
|
|
359
646
|
// collectShields never shields a line beyond the last one, so the offset exists.
|
|
360
647
|
const lineStart = lineOffsets[line - 1];
|
|
361
648
|
const lineEnd = lineOffsets[line] ?? source.length;
|
|
362
|
-
const group =
|
|
649
|
+
const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
|
|
363
650
|
if (group.length === 0)
|
|
364
651
|
return undefined;
|
|
365
652
|
for (let index = lineStart; index < lineEnd; index += 1) {
|
|
366
653
|
if (isBlank(source[index]))
|
|
367
654
|
continue;
|
|
368
|
-
if (!group.some((
|
|
655
|
+
if (!group.some((unit) => unit.start <= index && index < unit.end))
|
|
369
656
|
return undefined;
|
|
370
657
|
}
|
|
371
|
-
return group;
|
|
658
|
+
return group.flatMap((unit) => unit.comments);
|
|
372
659
|
}
|
|
373
660
|
// These suppressions only apply to the following node/line when they stand on
|
|
374
661
|
// their own line — trailing after code they target (at most) their own line.
|
|
@@ -383,6 +670,15 @@ const TRAILING_TARGETS_OWN_LINE = new Set([
|
|
|
383
670
|
"skipcq",
|
|
384
671
|
"nosemgrep",
|
|
385
672
|
]);
|
|
673
|
+
// ast-grep reads a suppression's reach from the nodes beside it, comments
|
|
674
|
+
// included. It tells a trailing suppression from a standalone one by whether
|
|
675
|
+
// the node before it ends on its line, and keys a trailing one to where that
|
|
676
|
+
// node starts. On the file's first line, a standalone one covers the whole
|
|
677
|
+
// file when the node after it starts two or more lines down. Deleting a
|
|
678
|
+
// comment in front of it on its line can therefore turn it into a next-line
|
|
679
|
+
// suppression or move what it trails, and on the first line deleting the
|
|
680
|
+
// comment after it can make it file-wide — so those comments stay.
|
|
681
|
+
const SIBLING_SENSITIVE = new Set(["ast-grep-ignore"]);
|
|
386
682
|
// The suppressions tsc looks past blank and `//` lines for (see
|
|
387
683
|
// firstBlockingLines). They stay next-line directives as well: Flow, which
|
|
388
684
|
// honours them in TypeScript files, applies them to the very next line.
|
|
@@ -461,6 +757,7 @@ const NEXT_LINE_DIRECTIVES = new Set([
|
|
|
461
757
|
"pragma-allowlist-nextline-secret",
|
|
462
758
|
"cspell:disable-next",
|
|
463
759
|
"HTML",
|
|
760
|
+
"ast-grep-ignore",
|
|
464
761
|
]);
|
|
465
762
|
function isNextLineDirective(name) {
|
|
466
763
|
return name.endsWith("-next-line") || NEXT_LINE_DIRECTIVES.has(name);
|
|
@@ -479,19 +776,25 @@ function shieldedLineCount(comment) {
|
|
|
479
776
|
return match ? Math.max(1, Number(match[1])) : 1;
|
|
480
777
|
}
|
|
481
778
|
/**
|
|
482
|
-
* True when nothing but whitespace shares the
|
|
779
|
+
* True when nothing but whitespace shares the range's first and last lines.
|
|
483
780
|
* Lines are delimited by any ECMAScript line terminator, matching how the
|
|
484
781
|
* scanner numbers them (a lone `\r`, U+2028 and U+2029 count too).
|
|
485
782
|
*/
|
|
486
|
-
function occupiesWholeLines(source,
|
|
487
|
-
|
|
783
|
+
function occupiesWholeLines(source, range) {
|
|
784
|
+
if (!opensItsLine(source, range))
|
|
785
|
+
return false;
|
|
786
|
+
for (let index = range.end; index < source.length; index += 1) {
|
|
488
787
|
const char = source[index];
|
|
489
788
|
if (LINE_TERMINATOR.test(char))
|
|
490
789
|
break;
|
|
491
790
|
if (!isBlank(char))
|
|
492
791
|
return false;
|
|
493
792
|
}
|
|
494
|
-
|
|
793
|
+
return true;
|
|
794
|
+
}
|
|
795
|
+
/** True when nothing but whitespace precedes the range on its first line. */
|
|
796
|
+
function opensItsLine(source, range) {
|
|
797
|
+
for (let index = range.start - 1; index >= 0; index -= 1) {
|
|
495
798
|
const char = source[index];
|
|
496
799
|
if (LINE_TERMINATOR.test(char))
|
|
497
800
|
break;
|
|
@@ -500,114 +803,4 @@ function occupiesWholeLines(source, comment) {
|
|
|
500
803
|
}
|
|
501
804
|
return true;
|
|
502
805
|
}
|
|
503
|
-
const isHorizontalSpace = (char) => char === " " || char === "\t";
|
|
504
|
-
// A global copy of the line-break matcher, so exec() can resume from a removal's end offset.
|
|
505
|
-
const NEXT_LINE_BREAK = new RegExp(LINE_BREAK.source, "g");
|
|
506
|
-
/** End offset (exclusive) of the last line break in the chunk, or -1 when it has none. */
|
|
507
|
-
function lastLineBreakEnd(chunk) {
|
|
508
|
-
for (let index = chunk.length - 1; index >= 0; index -= 1) {
|
|
509
|
-
if (LINE_TERMINATOR.test(chunk[index]))
|
|
510
|
-
return index + 1;
|
|
511
|
-
}
|
|
512
|
-
return -1;
|
|
513
|
-
}
|
|
514
|
-
/**
|
|
515
|
-
* Splices sorted, non-overlapping comment ranges out of the source, tidying lines.
|
|
516
|
-
* Output is accumulated as segments with incremental line-state tracking, so the
|
|
517
|
-
* work stays linear in the source size even for comment-dense files.
|
|
518
|
-
*/
|
|
519
|
-
function splice(source, removals) {
|
|
520
|
-
const segments = [];
|
|
521
|
-
let cursor = 0;
|
|
522
|
-
/** Whether the output's current (unterminated) line is all whitespace so far. */
|
|
523
|
-
let lineIsBlank = true;
|
|
524
|
-
/** Last character of the output so far ("" while empty). */
|
|
525
|
-
let lastChar = "";
|
|
526
|
-
const push = (chunk) => {
|
|
527
|
-
if (chunk === "")
|
|
528
|
-
return;
|
|
529
|
-
segments.push(chunk);
|
|
530
|
-
const breakEnd = lastLineBreakEnd(chunk);
|
|
531
|
-
lineIsBlank = breakEnd === -1 ? lineIsBlank && isBlank(chunk) : isBlank(chunk.slice(breakEnd));
|
|
532
|
-
lastChar = chunk.slice(-1);
|
|
533
|
-
};
|
|
534
|
-
/** Deletes everything the output holds after its last line break. */
|
|
535
|
-
const dropCurrentLine = () => {
|
|
536
|
-
while (segments.length > 0) {
|
|
537
|
-
const last = segments[segments.length - 1];
|
|
538
|
-
const breakEnd = lastLineBreakEnd(last);
|
|
539
|
-
if (breakEnd === -1) {
|
|
540
|
-
segments.pop();
|
|
541
|
-
continue;
|
|
542
|
-
}
|
|
543
|
-
segments[segments.length - 1] = last.slice(0, breakEnd);
|
|
544
|
-
lineIsBlank = true;
|
|
545
|
-
lastChar = last.charAt(breakEnd - 1);
|
|
546
|
-
return;
|
|
547
|
-
}
|
|
548
|
-
lineIsBlank = true;
|
|
549
|
-
lastChar = "";
|
|
550
|
-
};
|
|
551
|
-
/** Deletes trailing spaces/tabs of the output's current line. */
|
|
552
|
-
const trimLineEnd = () => {
|
|
553
|
-
while (segments.length > 0) {
|
|
554
|
-
const last = segments[segments.length - 1];
|
|
555
|
-
let end = last.length;
|
|
556
|
-
while (end > 0 && isHorizontalSpace(last[end - 1]))
|
|
557
|
-
end -= 1;
|
|
558
|
-
if (end === 0) {
|
|
559
|
-
segments.pop();
|
|
560
|
-
continue;
|
|
561
|
-
}
|
|
562
|
-
if (end < last.length)
|
|
563
|
-
segments[segments.length - 1] = last.slice(0, end);
|
|
564
|
-
lastChar = segments[segments.length - 1].slice(-1);
|
|
565
|
-
return;
|
|
566
|
-
}
|
|
567
|
-
lastChar = "";
|
|
568
|
-
};
|
|
569
|
-
for (const comment of removals) {
|
|
570
|
-
push(source.slice(cursor, comment.start));
|
|
571
|
-
cursor = comment.end;
|
|
572
|
-
NEXT_LINE_BREAK.lastIndex = cursor;
|
|
573
|
-
const lineBreak = NEXT_LINE_BREAK.exec(source);
|
|
574
|
-
const contentEnd = lineBreak === null ? source.length : lineBreak.index;
|
|
575
|
-
const lineEnd = lineBreak === null ? source.length : lineBreak.index + lineBreak[0].length;
|
|
576
|
-
const restContent = source.slice(cursor, contentEnd);
|
|
577
|
-
if (lineIsBlank && isBlank(restContent)) {
|
|
578
|
-
// The comment occupied whole line(s): drop them entirely.
|
|
579
|
-
dropCurrentLine();
|
|
580
|
-
cursor = lineEnd;
|
|
581
|
-
}
|
|
582
|
-
else if (isBlank(restContent)) {
|
|
583
|
-
// Trailing comment after code: trim the gap, keep the line break.
|
|
584
|
-
trimLineEnd();
|
|
585
|
-
push(source.slice(contentEnd, lineEnd));
|
|
586
|
-
cursor = lineEnd;
|
|
587
|
-
}
|
|
588
|
-
else if (!lineIsBlank && LINE_TERMINATOR.test(comment.text)) {
|
|
589
|
-
// Code on both sides of a block comment that spans lines. Per ECMA-262
|
|
590
|
-
// such a comment acts as a line terminator for semicolon insertion, so
|
|
591
|
-
// replace it with a real line break (matching the file's style) instead
|
|
592
|
-
// of merging the lines.
|
|
593
|
-
trimLineEnd();
|
|
594
|
-
push(comment.text.includes("\r\n") ? "\r\n" : "\n");
|
|
595
|
-
}
|
|
596
|
-
else {
|
|
597
|
-
// Inline comment with code after it on the same line.
|
|
598
|
-
const next = source.charAt(cursor);
|
|
599
|
-
if (lastChar !== "" && !isBlank(lastChar) && !isBlank(next)) {
|
|
600
|
-
// `a/* x */b` would merge tokens: keep them apart.
|
|
601
|
-
push(" ");
|
|
602
|
-
}
|
|
603
|
-
else if (lastChar === "" || isBlank(lastChar)) {
|
|
604
|
-
// Collapse the horizontal whitespace that followed the comment.
|
|
605
|
-
while (cursor < lineEnd && isHorizontalSpace(source[cursor]))
|
|
606
|
-
cursor += 1;
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
segments.push(source.slice(cursor));
|
|
611
|
-
return segments.join("");
|
|
612
|
-
}
|
|
613
806
|
//# sourceMappingURL=remove.js.map
|