Skip to content

Commit

Permalink
More tests for nameof.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Sep 20, 2018
1 parent 7e08737 commit 11ec2d8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .paket/Paket.Restore.targets
Expand Up @@ -144,11 +144,12 @@
<PackageName>$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[0])</PackageName>
<PackageVersion>$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[1])</PackageVersion>
<AllPrivateAssets>$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[4])</AllPrivateAssets>
<CopyLocal>$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[5])</CopyLocal>
</PaketReferencesFileLinesInfo>
<PackageReference Include="%(PaketReferencesFileLinesInfo.PackageName)">
<Version>%(PaketReferencesFileLinesInfo.PackageVersion)</Version>
<PrivateAssets Condition=" ('%(PaketReferencesFileLinesInfo.AllPrivateAssets)' == 'true') Or ('$(PackAsTool)' == 'true') ">All</PrivateAssets>
<ExcludeAssets Condition="%(PaketReferencesFileLinesInfo.AllPrivateAssets) == 'exclude'">runtime</ExcludeAssets>
<ExcludeAssets Condition="%(PaketReferencesFileLinesInfo.CopyLocal) == 'false'">runtime</ExcludeAssets>
<Publish Condition=" '$(PackAsTool)' == 'true' ">true</Publish>
</PackageReference>
</ItemGroup>
Expand Down
62 changes: 62 additions & 0 deletions ReflectionAnalyzers.Tests/REFL016UseNameofTests/ValidCode.cs
Expand Up @@ -7,6 +7,68 @@ namespace ReflectionAnalyzers.Tests.REFL016UseNameofTests
internal class ValidCode
{
private static readonly DiagnosticAnalyzer Analyzer = new REFL016UseNameof();
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(REFL016UseNameof.DiagnosticId);

[Test]
public void TypeofDictionaryGetMethodAdd()
{
var testCode = @"
namespace RoslynSandbox
{
using System.Collections.Generic;
public class Foo
{
public Foo()
{
var member = typeof(Dictionary<string, object>).GetMethod(nameof(Dictionary<string, object>.Add));
}
}
}";
AnalyzerAssert.Valid(Analyzer, ExpectedDiagnostic, testCode);
}

[Test]
public void ThisGetTYpeGetStaticMethod()
{
var testCode = @"
namespace RoslynSandbox
{
using System.Collections.Generic;
public class Foo
{
public Foo()
{
var member = this.GetType().GetMethod(nameof(Add));
}
private static int Add(int x, int y) => x + y;
}
}";
AnalyzerAssert.Valid(Analyzer, ExpectedDiagnostic, testCode);
}

[Test]
public void ThisGetTYpeGetInstanceMethod()
{
var testCode = @"
namespace RoslynSandbox
{
using System.Collections.Generic;
public class Foo
{
public Foo()
{
var member = this.GetType().GetMethod(nameof(this.Add));
}
private int Add(int x, int y) => x + y;
}
}";
AnalyzerAssert.Valid(Analyzer, ExpectedDiagnostic, testCode);
}

[Test]
public void WhenThrowingArgumentException()
Expand Down
29 changes: 26 additions & 3 deletions ReflectionAnalyzers/REFL016UseNameof.cs
Expand Up @@ -32,10 +32,11 @@ public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(Handle, SyntaxKind.StringLiteralExpression);
context.RegisterSyntaxNodeAction(c => HandleLiteral(c), SyntaxKind.StringLiteralExpression);
context.RegisterSyntaxNodeAction(c => HandleNameof(c), SyntaxKind.NameOfKeyword);
}

private static void Handle(SyntaxNodeAnalysisContext context)
private static void HandleLiteral(SyntaxNodeAnalysisContext context)
{
if (context.IsExcludedFromAnalysis())
{
Expand All @@ -49,7 +50,8 @@ private static void Handle(SyntaxNodeAnalysisContext context)
{
if (argument.Parent is ArgumentListSyntax argumentList &&
argumentList.Parent is InvocationExpressionSyntax invocation &&
TryGetX(invocation, text, context, out var target))
TryGetX(invocation, text, context, out var target) &&
target.ContainingType != context.ContainingSymbol.ContainingType)
{
context.ReportDiagnostic(
Diagnostic.Create(
Expand Down Expand Up @@ -81,6 +83,27 @@ private static void Handle(SyntaxNodeAnalysisContext context)
}
}

private static void HandleNameof(SyntaxNodeAnalysisContext context)
{
//if (context.Node is InvocationExpressionSyntax nameof &&
// nameof.Parent is ArgumentSyntax argument &&
// context.SemanticModel.TryGetConstantValue(nameof, context.CancellationToken, out string name))
//{
// if (argument.Parent is ArgumentListSyntax argumentList &&
// argumentList.Parent is InvocationExpressionSyntax invocation &&
// TryGetX(invocation, name, context, out var target))
// {
// context.ReportDiagnostic(
// Diagnostic.Create(
// Descriptor,
// literal.GetLocation(),
// ImmutableDictionary<string, string>.Empty.Add(
// nameof(ISymbol),
// $"{target.ContainingType.ToMinimalDisplayString(context.SemanticModel, invocation.SpanStart)}.{target.Name}")));
// }
//}
}

private static bool IsVisible(LiteralExpressionSyntax literal, ILocalSymbol local, CancellationToken cancellationToken)
{
if (local.DeclaringSyntaxReferences.Length == 1 &&
Expand Down

0 comments on commit 11ec2d8

Please sign in to comment.