Skip to content

Commit fbfb1b4

Browse files
committed
Do not extract comments without a newline
1 parent cf5762a commit fbfb1b4

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

lib/elixir/lib/code/formatter.ex

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,19 +1873,25 @@ defmodule Code.Formatter do
18731873
end
18741874

18751875
defp each_quoted_to_algebra_with_comments([arg | args], acc, max_line, state, comments?, fun) do
1876-
{doc_start, doc_end} = traverse_line(arg, {@max_line, @min_line})
1876+
case traverse_line(arg, {@max_line, @min_line}) do
1877+
{@max_line, @min_line} ->
1878+
{doc_triplet, state} = fun.(arg, args, state)
1879+
acc = [doc_triplet | acc]
1880+
each_quoted_to_algebra_with_comments(args, acc, max_line, state, comments?, fun)
18771881

1878-
{acc, comments, comments?} =
1879-
extract_comments_before(doc_start, acc, state.comments, comments?)
1882+
{doc_start, doc_end} ->
1883+
{acc, comments, comments?} =
1884+
extract_comments_before(doc_start, acc, state.comments, comments?)
18801885

1881-
{doc_triplet, state} = fun.(arg, args, %{state | comments: comments})
1886+
{doc_triplet, state} = fun.(arg, args, %{state | comments: comments})
18821887

1883-
{acc, comments, comments?} =
1884-
extract_comments_trailing(doc_start, doc_end, acc, state.comments, comments?)
1888+
{acc, comments, comments?} =
1889+
extract_comments_trailing(doc_start, doc_end, acc, state.comments, comments?)
18851890

1886-
acc = [adjust_trailing_newlines(doc_triplet, doc_end, comments) | acc]
1887-
state = %{state | comments: comments}
1888-
each_quoted_to_algebra_with_comments(args, acc, max_line, state, comments?, fun)
1891+
acc = [adjust_trailing_newlines(doc_triplet, doc_end, comments) | acc]
1892+
state = %{state | comments: comments}
1893+
each_quoted_to_algebra_with_comments(args, acc, max_line, state, comments?, fun)
1894+
end
18891895
end
18901896

18911897
defp extract_comments_before(max, acc, [%{line: line} = comment | rest], _) when line < max do

lib/elixir/test/elixir/code_normalizer/formatted_ast_test.exs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ defmodule Code.Normalizer.FormatterASTTest do
1515
good = String.trim(good)
1616

1717
to_quoted_opts =
18-
[
19-
literal_encoder: &{:ok, {:__block__, &2, [&1]}},
20-
token_metadata: true,
21-
unescape: false
22-
] ++ opts
18+
Keyword.merge(
19+
[
20+
literal_encoder: &{:ok, {:__block__, &2, [&1]}},
21+
token_metadata: true,
22+
unescape: false
23+
],
24+
opts
25+
)
2326

2427
{quoted, comments} = Code.string_to_quoted_with_comments!(good, to_quoted_opts)
2528

@@ -464,6 +467,29 @@ defmodule Code.Normalizer.FormatterASTTest do
464467
"""
465468
end
466469

470+
test "handles comments with unescaped literal" do
471+
assert_same """
472+
# before
473+
Mix.install([:foo])
474+
# after
475+
""",
476+
literal_encoder: fn literal, _ -> {:ok, literal} end
477+
478+
assert_same """
479+
# before
480+
Mix.install([1 + 2, :foo])
481+
# after
482+
""",
483+
literal_encoder: fn literal, _ -> {:ok, literal} end
484+
485+
assert_same """
486+
# before
487+
Mix.install([:foo, 1 + 2])
488+
# after
489+
""",
490+
literal_encoder: fn literal, _ -> {:ok, literal} end
491+
end
492+
467493
test "before and after expressions with newlines" do
468494
assert_same """
469495
# before comment

0 commit comments

Comments
 (0)