Skip to content

Interspersed comments get moved to the top when aliasing modules #239

@kerryb

Description

@kerryb

An obscure minor issue that I came across when upgrading Styler from 1.4.2 to 1.5.0.

I ran mix format, and noticed that all the comments in my .credo.exs file had moved to the top. After a bit of playing around to see what I could remove from the file and still see the bug, it turned out to be because I accidentally had the same module included twice, which was triggering Styler to alias it. In my case removing the duplicate fixed it, but the duplication could have been valid so I thought I’d mention it :-)

Here’s a simple chunk of code that illustrates the issue:

Before

[
  # First comment
  Foo,

  # Second comment
  Foo,
  Foo.Bar.Baz,
  Foo.Bar.Baz,
  Bar,
  "An otherwise irrelevant long string that’s only here to stop the formatter putting the list on one line"
]

After running the formatter

# First comment

# Second comment
alias Foo.Bar.Baz

[
  Foo,
  Foo,
  Baz,
  Baz,
  Bar,
  "An otherwise irrelevant long string that’s only here to stop the formatter putting the list on one line"
]

Expected result

alias Foo.Bar.Baz

[
  # First comment
  Foo,

  # Second comment
  Foo,
  Baz,
  Baz,
  Bar,
  "An otherwise irrelevant long string that’s only here to stop the formatter putting the list on one line"
]

Also, it looks like it’s only a problem when the code is “loose” in a script – it doesn’t happen if it’s in a function in a module (maybe because the alias ends up in a different scope?), which probably makes it very unlikely that it’ll ever cause any real problems for anyone!

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