Block comment leaks into the line comments before it with `--commentStyle line`

Aperta Adatta ai principianti
#3,166 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
84/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
node.js, typescript

Direzione di ricerca

Inizia da collectCommentRanges() in src/lib/converter/comments/discovery.ts e segui il modo in cui raggruppa gli intervalli dei commenti su una singola riga e dei commenti di blocco; l’issue include il ciclo pertinente e una riproduzione. Controlla il lexer dei commenti di riga in src/lib/converter/comments/lineLexer.ts per capire come viene consumato l’intervallo. Aggiungi un test di regressione per i commenti di riga seguiti da un commento di blocco con commentStyle: "line"; il lavoro è completato quando il commento della dichiarazione contiene solo il commento di riga precedente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Search terms

Line comments followed by a block comment, commentStyle line, coverage pragma, istanbul ignore, garbled comment, collectCommentRanges

Expected Behavior

With --commentStyle line, a declaration takes its comment from the // lines above it, also where a block comment, such as a coverage pragma, sits between those lines and the declaration, since that style reads no block comment.

Actual Behavior

The comment runs on through the block. In the example below, the summary of add is Adds two numbers.\n* istanbul ignore next */. A /** */ block there leaks its tags as well: /** @pattern ^#[0-9a-f]{6}$ */ after a // line adds a tag @pattern whose content ends in */, and TypeDoc warns about an unknown block tag.

The cause is in collectCommentRanges(), which adds every range to the current group of line comments at the top of its loop, before it looks at the range's kind (discovery.ts#L700-L713):

for (const range of ranges || []) {
    collect.push(range);

    switch (range.kind) {
        case ts.SyntaxKind.MultiLineCommentTrivia:
            if (collect.length) {
                result.push(collect);
                collect = [];
            }
            result.push([range]);
            break;
        case ts.SyntaxKind.SingleLineCommentTrivia:
            collect.push(range);
            break;

So a block closes the group of the lines before it as that group's last range, and each line sits in its group twice. With commentStyle: "line", permittedRange() checks only ranges[0].kind, so it takes that group, and lexLineComments() lexes from ranges[0].pos to ranges[ranges.length - 1].end (lineLexer.ts#L10-L15), which is the end of the block.

Steps to reproduce the bug

mkdir repro && cd repro
mkdir src && cat > src/index.ts <<'TS'
// Adds two numbers.
/* istanbul ignore next */
export function add(a: number, b: number): number {
    return a + b;
}
TS
echo '{ "include": ["src"] }' > tsconfig.json

npx -y -p [email protected] -p [email protected] typedoc src/index.ts --name repro --commentStyle line --json docs.json
node -p 'JSON.stringify(require("./docs.json").children[0].signatures[0].comment, null, 2)'
{
  "summary": [
    {
      "kind": "text",
      "text": "Adds two numbers.\n* istanbul ignore next */"
    }
  ]
}

Would dropping the first collect.push(range); be the fix? With that line deleted from the dist/index.js of 0.28.20, the summary is Adds two numbers. alone. I also ran a file with lines alone, a block alone, a block before lines, lines before a block, lines across a blank line, and two blocks, under each of the styles jsdoc, block, line and all: the output changes only for lines before a block under line. I can open a pull request with a test, if that's welcome.

Environment

  • TypeDoc version: 0.28.20
  • TypeScript version: 6.0.3
  • Node.js version: 24.22.3
  • OS: Linux
Lingua principale
TypeScript
Stelle
8.5k
Fork
781
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di TypeStrong/typedoc

Tutte le issue di TypeStrong/typedoc

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.