@kongyo2/ts-comment-scanner 1.8.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +14 -9
- package/README.md +14 -9
- package/dist/directives.d.ts +27 -9
- package/dist/directives.d.ts.map +1 -1
- package/dist/directives.js +351 -153
- package/dist/directives.js.map +1 -1
- package/dist/jsx.d.ts +54 -0
- package/dist/jsx.d.ts.map +1 -0
- package/dist/jsx.js +193 -0
- package/dist/jsx.js.map +1 -0
- package/dist/ranges.d.ts +10 -0
- package/dist/ranges.d.ts.map +1 -0
- package/dist/ranges.js +19 -0
- package/dist/ranges.js.map +1 -0
- package/dist/remove.d.ts +18 -5
- package/dist/remove.d.ts.map +1 -1
- package/dist/remove.js +460 -169
- package/dist/remove.js.map +1 -1
- package/dist/scanner.d.ts +7 -0
- package/dist/scanner.d.ts.map +1 -1
- package/dist/scanner.js +26 -5
- package/dist/scanner.js.map +1 -1
- package/dist/splice.d.ts +48 -0
- package/dist/splice.d.ts.map +1 -0
- package/dist/splice.js +279 -0
- package/dist/splice.js.map +1 -0
- package/package.json +1 -1
- package/src/directives.ts +379 -155
- package/src/jsx.ts +263 -0
- package/src/ranges.ts +23 -0
- package/src/remove.ts +539 -174
- package/src/scanner.ts +33 -5
- package/src/splice.ts +317 -0
package/src/jsx.ts
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { LINE_TERMINATOR, isBlank } from "./lines.js";
|
|
3
|
+
import { firstIndexAtOrAfter, type Range } from "./ranges.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* JSX layout for comment removal.
|
|
7
|
+
*
|
|
8
|
+
* JSX has no comment syntax of its own: a comment among JSX children sits in
|
|
9
|
+
* an expression container, `{/* note */}`, which renders nothing. Removing
|
|
10
|
+
* the comment should take the braces along, but the braces also split the
|
|
11
|
+
* text around them into separate children, and the JSX transforms turn text
|
|
12
|
+
* into strings by trimming whitespace at line breaks — so dropping a
|
|
13
|
+
* container can merge two strings into one, or change the whitespace one of
|
|
14
|
+
* them keeps. The layout records what every stretch of children renders, so
|
|
15
|
+
* a removal can prove its output renders exactly the same.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** An expression container among JSX children that holds nothing but comments: `{/* note */}`. */
|
|
19
|
+
export interface JsxContainer extends Range {
|
|
20
|
+
/** Index into {@link JsxLayout.segments} of the stretch of children the container sits in. */
|
|
21
|
+
segment: number;
|
|
22
|
+
/**
|
|
23
|
+
* Whether a neighbouring child is JSX text that renders something. Only
|
|
24
|
+
* then can dropping the container change what the element renders: text
|
|
25
|
+
* that is nothing but indentation around line breaks renders nothing,
|
|
26
|
+
* however it is merged or trimmed.
|
|
27
|
+
*/
|
|
28
|
+
suspect: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* The comment-only container that comes next among the siblings, past
|
|
31
|
+
* whitespace holding a line break (the gap prettier skips when it looks
|
|
32
|
+
* for the node a `{/* prettier-ignore */}` applies to), if any.
|
|
33
|
+
*/
|
|
34
|
+
next: JsxContainer | undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface JsxLayout {
|
|
38
|
+
/**
|
|
39
|
+
* What each stretch of children between two tags or expressions renders —
|
|
40
|
+
* the strings the JSX transforms pass on, under every whitespace
|
|
41
|
+
* convention they follow — in a fixed traversal order. Removing comments
|
|
42
|
+
* must leave every entry unchanged.
|
|
43
|
+
*/
|
|
44
|
+
segments: string[];
|
|
45
|
+
/** Comment-only containers among JSX children, in document order. */
|
|
46
|
+
containers: JsxContainer[];
|
|
47
|
+
/** Offsets just past every opening tag (`<div>`, `<>`). */
|
|
48
|
+
openingTagEnds: ReadonlySet<number>;
|
|
49
|
+
/** Offsets of every closing tag (`</div>`, `</>`). */
|
|
50
|
+
closingTagStarts: ReadonlySet<number>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Lays out the children of the given JSX elements and fragments. `comments`
|
|
55
|
+
* are the source's comments, sorted, so containers holding nothing but
|
|
56
|
+
* comments can be told apart from empty or malformed ones.
|
|
57
|
+
*/
|
|
58
|
+
export function jsxLayout(
|
|
59
|
+
source: string,
|
|
60
|
+
sourceFile: ts.SourceFile,
|
|
61
|
+
parents: readonly (ts.JsxElement | ts.JsxFragment)[],
|
|
62
|
+
comments: readonly Range[],
|
|
63
|
+
): JsxLayout {
|
|
64
|
+
const segments: string[] = [];
|
|
65
|
+
const containers: JsxContainer[] = [];
|
|
66
|
+
const openingTagEnds = new Set<number>();
|
|
67
|
+
const closingTagStarts = new Set<number>();
|
|
68
|
+
|
|
69
|
+
for (const parent of parents) {
|
|
70
|
+
const [opening, closing] = ts.isJsxElement(parent)
|
|
71
|
+
? [parent.openingElement, parent.closingElement]
|
|
72
|
+
: [parent.openingFragment, parent.closingFragment];
|
|
73
|
+
openingTagEnds.add(opening.end);
|
|
74
|
+
closingTagStarts.add(closing.getStart(sourceFile));
|
|
75
|
+
|
|
76
|
+
const children = parent.children;
|
|
77
|
+
const own = children.map((child) => commentOnlyContainer(source, sourceFile, child, comments));
|
|
78
|
+
for (const [index, container] of own.entries()) {
|
|
79
|
+
if (container === undefined) continue;
|
|
80
|
+
container.suspect = rendersText(children[index - 1]) || rendersText(children[index + 1]);
|
|
81
|
+
let next = index + 1;
|
|
82
|
+
while (isPrettierBlank(children[next])) next += 1;
|
|
83
|
+
container.next = own[next];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let parts: RenderedPart[] = [];
|
|
87
|
+
for (const [index, child] of children.entries()) {
|
|
88
|
+
const container = own[index];
|
|
89
|
+
if (container !== undefined) {
|
|
90
|
+
container.segment = segments.length;
|
|
91
|
+
containers.push(container);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const part = renderedPart(child);
|
|
95
|
+
if (part === BOUNDARY) {
|
|
96
|
+
segments.push(renderParts(parts));
|
|
97
|
+
parts = [];
|
|
98
|
+
} else if (part !== undefined) {
|
|
99
|
+
parts.push(part);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
segments.push(renderParts(parts));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
containers.sort((a, b) => a.start - b.start);
|
|
106
|
+
return { segments, containers, openingTagEnds, closingTagStarts };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const BOUNDARY = Symbol("boundary");
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* What a child contributes to its stretch of rendered strings: text, a
|
|
113
|
+
* string literal (`{" "}` is passed on as the very string a text child
|
|
114
|
+
* would be), nothing at all (an empty container), or a boundary that ends
|
|
115
|
+
* the stretch (a tag or any other expression).
|
|
116
|
+
*/
|
|
117
|
+
function renderedPart(child: ts.JsxChild): RenderedPart | typeof BOUNDARY | undefined {
|
|
118
|
+
if (ts.isJsxText(child)) return { text: child.text };
|
|
119
|
+
if (!ts.isJsxExpression(child) || child.dotDotDotToken !== undefined) return BOUNDARY;
|
|
120
|
+
if (child.expression === undefined) return undefined;
|
|
121
|
+
return ts.isStringLiteral(child.expression) ? { literal: child.expression.text } : BOUNDARY;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The child as a comment-only container: an expression container without an
|
|
126
|
+
* expression whose braces enclose nothing but whitespace and at least one
|
|
127
|
+
* comment. Anything else (an expression, a spread, `{}`, a container the
|
|
128
|
+
* parser had to recover from) is not one.
|
|
129
|
+
*/
|
|
130
|
+
function commentOnlyContainer(
|
|
131
|
+
source: string,
|
|
132
|
+
sourceFile: ts.SourceFile,
|
|
133
|
+
child: ts.JsxChild,
|
|
134
|
+
comments: readonly Range[],
|
|
135
|
+
): JsxContainer | undefined {
|
|
136
|
+
if (!ts.isJsxExpression(child) || child.expression !== undefined || child.dotDotDotToken !== undefined) {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
// The parser only leaves the expression out when a closing brace follows
|
|
140
|
+
// the opening one, so the container spans exactly `{` to `}`. What lies
|
|
141
|
+
// between them is trivia: whitespace and comments, but possibly also a
|
|
142
|
+
// merge-conflict marker, which is no comment.
|
|
143
|
+
const start = child.getStart(sourceFile);
|
|
144
|
+
const end = child.end;
|
|
145
|
+
let cursor = start + 1;
|
|
146
|
+
let uncommented = "";
|
|
147
|
+
let index = firstIndexAtOrAfter(comments, cursor, (comment) => comment.start);
|
|
148
|
+
for (let comment = comments[index]; comment !== undefined && comment.end < end; comment = comments[index]) {
|
|
149
|
+
uncommented += source.slice(cursor, comment.start);
|
|
150
|
+
cursor = comment.end;
|
|
151
|
+
index += 1;
|
|
152
|
+
}
|
|
153
|
+
uncommented += source.slice(cursor, end - 1);
|
|
154
|
+
return cursor > start + 1 && isBlank(uncommented)
|
|
155
|
+
? { start, end, segment: -1, suspect: false, next: undefined }
|
|
156
|
+
: undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Text made of nothing but spaces, tabs and at least one line break renders
|
|
160
|
+
// nothing under any of the conventions below, and stays that way when it
|
|
161
|
+
// loses spaces or tabs or is merged with more of its kind.
|
|
162
|
+
const INERT_TEXT = /^[ \t]*(?:(?:\r\n?|\n)[ \t]*)+$/;
|
|
163
|
+
|
|
164
|
+
function rendersText(child: ts.JsxChild | undefined): boolean {
|
|
165
|
+
return child !== undefined && ts.isJsxText(child) && !INERT_TEXT.test(child.text);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Text prettier steps over looking for an ignore comment's target: its JSX whitespace with a line feed. */
|
|
169
|
+
function isPrettierBlank(child: ts.JsxChild | undefined): boolean {
|
|
170
|
+
return child !== undefined && ts.isJsxText(child) && /^[ \t\r\n]*$/.test(child.text) && child.text.includes("\n");
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
type RenderedPart = { text: string } | { literal: string };
|
|
174
|
+
|
|
175
|
+
/** The strings a stretch of children renders, under every whitespace convention, as one comparable value. */
|
|
176
|
+
function renderParts(parts: readonly RenderedPart[]): string {
|
|
177
|
+
return JSON.stringify(
|
|
178
|
+
TEXT_RENDERINGS.map((render) =>
|
|
179
|
+
parts.flatMap((part) => {
|
|
180
|
+
if ("literal" in part) return [part.literal];
|
|
181
|
+
const rendered = render(part.text);
|
|
182
|
+
return rendered === undefined ? [] : [rendered];
|
|
183
|
+
}),
|
|
184
|
+
),
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The string a JSX transform makes of a JSX text, or undefined when the text renders nothing. */
|
|
189
|
+
type TextRendering = (text: string) => string | undefined;
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The convention of the transforms that trim before they decode entities —
|
|
193
|
+
* TypeScript's `fixupWhitespaceAndDecodeEntities` (tsc 6 and 7 alike) and
|
|
194
|
+
* esbuild's `fixWhitespaceAndDecodeJSXEntities`: every line loses the
|
|
195
|
+
* whitespace at its ends, except that the first line keeps its leading and
|
|
196
|
+
* the last line its trailing whitespace; lines left empty are dropped and
|
|
197
|
+
* the rest joined with single spaces. Entities stay encoded here — both
|
|
198
|
+
* decode them line by line after trimming, so equal spellings decode equally.
|
|
199
|
+
*/
|
|
200
|
+
function trimThenDecode(space: RegExp): TextRendering {
|
|
201
|
+
return (text) => {
|
|
202
|
+
let rendered: string | undefined;
|
|
203
|
+
let first = 0;
|
|
204
|
+
let last = -1;
|
|
205
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
206
|
+
const char = text[index] as string;
|
|
207
|
+
if (LINE_TERMINATOR.test(char)) {
|
|
208
|
+
if (first !== -1 && last !== -1) rendered = joinLine(rendered, text.slice(first, last + 1));
|
|
209
|
+
first = -1;
|
|
210
|
+
} else if (!space.test(char)) {
|
|
211
|
+
last = index;
|
|
212
|
+
if (first === -1) first = index;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return first === -1 ? rendered : joinLine(rendered, text.slice(first));
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function joinLine(rendered: string | undefined, line: string): string {
|
|
220
|
+
return rendered === undefined ? line : `${rendered} ${line}`;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Babel decodes numeric entities into the text before trimming it, so
|
|
224
|
+
// ` ` and ` ` count as the space and line break they spell. (Its
|
|
225
|
+
// named entities — the XHTML set — never decode to a space, tab or line
|
|
226
|
+
// break, so they can stay encoded.)
|
|
227
|
+
const NUMERIC_ENTITY = /&#(?:(\d+)|x([\da-fA-F]+));/g;
|
|
228
|
+
|
|
229
|
+
function decodeNumericEntity(entity: string, decimal: string | undefined, hex: string | undefined): string {
|
|
230
|
+
const codePoint = decimal === undefined ? Number.parseInt(hex as string, 16) : Number.parseInt(decimal, 10);
|
|
231
|
+
return codePoint <= 0x10ffff ? String.fromCodePoint(codePoint) : entity;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Babel's convention (`cleanJSXElementLiteralChild`), which React's and most
|
|
236
|
+
* other Babel-based JSX transforms share: the decoded text is split at CR,
|
|
237
|
+
* LF and CRLF, tabs become spaces, and lines lose the spaces touching a line
|
|
238
|
+
* break before the non-empty ones are joined with single spaces.
|
|
239
|
+
*/
|
|
240
|
+
function decodeThenTrim(text: string): string | undefined {
|
|
241
|
+
const lines = text.replace(NUMERIC_ENTITY, decodeNumericEntity).split(/\r\n|\n|\r/);
|
|
242
|
+
let lastNonEmptyLine = 0;
|
|
243
|
+
for (const [index, line] of lines.entries()) {
|
|
244
|
+
if (/[^ \t]/.test(line)) lastNonEmptyLine = index;
|
|
245
|
+
}
|
|
246
|
+
let rendered = "";
|
|
247
|
+
for (const [index, line] of lines.entries()) {
|
|
248
|
+
let trimmed = line.replace(/\t/g, " ");
|
|
249
|
+
if (index !== 0) trimmed = trimmed.replace(/^ +/, "");
|
|
250
|
+
if (index !== lines.length - 1) trimmed = trimmed.replace(/ +$/, "");
|
|
251
|
+
if (trimmed === "") continue;
|
|
252
|
+
rendered += index === lastNonEmptyLine ? trimmed : `${trimmed} `;
|
|
253
|
+
}
|
|
254
|
+
return rendered === "" ? undefined : rendered;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const TEXT_RENDERINGS: readonly TextRendering[] = [
|
|
258
|
+
// TypeScript's isWhiteSpaceSingleLine (tsc 6 and the Go port).
|
|
259
|
+
trimThenDecode(/[ \t\v\f\u0085 - ]/),
|
|
260
|
+
// esbuild's IsWhitespace, which lacks NEL and the zero-width space.
|
|
261
|
+
trimThenDecode(/[ \t\v\f - ]/),
|
|
262
|
+
decodeThenTrim,
|
|
263
|
+
];
|
package/src/ranges.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** A span of a source string: `start` inclusive, `end` exclusive. */
|
|
2
|
+
export interface Range {
|
|
3
|
+
start: number;
|
|
4
|
+
end: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/** Index of the first item whose key is at least `value`, by binary search over ascending keys. */
|
|
8
|
+
export function firstIndexAtOrAfter<T>(items: readonly T[], value: number, key: (item: T) => number): number {
|
|
9
|
+
let low = 0;
|
|
10
|
+
let high = items.length;
|
|
11
|
+
while (low < high) {
|
|
12
|
+
const middle = (low + high) >> 1;
|
|
13
|
+
if (key(items[middle] as T) < value) low = middle + 1;
|
|
14
|
+
else high = middle;
|
|
15
|
+
}
|
|
16
|
+
return low;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The range among sorted, non-overlapping ones that contains the offset, if any. */
|
|
20
|
+
export function rangeAt<T extends Range>(ranges: readonly T[], offset: number): T | undefined {
|
|
21
|
+
const range = ranges[firstIndexAtOrAfter(ranges, offset + 1, (candidate) => candidate.start) - 1];
|
|
22
|
+
return range !== undefined && offset < range.end ? range : undefined;
|
|
23
|
+
}
|