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

NetFabric / NetFabric.CodeAnalysis / 6277926364

22 Sep 2023 06:38PM UTC coverage: 73.953% (-9.2%) from 83.158%
6277926364

push

github

web-flow
Add IsIndexable (#28)

323 of 452 branches covered (0.0%)

Branch coverage included in aggregate %.

349 of 349 new or added lines in 18 files covered. (100.0%)

648 of 861 relevant lines covered (75.26%)

20.93 hits per line

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

92.31
/NetFabric.CodeAnalysis/ITypeSymbolExtensions.IsIndexable.cs
1
using Microsoft.CodeAnalysis;
2
using System.Diagnostics.CodeAnalysis;
3
using System.Reflection;
4

5
namespace NetFabric.CodeAnalysis;
6

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

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

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

91
        var indexer = default(IPropertySymbol);
9✔
92
        if (typeSymbol.TypeKind != TypeKind.Array)
9✔
93
        {
94
            indexer = typeSymbol.GetPublicReadIndexer(countOrLength.GetMethod.ReturnType);
8✔
95
            if (indexer is null)
8✔
96
            {
97
                indexableSymbols = default;
2✔
98
                error = IsIndexableError.MissingIndexer;
2✔
99
                return false;
2✔
100
            }
101
        }
102

103
        indexableSymbols = new IndexableSymbols(indexer, countOrLength);
7✔
104
        error = IsIndexableError.None;
7✔
105
        return true;
7✔
106
    }
107
}
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

© 2025 Coveralls, Inc