• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Sholtee / proxygen / 1048

30 Apr 2025 05:58AM UTC coverage: 92.635% (-0.07%) from 92.705%
1048

push

appveyor

Sholtee
visibility check is supposed to be done during initial resolution

4792 of 5173 relevant lines covered (92.63%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.15
/SRC/Private/SyntaxFactories/ClassSyntaxFactoryBase.Property.cs
1
/********************************************************************************
2
* ClassSyntaxFactoryBase.Property.cs                                            *
3
*                                                                               *
4
* Author: Denes Solti                                                           *
5
********************************************************************************/
6
using System;
7
using System.Collections.Generic;
8
using System.Diagnostics;
9
using System.Linq;
10

11
using Microsoft.CodeAnalysis.CSharp;
12
using Microsoft.CodeAnalysis.CSharp.Syntax;
13

14
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
15

16
namespace Solti.Utils.Proxy.Internals
17
{
18
    internal partial class ClassSyntaxFactoryBase
19
    {
20
        /// <summary>
21
        /// <code>
22
        /// target.Property                           
23
        /// // OR                                         
24
        /// target.Property[index]
25
        /// </code>
26
        /// </summary>
27
        #if DEBUG
28
        internal
29
        #endif
30
        protected ExpressionSyntax PropertyAccess(IPropertyInfo property, ExpressionSyntax? target, ITypeInfo? castTargetTo = null) => PropertyAccess
1✔
31
        (
1✔
32
            property, 
1✔
33
            target, 
1✔
34
            castTargetTo, 
1✔
35
            property.Indices.Select(static param => Argument(IdentifierName(param.Name)))
1✔
36
        );
1✔
37

38
        /// <summary>
39
        /// <code>
40
        /// target.Property         
41
        /// // OR        
42
        /// target.Property[index]
43
        /// </code>
44
        /// </summary>
45
        #if DEBUG
46
        internal
47
        #endif
48
        protected ExpressionSyntax PropertyAccess(IPropertyInfo property, ExpressionSyntax? target, ITypeInfo? castTargetTo = null, IEnumerable<ArgumentSyntax>? indices = null) => !property.Indices.Any()
1✔
49
            ? MemberAccess
1✔
50
            (
1✔
51
                target,
1✔
52
                property,
1✔
53
                castTargetTo
1✔
54
            )
1✔
55
            : (ExpressionSyntax) ElementAccessExpression
1✔
56
            (
1✔
57
                AmendTarget(target, property, castTargetTo),
1✔
58
                BracketedArgumentList
1✔
59
                (
1✔
60
                    arguments: indices!.ToSyntaxList()
1✔
61
                )
1✔
62
            );
1✔
63

64
        private TDeclaration ResolveProperty<TDeclaration>(IPropertyInfo property, Func<IPropertyInfo, TDeclaration> fact, CSharpSyntaxNode? getBody, CSharpSyntaxNode? setBody, bool allowProtected) where TDeclaration : BasePropertyDeclarationSyntax
65
        {
1✔
66
            //
67
            // Starting from .NET 5.0 interface members may have visibility.
68
            //
69

70
            Visibility.Check(property, ContainingAssembly, allowProtected);
1✔
71

72
            TDeclaration result = fact(property);
1✔
73

74
            IMethodInfo backingMethodHavingHigherVisibility = (property.GetMethod?.AccessModifiers ?? AccessModifiers.Unknown) > (property.SetMethod?.AccessModifiers ?? AccessModifiers.Unknown)
×
75
                ? property.GetMethod!
×
76
                : property.SetMethod!;
×
77

78
            List<AccessorDeclarationSyntax> accessors = new(2);
1✔
79

80
            if (property.DeclaringType.Flags.HasFlag(TypeInfoFlags.IsInterface))
1✔
81
            {
1✔
82
                CheckNotStaticAbstract(property);
1✔
83

84
                if (property.GetMethod is not null) accessors.Add
1✔
85
                (
1✔
86
                    ResolveAccessor(SyntaxKind.GetAccessorDeclaration, getBody)
1✔
87
                );
1✔
88

89
                if (property.SetMethod is not null) accessors.Add
1✔
90
                (
1✔
91
                    ResolveAccessor(SyntaxKind.SetAccessorDeclaration, setBody)
1✔
92
                );
1✔
93

94
                result = (TDeclaration) result.WithExplicitInterfaceSpecifier
1✔
95
                (
1✔
96
                    explicitInterfaceSpecifier: ExplicitInterfaceSpecifier
1✔
97
                    (
1✔
98
                        (NameSyntax)ResolveType(property.DeclaringType)
1✔
99
                    )
1✔
100
                );
1✔
101
            }
1✔
102
            else
103
            {
1✔
104
                List<SyntaxKind> tokens = [..ResolveAccessModifiers(backingMethodHavingHigherVisibility)];
1✔
105

106
                IMemberInfo underlyingMethod = property.GetMethod ?? property.SetMethod!;
×
107

108
                tokens.Add(underlyingMethod.IsVirtual || underlyingMethod.IsAbstract ? SyntaxKind.OverrideKeyword : SyntaxKind.NewKeyword);
×
109

110
                if (property.GetMethod is not null) accessors.Add
1✔
111
                (
1✔
112
                    ResolveAccessor(SyntaxKind.GetAccessorDeclaration, getBody, property.GetMethod)
1✔
113
                );
1✔
114

115
                if (property.SetMethod is not null) accessors.Add
1✔
116
                (
1✔
117
                    ResolveAccessor(SyntaxKind.SetAccessorDeclaration, setBody, property.SetMethod)
1✔
118
                );
1✔
119

120
                result = (TDeclaration) result.WithModifiers
×
121
                (
×
122
                    TokenList
×
123
                    (
×
124
                        tokens.Select(Token)
×
125
                    )
×
126
                );
×
127

128
                AccessorDeclarationSyntax ResolveAccessor(SyntaxKind kind, CSharpSyntaxNode? body, IMethodInfo backingMethod) => ClassSyntaxFactoryBase.ResolveAccessor
1✔
129
                (
1✔
130
                    kind,
1✔
131
                    body,
1✔
132

1✔
133
                    //
1✔
134
                    // Accessor cannot have higher visibility than the property's
1✔
135
                    //
1✔
136

1✔
137
                    backingMethod!.AccessModifiers < backingMethodHavingHigherVisibility.AccessModifiers
1✔
138
                        ? ResolveAccessModifiers(backingMethod)
1✔
139
                        : []
1✔
140
                );
1✔
141
            }
1✔
142
  
143
            return (TDeclaration) result.WithAccessorList
1✔
144
            (
1✔
145
                accessorList: AccessorList
1✔
146
                (
1✔
147
                    accessors: List(accessors)
1✔
148
                )
1✔
149
            );
1✔
150
        }
1✔
151

152
        /// <summary>
153
        /// <code>
154
        /// int IInterface[T].Prop
155
        /// {                      
156
        ///   get{...}            
157
        ///   set{...}           
158
        /// }                    
159
        /// </code>
160
        /// or
161
        /// <code>
162
        /// public override int Prop
163
        /// {                      
164
        ///   get{...}            
165
        ///   protected set{...}           
166
        /// }                    
167
        /// </code>
168
        /// </summary>
169
        #if DEBUG
170
        internal
171
        #endif
172
        protected PropertyDeclarationSyntax ResolveProperty(IPropertyInfo property, CSharpSyntaxNode? getBody, CSharpSyntaxNode? setBody, bool allowProtected = false) => ResolveProperty
1✔
173
        (
1✔
174
            property,
1✔
175
            property => PropertyDeclaration
1✔
176
            (
1✔
177
                type: ResolveType(property.Type),
1✔
178
                identifier: Identifier(property.Name)
1✔
179
            ),
1✔
180
            getBody,
1✔
181
            setBody,
1✔
182
            allowProtected
1✔
183
        );
1✔
184

185
        /// <summary>
186
        /// <code>
187
        /// int IInterface.this[string index, ...]
188
        /// {                                     
189
        ///   get{...}                            
190
        ///   set{...}                           
191
        /// }            
192
        /// </code>
193
        /// or
194
        /// <code>
195
        /// public override int this[string index, ...]
196
        /// {                                     
197
        ///   get{...}                            
198
        ///   protected set{...}                           
199
        /// }            
200
        /// </code>
201
        /// </summary>
202
        #if DEBUG
203
        internal
204
        #endif
205
        protected IndexerDeclarationSyntax ResolveIndexer(IPropertyInfo property, CSharpSyntaxNode? getBody, CSharpSyntaxNode? setBody, bool allowProtected = false) => ResolveProperty
1✔
206
        (
1✔
207
            property,
1✔
208
            property => 
1✔
209
                IndexerDeclaration
1✔
210
                (
1✔
211
                    type: ResolveType(property.Type)
1✔
212
                )
1✔
213
                .WithParameterList
1✔
214
                (
1✔
215
                    parameterList: BracketedParameterList
1✔
216
                    (
1✔
217
                        parameters: property.Indices.ToSyntaxList
1✔
218
                        (
1✔
219
                            index => Parameter
1✔
220
                            (
1✔
221
                                identifier: Identifier(index.Name)
1✔
222
                            )
1✔
223
                            .WithType
1✔
224
                            (
1✔
225
                                type: ResolveType(index.Type)
1✔
226
                            )
1✔
227
                        )
1✔
228
                    )
1✔
229
                ),
1✔
230
            getBody,
1✔
231
            setBody,
1✔
232
            allowProtected
1✔
233
        );
1✔
234

235
        #if DEBUG
236
        internal
237
        #endif
238
        protected virtual ClassDeclarationSyntax ResolveProperties(ClassDeclarationSyntax cls, object context) => cls;
1✔
239

240
        #if DEBUG
241
        internal
242
        #endif
243
        protected virtual ClassDeclarationSyntax ResolveProperty(ClassDeclarationSyntax cls, object context, IPropertyInfo property) => throw new NotImplementedException();
×
244
    }
245
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc