Using locals_without_parens with Code.format_string!

Hi,

For some reason the :locals_without_parens seems to be ignored:

iex(1)> q = quote do
...(1)> myfun :a, :b
...(1)> end
{:myfun, [], [:a, :b]}
iex(2)> s = Macro.to_string(q)
"myfun(:a, :b)"
iex(3)> Code.format_string! s, locals_without_parens: [myfun: 2]
["myfun", "(", "", ":a", ",", " ", ":b", "", ")"]

Am I missing something ?

Thank you

The setting afaik never removes existing parenthesis. It only prevents the formatter from adding ones.

1 Like

Oh … I see, thank you.

I tried to add the no_parens: true flag with Macro.postwalk before calling Macro.to_string but it does not work either.

Only thing that works then is the following:

|> Macro.to_string(fn
  {:myfun, meta, args}, _ ->
    "myfun #{args |> Enum.map(&Macro.to_string/1) |> Enum.join(", ")}"

  _, other ->
    other
end)

I thing it is fragile but it will do for now.