Skip to content

Commit 91ff762

Browse files
committed
Cleanups
1 parent b12a9a3 commit 91ff762

File tree

2 files changed

+126
-26
lines changed

2 files changed

+126
-26
lines changed

lib/elixir/lib/code/comments.ex

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ defmodule Code.Comments do
269269
end_line = get_end_line(quoted, start_line)
270270

271271
{trailing_comments, comments} =
272-
Enum.split_with(comments, &(&1.line > start_line and &1.line < end_line))
272+
split_trailing_comments(comments, start_line, end_line)
273273

274274
quoted = append_comments(quoted, :inner_comments, trailing_comments)
275275

@@ -281,7 +281,7 @@ defmodule Code.Comments do
281281
end_line = get_end_line(quoted, start_line)
282282

283283
{trailing_comments, comments} =
284-
Enum.split_with(comments, &(&1.line > start_line and &1.line < end_line))
284+
split_trailing_comments(comments, start_line, end_line)
285285

286286
last_value = append_comments(last_value, :trailing_comments, trailing_comments)
287287

@@ -295,7 +295,7 @@ defmodule Code.Comments do
295295
end_line = get_end_line(quoted, start_line)
296296

297297
{trailing_comments, comments} =
298-
Enum.split_with(comments, &(&1.line > start_line and &1.line < end_line))
298+
split_trailing_comments(comments, start_line, end_line)
299299

300300
unquote_splicing =
301301
append_comments(unquote_splicing, :trailing_comments, trailing_comments)
@@ -316,7 +316,7 @@ defmodule Code.Comments do
316316
end_line = get_end_line(quoted, start_line)
317317

318318
{trailing_comments, comments} =
319-
Enum.split_with(comments, &(&1.line > start_line and &1.line < end_line))
319+
split_trailing_comments(comments, start_line, end_line)
320320

321321
value = append_comments(value, :trailing_comments, trailing_comments)
322322

@@ -342,7 +342,7 @@ defmodule Code.Comments do
342342
end_line = get_end_line(quoted, start_line)
343343

344344
{trailing_comments, comments} =
345-
Enum.split_with(comments, &(&1.line > start_line and &1.line < end_line))
345+
split_trailing_comments(comments, start_line, end_line)
346346

347347
right = append_comments(right, :trailing_comments, trailing_comments)
348348

@@ -366,7 +366,7 @@ defmodule Code.Comments do
366366
line = get_line(call)
367367

368368
{trailing_comments, comments} =
369-
Enum.split_with(state.comments, &(&1.line > line and &1.line < end_line))
369+
split_trailing_comments(state.comments, line, end_line)
370370

371371
call = append_comments(call, :trailing_comments, trailing_comments)
372372

@@ -380,7 +380,7 @@ defmodule Code.Comments do
380380
case left do
381381
[] ->
382382
{leading_comments, comments} =
383-
Enum.split_with(comments, &(&1.line > block_start and &1.line < start_line))
383+
split_leading_comments(comments, block_start, start_line)
384384

385385
quoted = append_comments(quoted, :leading_comments, leading_comments)
386386

@@ -401,11 +401,11 @@ defmodule Code.Comments do
401401
with true <- is_atom(form),
402402
<<"sigil_", _name::binary>> <- Atom.to_string(form),
403403
true <- not is_nil(meta) do
404-
[content, modifiers] = args
404+
[content | modifiers] = args
405405

406406
{content, state} = merge_mixed_comments(content, state)
407407

408-
quoted = put_args(quoted, [content, modifiers])
408+
quoted = put_args(quoted, [content | modifiers])
409409

410410
{quoted, state}
411411
else
@@ -461,7 +461,7 @@ defmodule Code.Comments do
461461
line = get_line(last_value)
462462

463463
{trailing_comments, comments} =
464-
Enum.split_with(comments, &(&1.line > line and &1.line < end_line))
464+
split_trailing_comments(comments, line, end_line)
465465

466466
last_value = append_comments(last_value, :trailing_comments, trailing_comments)
467467

@@ -488,7 +488,7 @@ defmodule Code.Comments do
488488
start_line = get_line(last_block_arg)
489489

490490
{trailing_comments, comments} =
491-
Enum.split_with(comments, &(&1.line > start_line and &1.line < end_line))
491+
split_trailing_comments(comments, start_line, end_line)
492492

493493
last_block_arg = append_comments(last_block_arg, :trailing_comments, trailing_comments)
494494

@@ -510,7 +510,7 @@ defmodule Code.Comments do
510510
line = get_end_line(last_arg, get_line(last_arg))
511511

512512
{trailing_comments, comments} =
513-
Enum.split_with(comments, &(&1.line > line and &1.line < end_line))
513+
split_trailing_comments(comments, line, end_line)
514514

