Skip to content

Comment flush edge cases: EOF trailing comment moves to its own line; pad-space artifacts break print idempotency #175

Description

@zloirock

Three boundary conditions in the comment flush machinery (all with parser-native locs):

  1. A trailing comment at EOF without a final newline moves onto its own line:

    console.log(1); // keep me

    (no trailing \n) prints

    console.log(1);
    // keep me

    Line-bound directives (eslint-disable-line and friends) stop covering their statement. Root: flush_trailing_comments requires before(comment.loc.end, next) STRICTLY, and at EOF-no-newline the comment ends exactly at Program.loc.end.

  2. Multiline block comment before a statement leaves a stray pad space (the newline after */ is intentional per multiline block comments should have new line after them #82 - the leftover is the pad):

    /* lead
     */ console.log(1);

    prints */\n console.log(1); - note the leading space. The next print of that output drops it, so the first print is not a fixed point.

  3. Comment-only file without a final newline grows a space per print:

    // alone

    (no trailing \n) prints // alone - the pad space intended for a following statement lands INSIDE the line-comment token, so every reprint grows the comment's text by one space (non-converging).

Context: found by a parse -> print -> reparse structural gate over a ~8.5k-file TS/JS corpus while adopting esrap in core-js's build plugin, then re-verified in isolation. esrap 2.3.5; the ASTs below come from @typescript-eslint/typescript-estree 8.67 with loc/range enabled, so the repros are independent of our own (oxc-based) pipeline.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions