Home > Mobile >  for elixir, vscode's outline can't list defp function after adding `private do`
for elixir, vscode's outline can't list defp function after adding `private do`

Time:08-07

package private has been used for unit-testing defp function. But in vscode the functions in the private do ... end scope can't be listed. Is it possible to solve it or just accept it?

private scope function

outline view outline view image

CodePudding user response:

This private library is a straightforward AST transformation that basically traverses the AST within the private/1 block and substitutes defp with def in test env.

Unfortunately, ElixirLS is not capable of showing the expanded AST as it becomes after compilation, it uses its own simple parser and hence it does not see the expanded AST. So yeah, without the explicit modifications of ElixirLS and/or VSCode plugin it’s impossible to see the expanded AST. Neither would you see any injected by use Foo code, etc.


Despite the package being originally created by Dave Thomas, I would strongly discourage its usage at all though. It has two major drawbacks:

  • testing of private functions is plain wrong, they are implementation details, and testing the implementation details brings more harm than profit;
  • testing the code which is not the same as one running in production is meaningless and dangerous.

The former rule above should be obvious, the latter requires some elaboration. differently treats and even compiles private and public functions. Private functions might be inlined, what, generally speaking, changes the preemptive concurrency context switching by ErlangVM. In some rare cases, this might lead to drastically hard to catch randomly raising concurrency bugs and race conditions. Yes, this is extremely unlikely, but it’s still possible.

That said, you’d better stop testing private functions at all rather than trying to make VSCode show screwed up AST.

  • Related