-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from Vannevelj/issue25
Implemented #25
- Loading branch information
Showing
8 changed files
with
1,512 additions
and
5 deletions.
There are no files selected for viewing
1,143 changes: 1,143 additions & 0 deletions
1,143
...gnostics/VSDiagnostics/VSDiagnostics.Test/Tests/General/NamingConventionsAnalyzerTests.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 11 additions & 5 deletions
16
VSDiagnostics/VSDiagnostics/VSDiagnostics.Test/packages.config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...gnostics/VSDiagnostics/Diagnostics/General/NamingConventions/NamingConventionsAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using VSDiagnostics.Utilities; | ||
|
||
namespace VSDiagnostics.Diagnostics.General.NamingConventions | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class NamingConventionsAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public const string DiagnosticId = nameof(NamingConventionsAnalyzer); | ||
internal const string Title = "A member does not follow naming conventions."; | ||
internal const string Message = "The {0} {1} does not follow naming conventions. Should be {2}."; | ||
internal const string Category = "General"; | ||
internal const DiagnosticSeverity Severity = DiagnosticSeverity.Warning; | ||
internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, Message, Category, Severity, true); | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.RegisterSyntaxNodeAction(AnalyzeSymbol, | ||
SyntaxKind.FieldDeclaration, | ||
SyntaxKind.PropertyDeclaration, | ||
SyntaxKind.MethodDeclaration, | ||
SyntaxKind.ClassDeclaration, | ||
SyntaxKind.InterfaceDeclaration, | ||
SyntaxKind.LocalDeclarationStatement, | ||
SyntaxKind.Parameter); | ||
} | ||
|
||
private void AnalyzeSymbol(SyntaxNodeAnalysisContext context) | ||
{ | ||
var nodeAsField = context.Node as FieldDeclarationSyntax; | ||
if (nodeAsField != null) | ||
{ | ||
if (nodeAsField.Declaration == null) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var variable in nodeAsField.Declaration.Variables) | ||
{ | ||
if (nodeAsField.Modifiers.Any(x => new[] { "internal", "protected", "public" }.Contains(x.Text))) | ||
{ | ||
CheckNaming(variable.Identifier, "field", NamingConvention.UpperCamelCase, context); | ||
} | ||
else if (nodeAsField.Modifiers.Any(x => x.Text == "private") || nodeAsField.Modifiers.Count == 0 /* no access modifier defaults to private */) | ||
{ | ||
CheckNaming(variable.Identifier, "field", NamingConvention.UnderscoreLowerCamelCase, context); | ||
} | ||
else | ||
{ | ||
return; // Code is in an incomplete state | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
var nodeAsProperty = context.Node as PropertyDeclarationSyntax; | ||
if (nodeAsProperty != null) | ||
{ | ||
CheckNaming(nodeAsProperty.Identifier, "property", NamingConvention.UpperCamelCase, context); | ||
} | ||
|
||
var nodeAsMethod = context.Node as MethodDeclarationSyntax; | ||
if (nodeAsMethod != null) | ||
{ | ||
CheckNaming(nodeAsMethod.Identifier, "method", NamingConvention.UpperCamelCase, context); | ||
} | ||
|
||
var nodeAsClass = context.Node as ClassDeclarationSyntax; | ||
if (nodeAsClass != null) | ||
{ | ||
CheckNaming(nodeAsClass.Identifier, "class", NamingConvention.UpperCamelCase, context); | ||
} | ||
|
||
var nodeAsInterface = context.Node as InterfaceDeclarationSyntax; | ||
if (nodeAsInterface != null) | ||
{ | ||
CheckNaming(nodeAsInterface.Identifier, "interface", NamingConvention.InterfacePrefixUpperCamelCase, context); | ||
} | ||
|
||
var nodeAsLocal = context.Node as LocalDeclarationStatementSyntax; | ||
if (nodeAsLocal != null) | ||
{ | ||
if (nodeAsLocal.Declaration == null) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var variable in nodeAsLocal.Declaration.Variables) | ||
{ | ||
CheckNaming(variable.Identifier, "local", NamingConvention.LowerCamelCase, context); | ||
} | ||
|
||
return; | ||
} | ||
|
||
var nodeAsParameter = context.Node as ParameterSyntax; | ||
if (nodeAsParameter != null) | ||
{ | ||
CheckNaming(nodeAsParameter.Identifier, "parameter", NamingConvention.LowerCamelCase, context); | ||
} | ||
} | ||
|
||
private void CheckNaming(SyntaxToken currentIdentifier, string memberType, NamingConvention convention, SyntaxNodeAnalysisContext context) | ||
{ | ||
var conventionedIdentifier = currentIdentifier.WithConvention(convention); | ||
if (conventionedIdentifier.Text != currentIdentifier.Text) | ||
{ | ||
context.ReportDiagnostic(Diagnostic.Create(Rule, currentIdentifier.GetLocation(), memberType, currentIdentifier.Text, conventionedIdentifier.Text)); | ||
} | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...agnostics/VSDiagnostics/Diagnostics/General/NamingConventions/NamingConventionsCodeFix.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using VSDiagnostics.Utilities; | ||
|
||
namespace VSDiagnostics.Diagnostics.General.NamingConventions | ||
{ | ||
[ExportCodeFixProvider("NamingConventions", LanguageNames.CSharp), Shared] | ||
public class NamingConventionsCodeFix : CodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(NamingConventionsAnalyzer.DiagnosticId); | ||
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
|
||
public override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
var diagnostic = context.Diagnostics.First(); | ||
var diagnosticSpan = diagnostic.Location.SourceSpan; | ||
|
||
var identifier = root.FindToken(diagnosticSpan.Start); | ||
context.RegisterCodeFix(CodeAction.Create("Rename", x => RenameAsync(context.Document, root, identifier)), diagnostic); | ||
} | ||
|
||
private Task<Solution> RenameAsync(Document document, SyntaxNode root, SyntaxToken identifier) | ||
{ | ||
var identifierParent = identifier.Parent; | ||
var newIdentifier = default(SyntaxToken); | ||
|
||
do | ||
{ | ||
var parentAsField = identifierParent as FieldDeclarationSyntax; | ||
if (parentAsField != null) | ||
{ | ||
if (parentAsField.Modifiers.Any(x => new[] { "internal", "protected", "public" }.Contains(x.Text))) | ||
{ | ||
newIdentifier = identifier.WithConvention(NamingConvention.UpperCamelCase); | ||
} | ||
else | ||
{ | ||
newIdentifier = identifier.WithConvention(NamingConvention.UnderscoreLowerCamelCase); | ||
} | ||
break; | ||
} | ||
|
||
if (identifierParent is PropertyDeclarationSyntax || identifierParent is MethodDeclarationSyntax || identifierParent is ClassDeclarationSyntax) | ||
{ | ||
newIdentifier = identifier.WithConvention(NamingConvention.UpperCamelCase); | ||
break; | ||
} | ||
|
||
if (identifierParent is LocalDeclarationStatementSyntax || identifierParent is ParameterSyntax) | ||
{ | ||
newIdentifier = identifier.WithConvention(NamingConvention.LowerCamelCase); | ||
break; | ||
} | ||
|
||
if (identifierParent is InterfaceDeclarationSyntax) | ||
{ | ||
newIdentifier = identifier.WithConvention(NamingConvention.InterfacePrefixUpperCamelCase); | ||
break; | ||
} | ||
|
||
identifierParent = identifierParent.Parent; | ||
} while (identifierParent != null); | ||
|
||
var newParent = identifierParent.ReplaceToken(identifier, newIdentifier); | ||
var newRoot = root.ReplaceNode(identifierParent, newParent); | ||
return Task.FromResult(document.WithSyntaxRoot(newRoot).Project.Solution); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.