Repository navigation
Conversation
Following Steep, preserve the lexical nesting through `class << self` and rebind only the method kind via a `@singleton_class_depth` flag (reset on entry to a nested class/module body). `def x` becomes a singleton method of the enclosing class; `class Inner` / `FOO = 1` flatten via lexical resolution. Constructs with no RBS representation (`NestedSingletonScope`, `TopLevelSingletonScope`, `NonSelfSingletonScope`) are diagnosed. Mixin / attr stay `NotImplementedYet`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Instead of rewriting `def` inside `class << self` into a singleton DefMember at parse time, keep the source structure in the AST: `SingletonClassDecl` is a member of the enclosing class/module and holds its `def`s as written. MethodBuilder and `rbs validate` interpret them as singleton methods of the enclosing class. Classes, modules, and constants inside `class << self` belong to the singleton class at runtime, so they are now reported with `ConstantInSingletonScope` instead of being flattened into the enclosing namespace. 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.
Summary
Add
class << selfsupport to the inline parser by introducing an AST node for the singleton class body.Approach
class << selfin a class or module body becomesAST::Ruby::Declarations::SingletonClassDecl, a member of the enclosingClassDecl/ModuleDecl. Thedefs inside are kept as written (instance methods of the singleton class), and the consumers interpret them:MethodBuilder#build_singletonbuilds them as singleton methods of the enclosing classrbs validatevalidates their method typesEnvironmentresolves type names in them, and registers no constant for the singleton class itselfThe parser pushes
SingletonClassDeclontomodule_nesting, socurrent_modulealone tells whether the current scope is a singleton class.Diagnostics
RBS has no notation for the singleton class of a singleton class, an anonymous singleton class, or constants on a singleton class. These are reported:
NestedSingletonScope: nestedclass << self, anddef self.xinsideclass << selfTopLevelSingletonScope:class << selfat top level (the singleton class ofmain)NonSelfSingletonScope:class << exprfor non-selfexprConstantInSingletonScope:class,module, and constant assignments insideclass << self. At runtime they are defined on the singleton class (Foo::InnerraisesNameError; it isFoo.singleton_class::Inner), so they are not flattened into the enclosing namespace.Annotations inside a rejected construct are also reported as
UnusedInlineAnnotation, because they are not read.Out of scope
Mixin (
include/extend/prepend) and attribute (attr_*) calls insideclass << selfremainNotImplementedYet. They can be supported later by adding the members toSingletonClassDecland handling them in the builders.🤖 Generated with Claude Code