@kongyo2/ts-comment-scanner 1.20.0 → 1.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/README.md +240 -52
  2. package/dist/args.d.ts.map +1 -1
  3. package/dist/args.js +23 -19
  4. package/dist/args.js.map +1 -1
  5. package/dist/astgrep.d.ts +5 -3
  6. package/dist/astgrep.d.ts.map +1 -1
  7. package/dist/astgrep.js +10 -12
  8. package/dist/astgrep.js.map +1 -1
  9. package/dist/containers.d.ts +48 -0
  10. package/dist/containers.d.ts.map +1 -0
  11. package/dist/containers.js +82 -0
  12. package/dist/containers.js.map +1 -0
  13. package/dist/coverage.d.ts +45 -0
  14. package/dist/coverage.d.ts.map +1 -0
  15. package/dist/coverage.js +92 -0
  16. package/dist/coverage.js.map +1 -0
  17. package/dist/directives.d.ts +21 -3
  18. package/dist/directives.d.ts.map +1 -1
  19. package/dist/directives.js +31 -23
  20. package/dist/directives.js.map +1 -1
  21. package/dist/files.d.ts +7 -3
  22. package/dist/files.d.ts.map +1 -1
  23. package/dist/files.js +129 -82
  24. package/dist/files.js.map +1 -1
  25. package/dist/imports.d.ts.map +1 -1
  26. package/dist/imports.js +14 -8
  27. package/dist/imports.js.map +1 -1
  28. package/dist/index.d.ts +1 -1
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +1 -1
  31. package/dist/index.js.map +1 -1
  32. package/dist/jsx.d.ts.map +1 -1
  33. package/dist/jsx.js +5 -6
  34. package/dist/jsx.js.map +1 -1
  35. package/dist/lines.d.ts +24 -5
  36. package/dist/lines.d.ts.map +1 -1
  37. package/dist/lines.js +50 -16
  38. package/dist/lines.js.map +1 -1
  39. package/dist/ranges.d.ts +8 -0
  40. package/dist/ranges.d.ts.map +1 -1
  41. package/dist/ranges.js +12 -0
  42. package/dist/ranges.js.map +1 -1
  43. package/dist/remove.d.ts +18 -14
  44. package/dist/remove.d.ts.map +1 -1
  45. package/dist/remove.js +69 -630
  46. package/dist/remove.js.map +1 -1
  47. package/dist/run.d.ts.map +1 -1
  48. package/dist/run.js +21 -28
  49. package/dist/run.js.map +1 -1
  50. package/dist/scanner.d.ts +34 -0
  51. package/dist/scanner.d.ts.map +1 -1
  52. package/dist/scanner.js +137 -31
  53. package/dist/scanner.js.map +1 -1
  54. package/dist/shields.d.ts +72 -0
  55. package/dist/shields.d.ts.map +1 -0
  56. package/dist/shields.js +624 -0
  57. package/dist/shields.js.map +1 -0
  58. package/dist/splice.d.ts +42 -11
  59. package/dist/splice.d.ts.map +1 -1
  60. package/dist/splice.js +189 -56
  61. package/dist/splice.js.map +1 -1
  62. package/package.json +2 -1
  63. package/src/args.ts +21 -21
  64. package/src/astgrep.ts +11 -13
  65. package/src/containers.ts +128 -0
  66. package/src/coverage.ts +110 -0
  67. package/src/directives.ts +38 -20
  68. package/src/files.ts +169 -91
  69. package/src/imports.ts +15 -8
  70. package/src/index.ts +1 -0
  71. package/src/jsx.ts +5 -6
  72. package/src/lines.ts +55 -14
  73. package/src/ranges.ts +17 -0
  74. package/src/remove.ts +82 -730
  75. package/src/run.ts +22 -34
  76. package/src/scanner.ts +180 -32
  77. package/src/shields.ts +701 -0
  78. package/src/splice.ts +224 -60
package/src/remove.ts CHANGED
@@ -1,21 +1,18 @@
1
- import { astGrepReach, isFileWidePosition, rowStartOffsets, type AstGrepReach, type PrecedingNode } from "./astgrep.js";
2
- import {
3
- activePositionalDirectives,
4
- bunMarkerOffset,
5
- carriesAstGrepSuppression,
6
- isLegalComment,
7
- type DirectivePlacement,
8
- } from "./directives.js";
1
+ import { isFileWidePosition } from "./astgrep.js";
2
+ import { attachContainers, emptiedBy, mergeContainers, type Container } from "./containers.js";
3
+ import { activePositionalDirectives, bunMarkerOffset, isLegalComment, type DirectivePlacement } from "./directives.js";
9
4
  import type { ImportGroupGap } from "./imports.js";
10
- import type { JsxContainer } from "./jsx.js";
11
- import { LINE_TERMINATOR, isBlank, lineStartOffsets } from "./lines.js";
12
- import { firstIndexAtOrAfter, type Range } from "./ranges.js";
13
- import { analyzeSource } from "./scanner.js";
14
- import { applyEdits, blankLineEdits, type Edit } from "./splice.js";
5
+ import { LINE_TERMINATOR, isBlank } from "./lines.js";
6
+ import { byStart, type Range } from "./ranges.js";
7
+ import { analyzeSource, type ScanOptions, type SourceAnalysis } from "./scanner.js";
8
+ import { shieldDirectives, type ShieldContext } from "./shields.js";
9
+ import { applyEdits, blankLineEdits, heldLines, type Edit, type ShieldedLines } from "./splice.js";
15
10
  import type { Comment } from "./types.js";
16
11
 
