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

Add command spoom srb sigs translate to translate RBI signatures into RBS comments #611

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Morriar
Copy link
Collaborator

@Morriar Morriar commented Oct 3, 2024

Consider the following file.rb:

# typed: strict

class Foo
  sig { returns(String) }
  attr_reader :x

  sig { params(x: String).void }
  def initialize(x)
    @x = x
  end
end

We can translate the RBI signatures it contains using the introduced Spoom command:

$ spoom srb sigs translate file.rb

Wich will update the file contents as follows:

# typed: strict

class Foo
  #: String
  attr_reader :x

  #: (String x) -> void
  def initialize(x)
    @x = x
  end
end

Signed-off-by: Alexandre Terrasa <[email protected]>
@Morriar Morriar added the feature New feature label Oct 3, 2024
@Morriar Morriar self-assigned this Oct 3, 2024
@Morriar Morriar requested a review from a team as a code owner October 3, 2024 19:08

LINE_BREAK = T.let(0x0A, Integer)

# After character 0xFFFF, UTF-16 considers characters to have length 2 and we have to account for that
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this complexity wrt to UTF-16 codepoints was only about the fact that VS Code only considers UTF-16 but our code is in UTF-8.

I am not sure if we need the same complexity here. We should be able to treat it as a string of codepoints (or even bytes).

sigs = translator.sigs.sort_by { |sig, _rbs_string| -T.must(sig.loc&.begin_line) }

sigs.each do |sig, rbs_string|
scanner = Scanner.new(ruby_contents, Encoding::UTF_8)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you know that the source is UTF-8?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be:

Suggested change
scanner = Scanner.new(ruby_contents, Encoding::UTF_8)
scanner = Scanner.new(ruby_contents, ruby_contents.encoding)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants