Skip to content

--reorder-code detaches trailing comments from top-level declarations and re-attaches them above the following declaration #271

Description

@minami110

Impact

After --reorder-code, a trailing (same-line) comment on a top-level declaration is moved to its own line below the declaration — which places it directly above the next declaration. The comment now reads as documentation for the wrong member, silently corrupting comment ownership across a codebase-wide reorder (e.g. a # read-only — do not mutate note ends up appearing to document the unrelated next field).

This looks like a --reorder-code-path sibling of #83 (fixed long ago for plain formatting — plain formatting preserves these comments correctly today).

Minimal repro

repro.gd:

extends Node

const A := 1 # comment for A
const B := 2
gdscript-formatter --reorder-code repro.gd

Actual output

extends Node

const A := 1
# comment for A
const B := 2

# comment for A is now visually (and for doc tooling, semantically) attached to const B.

Expected

extends Node

const A := 1 # comment for A
const B := 2

Trailing comments should stay on their declaration's line — exactly what plain formatting (without --reorder-code) already does.

Boundary (verified individually on 0.21.0)

Does NOT detach:

  • Plain formatting without --reorder-code
  • A trailing comment on the last declaration of the file (nothing follows it)
  • Trailing comments on statements inside function bodies

DOES detach:

  • Trailing comments on top-level const / var declarations followed by another declaration (also observed landing above a following func)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions