@kongyo2/ts-comment-scanner 1.11.0 → 1.15.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/src/splice.ts CHANGED
@@ -60,8 +60,12 @@ export function applyEdits(source: string, edits: readonly Edit[]): string {
60
60
  lastChar = chunk.slice(-1);
61
61
  };
62
62
 
63
- /** Deletes everything the output holds after its last line break. */
64
- const dropCurrentLine = (): void => {
63
+ /**
64
+ * Deletes everything the output holds after its last line break, and that
65
+ * break too when `takeBreak` asks for it and the break holds no line feed;
66
+ * returns whether it went.
67
+ */
68
+ const dropCurrentLine = (takeBreak: boolean): boolean => {
65
69
  while (segments.length > 0) {
66
70
  const last = segments[segments.length - 1] as string;
67
71
  const breakEnd = lastLineBreakEnd(last);
@@ -69,13 +73,15 @@ export function applyEdits(source: string, edits: readonly Edit[]): string {
69
73
  segments.pop();
70
74
  continue;
71
75
  }
72
- segments[segments.length - 1] = last.slice(0, breakEnd);
73
- lineIsBlank = true;
74
- lastChar = last.charAt(breakEnd - 1);
75
- return;
76
+ const tookBreak = takeBreak && last.charAt(breakEnd - 1) !== "\n";
77
+ segments[segments.length - 1] = last.slice(0, tookBreak ? breakEnd - 1 : breakEnd);
78
+ lineIsBlank = !tookBreak;
79
+ lastChar = last.charAt(breakEnd - (tookBreak ? 2 : 1));
80
+ return tookBreak;
76
81
  }
77
82
  lineIsBlank = true;
78
83
  lastChar = "";
84
+ return false;
79
85
  };
80
86
 
81
87
  /**
@@ -119,9 +125,13 @@ export function applyEdits(source: string, edits: readonly Edit[]): string {
119
125
  const deleted = source.slice(edit.start, edit.end);
120
126
 
121
127
  if (lineIsBlank && isBlank(restContent)) {
122
- // The range occupied whole line(s): drop them entirely.
123
- dropCurrentLine();
124
- cursor = lineEnd;
128
+ // The range occupied whole line(s): drop them entirely, with the
129
+ // break that ends them — or, when that one is a line feed and the
130
+ // break in front of them is not, with that one instead. tree-sitter
131
+ // (and so ast-grep) starts a row at a line feed alone, so the rows
132
+ // around the dropped lines stay apart, while tsc sees one break either
133
+ // way.
134
+ cursor = dropCurrentLine(lineBreak?.[0].endsWith("\n") === true) ? contentEnd : lineEnd;
125
135
  } else if (isBlank(restContent)) {
126
136
  // Trailing after code: trim the gap, keep the line break.
127
137
  trimLineEnd();