Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Base Analyzer #45595

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.CSharp.ConvertNameOf
{
Expand All @@ -25,7 +18,7 @@ internal class CSharpConvertNameOfDiagnosticAnalyzer : AbstractBuiltInCodeStyleD
{
public CSharpConvertNameOfDiagnosticAnalyzer()
: base(IDEDiagnosticIds.ConvertNameOfDiagnosticId,
CSharpCodeStyleOptions.PreferBraces, //TODO: Update code style options
option: null,
LanguageNames.CSharp,
new LocalizableResourceString(
nameof(CSharpAnalyzersResources.Convert_type_name_to_nameof), CSharpAnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources)))
Expand All @@ -34,51 +27,63 @@ public CSharpConvertNameOfDiagnosticAnalyzer()

protected override void InitializeWorker(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(AnalyzeSyntaxAction, SyntaxKind.TypeOfExpression);
context.RegisterOperationAction(AnalyzeTypeOfAction, OperationKind.TypeOf);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I was going to suggest switching to IOperation.

}

private void AnalyzeSyntaxAction(SyntaxNodeAnalysisContext syntaxContext)
private void AnalyzeTypeOfAction(OperationAnalysisContext context)
{
//var options = syntaxContext.Options;
var syntaxTree = syntaxContext.Node.SyntaxTree;
//var cancellationToken = syntaxContext.CancellationToken;
var node = syntaxContext.Node;
var syntaxTree = context.Operation.Syntax.SyntaxTree;
var node = context.Operation.Syntax;

// nameof was added in CSharp 6.0, so don't offer it for any languages before that time
if (((CSharpParseOptions)syntaxTree.Options).LanguageVersion < LanguageVersion.CSharp6)
{
return;
}

// TODO: Check for compiler errors on the declaration

var parent = node.Parent;

// We know that it is a typeof() instance, but we only want to offer the fix if it is a .Name access
if (!(node.Parent.IsKind(SyntaxKind.SimpleMemberAccessExpression) && parent.IsNameMemberAccess()))
if (!IsValidOperation(context.Operation))
{
return;
}

// TODO: if argument is primitive cases

//TODO: if argument is generic


// Current case can be effectively changed to a nameof instance so report a diagnostic
var location = Location.Create(syntaxTree, parent.Span);
var additionalLocations = ImmutableArray.Create(node.GetLocation());
var parent = node.Parent;
var location = parent.GetLocation();

syntaxContext.ReportDiagnostic(DiagnosticHelper.Create(
Descriptor,
location,
ReportDiagnostic.Hidden,
additionalLocations,
properties: null));
context.ReportDiagnostic(Diagnostic.Create(Descriptor, location));
}
// TODO: Overwrite GetAnalyzerCategory
// Overwrite GetAnalyzerCategory
public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
=> DiagnosticAnalyzerCategory.SemanticSpanAnalysis;

private static bool IsValidOperation(IOperation operation)
{
// Cast to a typeof operation & check parent is a property reference
var typeofOperation = (ITypeOfOperation)operation;
if (!(operation.Parent is IPropertyReferenceOperation))
{
return false;
}

// Check Parent is a .Name access
var operationParent = (IPropertyReferenceOperation)operation.Parent;
if (!(operationParent.Property.Name == "Name"))
{
return false;
}

// Check if it's a generic type
if (((INamedTypeSymbol)(typeofOperation).TypeOperand).IsGenericType)
{
return false;
}

// Check if it's a primitive type
if (!(((ITypeSymbol)(typeofOperation.TypeOperand)).SpecialType.ToPredefinedType() is PredefinedType.None))
{
return false;
}
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,10 @@ public static bool IsLeftSideOfCompoundAssignExpression(this SyntaxNode node)
/// </summary>
public static bool IsNameMemberAccess(this SyntaxNode node)
{
var access = node.IsKind(SyntaxKind.SimpleMemberAccessExpression);
if (node != null && access)
if (node != null && node.IsKind(SyntaxKind.SimpleMemberAccessExpression))
{
var name = ((MemberAccessExpressionSyntax)node).Name;

if (name.Identifier.ValueText == "Name")
{
return true;
}
return name.Identifier.ValueText == "Name";
}
return false;
}
Expand Down