Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Oct 22, 2024
1 parent 66775f5 commit 8a3f7b5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
11 changes: 9 additions & 2 deletions lib/elixir/lib/code/normalizer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,10 @@ defmodule Code.Normalizer do
# def foo, do: :ok
normalize_kw_blocks(form, meta, args, state)

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

Expand Down Expand Up @@ -382,6 +383,12 @@ defmodule Code.Normalizer do
end
end

defp block_keyword?([{{:__block__, _, [key]}, _val} | tail]) when is_atom(key),
do: block_keyword?(tail)

defp block_keyword?([]), do: true
defp block_keyword?(_), do: false

defp allow_keyword?(:when, 2), do: true
defp allow_keyword?(:{}, _), do: false
defp allow_keyword?(op, arity), do: not is_atom(op) or not Macro.operator?(op, arity)
Expand Down
32 changes: 17 additions & 15 deletions lib/elixir/test/elixir/code_normalizer/quoted_ast_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -629,41 +629,43 @@ defmodule Code.Normalizer.QuotedASTTest do
assert quoted_to_string(quote(do: foo |> [bar: :baz])) == "foo |> [bar: :baz]"
end

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

ast = Code.string_to_quoted!(input)
assert quoted_to_string(ast) == expected

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

ast =
Code.string_to_quoted!(input,
literal_encoder: &{:ok, {:__block__, &2, [&1]}}
)
ast = Code.string_to_quoted!(input, token_metadata: true)
assert quoted_to_string(ast) == expected

ast = Code.string_to_quoted!(input, literal_encoder: encoder, token_metadata: true)
assert quoted_to_string(ast) == expected
end

test "keyword arg edge case: literal encoder" do
test "keyword arg with tokenizer options" do
input = """
foo(Bar) do
:ok
end
end\
"""

expected = String.trim(input)

ast = Code.string_to_quoted!(input)
assert quoted_to_string(ast) == input

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

ast =
Code.string_to_quoted!(input,
literal_encoder: &{:ok, {:__block__, &2, [&1]}}
)
ast = Code.string_to_quoted!(input, token_metadata: true)
assert quoted_to_string(ast) == input

assert quoted_to_string(ast) == expected
ast = Code.string_to_quoted!(input, literal_encoder: encoder, token_metadata: true)
assert quoted_to_string(ast) == input
end

test "list in module attribute" do
Expand Down

0 comments on commit 8a3f7b5

Please sign in to comment.