@kongyo2/ts-comment-scanner 1.9.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 CHANGED
@@ -152,8 +152,10 @@ Removal is driven by the AST's comment ranges, so it never touches strings or co
152
152
  - **The line structure below next-line directives** (`@ts-expect-error`, `eslint-disable-next-line`, ...) **is preserved**: when removing comments would delete a line and shift the directive's target (e.g. a comment-only line), those comments are kept instead
153
153
  - **Comments a directive looks past for its target are kept too**: below tsc's `@ts-ignore`/`@ts-expect-error`, which skip blank and `//` lines to find their target, the first line tsc 6 or tsc 7 stops at (a block-comment line, say) is kept; between a directive that only counts as the last comment before its target (`@deno-types`, `/* GraphQL */`, ESLint's `// falls through`, ...) and that target, every comment is kept
154
154
  - **Position-dependent directives are never activated by a removal**: when deleting a leading comment would make prettier's `@format`/`@prettier` pragma or Deno's `deno-coverage-ignore-file` (honoured only in the file's first comment) or Bun's `// @bun` marker (honoured only at the very start of the file or on the line after a shebang) newly take effect in a kept comment, the leading comment is kept as well
155
- - Whitespace is inserted where removing a block comment would otherwise join tokens together (`a/* x */b` → `a b`)
155
+ - Whitespace is inserted where removing a block comment would otherwise join tokens together (`a/* x */b` → `a b`), and none is left where it only held the comment off a bracket or punctuation (`f(/* x */ a)` → `f(a)`, `{/* x */}` → `{}`)
156
156
  - Comment-only lines are removed entirely, and trailing comments are removed together with the preceding whitespace
157
+ - **No blank-line pile-ups**: where comment-only lines vanish, the blank lines around them close ranks to the widest gap the source had there (removing a comment that sat between two blank lines leaves one, not two). At the start or end of the file or of a block (`{`, `(`, `[`, JSX tags), the blank lines that only separated the comment from the code go with it, while those already against the edge stay. Blank lines a next-line directive reaches stay put
158
+ - **JSX comments go with their braces**: `{/* ... */}` is removed as a whole — its line too, when it stands alone — instead of leaving `{ }` behind. Where the braces also keep rendered text apart, dropping them would change what the element renders (JSX merges and trims text across them), so `{" "}` takes their place when that renders the same (`<a /> {/* x */}` at the end of a line → `<a />{" "}`), and otherwise `{}` stays. The output is re-parsed and every element's rendered children compared under the whitespace rules of TypeScript, esbuild and Babel. A comment container right after a directive in braces that applies to the next sibling (`{/* prettier-ignore */}`, ...) is kept
157
159
  - The result is re-scanned to verify that the surviving comments match the protected set, directive meaning included; if the outcome is unexpected the file is left unchanged and an error is reported
158
160
  - Writes go through a temporary file and an atomic rename, so a failed write or a full disk can never truncate the original
159
161
 
package/README.md CHANGED
@@ -152,8 +152,10 @@ ts-comment-scanner --remove --remove-directives --remove-legal src
152
152
  - `@ts-expect-error` や `eslint-disable-next-line` など**次行を対象にする指示子の直下の行構造を保持**: 削除すると行が消えて指示子の適用先がずれる場合(コメントだけの行など)、その行のコメントは削除せずに保持
153
153
  - **指示子が対象を探す途中のコメントも保持**: 空行と `//` 行を読み飛ばして対象行を探す tsc の `@ts-ignore`/`@ts-expect-error` では、tsc 6・tsc 7 がそれぞれ読み飛ばせずに止まる最初の行(ブロックコメントの行など)を保持。対象の直前にある最後のコメントでのみ有効な指示子(`@deno-types`・`/* GraphQL */`・ESLint の `// falls through` など)では、指示子と対象の間のコメントをすべて保持
154
154
  - **位置依存ディレクティブを削除で有効化しない**: prettier の `@format`/`@prettier` pragma や Deno の `deno-coverage-ignore-file`(ファイル最初のコメントでのみ有効)、Bun の `// @bun`(ファイル先頭か shebang の次の行でのみ有効)が、手前のコメント削除によって保持コメント内で新たに効き始める場合は、その手前のコメントも保持
155
- - ブロックコメント除去でトークンが結合してしまう位置には空白を挿入(`a/* x */b` → `a b`)
155
+ - ブロックコメント除去でトークンが結合してしまう位置には空白を挿入(`a/* x */b` → `a b`)。括弧や区切り記号との間を空けていただけの空白は残さない(`f(/* x */ a)` → `f(a)`、`{/* x */}` → `{}`)
156
156
  - コメントだけの行は行ごと削除、行末コメントは手前の空白ごと削除
157
+ - **空行を重複させない**: コメント行が消えた位置では、前後の空行を元のソースにあった最も広い間隔まで詰める(空行に挟まれたコメントを消しても空行は 1 行)。ファイルやブロック(`{`・`(`・`[`・JSX タグ)の先頭・末尾で、コメントとコードを隔てていただけの空行も一緒に削除。次行指示子の適用先になる空行は保持
158
+ - **JSX のコメントは波かっこごと削除**: `{/* … */}` は `{ }` を残さず丸ごと削除(単独の行なら行ごと)。ただし波かっこが前後のテキストを区切っている場合は、外すと JSX のテキスト結合・空白除去で描画が変わるため、同じ描画になるなら `{" "}` に置き換え(行末の `<a /> {/* x */}` → `<a />{" "}`)、それ以外は `{}` を残す。削除後のソースを再解析し、各要素の子要素の描画結果を TypeScript・esbuild・Babel の空白規則で比較して検証。`{/* prettier-ignore */}` など次の兄弟要素に効く波かっこ内の指示子の直後にあるコメントコンテナは保持
157
159
  - 削除後のソースを再スキャンし、残ったコメントが保持対象とディレクティブの意味ごと一致することを検証。想定外の結果になる場合はファイルを変更せずエラー報告
158
160
  - 書き込みは一時ファイル経由のアトミック置換(書き込み失敗やディスクフルで元ファイルが壊れない)
159
161
 
package/dist/jsx.d.ts ADDED
@@ -0,0 +1,54 @@
1
+ import ts from "typescript";
2
+ import { type Range } from "./ranges.js";
3
+ /**
4
+ * JSX layout for comment removal.
5
+ *
6
+ * JSX has no comment syntax of its own: a comment among JSX children sits in
7
+ * an expression container, `{/* note *​/}`, which renders nothing. Removing
8
+ * the comment should take the braces along, but the braces also split the
9
+ * text around them into separate children, and the JSX transforms turn text
10
+ * into strings by trimming whitespace at line breaks — so dropping a
11
+ * container can merge two strings into one, or change the whitespace one of
12
+ * them keeps. The layout records what every stretch of children renders, so
13
+ * a removal can prove its output renders exactly the same.
14
+ */
15
+ /** An expression container among JSX children that holds nothing but comments: `{/* note *​/}`. */
16
+ export interface JsxContainer extends Range {
17
+ /** Index into {@link JsxLayout.segments} of the stretch of children the container sits in. */
18
+ segment: number;
19
+ /**
20
+ * Whether a neighbouring child is JSX text that renders something. Only
21
+ * then can dropping the container change what the element renders: text
22
+ * that is nothing but indentation around line breaks renders nothing,
23
+ * however it is merged or trimmed.
24
+ */
25
+ suspect: boolean;
26
+ /**
27
+ * The comment-only container that comes next among the siblings, past
28
+ * whitespace holding a line break (the gap prettier skips when it looks
29
+ * for the node a `{/* prettier-ignore *​/}` applies to), if any.
30
+ */
31
+ next: JsxContainer | undefined;
32
+ }
33
+ export interface JsxLayout {
34
+ /**
35
+ * What each stretch of children between two tags or expressions renders —
36
+ * the strings the JSX transforms pass on, under every whitespace
37
+ * convention they follow — in a fixed traversal order. Removing comments
38
+ * must leave every entry unchanged.
39
+ */
40
+ segments: string[];
41
+ /** Comment-only containers among JSX children, in document order. */
42
+ containers: JsxContainer[];
43
+ /** Offsets just past every opening tag (`<div>`, `<>`). */
44
+ openingTagEnds: ReadonlySet<number>;
45
+ /** Offsets of every closing tag (`</div>`, `</>`). */
46
+ closingTagStarts: ReadonlySet<number>;
47
+ }
48
+ /**
49
+ * Lays out the children of the given JSX elements and fragments. `comments`
50
+ * are the source's comments, sorted, so containers holding nothing but
51
+ * comments can be told apart from empty or malformed ones.
52
+ */
53
+ export declare function jsxLayout(source: string, sourceFile: ts.SourceFile, parents: readonly (ts.JsxElement | ts.JsxFragment)[], comments: readonly Range[]): JsxLayout;
54
+ //# sourceMappingURL=jsx.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsx.d.ts","sourceRoot":"","sources":["../src/jsx.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAE5B,OAAO,EAAuB,KAAK,KAAK,EAAE,MAAM,aAAa,CAAC;AAE9D;;;;;;;;;;;GAWG;AAEH,mGAAmG;AACnG,MAAM,WAAW,YAAa,SAAQ,KAAK;IACzC,8FAA8F;IAC9F,OAAO,EAAE,MAAM,CAAC;IAChB;;;;;OAKG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;;;OAIG;IACH,IAAI,EAAE,YAAY,GAAG,SAAS,CAAC;CAChC;AAED,MAAM,WAAW,SAAS;IACxB;;;;;OAKG;IACH,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,qEAAqE;IACrE,UAAU,EAAE,YAAY,EAAE,CAAC;IAC3B,2DAA2D;IAC3D,cAAc,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACpC,sDAAsD;IACtD,gBAAgB,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;CACvC;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,EAAE,CAAC,UAAU,EACzB,OAAO,EAAE,SAAS,CAAC,EAAE,CAAC,UAAU,GAAG,EAAE,CAAC,WAAW,CAAC,EAAE,EACpD,QAAQ,EAAE,SAAS,KAAK,EAAE,GACzB,SAAS,CA4CX"}
package/dist/jsx.js ADDED
@@ -0,0 +1,193 @@
1
+ import ts from "typescript";
2
+ import { LINE_TERMINATOR, isBlank } from "./lines.js";
3
+ import { firstIndexAtOrAfter } from "./ranges.js";
4
+ /**
5
+ * Lays out the children of the given JSX elements and fragments. `comments`
6
+ * are the source's comments, sorted, so containers holding nothing but
7
+ * comments can be told apart from empty or malformed ones.
8
+ */
9
+ export function jsxLayout(source, sourceFile, parents, comments) {
10
+ const segments = [];
11
+ const containers = [];
12
+ const openingTagEnds = new Set();
13
+ const closingTagStarts = new Set();
14
+ for (const parent of parents) {
15
+ const [opening, closing] = ts.isJsxElement(parent)
16
+ ? [parent.openingElement, parent.closingElement]
17
+ : [parent.openingFragment, parent.closingFragment];
18
+ openingTagEnds.add(opening.end);
19
+ closingTagStarts.add(closing.getStart(sourceFile));
20
+ const children = parent.children;
21
+ const own = children.map((child) => commentOnlyContainer(source, sourceFile, child, comments));
22
+ for (const [index, container] of own.entries()) {
23
+ if (container === undefined)
24
+ continue;
25
+ container.suspect = rendersText(children[index - 1]) || rendersText(children[index + 1]);
26
+ let next = index + 1;
27
+ while (isPrettierBlank(children[next]))
28
+ next += 1;
29
+ container.next = own[next];
30
+ }
31
+ let parts = [];
32
+ for (const [index, child] of children.entries()) {
33
+ const container = own[index];
34
+ if (container !== undefined) {
35
+ container.segment = segments.length;
36
+ containers.push(container);
37
+ continue;
38
+ }
39
+ const part = renderedPart(child);
40
+ if (part === BOUNDARY) {
41
+ segments.push(renderParts(parts));
42
+ parts = [];
43
+ }
44
+ else if (part !== undefined) {
45
+ parts.push(part);
46
+ }
47
+ }
48
+ segments.push(renderParts(parts));
49
+ }
50
+ containers.sort((a, b) => a.start - b.start);
51
+ return { segments, containers, openingTagEnds, closingTagStarts };
52
+ }
53
+ const BOUNDARY = Symbol("boundary");
54
+ /**
55
+ * What a child contributes to its stretch of rendered strings: text, a
56
+ * string literal (`{" "}` is passed on as the very string a text child
57
+ * would be), nothing at all (an empty container), or a boundary that ends
58
+ * the stretch (a tag or any other expression).
59
+ */
60
+ function renderedPart(child) {
61
+ if (ts.isJsxText(child))
62
+ return { text: child.text };
63
+ if (!ts.isJsxExpression(child) || child.dotDotDotToken !== undefined)
64
+ return BOUNDARY;
65
+ if (child.expression === undefined)
66
+ return undefined;
67
+ return ts.isStringLiteral(child.expression) ? { literal: child.expression.text } : BOUNDARY;
68
+ }
69
+ /**
70
+ * The child as a comment-only container: an expression container without an
71
+ * expression whose braces enclose nothing but whitespace and at least one
72
+ * comment. Anything else (an expression, a spread, `{}`, a container the
73
+ * parser had to recover from) is not one.
74
+ */
75
+ function commentOnlyContainer(source, sourceFile, child, comments) {
76
+ if (!ts.isJsxExpression(child) || child.expression !== undefined || child.dotDotDotToken !== undefined) {
77
+ return undefined;
78
+ }
79
+ // The parser only leaves the expression out when a closing brace follows
80
+ // the opening one, so the container spans exactly `{` to `}`. What lies
81
+ // between them is trivia: whitespace and comments, but possibly also a
82
+ // merge-conflict marker, which is no comment.
83
+ const start = child.getStart(sourceFile);
84
+ const end = child.end;
85
+ let cursor = start + 1;
86
+ let uncommented = "";
87
+ let index = firstIndexAtOrAfter(comments, cursor, (comment) => comment.start);
88
+ for (let comment = comments[index]; comment !== undefined && comment.end < end; comment = comments[index]) {
89
+ uncommented += source.slice(cursor, comment.start);
90
+ cursor = comment.end;
91
+ index += 1;
92
+ }
93
+ uncommented += source.slice(cursor, end - 1);
94
+ return cursor > start + 1 && isBlank(uncommented)
95
+ ? { start, end, segment: -1, suspect: false, next: undefined }
96
+ : undefined;
97
+ }
98
+ // Text made of nothing but spaces, tabs and at least one line break renders
99
+ // nothing under any of the conventions below, and stays that way when it
100
+ // loses spaces or tabs or is merged with more of its kind.
101
+ const INERT_TEXT = /^[ \t]*(?:(?:\r\n?|\n)[ \t]*)+$/;
102
+ function rendersText(child) {
103
+ return child !== undefined && ts.isJsxText(child) && !INERT_TEXT.test(child.text);
104
+ }
105
+ /** Text prettier steps over looking for an ignore comment's target: its JSX whitespace with a line feed. */
106
+ function isPrettierBlank(child) {
107
+ return child !== undefined && ts.isJsxText(child) && /^[ \t\r\n]*$/.test(child.text) && child.text.includes("\n");
108
+ }
109
+ /** The strings a stretch of children renders, under every whitespace convention, as one comparable value. */
110
+ function renderParts(parts) {
111
+ return JSON.stringify(TEXT_RENDERINGS.map((render) => parts.flatMap((part) => {
112
+ if ("literal" in part)
113
+ return [part.literal];
114
+ const rendered = render(part.text);
115
+ return rendered === undefined ? [] : [rendered];
116
+ })));
117
+ }
118
+ /**
119
+ * The convention of the transforms that trim before they decode entities —
120
+ * TypeScript's `fixupWhitespaceAndDecodeEntities` (tsc 6 and 7 alike) and
121
+ * esbuild's `fixWhitespaceAndDecodeJSXEntities`: every line loses the
122
+ * whitespace at its ends, except that the first line keeps its leading and
123
+ * the last line its trailing whitespace; lines left empty are dropped and
124
+ * the rest joined with single spaces. Entities stay encoded here — both
125
+ * decode them line by line after trimming, so equal spellings decode equally.
126
+ */
127
+ function trimThenDecode(space) {
128
+ return (text) => {
129
+ let rendered;
130
+ let first = 0;
131
+ let last = -1;
132
+ for (let index = 0; index < text.length; index += 1) {
133
+ const char = text[index];
134
+ if (LINE_TERMINATOR.test(char)) {
135
+ if (first !== -1 && last !== -1)
136
+ rendered = joinLine(rendered, text.slice(first, last + 1));
137
+ first = -1;
138
+ }
139
+ else if (!space.test(char)) {
140
+ last = index;
141
+ if (first === -1)
142
+ first = index;
143
+ }
144
+ }
145
+ return first === -1 ? rendered : joinLine(rendered, text.slice(first));
146
+ };
147
+ }
148
+ function joinLine(rendered, line) {
149
+ return rendered === undefined ? line : `${rendered} ${line}`;
150
+ }
151
+ // Babel decodes numeric entities into the text before trimming it, so
152
+ // `&#32;` and `&#10;` count as the space and line break they spell. (Its
153
+ // named entities — the XHTML set — never decode to a space, tab or line
154
+ // break, so they can stay encoded.)
155
+ const NUMERIC_ENTITY = /&#(?:(\d+)|x([\da-fA-F]+));/g;
156
+ function decodeNumericEntity(entity, decimal, hex) {
157
+ const codePoint = decimal === undefined ? Number.parseInt(hex, 16) : Number.parseInt(decimal, 10);
158
+ return codePoint <= 0x10ffff ? String.fromCodePoint(codePoint) : entity;
159
+ }
160
+ /**
161
+ * Babel's convention (`cleanJSXElementLiteralChild`), which React's and most
162
+ * other Babel-based JSX transforms share: the decoded text is split at CR,
163
+ * LF and CRLF, tabs become spaces, and lines lose the spaces touching a line
164
+ * break before the non-empty ones are joined with single spaces.
165
+ */
166
+ function decodeThenTrim(text) {
167
+ const lines = text.replace(NUMERIC_ENTITY, decodeNumericEntity).split(/\r\n|\n|\r/);
168
+ let lastNonEmptyLine = 0;
169
+ for (const [index, line] of lines.entries()) {
170
+ if (/[^ \t]/.test(line))
171
+ lastNonEmptyLine = index;
172
+ }
173
+ let rendered = "";
174
+ for (const [index, line] of lines.entries()) {
175
+ let trimmed = line.replace(/\t/g, " ");
176
+ if (index !== 0)
177
+ trimmed = trimmed.replace(/^ +/, "");
178
+ if (index !== lines.length - 1)
179
+ trimmed = trimmed.replace(/ +$/, "");
180
+ if (trimmed === "")
181
+ continue;
182
+ rendered += index === lastNonEmptyLine ? trimmed : `${trimmed} `;
183
+ }
184
+ return rendered === "" ? undefined : rendered;
185
+ }
186
+ const TEXT_RENDERINGS = [
187
+ // TypeScript's isWhiteSpaceSingleLine (tsc 6 and the Go port).
188
+ trimThenDecode(/[ \t\v\f\u0085   -​   ]/),
189
+ // esbuild's IsWhitespace, which lacks NEL and the zero-width space.
190
+ trimThenDecode(/[ \t\v\f   -    ]/),
191
+ decodeThenTrim,
192
+ ];
193
+ //# sourceMappingURL=jsx.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsx.js","sourceRoot":"","sources":["../src/jsx.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAC5B,OAAO,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACtD,OAAO,EAAE,mBAAmB,EAAc,MAAM,aAAa,CAAC;AAkD9D;;;;GAIG;AACH,MAAM,UAAU,SAAS,CACvB,MAAc,EACd,UAAyB,EACzB,OAAoD,EACpD,QAA0B;IAE1B,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,MAAM,UAAU,GAAmB,EAAE,CAAC;IACtC,MAAM,cAAc,GAAG,IAAI,GAAG,EAAU,CAAC;IACzC,MAAM,gBAAgB,GAAG,IAAI,GAAG,EAAU,CAAC;IAE3C,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,MAAM,CAAC;YAChD,CAAC,CAAC,CAAC,MAAM,CAAC,cAAc,EAAE,MAAM,CAAC,cAAc,CAAC;YAChD,CAAC,CAAC,CAAC,MAAM,CAAC,eAAe,EAAE,MAAM,CAAC,eAAe,CAAC,CAAC;QACrD,cAAc,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAChC,gBAAgB,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC;QAEnD,MAAM,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;QACjC,MAAM,GAAG,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,oBAAoB,CAAC,MAAM,EAAE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AC,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IACpF,IAAI,gBAAgB,GAAG,CAAC,CAAC;IACzB,KAAK,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE,CAAC;QAC5C,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC;YAAE,gBAAgB,GAAG,KAAK,CAAC;IACpD,CAAC;IACD,IAAI,QAAQ,GAAG,EAAE,CAAC;IAClB,KAAK,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE,CAAC;QAC5C,IAAI,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QACvC,IAAI,KAAK,KAAK,CAAC;YAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QACtD,IAAI,KAAK,KAAK,KAAK,CAAC,MAAM,GAAG,CAAC;YAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QACrE,IAAI,OAAO,KAAK,EAAE;YAAE,SAAS;QAC7B,QAAQ,IAAI,KAAK,KAAK,gBAAgB,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,GAAG,CAAC;IACnE,CAAC;IACD,OAAO,QAAQ,KAAK,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC;AAChD,CAAC;AAED,MAAM,eAAe,GAA6B;IAChD,+DAA+D;IAC/D,cAAc,CAAC,0BAA0B,CAAC;IAC1C,oEAAoE;IACpE,cAAc,CAAC,oBAAoB,CAAC;IACpC,cAAc;CACf,CAAC"}
@@ -0,0 +1,10 @@
1
+ /** A span of a source string: `start` inclusive, `end` exclusive. */
2
+ export interface Range {
3
+ start: number;
4
+ end: number;
5
+ }
6
+ /** Index of the first item whose key is at least `value`, by binary search over ascending keys. */
7
+ export declare function firstIndexAtOrAfter<T>(items: readonly T[], value: number, key: (item: T) => number): number;
8
+ /** The range among sorted, non-overlapping ones that contains the offset, if any. */
9
+ export declare function rangeAt<T extends Range>(ranges: readonly T[], offset: number): T | undefined;
10
+ //# sourceMappingURL=ranges.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ranges.d.ts","sourceRoot":"","sources":["../src/ranges.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,MAAM,WAAW,KAAK;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACb;AAED,mGAAmG;AACnG,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,GAAG,MAAM,CAS3G;AAED,qFAAqF;AACrF,wBAAgB,OAAO,CAAC,CAAC,SAAS,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS,CAG5F"}
package/dist/ranges.js ADDED
@@ -0,0 +1,19 @@
1
+ /** Index of the first item whose key is at least `value`, by binary search over ascending keys. */
2
+ export function firstIndexAtOrAfter(items, value, key) {
3
+ let low = 0;
4
+ let high = items.length;
5
+ while (low < high) {
6
+ const middle = (low + high) >> 1;
7
+ if (key(items[middle]) < value)
8
+ low = middle + 1;
9
+ else
10
+ high = middle;
11
+ }
12
+ return low;
13
+ }
14
+ /** The range among sorted, non-overlapping ones that contains the offset, if any. */
15
+ export function rangeAt(ranges, offset) {
16
+ const range = ranges[firstIndexAtOrAfter(ranges, offset + 1, (candidate) => candidate.start) - 1];
17
+ return range !== undefined && offset < range.end ? range : undefined;
18
+ }
19
+ //# sourceMappingURL=ranges.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ranges.js","sourceRoot":"","sources":["../src/ranges.ts"],"names":[],"mappings":"AAMA,mGAAmG;AACnG,MAAM,UAAU,mBAAmB,CAAI,KAAmB,EAAE,KAAa,EAAE,GAAwB;IACjG,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,IAAI,IAAI,GAAG,KAAK,CAAC,MAAM,CAAC;IACxB,OAAO,GAAG,GAAG,IAAI,EAAE,CAAC;QAClB,MAAM,MAAM,GAAG,CAAC,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,GAAG,CAAC,KAAK,CAAC,MAAM,CAAM,CAAC,GAAG,KAAK;YAAE,GAAG,GAAG,MAAM,GAAG,CAAC,CAAC;;YACjD,IAAI,GAAG,MAAM,CAAC;IACrB,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED,qFAAqF;AACrF,MAAM,UAAU,OAAO,CAAkB,MAAoB,EAAE,MAAc;IAC3E,MAAM,KAAK,GAAG,MAAM,CAAC,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC,EAAE,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;IAClG,OAAO,KAAK,KAAK,SAAS,IAAI,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;AACvE,CAAC"}
package/dist/remove.d.ts CHANGED
@@ -15,10 +15,10 @@ export interface RemoveResult {
15
15
  /**
16
16
  * Comments kept because they are protected: directives, license headers,
17
17
  * comments whose removal would shift code under a next-line directive or
18
- * that sit between a directive and the code it reaches past them for, and
19
- * comments whose removal would move a position-dependent directive
20
- * (prettier's first-comment pragma, Deno's coverage file pragma, Bun's
21
- * file-start marker) into effect.
18
+ * that sit between a directive and the code it reaches past them for (or
19
+ * the JSX sibling it applies to), and comments whose removal would move a
20
+ * position-dependent directive (prettier's first-comment pragma, Deno's
21
+ * coverage file pragma, Bun's file-start marker) into effect.
22
22
  */
23
23
  kept: Comment[];
24
24
  /** Comments excluded from removal by the `shouldRemove` predicate. */
@@ -39,6 +39,17 @@ export interface RemoveResult {
39
39
  * shebang line), the leading comment is kept instead.
40
40
  * - A space is inserted where removing a block comment would merge two tokens,
41
41
  * and a line break is kept where the comment acted as one (ASI stays intact).
42
+ * - A JSX comment goes with its braces (`{/* note *​/}`), unless that would
43
+ * change what the element renders: the braces split the text around them
44
+ * into separate strings, so where they stand between text that renders,
45
+ * `{" "}` takes their place when that keeps the strings identical, and
46
+ * otherwise the braces stay as `{}`. The output is re-parsed to prove every
47
+ * element renders the same strings, under the whitespace rules of
48
+ * TypeScript, esbuild and Babel alike.
49
+ * - Blank lines that only separated a removed comment from the code around it
50
+ * go with it: no pile-up of blank lines where a comment stood between two
51
+ * of them, and at the start or end of the file or of a block only the blank
52
+ * lines already against that edge stay.
42
53
  * - A leading byte-order mark is preserved. Reported positions index into the
43
54
  * `source` string exactly as `scanComments(source)` reports them (a BOM
44
55
  * counts as its first character).
@@ -1 +1 @@
1
- {"version":3,"file":"remove.d.ts","sourceRoot":"","sources":["../src/remove.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAE1C,MAAM,WAAW,aAAa;IAC5B,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,wFAAwF;IACxF,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,4EAA4E;IAC5E,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,oFAAoF;IACpF,YAAY,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC;CAC9C;AAED,MAAM,WAAW,YAAY;IAC3B,oDAAoD;IACpD,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,EAAE,CAAC;IACnB;;;;;;;OAOG;IACH,IAAI,EAAE,OAAO,EAAE,CAAC;IAChB,sEAAsE;IACtE,OAAO,EAAE,OAAO,EAAE,CAAC;IACnB,OAAO,EAAE,OAAO,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