Skip to content
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

Unquoting default params results in elixir_locals.ensure_no_undefined_local/3 #48

Open
spencerdcarlson opened this issue Apr 5, 2021 · 2 comments

Comments

@spencerdcarlson
Copy link

Definition

defmodule ArgsValidator do
  use Decorator.Define, validate: 1

  def validate(_, _, %{args: function_args}) do
    quote do: unquote(function_args)
  end
end

Usage

use ArgsValidator

@decorate validate([&Ecto.UUID.cast/1])
  defdelegate resource(uuid, opts \\ []),
    to: ResourceByUUID,
    as: :call
    (elixir) src/elixir_locals.erl:108: :elixir_locals."-ensure_no_undefined_local/3-lc$^0/1-0-"/2
    (elixir) src/elixir_locals.erl:108: anonymous fn/3 in :elixir_locals.ensure_no_undefined_local/3
    (stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
** (exit) shutdown: 1
    (mix) lib/mix/tasks/compile.all.ex:59: Mix.Tasks.Compile.All.do_compile/4
    (mix) lib/mix/tasks/compile.all.ex:24: anonymous fn/1 in Mix.Tasks.Compile.All.run/1
    (mix) lib/mix/tasks/compile.all.ex:40: Mix.Tasks.Compile.All.with_logger_app/1
    (mix) lib/mix/task.ex:331: Mix.Task.run_task/3
    (mix) lib/mix/tasks/compile.ex:96: Mix.Tasks.Compile.run/1
    (mix) lib/mix/task.ex:331: Mix.Task.run_task/3
    (iex) lib/iex/helpers.ex:104: IEx.Helpers.recompile/1
@Ivor
Copy link

Ivor commented Oct 9, 2022

Printing out the args returns this for the variable with the default value:

{:\\, [line: 170], [{:attrs, [line: 170], nil}, {:%{}, [line: 170], []}]}

and returns this compile error:

....ex:170: undefined function \\/2

@Ivor
Copy link

Ivor commented Oct 9, 2022

Not something I am very familiar with, but I am wondering if this would work:

    args_with_or_default =
      Enum.reduce(args, [], fn
        {:\\, _line, [{_var_name, _var_line, nil}, _default_value]} = ast_for_default, acc ->
          new_ast_for_default =
            ast_for_default
            |> Tuple.delete_at(0)
            |> Tuple.insert_at(0, :||)

          acc ++ [new_ast_for_default]

        arg, acc ->
          acc ++ [arg]
      end)

And then using args_with_or_default as the args.

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

No branches or pull requests

2 participants