Skip to content

Keep trailing comments after nodes without closing metadata - #1

Closed
rubas-agent[bot] wants to merge 1 commit into
mainfrom
fix-positionless-comment-attachment
Closed

rubas-agent[bot] wants to merge 1 commit into
mainfrom
fix-positionless-comment-attachment

Conversation

@rubas-agent

@rubas-agent rubas-agent Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Since elixir-lang/elixir#15893, Code.quoted_to_algebra/2 moves a comment into the node before it when that node has no :closing or :end metadata. ASTs built by hand or with quote often have no such metadata. On main, this breaks the Igniter tests of Phoenix LiveView (test/phoenix_live_view/igniter/upgrade_to_1_1_test.exs).

Here a list holds a call without metadata, and a comment follows the call:

comments = [%{line: 2, column: 3, previous_eol_count: 1, next_eol_count: 1, text: "# comment"}]

{:__block__, [line: 1, closing: [line: 3]], [[{:foo, [], [1]}]]}
|> Code.quoted_to_algebra(comments: comments)
|> Inspect.Algebra.format(98)
|> IO.puts()

v1.20 and this branch:

[
  foo(1)
  # comment
]

main:

[
  foo(
    1
    # comment
  )
]

SteffenDE reported the same bug with a 3-tuple in a comment on elixir-lang/elixir#15893. Operators, 2-tuples, maps, lists and keyword lists without metadata break the same way.

The cause is comments_boundaries/3 in code.ex. A node without :closing or :end gives its children the boundary of its parent, or its own :end_of_expression line. The last child then takes every comment up to that line as its trailing comments.

With this change, such a node gives its children no boundary, so a comment stays with the nearest node that has :closing or :end. :-> passes the boundary on to its body, as :. already does. In Code.Formatter.map_to_algebra/4, the | of a map update now prints its trailing comments, because comments can attach to it now.

Tests: quoted_ast_test.exs has the 3-tuple, call and operator cases without metadata. comments_test.exs has parsed cases with a comment after an operator, an attribute, a list tail, a map update, and a single expression in parens. All of them fail on main.

@rubas-agent

rubas-agent Bot commented Oct 10, 2026

Copy link
Copy Markdown
Author

Upstream merged the fix in elixir-lang#15961.

@rubas-agent rubas-agent Bot closed this Oct 10, 2026
@rubas-agent
rubas-agent Bot deleted the fix-positionless-comment-attachment branch October 10, 2026 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant