< Summary

Information
Class: Hertzole.ScriptableValues.Generator.SyntaxExtensions
Assembly: Hertzole.ScriptableValues.Generator
File(s): Generator/Hertzole.ScriptableValues.Generator/Extensions/SyntaxExtensions.cs
Tag: 52_28197466029
Line coverage
29%
Covered lines: 9
Uncovered lines: 22
Coverable lines: 31
Total lines: 83
Line coverage: 29%
Branch coverage
25%
Covered branches: 5
Total branches: 20
Branch coverage: 25%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
TryGetFieldDeclaration(...)83.33%6681.81%
TryGetFieldOrPropertyWrapper(...)0%10100%
GetAttributeSymbol(...)0%440%

File(s)

Generator/Hertzole.ScriptableValues.Generator/Extensions/SyntaxExtensions.cs

#LineLine coverage
 1using System.Collections.Immutable;
 2using System.Threading;
 3using Microsoft.CodeAnalysis;
 4using Microsoft.CodeAnalysis.CSharp;
 5using Microsoft.CodeAnalysis.CSharp.Syntax;
 6
 7namespace Hertzole.ScriptableValues.Generator;
 8
 9internal static class SyntaxExtensions
 10{
 11    public static bool TryGetFieldDeclaration(this SyntaxNode node, out FieldDeclarationSyntax? fieldDeclaration)
 12    {
 2713        if (node.IsKind(SyntaxKind.FieldDeclaration))
 14        {
 015            fieldDeclaration = (FieldDeclarationSyntax) node;
 016            return true;
 17        }
 18
 2719        SyntaxNode? parent = node.Parent;
 7220        while (parent != null)
 21        {
 6022            if (parent.IsKind(SyntaxKind.FieldDeclaration))
 23            {
 1524                fieldDeclaration = (FieldDeclarationSyntax) parent;
 1525                return true;
 26            }
 27
 4528            parent = parent.Parent;
 29        }
 30
 1231        fieldDeclaration = null;
 1232        return false;
 33    }
 34
 35    public static bool TryGetFieldOrPropertyWrapper(this SyntaxNode node, in SemanticModel semanticModel, out ImmutableA
 36    {
 037        using ArrayBuilder<FieldOrPropertyWrapper> builder = new ArrayBuilder<FieldOrPropertyWrapper>();
 38
 039        if (node is FieldDeclarationSyntax fieldSyntax)
 40        {
 041            foreach (VariableDeclaratorSyntax variable in fieldSyntax.Declaration.Variables)
 42            {
 043                ISymbol? declareSymbol = semanticModel.GetDeclaredSymbol(variable);
 044                if (declareSymbol is not IFieldSymbol fieldSymbol)
 45                {
 46                    continue;
 47                }
 48
 049                builder.Add(new FieldOrPropertyWrapper(fieldSymbol));
 50            }
 51        }
 052        else if (node is PropertyDeclarationSyntax propertySyntax)
 53        {
 054            IPropertySymbol? symbol = semanticModel.GetDeclaredSymbol(propertySyntax);
 055            if (symbol is not IPropertySymbol propertySymbol)
 56            {
 057                return false;
 58            }
 59
 060            builder.Add(new FieldOrPropertyWrapper(propertySymbol));
 61        }
 62
 063        wrapper = builder.ToImmutable();
 064        return wrapper.Length > 0;
 065    }
 66
 67    public static INamedTypeSymbol? GetAttributeSymbol(this AttributeSyntax syntax, SemanticModel semanticModel, Cancell
 68    {
 069        if (semanticModel.GetSymbolInfo(syntax).Symbol is not IMethodSymbol methodSymbol)
 70        {
 071            return null;
 72        }
 73
 074        cancellation.ThrowIfCancellationRequested();
 75
 076        if (methodSymbol.ContainingType is INamedTypeSymbol attributeSymbol)
 77        {
 078            return attributeSymbol;
 79        }
 80
 081        return null;
 82    }
 83}