515515
last_arg = append_comments(last_arg, :trailing_comments, trailing_comments)
516516

@@ -523,7 +523,7 @@ defmodule Code.Comments do
523523

524524
nil ->
525525
{trailing_comments, comments} =
526-
Enum.split_with(comments, &(&1.line > start_line and &1.line < end_line))
526+
split_trailing_comments(comments, start_line, end_line)
527527

528528
quoted = append_comments(quoted, :inner_comments, trailing_comments)
529529
{quoted, comments}
@@ -544,10 +544,10 @@ defmodule Code.Comments do
544544
value_line = get_line(value)
545545

546546
{leading_comments, comments} =
547-
Enum.split_with(state.comments, &(&1.line > start_line and &1.line <= value_line))
547+
split_leading_comments(state.comments, start_line, value_line)
548548

549549
{trailing_comments, comments} =
550-
Enum.split_with(comments, &(&1.line > value_line and &1.line < end_line))
550+
split_trailing_comments(comments, value_line, end_line)
551551

552552
value = put_leading_comments(value, leading_comments)
553553
value = put_trailing_comments(value, trailing_comments)
@@ -566,10 +566,10 @@ defmodule Code.Comments do
566566
value_line = get_line(value)
567567

568568
{leading_comments, comments} =
569-
Enum.split_with(state.comments, &(&1.line > start_line and &1.line <= value_line))
569+
split_leading_comments(state.comments, start_line, value_line)
570570

571571
{trailing_comments, comments} =
572-
Enum.split_with(comments, &(&1.line > value_line and &1.line < end_line))
572+
split_trailing_comments(comments, value_line, end_line)
573573

574574
value = put_leading_comments(value, leading_comments)
575575
value = put_trailing_comments(value, trailing_comments)
@@ -590,7 +590,7 @@ defmodule Code.Comments do
590590
end_line = get_end_line(quoted, start_line)
591591

592592
{trailing_comments, comments} =
593-
Enum.split_with(comments, &(&1.line > start_line and &1.line < end_line))
593+
split_trailing_comments(comments, start_line, end_line)
594594

595595
last_value = append_comments(last_value, :trailing_comments, trailing_comments)
596596

@@ -603,7 +603,7 @@ defmodule Code.Comments do
603603
end_line = get_end_line(quoted, start_line)
604604

605605
{trailing_comments, comments} =
606-
Enum.split_with(comments, &(&1.line > start_line and &1.line < end_line))
606+
split_trailing_comments(comments, start_line, end_line)
607607

608608
unquote_splicing =
609609
append_comments(unquote_splicing, :trailing_comments, trailing_comments)
@@ -654,7 +654,7 @@ defmodule Code.Comments do
654654
case last_arg do
655655
nil ->
656656
{trailing_comments, comments} =
657-
Enum.split_with(comments, &(&1.line > block_start and &1.line < block_end))
657+
split_trailing_comments(comments, block_start, block_end)
658658

659659
block_args = append_comments(block_args, :inner_comments, trailing_comments)
660660

@@ -736,7 +736,7 @@ defmodule Code.Comments do
736736
stab_line = get_line(stab)
737737

738738
{leading_comments, comments} =
739-
Enum.split_with(comments, &(&1.line > block_start and &1.line < stab_line))
739+
split_leading_comments(comments, block_start, stab_line)
740740

741741
stab = append_comments(stab, :leading_comments, leading_comments)
742742

@@ -753,7 +753,7 @@ defmodule Code.Comments do
753753

754754
call ->
755755
{trailing_comments, comments} =
756-
Enum.split_with(comments, &(&1.line > start_line and &1.line < end_line))
756+
split_trailing_comments(comments, start_line, end_line)
757757

758758
call = append_comments(call, :trailing_comments, trailing_comments)
759759

@@ -775,7 +775,7 @@ defmodule Code.Comments do
775775
case last_arg do
776776
nil ->
777777
{trailing_comments, comments} =
778-
Enum.split_with(comments, &(&1.line > block_start and &1.line < block_end))
778+
split_trailing_comments(comments, block_start, block_end)
779779

780780
trailing_comments = Enum.sort_by(trailing_comments, & &1.line)
781781

@@ -810,13 +810,14 @@ defmodule Code.Comments do
810810
line =
811811
case last_arg do
812812
[] -> block_start
813+
[{_key, value} | _] -> get_line(value)
813814
[first | _] -> get_line(first)
814815
{_, _, _} -> get_line(last_arg)
815816
_ -> block_start
816817
end
817818

818819
{trailing_comments, comments} =
819-
Enum.split_with(comments, &(&1.line > line and &1.line < block_end))
820+
split_trailing_comments(comments, line, block_end)
820821

