Repository navigation
Conversation
`rbs` commands only loaded RBS files, so `rbs validate` could not check inline RBS declarations written in Ruby files. This adds `--inline DIR` as a library option alongside `-I DIR`. It loads all `.rb` files under the directory (or the given file) through `InlineParser`, using the same file finding rules as `-I`. `rbs validate` now validates the inline declarations too, and reports the inline parser diagnostics as warnings. This also fixes the error message snippet for locations in sub-buffers, which inline annotations use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
rbs validateonly checks RBS files. With inline RBS declarations, part of the signatures now live in Ruby files, and therbscommand has no way to load them. So inline declarations are never validated, and each tool that consumes them (type checkers, etc.) has to validate them by itself.If
rbs validatecan check inline declarations, tools can rely on "the signatures passrbs validate" as a precondition, regardless of the language they are implemented in.What this PR does
Adds
--inline DIRas a library option, next to-I DIR.$ rbs -I sig --inline lib validate-I.-I: a directory loads all.rbfiles under it (recursively, sorted), and a file is loaded regardless of its extension.FileFinder.each_filegets anextension:keyword so that both options share the implementation.EnvironmentLoader#add(inline:)collects the paths, andEnvironmentLoader#loadparses the files withInlineParserand addsSource::Rubyto the environment.validate,method,methods,ancestors, ...).rbs pathsshows the paths aslib (dir, inline).parse,annotate,subtract,bin/sort) don't use the loader, and they are not affected.rbs validateCLI::Validateassumed RBS AST and crashed on Ruby declarations (Location#aref(:type_params),super_class.args,member.nameof mixins). It now handles both.Validator#validate_class_aliasuses the whole location for Ruby alias declarations, which don't have theold_namechild location.InlineParserdiagnostics are reported as warnings, and they don't make the command fail.Error message snippet fix
DetailedMessageable#detailed_messageprinted the source line fromlocation.buffer. Inline annotations are parsed in sub-buffers, andLocation#start_lineis in the top buffer's coordinates, so the printed line was empty (or wrong). It now useslocation.buffer.top_buffer. This only happens with inline declarations, so it is included here.Open questions
class Foo < Struct.new(...)givesNonConstantSuperClassName, and RDoc's#:nodoc:givesAnnotationSyntaxError. These patterns are common in real code: running the parser over some installed gems found 12Struct.newsuperclasses in steep and 3#:nodoc:comments in rake. With errors, such a project couldn't passrbs validatewithout rewriting unrelated code. But this means a broken annotation like# @rbs (is only a warning. ShouldAnnotationSyntaxError, or some others, be errors?--inlinematchesInlineParserand the "inline RBS" term in the docs. There is no short form.Not changed
attr_readertype is checked as both the method and the instance variable, and an error in an ancestor is reported for each class)..rbsfiles behave the same way.🤖 Generated with Claude Code