17
12
  export interface RemoveOptions {
18
13
  jsx?: boolean;
14
+ /** The source is JavaScript (see ScanOptions.javascript). */
15
+ javascript?: boolean;
19
16
  /** Also remove compiler/linter directive comments. Default: false (kept for safety). */
20
17
  removeDirectives?: boolean;
21
18
  /** Also remove license/legal comments. Default: false (kept for safety). */
@@ -37,15 +34,12 @@ export interface RemoveResult {
37
34
  removed: Comment[];
38
35
  /**
39
36
  * Comments kept because they are protected: directives, license headers,
40
- * comments the `keep` predicate claims, comments whose removal would
41
- * change what an ast-grep suppression covers (the ones in front of it on
42
- * its row decide which node it trails), comments whose removal would shift
43
- * code under a next-line directive or join another line to one a directive
44
- * covers, or that sit between a directive and the code it reaches past
45
- * them for (or the JSX sibling it applies to), and comments whose removal
46
- * would move a position-dependent directive
47
- * (prettier's first-comment pragma, Deno's coverage file pragma, Bun's
48
- * file-start marker) into effect.
37
+ * comments the `keep` predicate claims, and the comments a directive that
38
+ * stays is read with — one that stands between it and the code it would
39
+ * otherwise apply to, or in front of it where it would otherwise open its
40
+ * line (see shieldDirectives) — or whose removal would move a
41
+ * position-dependent directive (prettier's first-comment pragma, Deno's
42
+ * coverage file pragma, Bun's file-start marker) into effect.
49
43
  */
50
44
  kept: Comment[];
51
45
  /** Comments excluded from removal by the `shouldRemove` predicate. */
@@ -60,11 +54,16 @@ export interface RemoveResult {
60
54
  * - AST-based ranges: strings, template literals and regexes are never affected.
61
55
  * - Directives (`@ts-expect-error`, `eslint-disable`, ...) and license headers are
62
56
  * kept by default, since deleting them changes build/lint behaviour.
63
- * - Surviving comments keep their directive semantics: when deleting a leading
64
- * comment would newly activate a position-dependent directive in a kept
65
- * comment (prettier's `@format` docblock pragma counts only as the file's
66
- * first comment, Bun's `// @bun` only at the very start or right after a
67
- * shebang line), the leading comment is kept instead.
57
+ * - Surviving comments keep their directive semantics. The lines a directive
58
+ * covers stay lines of their own: where deleting the comments on one would
59
+ * empty it, an empty line stays in its place, so that the code below moves
60
+ * up under no next-line directive or counted coverage hint, and where it
61
+ * would join one to another line, the comment's own line breaks stay. A
62
+ * comment stays only where a tool reads the directive with it: one between
63
+ * a directive and the code it would otherwise apply to, or one whose
64
+ * removal would newly activate a position-dependent directive (prettier's
65
+ * `@format` docblock pragma counts only as the file's first comment, Bun's
66
+ * `// @bun` only at the very start or right after a shebang line).
68
67
  * - A space is inserted where removing a block comment would merge two tokens,
69
68
  * and a line break is kept where the comment acted as one (ASI stays intact).
70
69
  * - A JSX comment goes with its braces (`{/* note *​/}`), unless that would
@@ -91,8 +90,8 @@ export interface RemoveResult {
91
90
  * set, an error is thrown instead of returning corrupted output.
92
91
  */
93
92
  export function removeComments(source: string, options: RemoveOptions = {}): RemoveResult {
94
- const jsx = options.jsx === true;
95
- const analysis = analyzeSource(source, { jsx });
93
+ const scan: ScanOptions = { jsx: options.jsx === true, javascript: options.javascript === true };
94
+ const analysis = analyzeSource(source, scan);
96
95
  const comments = analysis.comments;
97
96
 
98
97
  let removed: Comment[] = [];
@@ -114,9 +113,13 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
114
113
  for (const comment of container.comments) containerOf.set(comment, container);
115
114
  }
116
115
  const context: ShieldContext = {
116
+ comments,
117
117
  containerOf,
118
118
  skippable: skippableRanges(comments, containerOf, new Set(removed)),
119
119
  preceding: analysis.preceding,
120
+ directives: analysis.directives,
121
+ shadows: analysis.shadows,
122
+ casts: analysis.casts,
120
123
  };
121
124
  const search = containerSearch(containers);
122
125
  // Collapsing blank lines can only move a surviving comment to the very
@@ -130,7 +133,7 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
130
133
  const holders = new Set<Comment>();
131
134
 
132
135
  for (;;) {
133
- const shielding = shieldNextLineDirectives(source, removed, kept, skipped, context);
136
+ const shielding = shieldDirectives(source, removed, kept, skipped, context);
134
137
  removed = shielding.removed;
135
138
 
136
139
  if (removed.length === 0) {
@@ -138,12 +141,16 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
138
141
  }
139
142
 
140
143
  const removing = new Set(removed);
141
- search.track(new Set(containers.filter((container) => container.comments.every((c) => removing.has(c)))));
144
+ search.track(new Set(containers.filter((container) => emptiedBy(container, removing))));
142
145
  const edits = removalEdits(removed, containerOf, search.droppable, holders);
143
146
  const deletions = edits.filter((edit) => edit.replacement === undefined);
144
- const blankEdits = tidyBlankLines ? blankLineEdits(source, deletions, comments, shielding.lines, analysis.jsx) : [];
145
- const code = editKeepingBom(source, [...edits, ...blankEdits].sort(byStart));
146
- const output = analyzeSource(code, { jsx });
147
+ const lines: ShieldedLines = {
148
+ shielded: shielding.lines,
149
+ held: heldLines(source, deletions, comments, shielding.lines),
150
+ };
151
+ const blankEdits = tidyBlankLines ? blankLineEdits(source, deletions, comments, lines, analysis.jsx) : [];
152
+ const code = editKeepingBom(source, [...edits, ...blankEdits].sort(byStart), lines);
153
+ const output = analyzeSource(code, scan);
147
154
 
148
155
  if (search.adjust(changedSegments(analysis.jsx.segments, output.jsx.segments))) continue;
149
156
 
@@ -157,7 +164,7 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
157
164
  continue;
158
165
  }
159
166
 
160
- const survivor = firstChangedSurvivor(source, code, output.comments, kept, skipped);
167
+ const survivor = firstChangedSurvivor(source, analysis, code, output, kept, skipped);
161
168
  if (survivor === undefined) {
162
169
  return { code, removed, kept, skipped, changed: true };
163
170
  }
@@ -168,7 +175,9 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
168
175
 
169
176
  // A surviving comment changed meaning: with everything before it deleted
170
177
  // it became the file's first comment (or its very first bytes), turning a
171
- // previously inert positional directive live. Re-protect the removal
178
+ // previously inert positional directive live — or, in JavaScript, a
179
+ // multi-line comment in front of it went for a line break, after which
180
+ // tsc reads it as the JSDoc of the code behind it. Re-protect the removal
172
181
  // candidate closest before it and try again: that comment then keeps
173
182
  // holding the survivor away from the activating position, exactly as the
174
183
  // input did. (Looping, because the re-protected comment can itself be a
@@ -188,9 +197,14 @@ function unexpectedResult(): Error {
188
197
  return new Error("comment removal produced an unexpected result; refusing to continue");
189
198
  }
190
199
 
191
- /** Sort comparator for document order: ranges are ordered by where they start. */
192
- function byStart(a: Range, b: Range): number {
193
- return a.start - b.start;
200
+ /**
201
+ * The items of the source's list and of the output's that correspond, in
202
+ * pairs; throws when the lists differ in length, since removing comments
203
+ * never adds or drops what they list.
204
+ */
205
+ function corresponding<T>(before: readonly T[], after: readonly T[]): Array<[T, T]> {
206
+ if (before.length !== after.length) throw unexpectedResult();
207
+ return before.map((item, index) => [item, after[index] as T]);
194
208
  }
195
209
 
196
210
  /**
@@ -198,29 +212,28 @@ function byStart(a: Range, b: Range): number {
198
212
  * survive, in document order (removal never reorders them). Throws when the
199
213
  * texts themselves differ (the output would be corrupted) and returns the
200
214
  * first surviving comment whose directive semantics changed, or undefined
201
- * when the output is exactly as expected. Besides the canonical directive
202
- * name, the position-dependent pragmas are compared on their own: a comment
215
+ * when the output is exactly as expected. Every directive a comment carries
216
+ * is compared, and what tsc reads from it as JSDoc in JavaScript. The
217
+ * position-dependent pragmas are compared on their own besides: a comment
203
218
  * can carry an inert `@format` underneath the directive an earlier rule
204
- * already named (an eslint config block, say), and that pragma going live
205
- * must not hide behind the unchanged name.
219
+ * already named (an eslint config block, say), and where an ast-grep
220
+ * suppression covers the file does not show in its name.
206
221
  */
207
222
  function firstChangedSurvivor(
208
223
  source: string,
224
+ analysis: SourceAnalysis,
209
225
  code: string,
210
- actual: Comment[],
226
+ output: SourceAnalysis,
211
227
  kept: Comment[],
212
228
  skipped: Comment[],
213
229
  ): Comment | undefined {
214
- const expected = [...kept, ...skipped].sort(byStart);
215
- if (actual.length !== expected.length) {
216
- throw unexpectedResult();
217
- }
218
- for (const [index, comment] of expected.entries()) {
219
- const survivor = actual[index] as Comment;
230
+ const carried = (of: SourceAnalysis, comment: Comment): string =>
231
+ JSON.stringify([of.directives.get(comment) ?? [], of.typed.get(comment) ?? []]);
232
+ for (const [comment, survivor] of corresponding([...kept, ...skipped].sort(byStart), output.comments)) {
220
233
  if (survivor.text !== comment.text) {
221
234
  throw unexpectedResult();
222
235
  }
223
- if (survivor.directive !== comment.directive) return comment;
236
+ if (carried(output, survivor) !== carried(analysis, comment)) return comment;
224
237
  const before = activePositionalDirectives(comment.kind, comment.text, positionalPlacement(source, comment));
225
238
  const after = activePositionalDirectives(survivor.kind, survivor.text, positionalPlacement(code, survivor));
226
239
  if (before.join("\n") !== after.join("\n")) return comment;
@@ -278,10 +291,8 @@ function gapHolders(
278
291
  removals: readonly Comment[],
279
292
  holders: ReadonlySet<Comment>,
280
293
  ): Comment[] {
281
- if (before.length !== after.length) throw unexpectedResult();
282
294
  const found: Comment[] = [];
283
- for (const [index, gap] of before.entries()) {
284
- const { splits, spaced } = after[index] as ImportGroupGap;
295
+ for (const [gap, { splits, spaced }] of corresponding(before, after)) {
285
296
  const intact =
286
297
  gap.splits.every((sorter) => splits.includes(sorter)) && gap.spaced.every((linter) => spaced.includes(linter));
287
298
  if (intact) continue;
@@ -303,14 +314,14 @@ function gapHolders(
303
314
  * comment-only first line took it along: the mark is an encoding artifact,
304
315
  * not line content.
305
316
  */
306
- function editKeepingBom(source: string, edits: readonly Edit[]): string {
307
- if (!source.startsWith("")) return applyEdits(source, edits);
317
+ function editKeepingBom(source: string, edits: readonly Edit[], lines: ShieldedLines): string {
318
+ if (!source.startsWith("")) return applyEdits(source, edits, lines);
308
319
  // The editing itself works on the BOM-less body (with ranges shifted to
309
320
  // match) so its line handling never sees the mark, then the mark is
310
321
  // re-attached; this keeps `// gone\n...` from losing the mark with
311
322
  // the dropped line.
312
323
  const shifted = edits.map((edit) => ({ ...edit, start: edit.start - 1, end: edit.end - 1 }));
313
- return `${applyEdits(source.slice(1), shifted)}`;
324
+ return `${applyEdits(source.slice(1), shifted, lines)}`;
314
325
  }
315
326
 
316
327
  function isProtected(comment: Comment, options: RemoveOptions): boolean {
@@ -320,75 +331,6 @@ function isProtected(comment: Comment, options: RemoveOptions): boolean {
320
331
  return false;
321
332
  }
322
333
 
323
- /**
324
- * How a comment-only JSX container goes once all its comments do: braces
325
- * and all; replaced by `{" "}` together with the spaces that held it off its
326
- * neighbour; or with its braces kept, only the comments going.
327
- */
328
- type ContainerMode = "drop" | "space" | "keep";
329
-
330
- /** A comment-only JSX container (`{/* note *​/}`) with the comments in it. */
331
- interface Container extends Range {
332
- segment: number;
333
- suspect: boolean;
334
- /** The comment-only container right after it among the siblings, if any. */
335
- next: Container | undefined;
336
- comments: Comment[];
337
- mode: ContainerMode;
338
- /** What `{" "}` would replace, when the container shares its line and is held off by spaces. */
339
- spaceRange: Range | undefined;
340
- }
341
-
342
- function attachContainers(source: string, layouts: readonly JsxContainer[], comments: readonly Comment[]): Container[] {
343
- const containers = new Map<JsxContainer, Container>();
344
- for (const layout of layouts) {
345
- const inside: Comment[] = [];
346
- let index = firstIndexAtOrAfter(comments, layout.start, (comment) => comment.start);
347
- for (let comment = comments[index]; comment !== undefined && comment.end < layout.end; comment = comments[index]) {
348
- inside.push(comment);
349
- index += 1;
350
- }
351
- containers.set(layout, {
352
- start: layout.start,
353
- end: layout.end,
354
- segment: layout.segment,
355
- suspect: layout.suspect,
356
- next: undefined,
357
- comments: inside,
358
- mode: "drop",
359
- spaceRange: spaceRange(source, layout),
360
- });
361
- }
362
- for (const [layout, container] of containers) {
363
- if (layout.next !== undefined) container.next = containers.get(layout.next);
364
- }
365
- return [...containers.values()];
366
- }
367
-
368
- const isHorizontalSpace = (char: string | undefined): boolean => char === " " || char === "\t";
369
-
370
- /**
371
- * The range `{" "}` would take the place of: a one-line container and the
372
- * spaces before it when code precedes it on its line (`<b /> {/* x *​/}`), or
373
- * the spaces after it when it opens its line and code follows. Undefined when
374
- * no spaces hold it off anything on its line.
375
- */
376
- function spaceRange(source: string, container: Range): Range | undefined {
377
- if (LINE_TERMINATOR.test(source.slice(container.start, container.end))) return undefined;
378
- let lineStart = container.start;
379
- while (lineStart > 0 && !LINE_TERMINATOR.test(source[lineStart - 1] as string)) lineStart -= 1;
380
- let before = container.start;
381
- while (isHorizontalSpace(source[before - 1])) before -= 1;
382
- let after = container.end;
383
- while (isHorizontalSpace(source[after])) after += 1;
384
-
385
- if (!isBlank(source.slice(lineStart, container.start))) {
386
- return before < container.start ? { start: before, end: container.end } : undefined;
387
- }
388
- const codeAfter = after < source.length && !LINE_TERMINATOR.test(source[after] as string);
389
- return codeAfter && after > container.end ? { start: container.start, end: after } : undefined;
390
- }
391
-
392
334
  /**
393
335
  * Comments, and the comment-only containers that may go along with theirs,
394
336
  * as sorted ranges: what the search for the code after a directive skips.
@@ -401,16 +343,7 @@ function skippableRanges(
401
343
  containerOf: ReadonlyMap<Comment, Container>,
402
344
  candidates: ReadonlySet<Comment>,
403
345
  ): Range[] {
404
- const ranges: Range[] = [];
405
- for (const comment of comments) {
406
- const container = containerOf.get(comment);
407
- if (container === undefined || !container.comments.every((inside) => candidates.has(inside))) {
408
- ranges.push(comment);
409
- } else if (ranges[ranges.length - 1] !== container) {
410
- ranges.push(container);
411
- }
412
- }
413
- return ranges;
346
+ return mergeContainers(comments, containerOf, (container) => emptiedBy(container, candidates));
414
347
  }
415
348
 
416
349
  /**
@@ -424,28 +357,24 @@ function removalEdits(
424
357
  droppable: ReadonlySet<Container>,
425
358
  holders: ReadonlySet<Comment>,
426
359
  ): Edit[] {
427
- const edits: Edit[] = [];
428
- for (const comment of removed) {
429
- const container = containerOf.get(comment);
430
- if (container === undefined || !droppable.has(container) || container.mode === "keep") {
431
- edits.push({ start: comment.start, end: comment.end, blankLine: holders.has(comment) });
432
- } else if (comment === container.comments[0]) {
433
- edits.push(
434
- container.mode === "drop"
435
- ? { start: container.start, end: container.end, jsx: true }
436
- : { ...(container.spaceRange as Range), replacement: '{" "}' },
437
- );
438
- }
439
- }
440
- return edits;
360
+ const units = mergeContainers(
361
+ removed,
362
+ containerOf,
363
+ (container) => droppable.has(container) && container.mode !== "keep",
364
+ );
365
+ return units.map((unit): Edit => {
366
+ if (!("comments" in unit)) return { start: unit.start, end: unit.end, blankLine: holders.has(unit) };
367
+ return unit.mode === "drop"
368
+ ? { start: unit.start, end: unit.end, jsx: true }
369
+ : { ...(unit.spaceRange as Range), replacement: '{" "}' };
370
+ });
441
371
  }
442
372
 
443
373
  /** Indexes of the JSX stretches whose rendering differs; throws when the structure itself does. */
444
374
  function changedSegments(before: readonly string[], after: readonly string[]): Set<number> {
445
- if (before.length !== after.length) throw unexpectedResult();
446
375
  const changed = new Set<number>();
447
- for (const [index, rendering] of before.entries()) {
448
- if (rendering !== after[index]) changed.add(index);
376
+ for (const [index, [rendering, now]] of corresponding(before, after).entries()) {
377
+ if (rendering !== now) changed.add(index);
449
378
  }
450
379
  return changed;
451
380
  }
@@ -535,580 +464,3 @@ function containerSearch(containers: readonly Container[]): ContainerSearch {
535
464
  },
536
465
  };
537
466
  }
538
-
539
- interface ShieldContext {
540
- /** The comment-only JSX container each comment in one sits in. */
541
- containerOf: ReadonlyMap<Comment, Container>;
542
- /** Comments and the containers that may go with theirs, sorted: what code starts after. */
543
- skippable: readonly Range[];
544
- /** What stands in front of each comment carrying an ast-grep suppression (see SourceAnalysis). */
545
- preceding: ReadonlyMap<Comment, PrecedingNode>;
546
- }
547
-
548
- interface Shielding {
549
- removed: Comment[];
550
- /**
551
- * Lines (1-based) that a surviving directive reaches: the ones below a
552
- * next-line directive, and the one a line directive stands on.
553
- */
554
- lines: Set<number>;
555
- }
556
-
557
- /**
558
- * What deleting part of the removal takes out of the source: a single
559
- * comment, or a comment-only JSX container going braces and all.
560
- */
561
- interface RemovalUnit extends Range {
562
- comments: readonly Comment[];
563
- container?: Container;
564
- }
565
-
566
- /**
567
- * Re-protects comments whose removal would delete a physical line sitting
568
- * under a surviving next-line directive (`@ts-expect-error`,
569
- * `eslint-disable-next-line`, ...). Dropping such a line would shift the
570
- * following code up, silently changing which line the directive applies to —
571
- * and with it the compiler or linter behaviour. Nor may a removal join
572
- * another line to one a directive covers, the line a line directive
573
- * (`eslint-disable-line`, ...) stands on included. Directives whose tools
574
- * reach further get more: below a tsc suppression the first line each
575
- * compiler cannot step over stays, oxlint's line-feed rows add the lines they
576
- * run over, between a directive that only counts as the last comment before
577
- * its target (`@deno-types`, ...) or a coverage hint that ast-v8-to-istanbul
578
- * only applies to the node right after it and that target every comment
579
- * stays, and a JSX comment container right after a directive in a container
580
- * (`{/* prettier-ignore *​/}`, which applies to the next sibling) stays too.
581
- *
582
- * The line checks work on whole lines, not single comments: a line holding
583
- * several removable comments (`/* one *​/ /* two *​/`) vanishes just the same,
584
- * so every comment on it has to stay — and a comment-only JSX container takes
585
- * its braces along. Runs to a fixpoint because a re-protected comment can
586
- * itself be a next-line directive shielding further lines (when directives
587
- * are being removed selectively).
588
- */
589
- function shieldNextLineDirectives(
590
- source: string,
591
- removed: Comment[],
592
- kept: Comment[],
593
- skipped: Comment[],
594
- context: ShieldContext,
595
- ): Shielding {
596
- const lineOffsets = lineStartOffsets(source);
597
- const totalLines = lineOffsets.length;
598
- const shieldedLines = new Set<number>();
599
- const blockingLines = new Set<number>();
600
- const shieldedGaps: Array<readonly [number, number]> = [];
601
- const siblingTargets = new Set<Container>();
602
- const astGrepReaches: AstGrepReach[] = [];
603
- const processed = new Set<Comment>();
604
- let stillRemoved = removed;
605
- let units: RemovalUnit[] | undefined;
606
- const currentUnits = (): RemovalUnit[] => (units ??= removalUnits(stillRemoved, context.containerOf));
607
- let rows: number[] | undefined;
608
- /** The (1-based) line holding the offset. */
609
- const lineOf = (offset: number): number => firstIndexAtOrAfter(lineOffsets, offset + 1, (start) => start);
610
-
611
- const collectShields = (): boolean => {
612
- let added = false;
613
- const shieldLines = (from: number, to: number): void => {
614
- for (let line = from; line <= Math.min(to, totalLines); line += 1) {
615
- if (shieldedLines.has(line)) continue;
616
- shieldedLines.add(line);
617
- added = true;
618
- }
619
- };
620
- for (const comment of [...kept, ...skipped]) {
621
- if (processed.has(comment)) continue;
622
- processed.add(comment);
623
- const directive = comment.directive;
624
- if (directive === undefined) continue;
625
- if (carriesAstGrepSuppression(comment.text)) {
626
- const reach = astGrepReach(
627
- source,
628
- (rows ??= rowStartOffsets(source)),
629
- comment,
630
- context.preceding.get(comment) as PrecedingNode,
631
- );
632
- astGrepReaches.push(reach);
633
- if (reach.nextRow !== undefined && reach.nextRow.end > reach.nextRow.start) {
634
- for (let line = lineOf(reach.nextRow.start); line <= lineOf(reach.nextRow.end - 1); line += 1) {
635
- shieldedLines.add(line);
636
- }
637
- }
638
- added = true;
639
- }
640
- const lastComment = LAST_COMMENT_DIRECTIVES.has(directive);
641
- if (lastComment || NEXT_TOKEN_DIRECTIVES.has(directive)) {
642
- const codeStart = codeStartAfter(source, comment.end, context.skippable);
643
- if (codeStart > comment.end && codeStart < source.length) {
644
- shieldedGaps.push([comment.end, codeStart]);
645
- added = true;
646
- }
647
- if (lastComment) continue;
648
- }
649
- if (TSC_SUPPRESSIONS.has(directive)) {
650
- for (const line of firstBlockingLines(source, comment, lineOffsets, context.skippable)) {
651
- if (blockingLines.has(line)) continue;
652
- blockingLines.add(line);
653
- added = true;
654
- }
655
- }
656
- // oxlint reaches its rows' lines past the ones counted below.
657
- if (OXLINT_ROW_DIRECTIVES.has(directive)) {
658
- const reach = oxlintReachLines(source, (rows ??= rowStartOffsets(source)), lineOf, comment, directive);
659
- if (reach !== undefined) shieldLines(reach.from, reach.to);
660
- }
661
- // A line directive covers the line it stands on, which no removal may
662
- // join to another (see joiningLineGroup).
663
- if (isOwnLineDirective(directive)) shieldLines(comment.line, comment.line);
664
- if (!isNextLineDirective(directive)) continue;
665
- // In JSX, such a directive in braces applies to the sibling after it,
666
- // whatever line that is on (prettier leaves the element after
667
- // `{/* prettier-ignore */}` as is): a comment-only container standing
668
- // between them has to stay. Range and file pragmas target no sibling.
669
- const holder = context.containerOf.get(comment);
670
- if (holder?.next !== undefined && !siblingTargets.has(holder.next)) {
671
- siblingTargets.add(holder.next);
672
- added = true;
673
- }
674
- // A trailing formatter or scanner suppression (`a = [1]; // oxfmt-ignore`,
675
- // `secret(); // nosemgrep`) targets its own line, so it shields nothing
676
- // below it. In JSX, the container holding the comment is what has to
677
- // stand on lines of its own.
678
- if (TRAILING_TARGETS_OWN_LINE.has(directive) && !occupiesWholeLines(source, holder ?? comment)) continue;
679
- // Counted pragmas like `c8 ignore next 3` cover several lines.
680
- shieldLines(comment.endLine + 1, comment.endLine + shieldedLineCount(comment));
681
- }
682
- return added;
683
- };
684
-
685
- const protect = (group: readonly Comment[]): void => {
686
- const grouped = new Set(group);
687
- stillRemoved = stillRemoved.filter((comment) => !grouped.has(comment));
688
- kept.push(...group);
689
- units = undefined;
690
- };
691
-
692
- const protectShields = (): boolean => {
693
- let changed = false;
694
- for (const line of shieldedLines) {
695
- const group =
696
- vanishingLineGroup(source, line, lineOffsets, currentUnits()) ??
697
- joiningLineGroup(source, line, lineOffsets, currentUnits());
698
- if (group === undefined) continue;
699
- protect(group);
700
- changed = true;
701
- }
702
- for (const line of blockingLines) {
703
- const group = lineOpeningGroup(source, line, lineOffsets, currentUnits());
704
- if (group === undefined) continue;
705
- protect(group);
706
- changed = true;
707
- }
708
- for (const [from, to] of shieldedGaps) {
709
- const group = stillRemoved.filter((comment) => comment.start >= from && comment.start < to);
710
- if (group.length === 0) continue;
711
- protect(group);
712
- changed = true;
713
- }
714
- for (const target of siblingTargets) {
715
- const group = stillRemoved.filter((comment) => target.comments.includes(comment));
716
- if (group.length === 0) continue;
717
- protect(group);
718
- changed = true;
719
- }
720
- for (const reach of astGrepReaches) {
721
- const group = currentUnits().filter((unit) => reach.holds(unit));
722
- if (group.length === 0) continue;
723
- protect(group.flatMap((unit) => unit.comments));
724
- changed = true;
725
- }
726
- return changed;
727
- };
728
-
729
- collectShields();
730
- while (protectShields()) {
731
- if (!collectShields()) break;
732
- }
733
- if (stillRemoved.length !== removed.length) {
734
- kept.sort(byStart);
735
- }
736
- return { removed: stillRemoved, lines: shieldedLines };
737
- }
738
-
739
- /**
740
- * The removals as the edits delete them: a comment-only container whose
741
- * comments all go — and that is to go braces and all — is one unit spanning
742
- * its braces, every other comment a unit of its own. Sorted, as the comments.
743
- */
744
- function removalUnits(removals: readonly Comment[], containerOf: ReadonlyMap<Comment, Container>): RemovalUnit[] {
745
- const removing = new Set(removals);
746
- const units: RemovalUnit[] = [];
747
- for (const comment of removals) {
748
- const container = containerOf.get(comment);
749
- if (
750
- container === undefined ||
751
- container.mode !== "drop" ||
752
- !container.comments.every((inside) => removing.has(inside))
753
- ) {
754
- units.push({ start: comment.start, end: comment.end, comments: [comment] });
755
- } else if (units[units.length - 1]?.container !== container) {
756
- units.push({ start: container.start, end: container.end, comments: container.comments, container });
757
- }
758
- }
759
- return units;
760
- }
761
-
762
- /**
763
- * Offset of the first character at or after `from` that is neither
764
- * whitespace nor inside one of the (sorted, non-overlapping) ranges, or the
765
- * source length when only ranges follow. `from` lies past the start of at
766
- * least one range (the directive the search is for); when the closest of
767
- * them runs past `from` — a block comment opened on the directive's line —
768
- * the search resumes at its end.
769
- */
770
- function codeStartAfter(source: string, from: number, ranges: readonly Range[]): number {
771
- let next = firstIndexAtOrAfter(ranges, from, (range) => range.start);
772
- let index = Math.max(from, (ranges[next - 1] as Range).end);
773
- while (index < source.length) {
774
- const range = ranges[next];
775
- if (range !== undefined && range.start === index) {
776
- index = range.end;
777
- next += 1;
778
- } else if (isBlank(source[index] as string)) {
779
- index += 1;
780
- } else {
781
- return index;
782
- }
783
- }
784
- return source.length;
785
- }
786
-
787
- /**
788
- * For each TypeScript compiler, the first line between a tsc suppression and
789
- * the code on the lines below it that the compiler does not step over, if
790
- * any. Looking for the line a suppression applies to, a compiler skips the
791
- * lines it reads as blank or as a `//` comment (see TSC_PASSABLE_LINES); any
792
- * other line — one opening with a block comment, say — ends the search,
793
- * leaving the suppression without a target for that compiler. Deleting that
794
- * line would let the suppression reach the code instead, so it has to stay.
795
- * The two compilers can stop at different lines: a line holding only a
796
- * vertical tab stops tsc 7, while tsc 6 searches on to the next line it
797
- * cannot pass.
798
- */
799
- function firstBlockingLines(
800
- source: string,
801
- comment: Comment,
802
- lineOffsets: readonly number[],
803
- ranges: readonly Range[],
804
- ): number[] {
805
- // A suppression applies from the line below it on, so code sharing its line
806
- // (`/* @ts-ignore */ foo();`) does not end the search.
807
- const codeStart = codeStartAfter(source, lineOffsets[comment.endLine] ?? source.length, ranges);
808
- if (codeStart >= source.length) return [];
809
- const codeLine = firstIndexAtOrAfter(lineOffsets, codeStart + 1, (offset) => offset);
810
- const found: number[] = [];
811
- for (const passable of TSC_PASSABLE_LINES) {
812
- for (let line = comment.endLine + 1; line < codeLine; line += 1) {
813
- if (!passable.test(source.slice(lineOffsets[line - 1], lineOffsets[line]))) {
814
- found.push(line);
815
- break;
816
- }
817
- }
818
- }
819
- return found;
820
- }
821
-
822
- /**
823
- * The removals on the given line when one of them opens it (holds its first
824
- * non-whitespace character), so that deleting them would change how the
825
- * line starts. Returns undefined when something that survives opens the line
826
- * instead, or nothing on it is to be removed.
827
- */
828
- function lineOpeningGroup(
829
- source: string,
830
- line: number,
831
- lineOffsets: readonly number[],
832
- units: readonly RemovalUnit[],
833
- ): Comment[] | undefined {
834
- // A blocking line always lies above the code line, so the next offset exists.
835
- const lineStart = lineOffsets[line - 1] as number;
836
- const lineEnd = lineOffsets[line] as number;
837
- const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
838
- let first = lineStart;
839
- while (first < lineEnd && isBlank(source[first] as string)) first += 1;
840
- return group.some((unit) => unit.start <= first && first < unit.end)
841
- ? group.flatMap((unit) => unit.comments)
842
- : undefined;
843
- }
844
-
845
- /**
846
- * The removals that would make the given line disappear: every
847
- * non-whitespace character of the line lies inside one of them, so deleting
848
- * them deletes the physical line. Returns undefined when the line survives
849
- * (it holds code, a kept comment, or nothing to remove at all).
850
- */
851
- function vanishingLineGroup(
852
- source: string,
853
- line: number,
854
- lineOffsets: readonly number[],
855
- units: readonly RemovalUnit[],
856
- ): Comment[] | undefined {
857
- // collectShields never shields a line beyond the last one, so the offset exists.
858
- const lineStart = lineOffsets[line - 1] as number;
859
- const lineEnd = lineOffsets[line] ?? source.length;
860
- const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
861
- if (group.length === 0) return undefined;
862
- for (let index = lineStart; index < lineEnd; index += 1) {
863
- if (isBlank(source[index] as string)) continue;
864
- if (!group.some((unit) => unit.start <= index && index < unit.end)) return undefined;
865
- }
866
- return group.flatMap((unit) => unit.comments);
867
- }
868
-
869
- /**
870
- * The removals that would join the given line to another: they reach past
871
- * one of its ends, with something other than whitespace beyond them on the
872
- * line they run into. Deleting such a comment takes its line breaks along —
873
- * with nothing in their place right after an opening bracket or before
874
- * closing punctuation (see applyEdits) — so what stands beyond it can land on
875
- * the line, or what stands on the line move off it, and a directive covering
876
- * the line would cover other code. (With only whitespace beyond, the lines
877
- * the comment held vanish, which vanishingLineGroup weighs.)
878
- */
879
- function joiningLineGroup(
880
- source: string,
881
- line: number,
882
- lineOffsets: readonly number[],
883
- units: readonly RemovalUnit[],
884
- ): Comment[] | undefined {
885
- const lineStart = lineOffsets[line - 1] as number;
886
- const nextLineStart = lineOffsets[line];
887
- const lineEnd = nextLineStart ?? source.length;
888
- const group = units.filter(
889
- (unit) =>
890
- unit.start < lineEnd &&
891
- unit.end > lineStart &&
892
- ((nextLineStart !== undefined && unit.end > nextLineStart && !closesItsLine(source, unit)) ||
893
- (unit.start < lineStart && !opensItsLine(source, unit))),
894
- );
895
- return group.length === 0 ? undefined : group.flatMap((unit) => unit.comments);
896
- }
897
-
898
- // The directives oxlint reads on rows of its own (see oxlintReachLines).
899
- const OXLINT_ROW_DIRECTIVES = new Set([
900
- "eslint-disable-next-line",
901
- "oxlint-disable-next-line",
902
- "eslint-disable-line",
903
- "oxlint-disable-line",
904
- ]);
905
-
906
- /**
907
- * The lines (1-based, inclusive) that oxlint applies an `eslint-` or
908
- * `oxlint-` line or next-line comment to, past the ones the line-based
909
- * shields take in. oxlint finds the stretch on rows that only a line feed
910
- * ends (`split_inclusive('\n')` and `lines()` in its `disable_directives.rs`):
911
- * a next-line comment covers the rest of the row its text ends on and the
912
- * row after it, a line comment its own row up to the comment. A lone CR,
913
- * U+2028 or U+2029 — a line break to tsc and ESLint — ends no row there, so
914
- * a row can run over several of the lines counted here. Undefined when a
915
- * next-line comment stands on the file's last row: oxlint then covers the
916
- * rest of the file, which no removal changes.
917
- */
918
- function oxlintReachLines(
919
- source: string,
920
- rows: readonly number[],
921
- lineOf: (offset: number) => number,
922
- comment: Comment,
923
- directive: string,
924
- ): { from: number; to: number } | undefined {
925
- const rowOf = (offset: number): number => firstIndexAtOrAfter(rows, offset + 1, (start) => start) - 1;
926
- if (!directive.endsWith("-next-line")) {
927
- return { from: lineOf(rows[rowOf(comment.start)] as number), to: comment.line };
928
- }
929
- // The comment's text ends in front of a block comment's `*/`.
930
- const row = rowOf(comment.kind === "block" ? comment.end - 2 : comment.end);
931
- if (rows[row + 1] === undefined) return undefined;
932
- return { from: comment.endLine + 1, to: lineOf((rows[row + 2] ?? source.length) - 1) };
933
- }
934
-
935
- /**
936
- * Whether the directive covers the line it stands on: the `-line` forms of
937
- * the suppressions (`eslint-disable-line`, `tslint:disable-line`, ...).
938
- */
939
- function isOwnLineDirective(name: string): boolean {
940
- return name.endsWith("-line") && !name.endsWith("-next-line");
941
- }
942
-
943
- // These suppressions only apply to the following node/line when they stand on
944
- // their own line — trailing after code they target (at most) their own line.
945
- // Other next-line directives (`@ts-ignore`, `no-dd-sa`, ...) target the next
946
- // line even when they trail code.
947
- const TRAILING_TARGETS_OWN_LINE = new Set([
948
- "prettier-ignore",
949
- "oxfmt-ignore",
950
- "dprint-ignore",
951
- "lgtm",
952
- "codeql",
953
- "skipcq",
954
- "nosemgrep",
955
- ]);
956
-
957
- // The suppressions tsc looks past blank and `//` lines for (see
958
- // firstBlockingLines). They stay next-line directives as well: Flow, which
959
- // honours them in TypeScript files, applies them to the very next line.
960
- const TSC_SUPPRESSIONS = new Set(["@ts-ignore", "@ts-expect-error"]);
961
-
962
- // The lines each compiler steps over on its way up from a diagnostic to a
963
- // suppression, matched against the whole line, line break included. tsc 6
964
- // trims the line and accepts nothing or a `//` comment; tsc 7 accepts only
965
- // spaces and tabs before the line break or the `//`.
966
- const TSC_PASSABLE_LINES = [/^\s*(?:\/\/|$)/, /^[ \t]*(?:[\r\n]|\/\/)/];
967
-
968
- // Directives that only count while no other comment sits between them and
969
- // their target: deno_graph reads `@deno-types`/`@ts-types` from the last
970
- // comment before an import, ts-prune and graphql-tag-pluck from the closest
971
- // leading comment, and ESLint's no-fallthrough and default-case from the last
972
- // comment before the next case or the closing brace. Deleting the comments in
973
- // that gap would newly activate such a directive, so the whole gap is
974
- // shielded — trailing comments in front of the code included. With no code
975
- // left there is nothing to target, and the switch-clause comments are only
976
- // detected in front of the `case`, `default` or `}` their rules read them
977
- // before.
978
- const LAST_COMMENT_DIRECTIVES = new Set([
979
- "@deno-types",
980
- "@ts-types",
981
- "ts-prune-ignore-next",
982
- "GraphQL",
983
- "fallthrough",
984
- "no-default",
985
- ]);
986
-
987
- // The coverage hints ast-v8-to-istanbul, Vitest's v8 provider, applies to the
988
- // node that starts at the very token after them: with another comment in
989
- // between, the hint has no target, and deleting that comment would bring it
990
- // into effect. The gap up to the code is shielded like the one above, on top
991
- // of the lines below the hint, which c8, Node and monocart count.
992
- const NEXT_TOKEN_DIRECTIVES = new Set([
993
- "istanbul-ignore-next",
994
- "istanbul-ignore-if",
995
- "istanbul-ignore-else",
996
- "c8-ignore-next",
997
- "c8-ignore-if",
998
- "c8-ignore-else",
999
- "v8-ignore-next",
1000
- "v8-ignore-if",
1001
- "v8-ignore-else",
1002
- "node:coverage-ignore",
1003
- "node:coverage-ignore-if",
1004
- "node:coverage-ignore-else",
1005
- ]);
1006
-
1007
- // Directives that target the line or node directly below them. File- and
1008
- // range-scoped pragmas (`istanbul-ignore-file`, `c8-ignore-start`, ...) are
1009
- // absent: they do not target the following line, so they never shield it.
1010
- // They go by their names here: Vitest's v8 provider reads a range hint
1011
- // spelled out on any line of this file, and would drop the rest of the file
1012
- // from this project's coverage.
1013
- const NEXT_LINE_DIRECTIVES = new Set([
1014
- "@ts-ignore",
1015
- "@ts-expect-error",
1016
- "biome-ignore",
1017
- "deno-lint-ignore",
1018
- "deno-fmt-ignore",
1019
- "deno-coverage-ignore",
1020
- "prettier-ignore",
1021
- "oxfmt-ignore",
1022
- "dprint-ignore",
1023
- "rome-ignore",
1024
- "istanbul-ignore-next",
1025
- "istanbul-ignore-if",
1026
- "istanbul-ignore-else",
1027
- "c8-ignore-next",
1028
- "c8-ignore-if",
1029
- "c8-ignore-else",
1030
- "v8-ignore-next",
1031
- "v8-ignore-if",
1032
- "v8-ignore-else",
1033
- "node:coverage-ignore",
1034
- "node:coverage-ignore-if",
1035
- "node:coverage-ignore-else",
1036
- // Directives that hang off the node or line directly below them: a
1037
- // whole-line comment between them and their target would change what
1038
- // they apply to, so the line below stays shielded.
1039
- "million-ignore",
1040
- "@million-skip",
1041
- "@million-jsx-skip",
1042
- "deepcode-ignore",
1043
- "skipcq",
1044
- "lgtm",
1045
- "codeql",
1046
- "nosemgrep",
1047
- "no-dd-sa",
1048
- "datadog-disable",
1049
- "noinspection",
1050
- "language-injection",
1051
- "@next-codemod-error",
1052
- "@next-codemod-ignore",
1053
- "$FlowFixMe",
1054
- "$FlowExpectedError",
1055
- "$FlowIssue",
1056
- "$FlowIgnore",
1057
- "pragma-allowlist-nextline-secret",
1058
- "cspell:disable-next",
1059
- "HTML",
1060
- ]);
1061
-
1062
- function isNextLineDirective(name: string): boolean {
1063
- return name.endsWith("-next-line") || NEXT_LINE_DIRECTIVES.has(name);
1064
- }
1065
-
1066
- // The coverage pragmas a line count after `ignore next` applies to: c8
1067
- // (v8-to-istanbul), Node's test-runner coverage and monocart-coverage-reports
1068
- // count lines. istanbul and ast-v8-to-istanbul, Vitest's providers, apply the
1069
- // hint to the next node whatever count follows, so `istanbul ignore next 3`
1070
- // covers one line. Everything else — `@ts-expect-error`,
1071
- // `eslint-disable-next-line` and friends — covers exactly one line too, no
1072
- // matter what prose like "we ignore next 3 legacy calls" appears in the
1073
- // reason text.
1074
- const COUNTED_NEXT_LINE = new Set(["c8-ignore-next", "v8-ignore-next", "node:coverage-ignore"]);
1075
-
1076
- /**
1077
- * Number of following lines a next-line pragma covers (`c8 ignore next 3` →
1078
- * 3). monocart reads the count past any whitespace or none, and c8 and
1079
- * monocart stop at the last digit, so `next3` and `next 3x` count 3 as well.
1080
- */
1081
- function shieldedLineCount(comment: Comment): number {
1082
- if (comment.directive === undefined || !COUNTED_NEXT_LINE.has(comment.directive)) return 1;
1083
- const match = /\bignore\s*next\s*(\d+)/.exec(comment.text);
1084
- return match ? Math.max(1, Number(match[1])) : 1;
1085
- }
1086
-
1087
- /**
1088
- * True when nothing but whitespace shares the range's first and last lines.
1089
- * Lines are delimited by any ECMAScript line terminator, matching how the
1090
- * scanner numbers them (a lone `\r`, U+2028 and U+2029 count too).
1091
- */
1092
- function occupiesWholeLines(source: string, range: Range): boolean {
1093
- return opensItsLine(source, range) && closesItsLine(source, range);
1094
- }
1095
-
1096
- /** True when nothing but whitespace follows the range on its last line. */
1097
- function closesItsLine(source: string, range: Range): boolean {
1098
- for (let index = range.end; index < source.length; index += 1) {
1099
- const char = source[index] as string;
1100
- if (LINE_TERMINATOR.test(char)) break;
1101
- if (!isBlank(char)) return false;
1102
- }
1103
- return true;
1104
- }
1105
-
1106
- /** True when nothing but whitespace precedes the range on its first line. */
1107
- function opensItsLine(source: string, range: Range): boolean {
1108
- for (let index = range.start - 1; index >= 0; index -= 1) {
1109
- const char = source[index] as string;
1110
- if (LINE_TERMINATOR.test(char)) break;
1111
- if (!isBlank(char)) return false;
1112
- }
1113
- return true;
1114
- }