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

LC0005 Avoid duplicate diagnostic, add diagnostic field syntax length #640

Merged
Merged
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
29 changes: 29 additions & 0 deletions Design/Rule0005VariableCasingShouldNotDifferFromDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public override void Initialize(AnalysisContext context)
context.RegisterSyntaxNodeAction(new Action<SyntaxNodeAnalysisContext>(this.AnalyzeIdentifierName), SyntaxKind.IdentifierName);
context.RegisterSyntaxNodeAction(new Action<SyntaxNodeAnalysisContext>(this.AnalyzeQualifiedName), SyntaxKind.QualifiedName);
context.RegisterSyntaxNodeAction(new Action<SyntaxNodeAnalysisContext>(this.AnalyzeQualifiedNameWithoutNamespace), SyntaxKind.QualifiedName);
context.RegisterSyntaxNodeAction(new Action<SyntaxNodeAnalysisContext>(this.AnalyzeLengthDataType), SyntaxKind.LengthDataType);

context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.CheckForBuiltInMethodsWithCasingMismatch), new OperationKind[] {
OperationKind.InvocationExpression,
Expand Down Expand Up @@ -261,6 +262,18 @@ private void AnalyzeQualifiedNameWithoutNamespace(SyntaxNodeAnalysisContext ctx)
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0005VariableCasingShouldNotDifferFromDeclaration, identifierNameSyntax.GetLocation(), new object[] { StringExtensions.QuoteIdentifierIfNeeded(objectTypeSymbol.Name), "" }));
}

private void AnalyzeLengthDataType(SyntaxNodeAnalysisContext ctx)
{
if (ctx.Node is not LengthDataTypeSyntax node)
return;

SyntaxToken identifierToken = node.GetFirstToken();
if (!IsNavTypeKindWithDifferentCasing(identifierToken.ValueText, out string targetName))
return;

ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0005VariableCasingShouldNotDifferFromDeclaration, identifierToken.GetLocation(), new object[] { targetName, "" }));
}

private void CheckForBuiltInTypeCasingMismatch(SymbolAnalysisContext ctx)
{
AnalyseTokens(ctx);
Expand Down Expand Up @@ -290,6 +303,7 @@ private void AnalyseTokens(SymbolAnalysisContext ctx)
private void AnalyseNodes(SymbolAnalysisContext ctx)
{
IEnumerable<SyntaxNode> descendantNodes = ctx.Symbol.DeclaringSyntaxReference.GetSyntax().DescendantNodes()
.Where(t => t.Kind != SyntaxKind.LengthDataType) // handeld on AnalyzeLengthDataType method
.Where(n => !n.ToString().AsSpan().StartsWith("array"));

foreach (SyntaxNode descendantNode in descendantNodes)
Expand All @@ -305,6 +319,9 @@ private void AnalyseNodes(SymbolAnalysisContext ctx)
!syntaxNodeKindSpan.StartsWith("Enum") &&
!syntaxNodeKindSpan.StartsWith("Label"))
{
if (descendantNode is SimpleTypeReferenceSyntax simpleTypeRefSubstituteToken && simpleTypeRefSubstituteToken.DataType.Kind == SyntaxKind.LengthDataType)
continue; // handeld on AnalyzeLengthDataType method

var targetName = _navTypeKindStrings.FirstOrDefault(Kind =>
{
var kindSpan = Kind.AsSpan();
Expand Down Expand Up @@ -413,5 +430,17 @@ private bool OnlyDiffersInCasing(ReadOnlySpan<char> left, ReadOnlySpan<char> rig
return leftSpan.Equals(rightSpan, StringComparison.OrdinalIgnoreCase) &&
!leftSpan.Equals(rightSpan, StringComparison.Ordinal);
}

private static bool IsNavTypeKindWithDifferentCasing(string inputNavTypeKind, out string matchedNavTypeKind)
{
matchedNavTypeKind = _navTypeKindStrings.SingleOrDefault(Kind =>
{
var kindSpan = Kind.AsSpan();
return kindSpan.Equals(inputNavTypeKind.AsSpan(), StringComparison.OrdinalIgnoreCase) &&
!kindSpan.Equals(inputNavTypeKind.AsSpan(), StringComparison.Ordinal);
});

return matchedNavTypeKind is not null;
}
}
}