Skip to content

trailing_fragment option not working as expected in case stubs #14118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lukaszsamson opened this issue Dec 26, 2024 · 2 comments
Closed

trailing_fragment option not working as expected in case stubs #14118

lukaszsamson opened this issue Dec 26, 2024 · 2 comments

Comments

@lukaszsamson
Copy link
Contributor

Elixir and Erlang/OTP versions

Erlang/OTP 26 [erts-14.2.5.4] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit]

Elixir 1.18.1 (compiled with Erlang/OTP 26)

Operating system

any

Current behavior

This code (where | represents the cursor)

defmodule MyModule do
  def my_fun(a, b) do
    case a do
      ^b -> b
      %{b: ^b} = |a -> b
    end
  end
end

The cursor AST element lands on the wrong side of the second -> operator and the first one is transformed into a block as if the code was

defmodule MyModule do
  def my_fun(a, b) do
    case a do
      ^b ->
        b
        %{b: ^b} = __cursor__()
    end
  end
end
Code.Fragment.container_cursor_to_quoted("defmodule MyModule do\n def my_fun(a, b) do\n  case a do\n   ^b -> b\n   %{b: ^b} = ", trailing_fragment: "a ->b\n  end\n end\nend")
{:ok,
 {:defmodule, [line: 1],
  [
    {:__aliases__, [line: 1], [:MyModule]},
    [
      do: {:def, [line: 2],
       [
         {:my_fun, [line: 2], [{:a, [line: 2], nil}, {:b, [line: 2], nil}]},
         [
           do: {:case, [line: 3],
            [
              {:a, [line: 3], nil},
              [
                do: [
                  {:->, [line: 4],
                   [
                     [{:^, [line: 4], [{:b, [line: 4], nil}]}],
                     {:__block__, [],
                      [
                        {:b, [line: 4], nil},
                        {:=, [line: 5],
                         [
                           {:%{}, [line: 5],
                            [b: {:^, [line: 5], [{:b, [line: 5], nil}]}]},
                           {:__cursor__, [line: 5], []}
                         ]}
                      ]}
                   ]}
                ]
              ]
            ]}
         ]
       ]}
    ]
  ]}}

trailing_fragment option does not seem to have any effect

Code.Fragment.container_cursor_to_quoted("defmodule MyModule do\n def my_fun(a, b) do\n  case a do\n   ^b -> b\n   %{b: ^b} = ")
{:ok,
 {:defmodule, [line: 1],
  [
    {:__aliases__, [line: 1], [:MyModule]},
    [
      do: {:def, [line: 2],
       [
         {:my_fun, [line: 2], [{:a, [line: 2], nil}, {:b, [line: 2], nil}]},
         [
           do: {:case, [line: 3],
            [
              {:a, [line: 3], nil},
              [
                do: [
                  {:->, [line: 4],
                   [
                     [{:^, [line: 4], [{:b, [line: 4], nil}]}],
                     {:__block__, [],
                      [
                        {:b, [line: 4], nil},
                        {:=, [line: 5],
                         [
                           {:%{}, [line: 5],
                            [b: {:^, [line: 5], [{:b, [line: 5], nil}]}]},
                           {:__cursor__, [line: 5], []}
                         ]}
                      ]}
                   ]}
                ]
              ]
            ]}
         ]
       ]}
    ]
  ]}}

Expected behavior

I'm aware that this case in ambiguous but given the trailing fragment container_cursor_to_quoted should be able to resolve it in favour of correct AST

@josevalim
Copy link
Member

@lukaszsamson let me know if this is all good and I will do a new Elixir v1.18 release.

@lukaszsamson
Copy link
Contributor Author

@josevalim I tried out 1.18 branch with ba945e9 cherry picked and it looks good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants