Skip to content

Commit aecab13

Browse files
committed
Avoid creating singleton classes for non-namespace targets
1 parent 14702a0 commit aecab13

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

rust/rubydex/src/resolution.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,13 @@ impl<'a> Resolver<'a> {
10111011
let owner = self.graph.declarations().get(&owner_id).unwrap();
10121012
let owner_is_namespace = owner.as_namespace().is_some();
10131013

1014+
// Skip creating singletons when the target is a not a namespace or not promotable. For example:
1015+
// Foo = 1
1016+
// class << Foo; end
1017+
if singleton && !owner_is_namespace {
1018+
return Outcome::Unresolved(None);
1019+
}
1020+
10141021
// We don't prefix declarations with `Object::`
10151022
if owner_id != *OBJECT_ID {
10161023
fully_qualified_name.insert_str(0, "::");
@@ -5217,4 +5224,41 @@ mod tests {
52175224
assert_declaration_kind_eq!(context, "Foo::Bar", "Module");
52185225
assert_declaration_kind_eq!(context, "Foo::Bar::Baz", "Constant");
52195226
}
5227+
5228+
#[test]
5229+
fn singleton_class_block_for_promotable_constant() {
5230+
let mut context = GraphTest::new();
5231+
context.index_uri("file:///a.rb", {
5232+
r"
5233+
Foo = dynamic
5234+
5235+
class << Foo
5236+
def bar; end
5237+
end
5238+
"
5239+
});
5240+
5241+
context.resolve();
5242+
assert_declaration_kind_eq!(context, "Foo", "Module");
5243+
assert_declaration_exists!(context, "Foo::<Foo>#bar()");
5244+
}
5245+
5246+
#[test]
5247+
fn singleton_class_block_for_non_promotable_constant() {
5248+
let mut context = GraphTest::new();
5249+
context.index_uri("file:///a.rb", {
5250+
r"
5251+
Foo = 1
5252+
5253+
class << Foo
5254+
def bar; end
5255+
end
5256+
"
5257+
});
5258+
5259+
context.resolve();
5260+
assert_declaration_kind_eq!(context, "Foo", "Constant");
5261+
assert_declaration_does_not_exist!(context, "Foo::<Foo>");
5262+
assert_declaration_does_not_exist!(context, "Foo::<Foo>#bar()");
5263+
}
52205264
}

0 commit comments

Comments
 (0)