hrzndhrn / recode

A linter with autocorrection and a refactoring tool.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Task request: Redundant type definitions

antedeguemon opened this issue · comments

IMO it would be useful for large codebases if there was a task for fixing redundant type definitions. It doesn't need to actually resolve the type definitions, a superficial check would

An example of this issue in a codebase is the type role being defined in schemas/user.ex

defmodule Project.Schemas.User do
  @type role :: :admin | :guest
  
  # ...
end

Problem surges when another definition used its values - when it could reference User.role():

defmodule Project.Users do
  alias Project.Schemas.User

  # Note it uses `:admin | :guest` instead of referencing `User.role()`
  @spec update_role(User.t(), :admin | :guest) :: User.t() 
  def update_role(user, role) do
    # ...
  end 
end

Note the above only works for primitive types - if a type has a union with another type, then it complicates things as we would need to resolve the type definition with recursion:

@type guest_role :: :guest
@type admin_role :: :admin | :owner

@type role :: admin_role() | guest_role()

That sounds like a very good idea. Thank you.

Hey! I gave some more thought to this idea and I think this check is out of Recode's scope.

A linter with autocorrection and a refactoring tool.

I am closing the issue but feel free to re-open it in case you think it fits. 😸