Skip to content

Commit

Permalink
Merge pull request #45487 from m-redding/feature/convertNameOf
Browse files Browse the repository at this point in the history
Feature/convert name of
  • Loading branch information
Javier Matos Denizac authored Jun 26, 2020
2 parents 745419f + a977f28 commit e4b8e80
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal class CSharpConvertNameOfDiagnosticAnalyzer : AbstractBuiltInCodeStyleD
{
public CSharpConvertNameOfDiagnosticAnalyzer()
: base(IDEDiagnosticIds.ConvertNameOfDiagnosticId,
CSharpCodeStyleOptions.PreferBraces,
CSharpCodeStyleOptions.PreferBraces, //TODO: Update code style options
LanguageNames.CSharp,
new LocalizableResourceString(
nameof(CSharpAnalyzersResources.Convert_type_name_to_nameof), CSharpAnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources)))
Expand All @@ -44,22 +44,29 @@ private void AnalyzeSyntaxAction(SyntaxNodeAnalysisContext syntaxContext)
//var cancellationToken = syntaxContext.CancellationToken;
var node = syntaxContext.Node;

// TODO: Any relevant style options?

// nameof was added in CSharp 6.0, so don't offer it for any languages after that time
// 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 typeof(someType).Name declaration
// 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()))
{
return;
}

// TODO: if argument is primitive cases

// TODO: Check that the current span is the case we're looking for
//TODO: if argument is generic

// TODO: Filter cases that don't work

// TODO: Create and report the right diagnostic
var location = Location.Create(syntaxTree, node.Span);
// 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());

syntaxContext.ReportDiagnostic(DiagnosticHelper.Create(
Expand All @@ -73,6 +80,5 @@ private void AnalyzeSyntaxAction(SyntaxNodeAnalysisContext syntaxContext)
public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
=> DiagnosticAnalyzerCategory.SemanticSpanAnalysis;

// HELPERS GO HERE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,24 @@ public static bool IsLeftSideOfCompoundAssignExpression(this SyntaxNode node)
((AssignmentExpressionSyntax)node.Parent).Left == node;
}

/// <summary>
/// Returns if <paramref name="node"/> is a member access to the .Name attribute of a type.
/// </summary>
public static bool IsNameMemberAccess(this SyntaxNode node)
{
var access = node.IsKind(SyntaxKind.SimpleMemberAccessExpression);
if (node != null && access)
{
var name = ((MemberAccessExpressionSyntax)node).Name;

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

/// <summary>
/// Returns the list of using directives that affect <paramref name="node"/>. The list will be returned in
/// top down order.
Expand Down

0 comments on commit e4b8e80

Please sign in to comment.