From 11ec2d8557d227140230f7e82f5da86e5f53c47a Mon Sep 17 00:00:00 2001 From: JohanLarsson Date: Thu, 20 Sep 2018 18:54:20 +0200 Subject: [PATCH] More tests for nameof. --- .paket/Paket.Restore.targets | 3 +- .../REFL016UseNameofTests/ValidCode.cs | 62 +++++++++++++++++++ ReflectionAnalyzers/REFL016UseNameof.cs | 29 ++++++++- 3 files changed, 90 insertions(+), 4 deletions(-) diff --git a/.paket/Paket.Restore.targets b/.paket/Paket.Restore.targets index 6be03acb..305e736c 100644 --- a/.paket/Paket.Restore.targets +++ b/.paket/Paket.Restore.targets @@ -144,11 +144,12 @@ $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[0]) $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[1]) $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[4]) + $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[5]) %(PaketReferencesFileLinesInfo.PackageVersion) All - runtime + runtime true diff --git a/ReflectionAnalyzers.Tests/REFL016UseNameofTests/ValidCode.cs b/ReflectionAnalyzers.Tests/REFL016UseNameofTests/ValidCode.cs index dc04b768..e8768bd7 100644 --- a/ReflectionAnalyzers.Tests/REFL016UseNameofTests/ValidCode.cs +++ b/ReflectionAnalyzers.Tests/REFL016UseNameofTests/ValidCode.cs @@ -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).GetMethod(nameof(Dictionary.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() diff --git a/ReflectionAnalyzers/REFL016UseNameof.cs b/ReflectionAnalyzers/REFL016UseNameof.cs index d7d416c5..2c3d7800 100644 --- a/ReflectionAnalyzers/REFL016UseNameof.cs +++ b/ReflectionAnalyzers/REFL016UseNameof.cs @@ -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()) { @@ -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( @@ -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.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 &&