Skip to content

Commit a81d8e8

Browse files
committed
fix: skip comments and strings when counting braces in augmentation removal
The brace-depth parser that finds the end of a declare module block now skips // line comments, /* block comments */ (including JSDoc), and double-quoted string literals. This prevents unbalanced braces inside JSDoc (e.g. @example code with object literals) from causing incorrect block boundaries.
1 parent 3b7db4a commit a81d8e8

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

scripts/generate-api-docs/generate.mjs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,35 @@ for (const file of dtsFiles) {
192192
// Find the opening { of this declare module block
193193
const openBrace = content.indexOf("{", blockStart);
194194
if (openBrace < 0) break; // malformed — no opening brace, skip
195-
// Find the closing } by counting brace depth
195+
// Find the closing } by counting brace depth (skip braces inside comments and strings)
196196
let braceDepth = 0;
197197
let blockEnd = -1;
198198
for (let i = openBrace; i < content.length; i++) {
199-
if (content[i] === "{") braceDepth++;
200-
else if (content[i] === "}") {
199+
const ch = content[i];
200+
// Skip single-line comments
201+
if (ch === "/" && content[i + 1] === "/") {
202+
i = content.indexOf("\n", i);
203+
if (i < 0) break;
204+
continue;
205+
}
206+
// Skip block comments (including JSDoc)
207+
if (ch === "/" && content[i + 1] === "*") {
208+
i = content.indexOf("*/", i + 2);
209+
if (i < 0) break;
210+
i += 1; // will be incremented by for-loop to skip past '*/'
211+
continue;
212+
}
213+
// Skip string literals (double-quoted)
214+
if (ch === '"') {
215+
i++;
216+
while (i < content.length && content[i] !== '"') {
217+
if (content[i] === "\\") i++;
218+
i++;
219+
}
220+
continue;
221+
}
222+
if (ch === "{") braceDepth++;
223+
else if (ch === "}") {
201224
braceDepth--;
202225
if (braceDepth === 0) { blockEnd = i + 1; break; }
203226
}

0 commit comments

Comments
 (0)