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

NetFabric / NetFabric.CodeAnalysis / 6253798736

20 Sep 2023 08:33PM UTC coverage: 75.653% (-7.5%) from 83.158%
6253798736

Pull #28

github

aalmada
Fixes
Pull Request #28: Add IsIndexable

292 of 404 branches covered (0.0%)

Branch coverage included in aggregate %.

259 of 259 new or added lines in 15 files covered. (100.0%)

693 of 898 relevant lines covered (77.17%)

17.09 hits per line

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

90.91
/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 countOrLength = typeSymbol.GetPublicReadProperty(NameOf.Count);
7✔
82
        countOrLength ??= typeSymbol.GetPublicReadProperty(NameOf.Length);
7✔
83
        if (countOrLength is null)
7✔
84
        {
85
            indexableSymbols = default;
1✔
86
            error = IsIndexableError.MissingCountOrLength;
1✔
87
            return false;
1✔
88
        }
89

90
        var indexer = typeSymbol.GetPublicReadIndexer(countOrLength.GetMethod.ReturnType);
6✔
91
        if (indexer is null)
6✔
92
        {
93
            indexableSymbols = default;
2✔
94
            error = IsIndexableError.MissingIndexer;
2✔
95
            return false;
2✔
96
        }
97

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