Skip to content

Commit 8a3f7b5

Browse files
committed
Fixes
1 parent 66775f5 commit 8a3f7b5

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

lib/elixir/lib/code/normalizer.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,10 @@ defmodule Code.Normalizer do
352352
# def foo, do: :ok
353353
normalize_kw_blocks(form, meta, args, state)
354354

355-
match?([{:do, _} | _], last) and Keyword.keyword?(last) ->
355+
(match?([{:do, _} | _], last) and Keyword.keyword?(last)) or
356+
(match?([{{:__block__, _, [:do]}, _} | _], last) and block_keyword?(last)) ->
356357
# Non normalized kw blocks
357-
line = state.parent_meta[:line]
358+
line = state.parent_meta[:line] || meta[:line]
358359
meta = meta ++ [do: [line: line], end: [line: line]]
359360
normalize_kw_blocks(form, meta, args, state)
360361

@@ -382,6 +383,12 @@ defmodule Code.Normalizer do
382383
end
383384
end
384385

386+
defp block_keyword?([{{:__block__, _, [key]}, _val} | tail]) when is_atom(key),
387+
do: block_keyword?(tail)
388+
389+
defp block_keyword?([]), do: true
390+
defp block_keyword?(_), do: false
391+
385392
defp allow_keyword?(:when, 2), do: true
386393
defp allow_keyword?(:{}, _), do: false
387394
defp allow_keyword?(op, arity), do: not is_atom(op) or not Macro.operator?(op, arity)

lib/elixir/test/elixir/code_normalizer/quoted_ast_test.exs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -629,41 +629,43 @@ defmodule Code.Normalizer.QuotedASTTest do
629629
assert quoted_to_string(quote(do: foo |> [bar: :baz])) == "foo |> [bar: :baz]"
630630
end
631631

632-
test "keyword arg edge case: cursor" do
632+
test "keyword arg with cursor" do
633633
input = "def foo, do: :bar, __cursor__()"
634634
expected = "def foo, [{:do, :bar}, __cursor__()]"
635635

636636
ast = Code.string_to_quoted!(input)
637+
assert quoted_to_string(ast) == expected
637638

639+
encoder = &{:ok, {:__block__, &2, [&1]}}
640+
ast = Code.string_to_quoted!(input, literal_encoder: encoder)
638641
assert quoted_to_string(ast) == expected
639642

640-
ast =
641-
Code.string_to_quoted!(input,
642-
literal_encoder: &{:ok, {:__block__, &2, [&1]}}
643-
)
643+
ast = Code.string_to_quoted!(input, token_metadata: true)
644+
assert quoted_to_string(ast) == expected
644645

646+
ast = Code.string_to_quoted!(input, literal_encoder: encoder, token_metadata: true)
645647
assert quoted_to_string(ast) == expected
646648
end
647649

648-
test "keyword arg edge case: literal encoder" do
650+
test "keyword arg with tokenizer options" do
649651
input = """
650652
foo(Bar) do
651653
:ok
652-
end
654+
end\
653655
"""
654656

655-
expected = String.trim(input)
656-
657657
ast = Code.string_to_quoted!(input)
658+
assert quoted_to_string(ast) == input
658659

659-
assert quoted_to_string(ast) == expected
660+
encoder = &{:ok, {:__block__, &2, [&1]}}
661+
ast = Code.string_to_quoted!(input, literal_encoder: encoder)
662+
assert quoted_to_string(ast) == input
660663

661-
ast =
662-
Code.string_to_quoted!(input,
663-
literal_encoder: &{:ok, {:__block__, &2, [&1]}}
664-
)
664+
ast = Code.string_to_quoted!(input, token_metadata: true)
665+
assert quoted_to_string(ast) == input
665666

666-
assert quoted_to_string(ast) == expected
667+
ast = Code.string_to_quoted!(input, literal_encoder: encoder, token_metadata: true)
668+
assert quoted_to_string(ast) == input
667669
end
668670

669671
test "list in module attribute" do

0 commit comments

Comments
 (0)