@@ -40,9 +40,12 @@ public ImmutableArray<string> Protocols {
4040 init => protocols = value ;
4141 }
4242
43+ public BaseTypeDeclarationSyntax ? BaseDeclarationSyntax { get ; init ; }
44+
4345 /// <summary>
4446 /// Internal constructor added for testing purposes.
4547 /// </summary>
48+ /// <param name="declarationSyntax">The syntax used to declare the binding.</param>
4649 /// <param name="name">The name of the named type that created the code change.</param>
4750 /// <param name="namespace">The namespace that contains the named type.</param>
4851 /// <param name="fullyQualifiedSymbol">The fully qualified name of the symbol.</param>
@@ -74,6 +77,7 @@ internal Binding (EnumDeclarationSyntax enumDeclaration, INamedTypeSymbol symbol
7477 outerClasses : out outerClasses ,
7578 namespaces : out namespaces ,
7679 symbolAvailability : out availability ) ;
80+ BaseDeclarationSyntax = enumDeclaration ;
7781 BindingBindingInfo = new BindingInfo ( null , BindingType . SmartEnum ) ;
7882 FullyQualifiedSymbol = enumDeclaration . GetFullyQualifiedIdentifier ( context . SemanticModel ) ;
7983 UsingDirectives = enumDeclaration . SyntaxTree . CollectUsingStatements ( ) ;
@@ -91,24 +95,55 @@ internal Binding (EnumDeclarationSyntax enumDeclaration, INamedTypeSymbol symbol
9195 continue ;
9296 }
9397 var fieldData = enumValueSymbol . GetFieldData ( ) ;
98+ EnumMember ? enumMember ;
9499 // try and compute the library for this enum member
95100 if ( fieldData is null || ! context . TryComputeLibraryName ( fieldData . Value . LibraryName , Namespace [ ^ 1 ] ,
96- out string ? libraryName , out string ? libraryPath ) )
97- // could not calculate the library for the enum, do not add it
98- continue ;
99- // we do know we have a backing field, so we can set the binding type to smart enum
100- bindingType = BindingType . SmartEnum ;
101- var enumMember = new EnumMember (
102- name : enumValueDeclaration . Identifier . ToFullString ( ) . Trim ( ) ,
103- libraryName : libraryName ,
104- libraryPath : libraryPath ,
105- fieldData : enumValueSymbol . GetFieldData ( ) ,
106- symbolAvailability : enumValueSymbol . GetAvailabilityForSymbol ( ) // no parent availability, just the symbol
107- ) ;
108- bucket . Add ( enumMember ) ;
101+ out string ? libraryName , out string ? libraryPath ) ) {
102+ // create a member for those enum values that do not have a backing field, we will
103+ // set the binding type to regular enum if we do not know it yet
104+ if ( bindingType == BindingType . Unknown ) {
105+ bindingType = BindingType . Enum ;
106+ }
107+ enumMember = new EnumMember (
108+ name : enumValueDeclaration . Identifier . ToFullString ( ) . Trim ( ) ,
109+ libraryName : string . Empty ,
110+ libraryPath : null ,
111+ fieldData : enumValueSymbol . GetFieldData ( ) ,
112+ symbolAvailability : enumValueSymbol
113+ . GetAvailabilityForSymbol ( ) // no parent availability, just the symbol
114+ ) {
115+ DeclarationSyntax = enumValueDeclaration ,
116+ IsSmartMember = false
117+ } ;
118+ } else {
119+ // we do know we have a backing field, so we can set the binding type to smart enum
120+ bindingType = BindingType . SmartEnum ;
121+ enumMember = new EnumMember (
122+ name : enumValueDeclaration . Identifier . ToFullString ( ) . Trim ( ) ,
123+ libraryName : libraryName ,
124+ libraryPath : libraryPath ,
125+ fieldData : enumValueSymbol . GetFieldData ( ) ,
126+ symbolAvailability : enumValueSymbol
127+ . GetAvailabilityForSymbol ( ) // no parent availability, just the symbol
128+ ) {
129+ DeclarationSyntax = enumValueDeclaration ,
130+ IsSmartMember = true ,
131+ } ;
132+ }
133+ bucket . Add ( enumMember . Value ) ;
109134 }
110135
136+ if ( bindingType == BindingType . Unknown ) {
137+ // we could not find any enum member with a backing field this means that we are dealing with a regular enum
138+ // we will set it so that we can convert the availability information
139+ bindingType = BindingType . Enum ;
140+ }
111141 BindingBindingInfo = new ( null , bindingType ) ;
142+ // based on the type of enum we might need to remove enums that are not valid, this are enums without
143+ // a backing field in the case of smart enums
144+ if ( BindingBindingInfo . BindingType == BindingType . SmartEnum ) {
145+ bucket . RemoveAll ( em => ! em . IsSmartMember ) ;
146+ }
112147 EnumMembers = bucket . ToImmutable ( ) ;
113148 }
114149
@@ -236,6 +271,7 @@ internal static bool Skip (MethodDeclarationSyntax methodDeclarationSyntax, Sema
236271 /// <param name="context">The current compilation context.</param>
237272 internal Binding ( InterfaceDeclarationSyntax interfaceDeclarationSyntax , INamedTypeSymbol symbol , in RootContext context )
238273 {
274+ BaseDeclarationSyntax = interfaceDeclarationSyntax ;
239275 // basic properties of the binding
240276 FullyQualifiedSymbol = interfaceDeclarationSyntax . GetFullyQualifiedIdentifier ( context . SemanticModel ) ;
241277 UsingDirectives = interfaceDeclarationSyntax . SyntaxTree . CollectUsingStatements ( ) ;
0 commit comments