@@ -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 , "::" ) ;
@@ -5151,4 +5158,41 @@ mod tests {
51515158 assert_declaration_kind_eq ! ( context, "Foo::Bar" , "Module" ) ;
51525159 assert_declaration_kind_eq ! ( context, "Foo::Bar::Baz" , "Constant" ) ;
51535160 }
5161+
5162+ #[ test]
5163+ fn singleton_class_block_for_promotable_constant ( ) {
5164+ let mut context = GraphTest :: new ( ) ;
5165+ context. index_uri ( "file:///a.rb" , {
5166+ r"
5167+ Foo = dynamic
5168+
5169+ class << Foo
5170+ def bar; end
5171+ end
5172+ "
5173+ } ) ;
5174+
5175+ context. resolve ( ) ;
5176+ assert_declaration_kind_eq ! ( context, "Foo" , "Module" ) ;
5177+ assert_declaration_exists ! ( context, "Foo::<Foo>#bar()" ) ;
5178+ }
5179+
5180+ #[ test]
5181+ fn singleton_class_block_for_non_promotable_constant ( ) {
5182+ let mut context = GraphTest :: new ( ) ;
5183+ context. index_uri ( "file:///a.rb" , {
5184+ r"
5185+ Foo = 1
5186+
5187+ class << Foo
5188+ def bar; end
5189+ end
5190+ "
5191+ } ) ;
5192+
5193+ context. resolve ( ) ;
5194+ assert_declaration_kind_eq ! ( context, "Foo" , "Constant" ) ;
5195+ assert_declaration_does_not_exist ! ( context, "Foo::<Foo>" ) ;
5196+ assert_declaration_does_not_exist ! ( context, "Foo::<Foo>#bar()" ) ;
5197+ }
51545198}
0 commit comments