821822
last_arg = append_comments(last_arg, :trailing_comments, trailing_comments)
822823

@@ -938,4 +939,12 @@ defmodule Code.Comments do
938939
_ -> false
939940
end)
940941
end
942+
943+
defp split_leading_comments(comments, min, max) do
944+
Enum.split_with(comments, &(&1.line > min and &1.line <= max))
945+
end
946+
947+
defp split_trailing_comments(comments, min, max) do
948+
Enum.split_with(comments, &(&1.line > min and &1.line < max))
949+
end
941950
end

lib/elixir/test/elixir/code/ast_comments_test.exs

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ defmodule Code.AstCommentsTest do
44
use ExUnit.Case, async: true
55

66
def parse_string!(string) do
7-
Code.string_to_quoted!(string, include_comments: true, literal_encoder: &{:ok, {:__block__, &2, [&1]}}, emit_warnings: false)
7+
Code.string_to_quoted!(string,
8+
include_comments: true,
9+
literal_encoder: &{:ok, {:__block__, &2, [&1]}},
10+
emit_warnings: false
11+
)
812
end
913

1014
describe "merge_comments/2" do
@@ -210,7 +214,6 @@ defmodule Code.AstCommentsTest do
210214
#trailing 3
211215
] # trailing outside
212216
""")
213-
|> IO.inspect()
214217

215218
assert {:__block__, list_meta,
216219
[
@@ -671,5 +674,93 @@ defmodule Code.AstCommentsTest do
671674

672675
assert [%{line: 2, text: "# leading"}] = stab_meta[:leading_comments]
673676
end
677+
678+
# String Interpolations
679+
680+
test "merges comments in interpolations" do
681+
quoted =
682+
parse_string!(~S"""
683+
# leading
684+
"Hello #{world}"
685+
# trailing
686+
""")
687+
688+
assert {:<<>>, meta,
689+
[
690+
"Hello ",
691+
{:"::", _,
692+
[{{:., _, [Kernel, :to_string]}, _, [{:world, _, _}]}, {:binary, _, _}]}
693+
]} = quoted
694+
695+
assert [%{line: 1, text: "# leading"}] = meta[:leading_comments]
696+
assert [%{line: 3, text: "# trailing"}] = meta[:trailing_comments]
697+
end
698+
699+
test "merges comments in interpolated strings" do
700+
quoted =
701+
parse_string!(~S"""
702+
# leading
703+
"Hello #{
704+
# leading world
705+
world
706+
# trailing world
707+
}"
708+
# trailing
709+
""")
710+
711+
assert {:<<>>, meta,
712+
[
713+
"Hello ",
714+
{:"::", _,
715+
[{{:., _, [Kernel, :to_string]}, _, [{:world, world_meta, _}]}, {:binary, _, _}]}
716+
]} = quoted
717+
718+
assert [%{line: 1, text: "# leading"}] = meta[:leading_comments]
719+
assert [%{line: 3, text: "# leading world"}] = world_meta[:leading_comments]
720+
assert [%{line: 5, text: "# trailing world"}] = world_meta[:trailing_comments]
721+
assert [%{line: 7, text: "# trailing"}] = meta[:trailing_comments]
722+
end
723+
724+
# List interpolations
725+
726+
test "merges comments in list interpolations" do
727+
quoted =
728+
parse_string!(~S"""
729+
# leading
730+
'Hello #{world}'
731+
# trailing
732+
""")
733+
734+
assert {{:., _, [List, :to_charlist]}, meta,
735+
[
736+
["Hello ", {{:., _, [Kernel, :to_string]}, _, [{:world, _, _}]}]
737+
]} = quoted
738+
739+
assert [%{line: 1, text: "# leading"}] = meta[:leading_comments]
740+
assert [%{line: 3, text: "# trailing"}] = meta[:trailing_comments]
741+
end
742+
743+
test "merges comments in list interpolations with comments" do
744+
quoted =
745+
parse_string!(~S"""
746+
# leading
747+
'Hello #{
748+
# leading world
749+
world
750+
# trailing world
751+
}'
752+
# trailing
753+
""")
754+
755+
assert {{:., _, [List, :to_charlist]}, meta,
756+
[
757+
["Hello ", {{:., _, [Kernel, :to_string]}, _, [{:world, world_meta, _}]}]
758+
]} = quoted
759+
760+
assert [%{line: 1, text: "# leading"}] = meta[:leading_comments]
761+
assert [%{line: 3, text: "# leading world"}] = world_meta[:leading_comments]
762+
assert [%{line: 5, text: "# trailing world"}] = world_meta[:trailing_comments]
763+
assert [%{line: 7, text: "# trailing"}] = meta[:trailing_comments]
764+
end
674765
end
675766
end

0 commit comments

Comments
 (0)