@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/src/run.ts
CHANGED
|
@@ -48,6 +48,8 @@ Removal:
|
|
|
48
48
|
--dry-run With --remove: show what would be removed, change nothing
|
|
49
49
|
--remove-directives With --remove: also delete directive comments
|
|
50
50
|
--remove-legal With --remove: also delete license/legal comments
|
|
51
|
+
--keep <regex> With --remove: also keep comments matching the regular
|
|
52
|
+
expression, like directives (repeatable)
|
|
51
53
|
|
|
52
54
|
General:
|
|
53
55
|
-h, --help Show this help
|
|
@@ -58,7 +60,8 @@ skipping node_modules and .git. Ignore globs without a slash match file names,
|
|
|
58
60
|
and a trailing slash (dist/) restricts a glob to directories. JavaScript files
|
|
59
61
|
(.js, .jsx, .mjs, .cjs) added with --ext are parsed with JSX enabled. Removal
|
|
60
62
|
keeps directives and license headers unless explicitly requested, so builds
|
|
61
|
-
and linters keep working
|
|
63
|
+
and linters keep working; --keep protects the markers only a project's own
|
|
64
|
+
tooling reads, e.g. --keep "@skipMangle" or --keep "^// INJECT".
|
|
62
65
|
|
|
63
66
|
--diff narrows any scan or removal to files changed in git: a single revision
|
|
64
67
|
compares the working tree against it (HEAD covers all uncommitted work,
|
|
@@ -229,6 +232,7 @@ async function runRemove(options: CliOptions, collectOptions: CollectOptions, io
|
|
|
229
232
|
jsx: isJsxFile(file),
|
|
230
233
|
removeDirectives: options.removeDirectives,
|
|
231
234
|
removeLegal: options.removeLegal,
|
|
235
|
+
keep: (comment) => options.keep.some((pattern) => pattern.test(comment.text)),
|
|
232
236
|
shouldRemove: (comment) => inScope(comment, options.directives),
|
|
233
237
|
});
|
|
234
238
|
if (result.changed) {
|
package/src/scanner.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
2
|
import { bunMarkerOffset, detectDirective, type DirectivePlacement } from "./directives.js";
|
|
3
|
+
import { jsxLayout, type JsxLayout } from "./jsx.js";
|
|
4
|
+
import { isBlank } from "./lines.js";
|
|
5
|
+
import { firstIndexAtOrAfter } from "./ranges.js";
|
|
3
6
|
import type { Comment, CommentKind } from "./types.js";
|
|
4
7
|
|
|
5
8
|
export interface ScanOptions {
|
|
@@ -7,14 +10,27 @@ export interface ScanOptions {
|
|
|
7
10
|
}
|
|
8
11
|
|
|
9
12
|
export function scanComments(source: string, options: ScanOptions = {}): Comment[] {
|
|
13
|
+
return analyzeSource(source, options).comments;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A source's comments together with the layout of its JSX children, from a single parse. */
|
|
17
|
+
export interface SourceAnalysis {
|
|
18
|
+
comments: Comment[];
|
|
19
|
+
jsx: JsxLayout;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function analyzeSource(source: string, options: ScanOptions = {}): SourceAnalysis {
|
|
10
23
|
const jsx = options.jsx === true;
|
|
11
24
|
const fileName = jsx ? "module.tsx" : "module.ts";
|
|
12
25
|
const scriptKind = jsx ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
13
26
|
const sourceFile = ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true, scriptKind);
|
|
14
27
|
|
|
15
28
|
const jsxTextSpans: Array<[number, number]> = [];
|
|
29
|
+
const jsxParents: Array<ts.JsxElement | ts.JsxFragment> = [];
|
|
16
30
|
const ranges: ts.CommentRange[] = [];
|
|
17
31
|
const seen = new Set<number>();
|
|
32
|
+
/** Every token, in document order. */
|
|
33
|
+
const tokens: ts.Node[] = [];
|
|
18
34
|
|
|
19
35
|
const collect = (found: readonly ts.CommentRange[] | undefined): void => {
|
|
20
36
|
if (!found) return;
|
|
@@ -31,11 +47,13 @@ export function scanComments(source: string, options: ScanOptions = {}): Comment
|
|
|
31
47
|
const pending: ts.Node[] = [sourceFile];
|
|
32
48
|
while (pending.length > 0) {
|
|
33
49
|
const node = pending.pop() as ts.Node;
|
|
50
|
+
if (ts.isJsxElement(node) || ts.isJsxFragment(node)) jsxParents.push(node);
|
|
34
51
|
// JSDoc nodes start inside the comment text itself; descending into them
|
|
35
52
|
// would hide the comment (e.g. a trailing JSDoc attached to the
|
|
36
53
|
// end-of-file token). Treat them as trivia like any other comment.
|
|
37
54
|
const children = node.getChildren(sourceFile).filter((child) => !ts.isJSDoc(child));
|
|
38
55
|
if (children.length === 0) {
|
|
56
|
+
tokens.push(node);
|
|
39
57
|
if (node.kind === ts.SyntaxKind.JsxText) {
|
|
40
58
|
jsxTextSpans.push([node.getFullStart(), node.getEnd()]);
|
|
41
59
|
}
|
|
@@ -78,30 +96,39 @@ export function scanComments(source: string, options: ScanOptions = {}): Comment
|
|
|
78
96
|
const shebangEnd = ts.getShebang(source)?.length ?? 0;
|
|
79
97
|
const firstCommentPos = sorted.length > 0 ? (sorted[0] as ts.CommentRange).pos : -1;
|
|
80
98
|
const bunMarkerPos = bunMarkerOffset(source);
|
|
81
|
-
// The token after
|
|
82
|
-
//
|
|
83
|
-
//
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
99
|
+
// The token after a comment, past any further comments: no token overlaps
|
|
100
|
+
// a comment, so it is the first one ending at or past the comment's end
|
|
101
|
+
// (the end-of-file token ends at the text's end). Token ends never
|
|
102
|
+
// decrease in document order, so a binary search finds it — without
|
|
103
|
+
// skipping the leading trivia a token start would need.
|
|
104
|
+
const tokenAfter = (offset: number): ts.Node =>
|
|
105
|
+
tokens[firstIndexAtOrAfter(tokens, offset, (token) => token.end)] as ts.Node;
|
|
106
|
+
const placementOf = (range: ts.CommentRange): DirectivePlacement => {
|
|
107
|
+
// The switch slots walk the syntax tree, so they are worked out only for
|
|
108
|
+
// the comments a switch-clause rule matched, when it asks.
|
|
109
|
+
let slots: ReturnType<typeof switchSlots> | undefined;
|
|
110
|
+
const slotsOf = (): ReturnType<typeof switchSlots> => (slots ??= switchSlots(tokenAfter(range.end)));
|
|
111
|
+
const firstComment = range.pos === firstCommentPos;
|
|
112
|
+
return {
|
|
113
|
+
header: range.pos < firstTokenStart,
|
|
114
|
+
firstComment: firstComment && /^\s*$/.test(source.slice(shebangEnd, range.pos)),
|
|
115
|
+
fileStart: range.pos === bunMarkerPos,
|
|
116
|
+
fileWide: firstComment && isFileWidePosition(source, range.pos, range.end),
|
|
117
|
+
get fallthroughSlot() {
|
|
118
|
+
return slotsOf().fallthroughSlot;
|
|
119
|
+
},
|
|
120
|
+
get noDefaultSlot() {
|
|
121
|
+
return slotsOf().noDefaultSlot;
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
};
|
|
98
125
|
|
|
99
|
-
|
|
126
|
+
const comments = sorted.map((range): Comment => {
|
|
100
127
|
const kind: CommentKind = range.kind === ts.SyntaxKind.SingleLineCommentTrivia ? "line" : "block";
|
|
101
128
|
const text = source.slice(range.pos, range.end);
|
|
102
129
|
const startPosition = sourceFile.getLineAndCharacterOfPosition(range.pos);
|
|
103
130
|
const endPosition = sourceFile.getLineAndCharacterOfPosition(range.end);
|
|
104
|
-
const directive = detectDirective(kind, text, placementOf(range
|
|
131
|
+
const directive = detectDirective(kind, text, placementOf(range));
|
|
105
132
|
return {
|
|
106
133
|
kind,
|
|
107
134
|
text,
|
|
@@ -114,4 +141,80 @@ export function scanComments(source: string, options: ScanOptions = {}): Comment
|
|
|
114
141
|
...(directive === undefined ? {} : { directive }),
|
|
115
142
|
};
|
|
116
143
|
});
|
|
144
|
+
|
|
145
|
+
return { comments, jsx: jsxLayout(source, sourceFile, jsxParents, comments) };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Whether a comment from `start` to `end` sits where ast-grep's suppression
|
|
150
|
+
* comment covers the whole file (`get_suppression_kind` in its `combined.rs`):
|
|
151
|
+
* on the first line with nothing but whitespace in front of it, and with the
|
|
152
|
+
* next node — code or another comment — starting two or more lines further
|
|
153
|
+
* down, or with nothing after it at all. Lines are counted the way tree-sitter
|
|
154
|
+
* counts its rows, by line feeds alone.
|
|
155
|
+
*/
|
|
156
|
+
export function isFileWidePosition(text: string, start: number, end: number): boolean {
|
|
157
|
+
if (!opensFirstRow(text, start)) return false;
|
|
158
|
+
const next = text.slice(end).search(/\S/);
|
|
159
|
+
return next === -1 || lineFeedCount(text.slice(0, end + next)) >= 2;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Whether nothing but whitespace, and no line feed, precedes the offset: the start of tree-sitter's first row. */
|
|
163
|
+
export function opensFirstRow(text: string, offset: number): boolean {
|
|
164
|
+
const before = text.slice(0, offset);
|
|
165
|
+
return isBlank(before) && !before.includes("\n");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function lineFeedCount(text: string): number {
|
|
169
|
+
return text.split("\n").length - 1;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* The switch slots (see DirectivePlacement) a comment in front of the given
|
|
174
|
+
* token sits in. The token is what follows the comment past any further
|
|
175
|
+
* comments — the end-of-file token at the latest, whose parent is the file.
|
|
176
|
+
*/
|
|
177
|
+
function switchSlots(token: ts.Node): { fallthroughSlot: boolean; noDefaultSlot: boolean } {
|
|
178
|
+
let fallthroughSlot = false;
|
|
179
|
+
let noDefaultSlot = false;
|
|
180
|
+
const parent = token.parent;
|
|
181
|
+
if (
|
|
182
|
+
(token.kind === ts.SyntaxKind.CaseKeyword || token.kind === ts.SyntaxKind.DefaultKeyword) &&
|
|
183
|
+
ts.isCaseOrDefaultClause(parent)
|
|
184
|
+
) {
|
|
185
|
+
// In front of a clause: where ESLint, oxlint and deno_lint read the
|
|
186
|
+
// comment saying the clause before it falls through — so not the first.
|
|
187
|
+
fallthroughSlot = parent.parent.clauses[0] !== parent;
|
|
188
|
+
} else if (
|
|
189
|
+
token.kind === ts.SyntaxKind.CloseBraceToken &&
|
|
190
|
+
ts.isBlock(parent) &&
|
|
191
|
+
ts.isCaseOrDefaultClause(parent.parent)
|
|
192
|
+
) {
|
|
193
|
+
// Inside a block that is the last statement of a clause (its only one,
|
|
194
|
+
// for ESLint and oxlint), in front of its closing brace — read when
|
|
195
|
+
// another clause follows.
|
|
196
|
+
const clause = parent.parent;
|
|
197
|
+
const clauses = clause.parent.clauses;
|
|
198
|
+
fallthroughSlot = clause.statements.at(-1) === parent && clauses.at(-1) !== clause;
|
|
199
|
+
} else if (token.kind === ts.SyntaxKind.CloseBraceToken && ts.isCaseBlock(parent)) {
|
|
200
|
+
// After the last clause of a switch, in front of its closing brace:
|
|
201
|
+
// where ESLint's default-case reads its comment.
|
|
202
|
+
noDefaultSlot = lacksDefaultClause(parent);
|
|
203
|
+
}
|
|
204
|
+
// oxlint's default-case reads the last comment from the last clause's
|
|
205
|
+
// start to the switch's end, so the comments inside that clause count too
|
|
206
|
+
// — all but one in front of the clause's opening `case`.
|
|
207
|
+
for (let node = parent; !noDefaultSlot && !ts.isSourceFile(node); node = node.parent) {
|
|
208
|
+
noDefaultSlot =
|
|
209
|
+
ts.isCaseClause(node) &&
|
|
210
|
+
node.parent.clauses.at(-1) === node &&
|
|
211
|
+
lacksDefaultClause(node.parent) &&
|
|
212
|
+
(node !== parent || token.kind !== ts.SyntaxKind.CaseKeyword);
|
|
213
|
+
}
|
|
214
|
+
return { fallthroughSlot, noDefaultSlot };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Whether the switch has clauses but none of them is a default clause. */
|
|
218
|
+
function lacksDefaultClause(caseBlock: ts.CaseBlock): boolean {
|
|
219
|
+
return caseBlock.clauses.length > 0 && !caseBlock.clauses.some(ts.isDefaultClause);
|
|
117
220
|
}
|
package/src/splice.ts
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { LINE_BREAK, LINE_TERMINATOR, isBlank, lineStartOffsets } from "./lines.js";
|
|
2
|
+
import { firstIndexAtOrAfter, rangeAt, type Range } from "./ranges.js";
|
|
3
|
+
|
|
4
|
+
/** One change to the source: a range to delete (tidying its line) or to replace verbatim. */
|
|
5
|
+
export interface Edit extends Range {
|
|
6
|
+
/** Text put in place of the range as is; without it the range is deleted and its line tidied. */
|
|
7
|
+
replacement?: string;
|
|
8
|
+
/**
|
|
9
|
+
* The range is a JSX expression container. Nothing is ever inserted in
|
|
10
|
+
* its place — not even a space to keep its neighbours apart — because
|
|
11
|
+
* those neighbours are JSX text and tags, not tokens.
|
|
12
|
+
*/
|
|
13
|
+
jsx?: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const isHorizontalSpace = (char: string | undefined): boolean => char === " " || char === "\t";
|
|
17
|
+
|
|
18
|
+
// Brackets never merge with what follows them, nor punctuation with what
|
|
19
|
+
// precedes it, so removing a comment between them needs no space to keep
|
|
20
|
+
// the tokens apart, and the whitespace that only held the comment off them
|
|
21
|
+
// can go: `f(/* a */ x)` becomes `f(x)` and `{/* empty */}` becomes `{}`.
|
|
22
|
+
const OPENING_BRACKETS = "([{";
|
|
23
|
+
const CLOSING_PUNCTUATION = ")]},;";
|
|
24
|
+
|
|
25
|
+
/** Whether `before` and `after`, about to meet where a comment was, need a space between them. */
|
|
26
|
+
function needsSpace(before: string, after: string): boolean {
|
|
27
|
+
return !OPENING_BRACKETS.includes(before) && !CLOSING_PUNCTUATION.includes(after);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// A global copy of the line-break matcher, so exec() can resume from a removal's end offset.
|
|
31
|
+
const NEXT_LINE_BREAK = new RegExp(LINE_BREAK.source, "g");
|
|
32
|
+
|
|
33
|
+
/** End offset (exclusive) of the last line break in the chunk, or -1 when it has none. */
|
|
34
|
+
function lastLineBreakEnd(chunk: string): number {
|
|
35
|
+
for (let index = chunk.length - 1; index >= 0; index -= 1) {
|
|
36
|
+
if (LINE_TERMINATOR.test(chunk[index] as string)) return index + 1;
|
|
37
|
+
}
|
|
38
|
+
return -1;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Applies sorted, non-overlapping edits to the source, tidying the lines of
|
|
43
|
+
* deleted ranges. Output is accumulated as segments with incremental
|
|
44
|
+
* line-state tracking, so the work stays linear in the source size even for
|
|
45
|
+
* comment-dense files.
|
|
46
|
+
*/
|
|
47
|
+
export function applyEdits(source: string, edits: readonly Edit[]): string {
|
|
48
|
+
const segments: string[] = [];
|
|
49
|
+
let cursor = 0;
|
|
50
|
+
/** Whether the output's current (unterminated) line is all whitespace so far. */
|
|
51
|
+
let lineIsBlank = true;
|
|
52
|
+
/** Last character of the output so far ("" while empty). */
|
|
53
|
+
let lastChar = "";
|
|
54
|
+
|
|
55
|
+
const push = (chunk: string): void => {
|
|
56
|
+
if (chunk === "") return;
|
|
57
|
+
segments.push(chunk);
|
|
58
|
+
const breakEnd = lastLineBreakEnd(chunk);
|
|
59
|
+
lineIsBlank = breakEnd === -1 ? lineIsBlank && isBlank(chunk) : isBlank(chunk.slice(breakEnd));
|
|
60
|
+
lastChar = chunk.slice(-1);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** Deletes everything the output holds after its last line break. */
|
|
64
|
+
const dropCurrentLine = (): void => {
|
|
65
|
+
while (segments.length > 0) {
|
|
66
|
+
const last = segments[segments.length - 1] as string;
|
|
67
|
+
const breakEnd = lastLineBreakEnd(last);
|
|
68
|
+
if (breakEnd === -1) {
|
|
69
|
+
segments.pop();
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
segments[segments.length - 1] = last.slice(0, breakEnd);
|
|
73
|
+
lineIsBlank = true;
|
|
74
|
+
lastChar = last.charAt(breakEnd - 1);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
lineIsBlank = true;
|
|
78
|
+
lastChar = "";
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Deletes trailing spaces/tabs of the output's current line. Only called
|
|
83
|
+
* while that line holds code, which the trimming stops at.
|
|
84
|
+
*/
|
|
85
|
+
const trimLineEnd = (): void => {
|
|
86
|
+
for (;;) {
|
|
87
|
+
const last = segments[segments.length - 1] as string;
|
|
88
|
+
let end = last.length;
|
|
89
|
+
while (end > 0 && isHorizontalSpace(last[end - 1])) end -= 1;
|
|
90
|
+
if (end > 0) {
|
|
91
|
+
segments[segments.length - 1] = last.slice(0, end);
|
|
92
|
+
lastChar = last.charAt(end - 1);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
segments.pop();
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/** Offset of the first character at or after `from` that is not a space or tab. */
|
|
100
|
+
const skipSpaces = (from: number): number => {
|
|
101
|
+
let index = from;
|
|
102
|
+
while (isHorizontalSpace(source[index])) index += 1;
|
|
103
|
+
return index;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
for (const edit of edits) {
|
|
107
|
+
push(source.slice(cursor, edit.start));
|
|
108
|
+
cursor = edit.end;
|
|
109
|
+
if (edit.replacement !== undefined) {
|
|
110
|
+
push(edit.replacement);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
NEXT_LINE_BREAK.lastIndex = cursor;
|
|
115
|
+
const lineBreak = NEXT_LINE_BREAK.exec(source);
|
|
116
|
+
const contentEnd = lineBreak === null ? source.length : lineBreak.index;
|
|
117
|
+
const lineEnd = lineBreak === null ? source.length : lineBreak.index + lineBreak[0].length;
|
|
118
|
+
const restContent = source.slice(cursor, contentEnd);
|
|
119
|
+
const deleted = source.slice(edit.start, edit.end);
|
|
120
|
+
|
|
121
|
+
if (lineIsBlank && isBlank(restContent)) {
|
|
122
|
+
// The range occupied whole line(s): drop them entirely.
|
|
123
|
+
dropCurrentLine();
|
|
124
|
+
cursor = lineEnd;
|
|
125
|
+
} else if (isBlank(restContent)) {
|
|
126
|
+
// Trailing after code: trim the gap, keep the line break.
|
|
127
|
+
trimLineEnd();
|
|
128
|
+
push(source.slice(contentEnd, lineEnd));
|
|
129
|
+
cursor = lineEnd;
|
|
130
|
+
} else if (!lineIsBlank && LINE_TERMINATOR.test(deleted) && needsSpace(lastChar, source.charAt(cursor))) {
|
|
131
|
+
// Code on both sides of a range that spans lines. Per ECMA-262 a
|
|
132
|
+
// multi-line comment acts as a line terminator for semicolon
|
|
133
|
+
// insertion, so a real line break (matching the file's style) takes
|
|
134
|
+
// its place instead of merging the lines. (Right after an opening
|
|
135
|
+
// bracket or before closing punctuation a line terminator changes
|
|
136
|
+
// nothing, so a multi-line comment there goes without one.)
|
|
137
|
+
trimLineEnd();
|
|
138
|
+
push(deleted.includes("\r\n") ? "\r\n" : "\n");
|
|
139
|
+
} else if (lastChar !== "" && !isBlank(lastChar)) {
|
|
140
|
+
// Code right before the range, more on the same line after it.
|
|
141
|
+
if (edit.jsx === true) continue;
|
|
142
|
+
const next = source.charAt(cursor);
|
|
143
|
+
if (!isBlank(next)) {
|
|
144
|
+
// `a/* x */b` would merge tokens: keep them apart.
|
|
145
|
+
if (needsSpace(lastChar, next)) push(" ");
|
|
146
|
+
} else if (OPENING_BRACKETS.includes(lastChar)) {
|
|
147
|
+
// `(/* x */ a` — the space only held the comment off the code.
|
|
148
|
+
cursor = skipSpaces(cursor);
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
// Whitespace (or the line start) right before the range: the
|
|
152
|
+
// whitespace after it collapses into that.
|
|
153
|
+
cursor = skipSpaces(cursor);
|
|
154
|
+
if (edit.jsx !== true && !lineIsBlank && CLOSING_PUNCTUATION.includes(source.charAt(cursor))) {
|
|
155
|
+
// `a /* x */)` — the space before only held the comment off the code.
|
|
156
|
+
trimLineEnd();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
segments.push(source.slice(cursor));
|
|
162
|
+
return segments.join("");
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Where blocks open and close besides brackets: JSX tags, whose children are laid out like a block's. */
|
|
166
|
+
export interface BlockTags {
|
|
167
|
+
/** Offsets just past every opening tag. */
|
|
168
|
+
openingTagEnds: ReadonlySet<number>;
|
|
169
|
+
/** Offsets of every closing tag. */
|
|
170
|
+
closingTagStarts: ReadonlySet<number>;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
type LineKind = "vanishing" | "blank" | "code";
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Deletions of the blank lines that removing whole comment lines would pile
|
|
177
|
+
* up. Where comment-only lines vanish, the blank lines around them close
|
|
178
|
+
* ranks: two paragraphs that a comment stood between, each separated from it
|
|
179
|
+
* by a blank line, end up separated by two. Every run of blank and vanishing
|
|
180
|
+
* lines is therefore trimmed to as many blank lines as the widest gap it
|
|
181
|
+
* held — never more than the source had in one place, never fewer than it
|
|
182
|
+
* had on either side — except at the start or end of the file, or of a
|
|
183
|
+
* block (an opening bracket ending the line before, a closing one starting
|
|
184
|
+
* the line after, or JSX tags), where only the blank lines that already sat
|
|
185
|
+
* against that edge stay: the ones on the far side of the comment merely
|
|
186
|
+
* separated the comment from the code.
|
|
187
|
+
*
|
|
188
|
+
* `deletions` are the ranges about to be deleted, sorted; a line vanishes
|
|
189
|
+
* when every non-whitespace character on it is deleted. Blank lines hold
|
|
190
|
+
* nothing but spaces and tabs (a lone vertical tab or no-break space is
|
|
191
|
+
* content to tsc 7) and lie outside every comment. `shieldedLines` are
|
|
192
|
+
* lines a surviving next-line directive reaches (1-based): the blank lines
|
|
193
|
+
* among them stay, so no code moves into the directive's reach.
|
|
194
|
+
*/
|
|
195
|
+
export function blankLineEdits(
|
|
196
|
+
source: string,
|
|
197
|
+
deletions: readonly Range[],
|
|
198
|
+
comments: readonly Range[],
|
|
199
|
+
shieldedLines: ReadonlySet<number>,
|
|
200
|
+
tags: BlockTags,
|
|
201
|
+
): Edit[] {
|
|
202
|
+
const offsets = lineStartOffsets(source);
|
|
203
|
+
// A final line break ends the last line rather than opening an empty one.
|
|
204
|
+
const lineCount =
|
|
205
|
+
offsets.length > 1 && offsets[offsets.length - 1] === source.length ? offsets.length - 1 : offsets.length;
|
|
206
|
+
const lineStart = (line: number): number => offsets[line] as number;
|
|
207
|
+
const lineContentEnd = (line: number): number =>
|
|
208
|
+
endOfContent(source, lineStart(line), offsets[line + 1] ?? source.length);
|
|
209
|
+
|
|
210
|
+
const kinds: LineKind[] = [];
|
|
211
|
+
for (let line = 0; line < lineCount; line += 1) {
|
|
212
|
+
kinds.push(
|
|
213
|
+
lineKind(source, lineStart(line), offsets[line + 1] ?? source.length, lineContentEnd(line), deletions, comments),
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Offset of the first (or last) character on the line that survives the deletions and is not whitespace. */
|
|
218
|
+
const survivingEdge = (line: number, fromEnd: boolean): number | undefined => {
|
|
219
|
+
const start = lineStart(line);
|
|
220
|
+
const end = lineContentEnd(line);
|
|
221
|
+
for (let step = 0; step < end - start; step += 1) {
|
|
222
|
+
const index = fromEnd ? end - 1 - step : start + step;
|
|
223
|
+
if (!isBlank(source[index] as string) && rangeAt(deletions, index) === undefined) return index;
|
|
224
|
+
}
|
|
225
|
+
return undefined;
|
|
226
|
+
};
|
|
227
|
+
const opensBlock = (line: number): boolean => {
|
|
228
|
+
const index = survivingEdge(line, true);
|
|
229
|
+
return (
|
|
230
|
+
index !== undefined &&
|
|
231
|
+
rangeAt(comments, index) === undefined &&
|
|
232
|
+
(OPENING_BRACKETS.includes(source[index] as string) || tags.openingTagEnds.has(index + 1))
|
|
233
|
+
);
|
|
234
|
+
};
|
|
235
|
+
// (A line's first surviving character is never inside a comment unless it
|
|
236
|
+
// is the comment's opening slash, so no comment check is needed here.)
|
|
237
|
+
const closesBlock = (line: number): boolean => {
|
|
238
|
+
const index = survivingEdge(line, false);
|
|
239
|
+
return index !== undefined && (")]}".includes(source[index] as string) || tags.closingTagStarts.has(index));
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const edits: Edit[] = [];
|
|
243
|
+
let line = 0;
|
|
244
|
+
while (line < lineCount) {
|
|
245
|
+
if (kinds[line] === "code") {
|
|
246
|
+
line += 1;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
const first = line;
|
|
250
|
+
const blanks: number[] = [];
|
|
251
|
+
// Blank lines between consecutive vanishing lines, from before the first to after the last.
|
|
252
|
+
const gaps = [0];
|
|
253
|
+
for (; line < lineCount && kinds[line] !== "code"; line += 1) {
|
|
254
|
+
if (kinds[line] === "vanishing") {
|
|
255
|
+
gaps.push(0);
|
|
256
|
+
} else {
|
|
257
|
+
blanks.push(line);
|
|
258
|
+
gaps[gaps.length - 1] = (gaps[gaps.length - 1] as number) + 1;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (gaps.length === 1) continue;
|
|
262
|
+
|
|
263
|
+
const leading = gaps[0] as number;
|
|
264
|
+
const trailing = gaps[gaps.length - 1] as number;
|
|
265
|
+
const atStart = first === 0;
|
|
266
|
+
const atEnd = line === lineCount;
|
|
267
|
+
let keep: number;
|
|
268
|
+
if (atStart || atEnd) {
|
|
269
|
+
keep = atEnd ? (atStart ? 0 : trailing) : leading;
|
|
270
|
+
} else {
|
|
271
|
+
const opens = opensBlock(first - 1);
|
|
272
|
+
const closes = closesBlock(line);
|
|
273
|
+
if (opens && closes) keep = Math.min(leading, trailing);
|
|
274
|
+
else if (opens) keep = leading;
|
|
275
|
+
else if (closes) keep = trailing;
|
|
276
|
+
else keep = gaps.reduce((widest, gap) => Math.max(widest, gap));
|
|
277
|
+
}
|
|
278
|
+
for (const [position, blank] of blanks.entries()) {
|
|
279
|
+
if (shieldedLines.has(blank + 1)) keep = Math.max(keep, position + 1);
|
|
280
|
+
}
|
|
281
|
+
for (const blank of blanks.slice(keep)) {
|
|
282
|
+
edits.push({ start: lineStart(blank), end: lineContentEnd(blank) });
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return edits;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** Offset where the line's content ends, before its line break (CRLF counts as one). */
|
|
289
|
+
function endOfContent(source: string, start: number, end: number): number {
|
|
290
|
+
if (end - start >= 2 && source[end - 2] === "\r" && source[end - 1] === "\n") return end - 2;
|
|
291
|
+
if (end > start && LINE_TERMINATOR.test(source[end - 1] as string)) return end - 1;
|
|
292
|
+
return end;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function lineKind(
|
|
296
|
+
source: string,
|
|
297
|
+
start: number,
|
|
298
|
+
end: number,
|
|
299
|
+
contentEnd: number,
|
|
300
|
+
deletions: readonly Range[],
|
|
301
|
+
comments: readonly Range[],
|
|
302
|
+
): LineKind {
|
|
303
|
+
// Sorted, non-overlapping ranges end in ascending order too, so the first
|
|
304
|
+
// one ending past the line start is the only candidate for touching it.
|
|
305
|
+
const touches = (ranges: readonly Range[]): boolean => {
|
|
306
|
+
const range = ranges[firstIndexAtOrAfter(ranges, start + 1, (candidate) => candidate.end)];
|
|
307
|
+
return range !== undefined && range.start < end;
|
|
308
|
+
};
|
|
309
|
+
if (touches(deletions)) {
|
|
310
|
+
let covered = true;
|
|
311
|
+
for (let index = start; index < contentEnd && covered; index += 1) {
|
|
312
|
+
covered = isBlank(source[index] as string) || rangeAt(deletions, index) !== undefined;
|
|
313
|
+
}
|
|
314
|
+
if (covered) return "vanishing";
|
|
315
|
+
}
|
|
316
|
+
return /^[ \t]*$/.test(source.slice(start, contentEnd)) && !touches(comments) ? "blank" : "code";
|
|
317
|
+
}
|