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

Abierto Apto para principiantes
#3,166 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
84/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
node.js, typescript

Línea de trabajo

Empieza con collectCommentRanges() en src/lib/converter/comments/discovery.ts y sigue cómo agrupa los rangos de comentarios de una sola línea y de bloque; el issue incluye el bucle pertinente y una reproducción. Comprueba el lexer de comentarios de línea en src/lib/converter/comments/lineLexer.ts para entender cómo se consume el rango. Añade una prueba de regresión para comentarios de línea seguidos de un comentario de bloque con commentStyle: "line"; se considera terminado cuando el comentario de la declaración contiene solo el comentario de línea precedente.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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
Lenguaje dominante
TypeScript
Estrellas
8.5k
Forks
781
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de TypeStrong/typedoc

Todos los issues de TypeStrong/typedoc

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.