Help us shape the future of Go! Take the 2026 Go Developer Survey

rubyresolve

package
v0.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package rubyresolve implements the Ruby language in-process type resolver. It implements the typresolve.Resolver interface, resolving Ruby method calls and constant references using tree-sitter AST walking and a shared type registry built from extracted definitions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildRegistry

func BuildRegistry(defs []typresolve.ResolverDef) *typresolve.Registry

BuildRegistry builds a typresolve.Registry from ResolverDef entries. Registers classes, modules, methods (instance and singleton), and constants. Processes include/extend/prepend edges to populate EmbeddedTypes for MRO-based attribute lookup.

func BuildRequireMap

func BuildRequireMap(root *sitter.Node, content []byte, currentFile string) map[string]string

BuildRequireMap builds a per-file require map from the Ruby AST root. It walks top-level call nodes for require/require_relative, extracts string arguments, and resolves paths. Returns map[localBinding]modulePath.

func EvalBuiltinCall

func EvalBuiltinCall(name string, args *sitter.Node, content []byte, ctx interface{}) *typresolve.Type

EvalBuiltinCall evaluates return types of common Ruby builtin methods. The ctx parameter is unused for now but allows future extension.

func EvalExprType

func EvalExprType(ctx *ResolveContext, node *sitter.Node) *typresolve.Type

EvalExprType evaluates the type of a Ruby expression AST node using scope lookup, registry lookup, constant resolution, and method dispatch.

func IsBuiltinFunc

func IsBuiltinFunc(name string) bool

IsBuiltinFunc returns true if the given name is a Ruby builtin method/function.

func IsBuiltinType

func IsBuiltinType(name string) bool

IsBuiltinType returns true if the given name is a Ruby builtin class/module.

func LookupAttrAccessor

func LookupAttrAccessor(reg *typresolve.Registry, typeQN string, memberName string) *typresolve.RegisteredFunc

LookupAttrAccessor checks if a member name corresponds to an attr_reader/attr_writer/attr_accessor-generated method on the type. These are stored as methods with synthetic signatures in the registry.

func LookupAttribute

func LookupAttribute(reg *typresolve.Registry, typeQN string, memberName string) *typresolve.RegisteredFunc

LookupAttribute looks up a method or attribute on a named type, following the Ruby ancestor chain (include/extend/prepend stored as EmbeddedTypes) up to maxMRODepth levels deep. Returns the registered function if found.

func ParseScopeResolution

func ParseScopeResolution(node *sitter.Node, content []byte) string

ParseScopeResolution parses a scope_resolution tree-sitter node into its fully qualified string representation. It handles nested scope resolution (A::B::C) by walking the tree recursively.

Tree-sitter Ruby represents A::B::C as nested scope_resolution:

(scope_resolution
  scope: (scope_resolution
    scope: (constant) "A"
    name: (constant) "B")
  name: (constant) "C")

func ProcessStatement

func ProcessStatement(ctx *ResolveContext, node *sitter.Node)

ProcessStatement processes a Ruby statement node to bind variables into the current scope. Handles assignment (=), operator assignment, multiple assignment, for-in, and rescue clauses with exception variable binding.

func ResolveBuiltinType

func ResolveBuiltinType(name string) *typresolve.Type

ResolveBuiltinType returns a typresolve.Named type if the name is a Ruby builtin class, nil otherwise. Uses "Ruby::" prefix for builtin qualified names.

func ResolveCallsInFile

func ResolveCallsInFile(ctx *ResolveContext, root *sitter.Node, fileHash types.Hash, repoURL string, filePath string) []types.Edge

ResolveCallsInFile walks a Ruby file's AST resolving call expressions and emitting edges. Uses the shared registry, per-file scope chain, require map, and constant resolution.

func ResolveConstant

func ResolveConstant(constPath string, nesting []string) string

ResolveConstant resolves a Ruby constant path relative to the current nesting context. Ruby constant lookup walks outward from the current nesting.

Rules:

  • If constPath starts with "::", it is absolute: return constPath[2:].
  • Otherwise, return the fully qualified name by joining the full nesting with the constant path. This is the innermost candidate; callers that need the outward walk should use ResolveConstantInRegistry instead.

func ResolveConstantInRegistry

func ResolveConstantInRegistry(reg *typresolve.Registry, constPath string, nesting []string) string

ResolveConstantInRegistry resolves a Ruby constant using lexical scoping: try A::B::C::X, then A::B::X, then A::X, then X. Returns the first qualified name that exists in the registry. Falls back to the innermost candidate (full nesting + constPath) if nothing is found.

func ResolveNew

func ResolveNew(reg *typresolve.Registry, typeQN string) *typresolve.RegisteredFunc

ResolveNew resolves ClassName.new to the initialize method. When ClassName.new is called, Ruby dispatches to initialize.

func ResolveRequire

func ResolveRequire(requirePath string, currentFile string, isRelative bool) (string, bool)

ResolveRequire resolves a require or require_relative path to a module-level qualified name. For require_relative, the path is resolved relative to the directory of currentFile. For require, the path is returned as-is (gem paths are already qualified). The ".rb" extension is stripped if present. Returns (resolvedModulePath, true) on success, ("", false) on failure.

Types

type ResolveContext

type ResolveContext struct {
	Registry        *typresolve.Registry
	Scope           *typresolve.Scope
	Requires        map[string]string // local binding -> module path
	Nesting         []string          // current module/class nesting stack
	CurrentFile     string            // current file path
	Content         []byte            // source file content
	EnclosingFuncQN string            // QN of the current method being resolved
}

ResolveContext holds per-file state for type resolution.

type RubyResolver

type RubyResolver struct {
	// contains filtered or unexported fields
}

RubyResolver implements typresolve.Resolver for Ruby. It uses tree-sitter AST walking and a shared type registry to resolve call expressions and constant references without requiring an external LSP server.

func NewRubyResolver

func NewRubyResolver() *RubyResolver

NewRubyResolver creates a new RubyResolver.

func (*RubyResolver) InitWorkspace

func (r *RubyResolver) InitWorkspace(ctx context.Context, defs []typresolve.ResolverDef) error

InitWorkspace builds the cross-file type registry from extracted definitions. Called once before any ResolveFile calls.

func (*RubyResolver) Language

func (r *RubyResolver) Language() string

Language returns "ruby".

func (*RubyResolver) ResolveFile

func (r *RubyResolver) ResolveFile(ctx context.Context, opts typresolve.ResolveFileOpts) ([]types.Edge, error)

ResolveFile resolves type references in a single Ruby file. Thread-safe after InitWorkspace completes (registry is read-only, all mutable state is stack-local).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL