hrzndhrn / recode

A linter with autocorrection and a refactoring tool.
MIT License
283 stars 15 forks source link

Task request: Redundant type definitions #32

Closed antedeguemon closed 1 year ago

antedeguemon commented 1 year ago

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
antedeguemon commented 1 year ago

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()
NickNeck commented 1 year ago

That sounds like a very good idea. Thank you.

antedeguemon commented 1 year ago

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. 😸