Skip to content

trailing_fragment option not working as expected in case stubs #14118

Closed
@lukaszsamson

Description

@lukaszsamson

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions