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

NetFabric / NetFabric.CodeAnalysis / 6228495305

18 Sep 2023 10:04PM UTC coverage: 82.476% (-0.7%) from 83.158%
6228495305

Pull #28

github

aalmada
Add IsIndexable
Pull Request #28: Add IsIndexable

225 of 266 branches covered (0.0%)

Branch coverage included in aggregate %.

110 of 110 new or added lines in 11 files covered. (100.0%)

641 of 784 relevant lines covered (81.76%)

17.66 hits per line

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

92.0
/NetFabric.CodeAnalysis/ITypeSymbolExtensions.IsIndexable.cs
1
using Microsoft.CodeAnalysis;
2
using System.Diagnostics.CodeAnalysis;
3

4
namespace NetFabric.CodeAnalysis;
5

6
public static partial class ITypeSymbolExtensions
7
{
8
    /// <summary>
9
    /// Checks if the provided <paramref name="typeSymbol"/> is indexable.
10
    /// </summary>
11
    /// <param name="typeSymbol">The <see cref="ITypeSymbol"/> to be checked for indexability.</param>
12
    /// <returns>
13
    /// <c>true</c> if the <paramref name="typeSymbol"/> is indexable; otherwise, <c>false</c>.
14
    /// </returns>
15
    /// <remarks>
16
    /// This method examines the provided <paramref name="typeSymbol"/> to determine if it can be
17
    /// indexed. To be considered indexable, the type must have an indexer with a single dimension
18
    /// of type <see cref="int"/> and a readable property named <c>Count</c> or <c>Length</c> of
19
    /// type <see cref="int"/>.
20
    /// </remarks>
21
    public static bool IsIndexable(this ITypeSymbol typeSymbol)
22
        => IsIndexable(typeSymbol, out _, out _);
×
23

24
    /// <summary>
25
    /// Checks if the provided <paramref name="typeSymbol"/> is indexable, and if so, retrieves
26
    /// information about the indexable symbols.
27
    /// </summary>
28
    /// <param name="typeSymbol">The <see cref="ITypeSymbol"/> to be checked for indexability.</param>
29
    /// <param name="indexableSymbols">
30
    /// When the method returns <c>true</c>, this parameter will contain information about the
31
    /// indexable symbols associated with the <paramref name="typeSymbol"/>. If the method returns
32
    /// <c>false</c>, this parameter will be <c>null</c>.
33
    /// </param>
34
    /// <returns>
35
    /// <c>true</c> if the <paramref name="typeSymbol"/> is indexable; otherwise, <c>false</c>.
36
    /// </returns>
37
    /// <remarks>
38
    /// This method examines the provided <paramref name="typeSymbol"/> to determine if it can be
39
    /// indexed. To be considered indexable, the type must have an indexer with a single dimension
40
    /// of type <see cref="int"/> and a readable property named <c>Count</c> or <c>Length</c> of
41
    /// type <see cref="int"/>.
42
    /// 
43
    /// If the type is indexable, the method provides information about the indexable symbols
44
    /// associated with it, which can be useful for various code analysis tasks.
45
    /// </remarks>
46
    public static bool IsIndexable(this ITypeSymbol typeSymbol,
47
        [NotNullWhen(true)] out IndexableSymbols? indexableSymbols)
48
        => IsIndexable(typeSymbol, out indexableSymbols, out _);
×
49

50
    /// <summary>
51
    /// Checks if the provided <paramref name="typeSymbol"/> is indexable, and if so, retrieves
52
    /// information about the indexable symbols and any potential error.
53
    /// </summary>
54
    /// <param name="typeSymbol">The <see cref="ITypeSymbol"/> to be checked for indexability.</param>
55
    /// <param name="indexableSymbols">
56
    /// When the method returns <c>true</c>, this parameter will contain information about the
57
    /// indexable symbols associated with the <paramref name="typeSymbol"/>. If the method returns
58
    /// <c>false</c>, this parameter will be <c>null</c>.
59
    /// </param>
60
    /// <param name="error">
61
    /// When the method returns <c>false</c>, this parameter will contain information about any
62
    /// error encountered while determining indexability. If the method returns <c>true</c>, this
63
    /// parameter will be <see cref="IsIndexableError.None"/>.
64
    /// </param>
65
    /// <returns>
66
    /// <c>true</c> if the <paramref name="typeSymbol"/> is indexable; otherwise, <c>false</c>.
67
    /// </returns>
68
    /// <remarks>
69
    /// This method examines the provided <paramref name="typeSymbol"/> to determine if it can be
70
    /// indexed. To be considered indexable, the type must have an indexer with a single dimension
71
    /// of type <see cref="int"/> and a readable property named <c>Count</c> or <c>Length</c> of
72
    /// type <see cref="int"/>.
73
    /// 
74
    /// If the type is indexable, the method provides information about the indexable symbols
75
    /// associated with it, which can be useful for various code analysis tasks.
76
    /// </remarks>
77
    public static bool IsIndexable(this ITypeSymbol typeSymbol,
78
    [NotNullWhen(true)] out IndexableSymbols? indexableSymbols,
79
        out IsIndexableError error)
80
    {
81
        var indexer = typeSymbol.GetPublicReadIndexer(typeof(int));
4✔
82
        if (indexer is null)
4✔
83
        {
84
            indexableSymbols = default;
1✔
85
            error = IsIndexableError.MissingIndexer;
1✔
86
            return false;
1✔
87
        }
88

89
        var countOrLength = typeSymbol.GetPublicReadProperty(NameOf.Count);
3✔
90
        countOrLength ??= typeSymbol.GetPublicReadProperty(NameOf.Length);
3✔
91
        if (countOrLength is null)
3✔
92
        {
93
            indexableSymbols = default;
1✔
94
            error = IsIndexableError.MissingCountOrLength;
1✔
95
            return false;
1✔
96
        }
97

98
        indexableSymbols = new IndexableSymbols(
2✔
99
            indexer,
2✔
100
            countOrLength
2✔
101
        );
2✔
102
        error = IsIndexableError.None;
2✔
103
        return true;
2✔
104
    }
105
}
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