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

rbs prototype rb can have duplicated declarations #211

Closed
pocke opened this issue Feb 23, 2020 · 2 comments
Closed

rbs prototype rb can have duplicated declarations #211

pocke opened this issue Feb 23, 2020 · 2 comments

Comments

@pocke
Copy link
Member

pocke commented Feb 23, 2020

Problem

rbs prototype rb command may generate duplicated declarations.
For example:

a.rb

class A
  def foo() end
end

class A
  class B
    def bar() end
  end
end
$ rbs prototype rb c.rb
class A
  def foo: () -> untyped
end

class A
end

class A::B
  def bar: () -> untyped
end

In this case, class A is duplicated.

And it easily causes in the real applications.

Solution

Now I avoid the problem with the following code.
full version: https://gist.github.com/pocke/a3faa4b87416c3d899e626f9197f2174#file-steep-rake-L13-L33

decls = Ruby::Signature::Parser.parse_signature(sig_base_path.read)
decl_map = {}
decls.each do |decl|
  if d = decl_map[decl.name]
    # TODO: Is it right for decl is not a class / module?
    c = Ruby::Signature::AST::Declarations::Class
    m = Ruby::Signature::AST::Declarations::Module
    next unless d.is_a?(c) || d.is_a?(m)
    next unless decl.is_a?(c) || decl.is_a?(m)

    d.members.concat decl.members
    if d.is_a?(c) && decl.is_a?(c)
      decl_map[decl.name] = c.new(name: d.name, type_params: d.type_params, super_class: d.super_class || decl.super_class, members: d.members, annotations: d.annotations, location: d.location, comment: d.comment)
    end
  else
    decl_map[decl.name] = decl
  end
end

But I think rbs command should provide a solution(s) for the problem.

So, I think it is better if rbs prototype rb command displays deduped declarations.

And we can implement rbs dedupe sub-command as an alternative. The sub-command may be useful for RBS files that are generated by other programs. But I'm not sure the sub-command is actually necessary.

By the way, I guess rbs prototype rbi also has the same issue.

How about the ideas? I'd like to implement them if they are acceptable.

@soutaro
Copy link
Member

soutaro commented Feb 25, 2020

It's a good point. However, I don't think deduping declarations makes much sense. The tool is for prototyping and none of these fixes will completely solve the problem.

My suggestion is the simplest workaround: to stop generating declarations for empty classes/modules.

@pocke
Copy link
Member Author

pocke commented Dec 31, 2021

I'll close this issue because RBS became to accept duplicate class declaretions.

@pocke pocke closed this as completed Dec 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants