Skip to content

Commit

Permalink
Improve MA0151 to support the Name and Type named arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Jan 6, 2024
1 parent 86c9051 commit 2807d2e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

Expand Down Expand Up @@ -46,19 +47,18 @@ private static void AnalyzeNamedType(SymbolAnalysisContext context, INamedTypeSy
if (!attribute.AttributeClass.IsEqualTo(attributeSymbol))
continue;

if (attribute.ConstructorArguments is [{ Kind: TypedConstantKind.Primitive, IsNull: false, Type.SpecialType: SpecialType.System_String, Value: string value }, ..])
{
var members = ParseMembers(value.AsSpan());
if (members is not null)
if (attribute.ConstructorArguments is [{ Kind: TypedConstantKind.Primitive, Value: string value }, ..])
{
foreach (var member in members)
{
if (!MemberExists(symbol, member))
{
context.ReportDiagnostic(Rule, attribute, member);
return;
}
}
ValidateValue(context, symbol, attribute, value);
}
}

foreach (var argument in attribute.NamedArguments)
{
if (argument.Key is nameof(DebuggerDisplayAttribute.Name) or nameof(DebuggerDisplayAttribute.Type) && argument.Value is { Kind: TypedConstantKind.Primitive, Value: string value2 })
{
ValidateValue(context, symbol, attribute, value2);
}
}
}
Expand Down Expand Up @@ -111,5 +111,21 @@ static string GetMemberName(ReadOnlySpan<char> member)

return result;
}

static void ValidateValue(SymbolAnalysisContext context, INamedTypeSymbol symbol, AttributeData attribute, string value)
{
var members = ParseMembers(value.AsSpan());
if (members is not null)
{
foreach (var member in members)
{
if (!MemberExists(symbol, member))
{
context.ReportDiagnostic(Rule, attribute, member);
return;
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,46 @@ await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

[Theory]
[InlineData("Invalid")]
[InlineData("Invalid,np")]
[InlineData("Invalid()")]
[InlineData("Invalid.Length")]
public async Task UnknownMember_Name(string memberName)
{
var sourceCode = $$"""
using System.Diagnostics;
[[|DebuggerDisplay("", Name = "{{{memberName}}}")|]]
public class Dummy
{
public string Display { get; }
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

[Theory]
[InlineData("Invalid")]
[InlineData("Invalid,np")]
[InlineData("Invalid()")]
[InlineData("Invalid.Length")]
public async Task UnknownMember_Type(string memberName)
{
var sourceCode = $$"""
using System.Diagnostics;
[[|DebuggerDisplay("", Type = "{{{memberName}}}")|]]
public class Dummy
{
public string Display { get; }
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

[Fact]
public async Task Valid()
Expand All @@ -49,6 +89,38 @@ await CreateProjectBuilder()
.WithSourceCode(SourceCode)
.ValidateAsync();
}

[Fact]
public async Task Valid_Name()
{
const string SourceCode = """
using System.Diagnostics;
[DebuggerDisplay("", Name = "{Display}")]
public class Dummy
{
public string Display { get; }
}
""";
await CreateProjectBuilder()
.WithSourceCode(SourceCode)
.ValidateAsync();
}

[Fact]
public async Task Valid_Type()
{
const string SourceCode = """
using System.Diagnostics;
[DebuggerDisplay("", Type = "{Display}")]
public class Dummy
{
public string Display { get; }
}
""";
await CreateProjectBuilder()
.WithSourceCode(SourceCode)
.ValidateAsync();
}

[Fact]
public async Task ValidWithOptions()
Expand Down

0 comments on commit 2807d2e

Please sign in to comment.