Skip to content

Commit 96212bb

Browse files
committed
Attemptive fix for edge case in normalizer for keyword args - wip
1 parent 673fe4a commit 96212bb

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

lib/elixir/lib/code/normalizer.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ defmodule Code.Normalizer do
347347
args = normalize_args(args, %{state | parent_meta: meta})
348348
{form, meta, args}
349349

350-
Keyword.has_key?(meta, :do) or match?([{{:__block__, _, [:do]}, _} | _], last) ->
350+
Keyword.has_key?(meta, :do) ->
351351
# def foo do :ok end
352352
# def foo, do: :ok
353353
normalize_kw_blocks(form, meta, args, state)

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,45 @@ 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
633+
input = "def foo, do: :bar, __cursor__()"
634+
expected = "def foo, [{:do, :bar}, __cursor__()]"
635+
636+
ast = Code.string_to_quoted!(input, token_metadata: true)
637+
638+
assert quoted_to_string(ast) == expected
639+
640+
ast =
641+
Code.string_to_quoted!(input,
642+
token_metadata: true,
643+
literal_encoder: &{:ok, {:__block__, &2, [&1]}}
644+
)
645+
646+
assert quoted_to_string(ast) == expected
647+
end
648+
649+
test "keyword arg edge case: literal encoder" do
650+
input = """
651+
foo Bar do
652+
:ok
653+
end
654+
"""
655+
656+
expected = String.trim(input)
657+
658+
ast = Code.string_to_quoted!(input, token_metadata: true)
659+
660+
assert quoted_to_string(ast) == expected
661+
662+
ast =
663+
Code.string_to_quoted!(input,
664+
token_metadata: true,
665+
literal_encoder: &{:ok, {:__block__, &2, [&1]}}
666+
)
667+
668+
assert quoted_to_string(ast) == expected
669+
end
670+
632671
test "list in module attribute" do
633672
assert quoted_to_string(
634673
quote do

0 commit comments

Comments
 (0)