Module: Rbs::Merge::Backends::RbsBackend

Defined in:
lib/rbs/merge/backends/rbs_backend.rb

Overview

Note:

This backend only works on MRI Ruby (the RBS gem has C extensions)

Note:

This backend only parses RBS type signature files

RBS gem backend using Ruby’s official RBS parser

This backend wraps the RBS gem, Ruby’s official type signature parser.
Unlike tree-sitter backends which are language-agnostic runtime parsers,
the RBS gem is specifically designed for parsing RBS type signature files.

The RBS gem provides:

  • Rich AST with declaration types (Class, Module, Interface, TypeAlias, etc.)
  • Member types (MethodDefinition, Alias, AttrReader, etc.)
  • Location information for all nodes
  • Full RBS language support including generics, type aliases, etc.

Examples:

Basic usage via TreeHaver

TreeHaver.register_language(:rbs,
  backend_module: Rbs::Merge::Backends::RbsBackend,
  backend_type: :rbs,
  gem_name: "rbs")
parser = TreeHaver.parser_for(:rbs)
tree = parser.parse(rbs_source)
root = tree.root_node

Direct usage

parser = Rbs::Merge::Backends::RbsBackend::Parser.new
parser.language = Rbs::Merge::Backends::RbsBackend::Language.rbs
tree = parser.parse(rbs_source)

See Also:

Defined Under Namespace

Classes: Language, Node, Parser, Tree

Class Method Summary collapse

Class Method Details

.available?Boolean

Check if the RBS backend is available

Attempts to require rbs on first call and caches the result.
The RBS gem only works on MRI Ruby (C extension).

Examples:

if Rbs::Merge::Backends::RbsBackend.available?
  puts "RBS backend is ready"
end

Returns:

  • (Boolean)

    true if rbs gem is available



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 50

def available?
  return @loaded if @load_attempted # rubocop:disable ThreadSafety/ClassInstanceVariable
  @load_attempted = true # rubocop:disable ThreadSafety/ClassInstanceVariable
  begin
    require "rbs"
    # Verify it can actually parse - just requiring isn't enough
    buffer = ::RBS::Buffer.new(name: "test.rbs", content: "class Foo end")
    ::RBS::Parser.parse_signature(buffer)
    @loaded = true # rubocop:disable ThreadSafety/ClassInstanceVariable
  rescue LoadError
    @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
  rescue StandardError
    @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
  end
  @loaded # rubocop:disable ThreadSafety/ClassInstanceVariable
end

.capabilitiesHash{Symbol => Object}

Get capabilities supported by this backend

Examples:

Rbs::Merge::Backends::RbsBackend.capabilities
# => { backend: :rbs, query: false, rbs_only: true, ... }

Returns:

  • (Hash{Symbol => Object})

    capability map



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 82

def capabilities
  return {} unless available?
  {
    backend: :rbs,
    query: false,           # RBS doesn't have tree-sitter-style queries
    bytes_field: true,      # RBS provides byte offsets via Location
    incremental: false,     # RBS doesn't support incremental parsing
    pure_ruby: false,       # RBS gem has native C extension
    rbs_only: true,         # RBS gem only parses RBS type signatures
    error_tolerant: false,  # RBS parser raises on errors
    mri_only: true,         # RBS gem C extension only works on MRI
  }
end

.reset!void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Reset the load state (primarily for testing)



71
72
73
74
# File 'lib/rbs/merge/backends/rbs_backend.rb', line 71

def reset!
  @load_attempted = false # rubocop:disable ThreadSafety/ClassInstanceVariable
  @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
end